切换到宽版
  • 8810阅读
  • 8回复

[动态规划]动态规划——动态规划的技巧——阶段的划分和状态的表示 [复制链接]

上一主题 下一主题
离线arronking
 
只看楼主 正序阅读 0 发表于: 2005-10-30
在动态规划的设计过程中,阶段的划分和状态的表示是非常重要的两步,这两步会直接影响该问题的计算复杂性,有时候阶段划分或状态表示的不合理还会使得动态规划法不适用。

[例9] 街道问题

在下图中找出从左下角到右上角的最短路径,每步只能向右方或上方走。



这是一道简单而又典型的动态规划题,许多介绍动态规划的书与文章中都拿它来做例子。通常,书上的解答是这样的:



按照图中的虚线来划分阶段,即阶段变量k表示走过的步数,而状态变量xk表示当前处于这一阶段上的哪一点。这时的模型实际上已经转化成了一个特殊的多段图。用决策变量uk=0表示向右走,uk=1表示向上走,则状态转移方程如下:



(这里的row是地图竖直方向的行数)

我们看到,这个状态转移方程需要根据k的取值分两种情况讨论,显得非常麻烦。相应的,把它代入规划方程而付诸实现时,算法也很繁。因而我们在实现时,一般是不会这么做的,而代之以下面方法:



(这里Distance表示相邻两点间的边长)

这样做确实要比上面的方法简单多了,但是它已经破坏了动态规划的本来面目,而不存在明确的阶段特征了。如果说这种方法是以地图中的行(A、B、C、D)来划分阶段的话,那么它的"状态转移"就不全是在两个阶段之间进行的了。



也许这没什么大不了的,因为实践比理论更有说服力。但是,如果我们把题目扩展一下:在地图中找出从左下角到右上角的两条路径,两条路径中的任何一条边都不能重叠,并且要求两条路径的总长度最短。这时,再用这种"简单"的方法就不太好办了。

如果非得套用这种方法的话,则最优指标函数就需要有四维的下标,并且难以处理两条路径"不能重叠"的问题。

而我们回到原先"标准"的动态规划法,就会发现这个问题很好解决,只需要加一维状态变量就成了。即用xk=(ak,bk)分别表示两条路径走到阶段k时所处的位置,相应的,决策变量也增加一维,用uk=(xk,yk)分别表示两条路径的行走方向。状态转移时将两条路径分别考虑



在写规划方程时,只要对两条路径走到同一个点的情况稍微处理一下,减少可选的决策个数:



从这个例子可以看出,合理地划分阶段和选择状态可以给解题带来方便。

[例10] LITTLE SHOP OF FLOWERS (IOI’99)

PROBLEM

You want to arrange the window of your flower shop in a most pleasant way. You have F bunches of flowers, each being of a different kind, and at least as many vases ordered in a row. The vases are glued onto the shelf and are numbered consecutively 1 through V, where V is the number of vases, from left to right so that the vase 1 is the leftmost, and the vase V is the rightmost vase. The bunches are moveable and are uniquely identified by integers between 1 and F. These id-numbers have a significance: They determine the required order of appearance of the flower bunches in the row of vases so that the bunch i must be in a vase to the left of the vase containing bunch j whenever i < j. Suppose, for example, you have a bunch of azaleas (id-number=1), a bunch of begonias (id-number=2) and a bunch of carnations (id-number=3). Now, all the bunches must be put into the vases keeping their id-numbers in order. The bunch of azaleas must be in a vase to the left of begonias, and the bunch of begonias must be in a vase to the left of carnations. If there are more vases than bunches of flowers then the excess will be left empty. A vase can hold only one bunch of flowers.

Each vase has a distinct characteristic (just like flowers do). Hence, putting a bunch of flowers in a vase results in a certain aesthetic value, expressed by an integer. The aesthetic values are presented in a table as shown below. Leaving a vase empty has an aesthetic value of 0.



According to the table, azaleas, for example, would look great in vase 2, but they would look awful in vase 4.

To achieve the most pleasant effect you have to maximize the sum of aesthetic values for the arrangement while keeping the required ordering of the flowers. If more than one arrangement has the maximal sum value, any one of them will be acceptable. You have to produce exactly one arrangement.

ASSUMPTIONS

1 <= F <= 100 where F is the number of the bunches of flowers. The bunches are numbered 1 through F.
F <= V <= 100 where V is the number of vases.
-50 <= Aij <= 50 where Aij is the aesthetic value obtained by putting the flower bunch i into the vase j.
INPUT

The input is a text file named flower.inp.

The first line contains two numbers: F, V.
The following F lines: Each of these lines contains V integers, so that Aij is given as the jth number on the (i+1)st line of the input file.
OUTPUT

The output must be a text file named flower.out consisting of two lines:

The first line will contain the sum of aesthetic values for your arrangement.
The second line must present the arrangement as a list of F numbers, so that the k’th number on this line identifies the vase in which the bunch k is put.
EXAMPLE

