Course 9: Deterministic dynamic programming

**Author: OptimizationCity Group**

**Introduction**

Dynamic programming is a type of mathematical technique that is able to divide large and complex problems into smaller and simpler problems and by solving each of them, obtain the optimal solution to the main problem. This method, in fact, is used for a series of consecutive and dependent decisions. Because these decisions are made in several stages, they are called multi-stage decision problems. A type of multi-stage decision making problem is ** dynamic planning**.

Dynamic programming attempts to achieve maximum efficiency by determining a combination of successive decisions. Unlike methods such as linear programming and integer programming which are standard and limited, the dynamic programming method does not have a specific and standard framework for formulating problems, and in fact, what dynamic programming does is to provide a general method to solve problems. By using dynamic programming, a wide range of programming problems can be solved, including linear programming, nonlinear programming, network, integer programming, and so on. However, using this method in solving linear programming problems, especially in cases where the dimensions of the problem are large, is not very economical.

In spite of such a wide range and capability in solving different types of problems, in order to be able to solve the problem by dynamic programming, we must know the general structure of the dynamic programming problem and specify the ** stages** and

**of the problem. Dynamic planning is used in various fields such as production and sales planning, workforce planning, production and inventory control, and maintenance system.**

*states*The characteristics of dynamic programming problems are as follows:

**1-** If we divide every dynamic program into several smaller problems, each of those smaller problems is called a ** stage**. At each stage, a decision must be made that affects the next stages.

**2-** At each stage, it should be determined in what situation we are. Therefore, each stage includes one or more states that connect it to the previous and subsequent stages. The ** state** at each step is determined by two things: (1) the previous state and (2) is the decision made at the previous step. Decisions are made at each stage according to the state at the current stage. The number of states can be finite or infinite.

**3-** At each stage, by making a decision, the state of the current stage is transferred to the state of the next stage.

**4-** Assuming that the state in one stage is known, the ** optimal policy** regarding the remaining stages is independent of the policy adopted in the previous stages.

**5-** Having the current state of the system contains all the information needed to determine the optimal policy related to the remaining steps.

**6-** The method of solving these problems starts with finding the optimal solution for all the states of the last stage.

**7-** The optimal policy of all states of stage n can be determined by a ** return relationship** and assuming that the optimal policy of all states of stage (n+1) is known. The return relationship is as follows.

In the above relationship, the system is in stage n and state s, and the goal is to find x_{n} such that the term *f _{n}(s,x_{n})* or

*p(s,x*is minimized.

_{n}) + f^{*}_{n+1}(x_{n})**8-** The solution method ** is backward movement**. This is how the solution is done from the last step to the first step and it is obtained by using the

**from one step to the previous step.**

*recursive relationship***9-** In dynamic programming, knowing the current state of the system contains all the information needed to determine the optimal policy related to the remaining steps. This property is called the ** Principle of Optimality**.

**10-** The method of solving these problems starts with finding the optimal solution for all the states of the last stage.

Since there is no single structure to solve all problems in dynamic programing, here we try to solve different examples from different areas to understand how to solve such problems.

**Examples**

**Example:**

The World Health Organization plans to send 5 medical groups to three countries for health and medical education in poor countries. This organization should determine how many groups should be assigned to each country in order to maximize the efficiency of all 3 countries. The criterion for measuring efficiency is the increase in life expectancy, which is based on the number of medical groups in the following table:

**Solution:**

x_{n}: representing the number of medical groups assigned to the nth country.

s (state): The number of groups that have not yet been allocated in previous steps.

p_{i}(x_{i}): The measure of efficiency for assigning medical group x_{i} to the i-th country.

The mathematical programming model of the above problem is as follows:

Using the concepts mentioned about the dynamic programming problem, fn(s,xn) becomes as follows:

Using the above, the solution process can be started from n=3 (the last step).

In step 3 we are looking to find the number of medical groups for the country 3. S can take a value from 0 to 5 (integer numbers). If s=0, it means that there is no group left for the country 3 to be allocated and the allocation has been done in the previous steps, so in the country 3, according to the table of basic information, it is not possible to increase the life expectancy and therefore fn* becomes zero.

If s=1, it means a group is not assigned and can be assigned to the country 3. According to the table, fn* is equal to 50. This process is repeated for s from 2 to 5.

The table for the second stage (n=2) is as follows:

To explain how to calculate the above table (n=2), we describe some cells in detail.

**Column x _{2}=0**

In the case where s=0 (that is, all the medical groups in the country 1 have been allocated), there are no medical groups left for the countries 2 and 3 to be allocated, and therefore the value of f_{2}^{*}(0) becomes zero.

