Sie sind auf Seite 1von 41

Operations Research I

Nadia Brauner

Nadia.Brauner@imag.fr

Dynamic Programming

1
Dynamic Programming
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

Plan

1 Some games

2 Principle of Optimality

3 Dynamic Programming

4 The knapsack

5 Typical problems

3
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

Plan

1 Some games

2 Principle of Optimality

3 Dynamic Programming

4 The knapsack

5 Typical problems

4
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

Some games

The snake in the pyramid

Find a path from the top to the bottom of the pyramid that
maximizes the sum of the numbers encountered
3
7 4
2 4 6
8 5 9 3
6 7 4 2 8

3 3
7 4 7 4
2 4 6 2 4 6
8 5 9 3 8 5 9 3
6 7 4 2 8 6 7 4 2 8

5
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

Some games

The snake in the pyramid

33
710 47
212 414 613
820 519 923 316
626 727 427 225 826

6
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

Some games

Sharing coins

Divide the following n coins into two equal sets


{5 9 3 8 2 5}

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 V V
2 V V V V
3 V V V V V V V
4 V V V V V V V V V

Construct the table :


m(i, j) = V if I can have a total of j with the first i coins
m(i, 0) = V for i = 0 to n
m(i, j) = m(i − 1, j) or m(i − 1, j − coin(i))
i = 1 to n and j = coin(i) to 16.

7
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

Some games

Dynamic Programming or the art of recursion


term introduced by Bellman in 1950
Programming, fashionable word
Dynamic, positive word, suggests evolving processes
General method (not dedicated to a specific problem)
Problems having structural properties

8
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

Plan

1 Some games

2 Principle of Optimality

3 Dynamic Programming

4 The knapsack

5 Typical problems

9
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

Principle of Optimality

Solve problem P for instance I


Specific structure of P
Pieces of an optimal solution are optimal

P1 P2 P
3

Problem P can be decomposed into sub-problems P1 , . . . , Pk


The optimum on P is obtained from the optima of the sub
problems

10
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

Principle of Optimality

Principle of Optimality
The optimum on instance I can be constructed from optimal
solutions for simpler instances I1 , . . . , Ik

OPT (I ) = f (OPT (I1 ), . . . , OPT (Ik ))

recursive formulation of OPT (I )


It is enough to calculate the optimum for
OPT (I1 ), . . . , OPT (Ik ) and to apply f
Each OPT (Ij ) is then expressed in terms of simpler instances
Until a basic instance, I (directly computable) is obtained

11
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

Principle of Optimality

Recursive computation of the optimum


I1
I
I2

I3

12
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

Plan

1 Some games

2 Principle of Optimality

3 Dynamic Programming

4 The knapsack

5 Typical problems

13
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

Dynamic Programming

Shortest paths in DAG is easy

DAG
DAG : Directed Acyclic Graph
nodes can be linearized
all arcs from left to right
in DP
the DAG is implicit
nodes : sub-problems
edges : dependencies between sub-problems
If the vertices are viewed from left to right, all predecessors are
known : the information needed is available
Every DP has an underlying DAG structure
14
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

Dynamic Programming

We study problems that are getting larger


increase the values of numbers
or the quantity of information needed
Paradigm :
identify a collection of sub-problems and solve them one by
one (the smallest first)
use the information on the smallest to solve the largest

15
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

Dynamic Programming

Classical problems in computer science


(Algorithms by Dasgupta, Papadimitriou and Vazirani, Chapter 6)
longest increasing subsequences
edit distance and genetics
matrix multiplication
shortest paths
Independent set in trees

16
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

Dynamic Programming

The steps
Find the sub-problems
Find the relation (write down the recurrence formula)
Find an order that respects the recurrence
Define the base cases

17
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

Dynamic Programming

Some principles
In general, do not use recursion
risk of calculating many times the same sub-problem
If recursivity is interesting, use memoisation
If only a subset of the sub-problems has to be computed
In a state, store the necessary information to reach the
optimum
Dominance properties to decrease the number of states

18
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

Dynamic Programming

Complexity
Time complexity : often based on the number of states
(nodes of the DAG)
Space complexity : needs some thought.
Do not necessarily store all the states...
See page 178 of the book Algorithms by Dasgupta et al.

19
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

Plan

1 Some games

2 Principle of Optimality

3 Dynamic Programming

4 The knapsack

5 Typical problems

20
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

Combinatorial problems

A typical combinatorial optimization problem


INSTANCE : A set of objects 1, . . . , n, with weights ci
FEASIBLE SOLUTIONS : A set F of parts of {1, . . . , n}
CRITERION maximize
X
c(S) = ci
i∈S

The set F is in general defined by constraints.


Its cardinality can be very large (here potentially 2n )

21
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

The knapsack

Example
A hiker wants to fill his knapsack of capacity 6kg with the most
useful objects
utility weight
A a picture 12 0.2
B a bottle 7 2
C another bottle 3 2
D a pullover 4 0.8
E chocolate bars 5 1
F dried fruit 8 1

22
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

The knapsack