flower.inp:

3 5
7 23 -5 -24 16
5 21 -4 10 23
-21 5 -4 -20 20


flower.out:

53
2 4 5


EVALUATION

Your program will be allowed to run 2 seconds.
No partial credit can be obtained for a test case.
本题虽然是IOI’99中较为简单的一题,但其中大有文章可作。说它简单,是因为它有序,因此我们一眼便可看出这题应该用动态规划来解决。但是,如何动态规划呢?如何划分阶段,又如何选择状态呢?

<方法1>

以花束的编号来划分阶段。在这里,第k阶段布置第k束花,共有F束花,有F+1个阶段,增加第F+1阶段是为了计算的方便;状态变量xk表示第k束花所在的花瓶。而对于每一个状态xk,决策uk就是第k+1束花放置的花瓶号;最优指标函数fk(xk)表示从第k束花到第n束花所得到的最大美学值;A(i,j)是花束i插在花瓶j中的美学值,V是花瓶总数,F是花的总数。

状态转移方程为

规划方程为



边界条件为:

,

事实上这是一个虚拟的边界。

最后要求的最大美学价值是



<方法2>

方法1的规划方程中的允许决策空间:xk+1≤uk≤V-(F-k)+1 比较麻烦,因此有待改进。还是以花束的编号来划分阶段,第k阶段布置第k束花;状态变量xk表示第k束花所在的花瓶;注意,这里我们考虑倒过来布置花瓶,即从第F束花开始布置到第1束花。于是状态变量uk表示第k-1束花所在的花瓶;最优指标fk(xk)表示从第一束花到第k束花所获得的美学价值;A(i,j)是花束i插在花瓶j中的美学值,V是花瓶总数,F是花的总数。则状态转移方程为:



规划方程为:



增加的虚拟边界条件为:



最后要求的最大美学价值是:



可以看出,这种方法实质上和方法1没有区别,但是允许决策空间的表示变得简单了。

<方法3>

以花瓶的数目来划分阶段,第k个阶段决定花瓶k中是否放花;状态变量xk表示前k个花瓶中放了多少花;而对于任意一个状态xk,决策就是第xk束花是否放在第k个花瓶中,用变量uk=1或0来表示。最优指标函数fk(xk)表示前k个花瓶中插了xk束花,所能取得的最大美学值。注意,这里仍然是倒过来考虑。

状态转移方程为



规划方程为



边界条件为



三种不同的方法都成功地解决了问题,只不过因为阶段的划分不同,状态的表示不同,决策的选择有多有少,所以算法的时间复杂度也就不同。

这个例子具有很大的普遍性。有很多的多阶段决策问题都有着不止一种的阶段划分方法,因而往往就有不止一种的规划方法。有时各种方法所产生的效果是差不多的,但更多的时候,就像我们的例子一样,两种方法会在某个方面有些区别。所以,在用动态规划解题的时候,可以多想一想是否有其它的解法。对于不同的解法,要注意比较,好的算法好在哪里,差一点的算法差在哪里。从各种不同算法的比较中,我们可以更深刻地领会动态规划的构思技巧。
1条评分
caithagoras 威望 +5 - 2005-10-30
大秦魂不相信强盗悔忏,
只能用复仇雪耻的战争,
讨回我秦汉高贵的尊严。
强秦何曾看过六国脸色,
大汉何曾求过匈奴道歉?
用无坚不摧的滚滚铁骑,
踏平那敌国的巍峨宫殿!
离线kai^f^p^kai
只看该作者 8 发表于: 2007-08-30
好帖,可图看不到啊
离线hy6210cs
只看该作者 7 发表于: 2007-02-12
还不错~~~~~~~~~~~~~~~~
离线hy6210cs
只看该作者 6 发表于: 2007-02-12
不是把~~~~~~~~~~~~~~~~~~~~~
离线arronking
只看该作者 5 发表于: 2007-02-08
呵呵,晕死哦,两个回复之间居然相隔一年多时间啊
大秦魂不相信强盗悔忏,
只能用复仇雪耻的战争,
讨回我秦汉高贵的尊严。
强秦何曾看过六国脸色,
大汉何曾求过匈奴道歉?
用无坚不摧的滚滚铁骑,
踏平那敌国的巍峨宫殿!
离线hy6210cs
只看该作者 4 发表于: 2007-02-05
是呀!!!!!!!!!!!!!!!太累了~~~~~~~~~~~~~~~~
离线gujoe
只看该作者 3 发表于: 2006-08-08
书上都有,发上来看起反而累
离线steven4711
只看该作者 2 发表于: 2005-11-03
英语我看不懂哦
离线李逍遥
只看该作者 1 发表于: 2005-10-30
斑竹发点汉语的行吗?
快速回复
限100 字节
 
上一个 下一个