In the case that s=1, it means that one medical group can be assigned to countries 2 and 3. In this column, x_{2}=0, which means that the group should not be assigned to the country 2, and one group remains, which is assigned to the country 3. Now we return to the table n=3 and get the optimal value for s=1 being equal to 50. Therefore, f_{2}(x_{2},s) is equal to 0 (Life expectancy for the country 2) + 50 (Life expectancy for the country 3).

**Column x _{2}=1**

As you can see, for s=0, a dashed line has been drawn. Because if s=0, it means that there are no group left for allocation for countries 2 and 3, and therefore the allocation of a group for the country 2 (x_{2}=1) does not make sense. Therefore, this point is expressed with a dash, -.

If s=1, it means that a medical group can be allocated, which can be allocated in countries 2 and 3. Because x_{2}=1, this group is assigned to the country 1 and according to the given table, the increase in life expectancy will be equal to 20 years. Because the allocation of 1 medical group in the country 2does not leave any group for the country 3, therefore Life expectancy is zero for the country 3, so f_{2}(x_{2},s) becomes 20+0.

If s=2, that means 2 medical groups can be allocated in countries 2 and 3, and since x_{2}=1, one group in the country 2 and one group in the country 3 should be allocated. In this case f_{2}(x_{2},s) is equal to 20 (life expectancy by assigning one group in the country 2) + 50 (life expectancy by assigning one group in the country 3).

The f_{2}^{*}(s) column is equal to the maximum f_{2}(x_{2},s) for all x_{2}, and x_{2} corresponding to higher values is placed in x_{2}^{*}.

In the table of the last stage (n=1), since no allocation has been made, there are 5 medical groups left that can be allocated to countries 1, 2 and 3. If x_{n}=0, it means that no medical group is assigned in the country 1 and the remaining 5 groups are used for the countries 2 and 3. Therefore, the optimal value is equal to 160. The way to calculate other values is similar to table n=2. The optimal allocation of medical groups in the form of x_{1}=1, x_{2}=3, x_{3}=1, will cause 170 years of life expectancy.

**Example:**

The activity of a workshop fluctuates a lot in different seasons. Due to the high cost of training and hiring new workers, the factory manager does not want to fire additional workers. On the other hand, he does not want to have a large number of workers working during the off-season. Due to the nature of the products manufactored in this workshop, it is not possible to manufacture and store the products. With these conditions, the factory manager wants to know the policy of hiring workers in different seasons. The number of workers required for each season is shown in the table below.

The number of workers in each season cannot be less than the above values. Each additional worker costs about $2,000 per season. In addition, the costs of hiring/firing workers are equal to the product of $200 multiplied by the square of the difference in the employment level of two consecutive seasons. The number of workers can be a fraction because it is possible to hire part-time. What should be the level of employment in each season to minimize the cost:

**Solution:**

n: number of chapters

x_{n}: employment level in the nth season

S: Recruitment level

r_{n}: minimum manpower required in the nth stage

We start at step n=4 (spring).

In n=4 (spring), because the number of workers cannot exceed 255, therefore, section 2000 (x_{n}-255) is equal to zero.

For n=3 (winter), f_{3}(x_{n},s) becomes as follows:

The number of workers in winter is equal to x_{3}, and because at the beginning of this season, s workers come from the previous season, so (x_{3}-s) workers must be hired or fired. The cost of hiring and firing (x_{3}-s) will be equal to 200(x_{3}-s)^{2}. Considering that the required number of workers is equal to 200, if the number of workers in this season (x_{3}) exceeds 200, additional worker fee should be paid. The cost of stage 4 (spring) is obtained as the number of workers in this season (x_{3}). Since, at the beginning of the spring, the the number of workers is equal to x_{3}, the total hiring cost is equal to 200(255-x_{3})^{2}.

In order to obtain the minimum of the function f_{3}(x_{n},s), we must take the partial derivative of this function in terms of x_{3}, and therefore we have:

Then

Because the second derivative is positive, therefore x_{3}^{*} is minimum. Because (s+250)/2 is in the range of (245,252.5), therefore (s+250)/2 is the optimal value of x_{3}. After replacing x_{3} with (s+250)/2 in f_{3}(x_{3}^{*},s) we have:

For stage n=2 (autumn), the calculations are done as follows:

To find x_{2}^{*}, the following model is solved:

By taking the derivative and setting the derivative equal to zero, we will have:

If 240≤s≤255, then x_{2}* =(2s+240)/3. But if 220≤s≤240, x_{2}* will be less than 240 and the minimum f_{2}(s,x_{2}) will be within 220≤ s≤240 occurs at x_{2}*=240. The summary of the results for n=2 is given in the following table:

For n=1 (summer) we have:

The value of f_{2}^{*}(x_{1}) has two different values in the interval 220≤x_{1}≤240 and 240≤x_{1}≤250, so f_{1}(s,x_{1}) can be written as follows:

