Example: Dynamic Programming - Getting Started

1. Basic Concepts

    The dynamic programming process is: each decision depends on the current state, and then causes a state transition. A decision sequence is generated in a changing state, so this multi-stage optimization decision-making process is called dynamic programming.

2. Basic ideas and strategies

    The basic idea is similar to the divide-and-conquer method. It also decomposes the problem to be solved into several sub-problems (stages), and solves the sub-stages in sequence. The solution of the former sub-problem provides useful information for the solution of the latter sub-problem. When solving any sub-problem, various possible local solutions are listed, those local solutions that are likely to be optimal are retained by decision-making, and other local solutions are discarded. Solve each sub-problem in turn, and the last sub-problem is the solution to the initial problem.

    Since most of the problems solved by dynamic programming have overlapping sub-problems, in order to reduce repeated calculations, each sub-problem is solved only once, and the different states at different stages are stored in a two-dimensional array.

    The biggest difference from the divide-and-conquer method is that it is suitable for problems solved by dynamic programming, and the sub-problems obtained after decomposition are often not independent of each other (that is, the solution of the next sub-stage is based on the solution of the previous sub-stage. for further solutions) .

 


3. Applicable situations

Problems that can be solved by dynamic programming generally have three properties:

    (1) Optimization principle: If the solution of the subproblems contained in the optimal solution of the problem is also optimal, the problem is said to have the optimal substructure, that is, the optimization principle is satisfied.

    (2) No aftereffect: that is, once the state of a certain stage is determined, it will not be affected by subsequent decisions of this state. That is to say, the process after a certain state will not affect the previous state, only related to the current state.

   (3) There are overlapping sub-problems: that is, the sub-problems are not independent, and a sub-problem may be used multiple times in the next stage of decision-making. ( This property is not a necessary condition for the application of dynamic programming, but without this property, the dynamic programming algorithm has no advantage over other algorithms )

 


Fourth, the basic steps of solving

     The problem dealt with by dynamic programming is a multi-stage decision problem , which generally starts from the initial state and reaches the end state through the selection of intermediate stage decisions. These decisions form a decision sequence, and at the same time determine an activity route (usually the optimal activity route) to complete the entire process. as the picture shows. The design of dynamic programming has a certain pattern, which generally goes through the following steps.

    Initial state→│decision1│→│decision2│→…→│decision n│→end state

                      Figure 1 Schematic diagram of the decision-making process of dynamic programming

    (1) Division into stages : According to the time or space characteristics of the problem, the problem is divided into several stages. When dividing the stages, pay attention to the fact that the divided stages must be ordered or sortable , otherwise the problem cannot be solved.

    (2) Determine the state and state variables : the various objective situations in which the problem is developed to various stages are represented by different states. Of course, the choice of state must satisfy no aftereffect.

    (3) Determine the decision and write the state transition equation : Because the decision and state transition have a natural connection, the state transition is to derive the state of this stage according to the state and decision of the previous stage . So if the decision is determined, the state transition equation can also be written. But in fact, it is often done in reverse, and the decision-making method and state transition equation are determined according to the relationship between the states of two adjacent stages .

    (4) Find boundary conditions : The given state transition equation is a recursive formula , which requires a recursive termination condition or boundary condition.

    一般,只要解决问题的阶段状态状态转移决策确定了,就可以写出状态转移方程(包括边界条件)。

实际应用中可以按以下几个简化的步骤进行设计:

    (1)分析最优解的性质,并刻画其结构特征。

    (2)递归的定义最优解。

    (3)以自底向上或自顶向下的记忆化方式(备忘录法)计算出最优值

    (4)根据计算最优值时得到的信息,构造问题的最优解

 


五、算法实现的说明

    动态规划的主要难点在于理论上的设计,也就是上面4个步骤的确定,一旦设计完成,实现部分就会非常简单。

     使用动态规划求解问题,最重要的就是确定动态规划三要素

    (1)问题的阶段 (2)每个阶段的状态

    (3)从前一个阶段转化到后一个阶段之间的递推关系

     递推关系必须是从次小的问题开始到较大的问题之间的转化,从这个角度来说,动态规划往往可以用递归程序来实现,不过因为递推可以充分利用前面保存的子问题的解来减少重复计算,所以对于大规模问题来说,有递归不可比拟的优势,这也是动态规划算法的核心之处

    确定了动态规划的这三要素,整个求解过程就可以用一个最优决策表来描述最优决策表是一个二维表,其中行表示决策的阶段,列表示问题状态,表格需要填写的数据一般对应此问题的在某个阶段某个状态下的最优值(如最短路径,最长公共子序列,最大价值等),填表的过程就是根据递推关系,从1行1列开始,以行或者列优先的顺序,依次填写表格,最后根据整个表格的数据通过简单的取舍或者运算求得问题的最优解。

          f(n,m)=max{f(n-1,m), f(n-1,m-w[n])+P(n,m)}

六、动态规划算法基本框架

for(j=1; j<=m; j=j+1) // 第一个阶段
   xn[j] = 初始值;

 for(i=n-1; i>=1; i=i-1)// 其他n-1个阶段
   for(j=1; j>=f(i); j=j+1)//f(i)与i有关的表达式
     xi[j]=j=max(或min){g(xi-1[j1:j2]), ......, g(xi-1[jk:jk+1])};

t = g(x1[j1:j2]); // 由子问题的最优解求解整个问题的最优解的方案

print(x1[j1]);

for(i=2; i<=n-1; i=i+1)
{  
     t = t-xi-1[ji];

     for(j=1; j>=f(i); j=j+1)
        if(t=xi[ji])
             break;
}

实例:动态规划——入门篇

    五大经常使用算法 之 动态规划法


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326705178&siteId=291194637