Classical optimization problem


use as much capacity as possible
Selection of investments
Appears in more complex problems

Model
INSTANCE :
FEASIBLE SOLUTIONS :
CRITERION :

23
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

The knapsack

Decomposition into subproblems

Instance I to be solved
Partition according to object n
F 0 = {S ∈ F|n ∈ / S} does not contain n
00
F = {S ∈ F|n ∈ S} contains n
We obtain OPT (I ) = max{c(S 0∗ ), c(S 00∗ )}
24
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

The knapsack

Decomposition into subproblems

F’ F’’

Two sub-problems to be solved

On F 0 : problem P restricted to the first n − 1 objects


On F 00 : again restricted to the first n − 1 objects
but structure of feasible solutions ? 25
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

The knapsack

Decomposition into subproblems

F’ F’’

Describing F 00 as {S ∈ F|n ∈ S} is ineffective


⇒ explicit enumeration of all solutions
F 00 must be described as a sub problem of P
26
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

The knapsack

Knapsack
Instance: n objects of weight wi and of utility ui , a
knapsack of size W .
Solution: subset S of objects such that w (S) ≤ W .
Criterion: the total utility u(S) of the objects

What is the value of OPT (I ) regarding object n ?


How to write the optimality principle ?

27
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

The knapsack

Parametrization
Optimality principle : the problems which appear in the
decomposition correspond to the initial problem on simpler
instances
Instance I 0 for the sub-problem
⇒ I 0 differs from I by some parameters (integers) p1 , . . . , pl
For the knapsack : the objects under consideration and the
size of the knapsack
We describe I 0 by the value of its parameters (x10 , . . . , xl0 )

Definition
A state is the vector of parameters (x1 , . . . , xl ) which describes the
sub-instance

28
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

The knapsack

State graph
Parameter vector (x1 , . . . , xl ) : state
Dependencies between the instances (calculation of f )

parametre 2

I
3

0
parametre 1
0 1 2 3 4 5 6

29
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

The knapsack

Knapsack
Instance: n objects of weight wi and of utility ui , a
knapsack of size W .
Solution: subset S of objects such that w (S) ≤ W .
Criterion: the total utility u(S) of the objects

Draw the state graph for 4 objects of weight 1 and a knapsack


of capacity 3.
What can you notice ?

30
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

The knapsack

A state can be calculated many times


Idea : do not use recursion
Memorize the states instead of recalculating them
Just browse the states in an inverse topological order in the
state graph

Evaluate the base cases OPT [0, . . . , 0].


Browse the states until X̄
For each state X , which depends on
X1 , . . . , Xk already evaluated memorize

OPT [X ] = f (OPT [X1 ], . . . , OPT [Xk ))

Return OPT [X̄ ]

31
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

The knapsack

Knapsack of size 7, with 4 objects


values of the objects 2 4 5 6
weight of the objects 2 3 4 5
Calculate table OPT

32
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

The knapsack

Effectiveness
What is the resolution time ?
It depends on
the number of states
the time t to evaluate function f in each state
The resolution time is then
X
t(x1 , . . . , xl )
(x1 ,...,xl )∈States

Often, one has a bound on t(x1 , . . . , xl ) ≤ T


The resolution time is upper bounded by

T × #States

33
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

The knapsack

Resolution time for the knapsack


What is the time to evaluate state (i, w ) ?
What is the number of states ?

34
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

The knapsack

Calculation of an optimal solution


Dynamic programming provides OPT (I )
How to obtain a solution S ∗ ?
Keep the pointers in the table : path in the state graph
Backtracking method

Describe an optimal solution for the knapsack from the table


OPT

35
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

Plan

1 Some games

2 Principle of Optimality

3 Dynamic Programming

4 The knapsack

5 Typical problems

36
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

Typical problems

Shortest paths
Shortest paths
Shortest reliable paths
All pairs shortest path (Floyd Warshall)
Travelling Salesman Problem
Notice that conditions on the distances (positive) or on the graph
(no absorbing cycle) are necessary

37
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

Typical problems

Inventory problems
time decomposed into periods
a demand for each period
limited production capacities
fixed or unitary production costs
limited storage capacity
unitary storage costs
Determine for each period the production in order to minimize the
costs

38
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

Typical problems

Resource allocation
w units of a resource available
T activities to which the resource can be allocated
If activity t is implemented at a level xt ≥ 0 then
gt (xt ) units of the resource are used,
a benefit rt (xt ) is obtained.
Objective : determine the allocation of resources that
maximizes the total benefit
If gt and rt are linear ⇒ LP
If the values of xt are discrete ⇒ DP

39
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

Typical problems

Equipment replacement problems


How long should a machine to be kept before being replaced by a
new one

40
Some games Principle of Optimality Dynamic Programming The knapsack Typical problems

Conclusion on Dynamic Programming

Paradigm that can be highly effective


No condition on the objective function...
. . .but the optimality principle has to be verified
Uses a lot of memory
Becomes ineffective if the state space si too large
Need to find dominance to reduce it

41

Das könnte Ihnen auch gefallen