To find the optimal value of f_{1}(s,x_{1}), first assume that x_{1}≤240, the derivative of f_{1}(s,x_{1}) in terms of x_{1} is as follows:

Considering that s=255, then x_{1}^{*}=240.9, so for all values of x_{1}≤240 we have:

Therefore, the minimum occurs in the interval x_{1}≤240 for x_{1}=240.

If 240≤x_{1}≤255 then:

And

Because at this stage, s=255, so in the interval 240≤x_{1}≤255, minimum f_{1}(s,x_{1}) is created for x_{1}=247.5, which is less than the minimum value of the objective function in the interval 220≤x_{1}≤240, so we have:

The summary of the n=1 results is given in the following table:

Therefore, the optimal policy is as follows and the total cost is 185,000.

**Example**

Solve the following linear programming using the dynamic programming method.

**Solution:**

n (the number of decision variables) is equal to 2.

(R_{1},R_{2},R_{3}) is the state vector where R_{i} is the residual value of source i.

x_{i} is the value of the i-th variable.

For n=2 , the value of function f_{2}(R_{1},R_{2},R_{3},x_{2}) is equal to 5x_{2} where the values of x_{2} must satisfy the constraints of 2x_{2}≤R_{2} and 2x_{2}≤R_{3}. Therefore, to calculate x_{2}^{*}, the following model must be solved:

The optimal solution of the above model is as follows:

For n=1

Because resources are not used in the first stage, it is R_{1}=4, R_{2}=12, R_{3}=18. Therefore, f_{1}^{*}becomes as follows.

notice that:

As (A) and (B), we have:

Considering that in x_{1}=2, both terms x_{1}-4.5, 3x_{1}+30 are maximized, so x_{1}^{*}=2 becomes. Then we have (R_{1},R_{2},R_{3})=(4-2,12-0,18-3*2)=(2,12,12) and x_{2}^{*}=6 and the total profit will be equal to 36.

**Example **

The owner of a chain store bought 5 boxes of strawberries to sell in three of his branches. The amount of strawberry sales in these three branches is unknown. Therefore, the store owner wants to allocate 5 boxes to three branches in such a way that the mathematical expectation of the total profit is maximized. It is possible that a store does not have strawberries. The following table shows the mathematical expectation of profit according to the number of strawberry boxes allocated.

Using dynamic programming, determine how to allocate these 5 boxes to three branches so that the mathematical expectation of total profit is maximized.

**Solution:**

x_{n}: number of strawberry boxes assigned to branch n.

p_{n}(x_{n}): Expected sales profit from allocating x_{n} boxes to branch n

s_{n}: Number of remaining boxes to allocate to branches

The number of stage is equal to 3.

s_{3}=0: In this case, there is no box left for allocation to branch 3, and therefore no profit can be expected.

s_{3}=1: In this case, one box is left and a profit of 4 units is earned from the allocation of this box.

x_{2}=0,s_{2}=0: In this case, there is no box left to be allocated to branch 2, and therefore, no profit is earned in branch 2 and branch 3.

x_{2}>0,s_{2}=0: Since there is no box left, it is not possible to assign the box to branch 2 and branch 3, and therefore it is indicated by -.

x_{2}=1, s_{2}=1: In this case, one box is left for allocation, and this box is allocated to branch 2, and there is no box left for branch 3, so the resulting profit is equal to 0+6.

x_{2}=2, s_{2}=3: In this case, there are 3 boxes left for allocation, which are two boxes for branch 2 and one box for branch 3, so the profit is equal to 4+11=15.

x_{2}=3, s_{2}=5: In this case, five boxes are left for allocation, three boxes are allocated to branch 2 and the remaining two boxes are allocated to branch 3. Therefore, the profit is equal to 9+15=24.

This problem has two optimal solutions as follows:

**Example:**

A student has 7 days a week to prepare for 4 exams. It takes at least one day to prepare each lesson. This student has decided not to study more than one lesson in a day to focus more. Therefore, he can spend one, two, three, or four days for each lesson. How can he prepare himself for the exam of 4 subjects by using dynamic programming. The score of this student for each subject is predicted as follows according to the number of days he studies:

**Solution:**

x_{n}: equal to the number of days studied for the n-th course

p_{n}(x_{n}): the grade obtained after x_{n} days of studying the n-th course

s_{n}: number of days left for study

The number of stage is 4.

s_{3}=2 and x_{3}=1: In this case, there are two days left for studying, one day of which is allocated to lesson 3, and the remaining day can be allocated to lesson 4. His expected grade is 4+4=8.

s_{3}=3 and x_{3}=1: In this case, there are three days left for studying, one day is used to study the lesson 3 and the remaining two days are allocated to the lesson 4. His expected grade is 4+4=8.

s_{3}=4 and x_{3}=2: In this case, there are four days left for studying, two days are used for studying the lesson 3 and the remaining two days are used for the lesson 4. His expected grade is 4+6=10.

s_{3}=5 and x_{3}=3: In this case, there are five days left for studying, three days are used for the lesson 3 and the remaining 2 days are for lesson 4. Therefore, his expected grade is equal to 7+4=11

s_{2}=3 and x_{2}=1: In this case, there are three days left for studying, one day is considered for the lesson 2. In this case, his expected grade is 5+8=13.

s_{2}=4 and x_{2}=2: In this case, there are four days left for studying, and two days are left for the lesson 2, in which case his expected grade is 8+6=14.

**Note:** Since, the student must allocate at least one day for each lesson, the remaining days for lesson 2 should not be less than three days.

Considering that the lesson 1 is at the beginning of the week, there are seven days left to study.

s_{1}=7 and x_{1}=3: In this case, there are seven days left for studying, three days are used for the lesson 1, and the rest are used for the next lessons. then his expected grade is 6+15=21. Based on the above table, the optimal solution is as follows:

**Example:**

The official of a political party plans the election campaign. For this purpose, he can use the services of five people to work in four areas. If an assistant works in more than one area, his efficiency will decrease, so each assistant is assigned to one area. Also, the areas can remain without assistants. It is estimated that the increase in the number of votes in each area according to the assigned assistants is described in the following table:

**Solution:**

x_{n}: the number of active assistants in the n-th field

p_{n}(x_{n}): number of votes when the assistant is assigned.

s_{n}: the number of unassigned assistants up to the nth stage.

The number of stages is 4.

If x_{3}=0, s_{3}=1, then there is only one assistant left for areas 3 and 4, which does not belong to area 3 and is assigned only to area 4, and the number of votes is equal to 0+3=3.

If x_{3}=1, s_{3}=2, then there are two assistants left for areas 3 and 4, one assistant is assigned to area 3 and one assistant is assigned to area 4. Then the number of votes is equal to 5+3=8.

If x_{3}=3, s_{3}=3, then three assistants are left to be assigned to areas 3 and 4, three assistant are assigned to area 3 and no one is assigned to area 4. Then the number of votes is equal to 11+0=11.

If and x_{3}=2, s_{3}=5, then five assistants are left to be allocated to areas 3 and 4, two assistants are allocated to area 3 and three assistants are allocated to area 4. Then the number of votes is equal to 12+9=21.

Based on the above tables, the optimal solution is as follows:

**Example:**

Solve the following linear programming problem using dynamic programming.

**Solution:**

Consider S=(R_{1},R_{2}) where R_{i} is the lack of i-th constraint.

The maximum value of f_{1}(.) occurs at x_{1}=2 and then f_{1}^{*}=50. Thus, the optimal solution is as follows.

**Example:**

Consider the following problem with initial cost. Solve this problem using dynamic programming.

**Solution:**

Solution: Consider s=(R_{1},R_{2}) where Ri is the lack of the i-th constraint.

Therefore, the optimal solution is as follows.

**Exercise:**

Solve the following nonlinear and integer programming problem using dynamic programming.

**Solution:**

State is the remaining value of the constraint. The number of stage is equal to 2.

The optimal solution is as follows:

**Example:**

Consider the following integer non-linear programming model. Calculate the optimal value using the dynamic programming method.

**Solution:**

The state is the remaining value of the constraint and the number of stage is equal to 3. The goal is to find the value of f_{1}^{*}(20):

Considering that for all the numbers between 0 and 4, the optimal value and f_{3}^{*}(s_{3}) and x_{3}^{*} are the same, so s_{3} is shown as an interval of 0 – 4 in the table. If s_{3} goes from 5 to 9, then x_{3} can be one and the value of f_{3}^{*}(s_{3}) is equal to 20.

Similar to table n=3, s_{2} is shown as an interval for convenience similar to table n=3.

Suppose s_{2}=5-6 and x_{2}=0, in this case x_{3} can be 1 and the value of the objective function equal to 20+0=20.

Suppose s_{2}=10-11 and x_{2}=1, in this case 5x_{3}≤3 – 4, so x_{3}=0. The value of the objective function is equal to 30+0=30.

Suppose s_{2}=17-18 and x_{2}=2, in this case s_{3}=3 – 4, so x_{3}=0. The value of the objective function is equal to 0+6=6.

The optimal solution is as follows:

**Example:**

Solve the following nonlinear programming model using dynamic programming.

**Solution:**

s_{n} is the amount of material remaining from the source in the nth step.

Therefore, x_{3}^{*}=2 is a maximum point.

According to the above, x_{1}^{*}=2 is a maximum point and we have: f_{1}^{*}(4,2)=8.

According to the above, x_{1}^{*}=1 is a minimum point.

To find the maximum solution, you should check the corner points:

Then the optimal solution is calculated.