0% found this document useful (0 votes)
81 views

Lecture 25: Dynamic Programming: Matlab Code: University of Southern California

This document summarizes a lecture on dynamic programming and provides examples using Matlab code. It introduces dynamic programming as an alternative search strategy that is faster than exhaustive search and solves problems by breaking them down into subproblems. The document provides an example of finding the shortest path through a network and represents it as a dynamic programming problem. It demonstrates representing the problem and solution in Matlab code by defining the network costs in a costs matrix.

Uploaded by

Laercio
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

Lecture 25: Dynamic Programming: Matlab Code: University of Southern California

This document summarizes a lecture on dynamic programming and provides examples using Matlab code. It introduces dynamic programming as an alternative search strategy that is faster than exhaustive search and solves problems by breaking them down into subproblems. The document provides an example of finding the shortest path through a network and represents it as a dynamic programming problem. It demonstrates representing the problem and solution in Matlab code by defining the network costs in a costs matrix.

Uploaded by

Laercio
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Lecture 25: Dynamic Programming: Matlab Code

University of Southern California

Linguistics 285

USC Linguistics

December 1, 2015

Linguistics 285 (USC Linguistics) Lecture 25: Dynamic Programming: Matlab Code December 1, 2015 1/1
Dynamic Programming Approach

I Dynamic Programming is an alternative search strategy that is faster


than Exhaustive search, slower than Greedy search, but gives the
optimal solution.
I View a problem as consisting of subproblems:
I Aim: Solve main problem
I To achieve that aim, you need to solve some subproblems
I To achieve the solution to these subproblems, you need to solve a set
of subsubproblems
I And so on...
I Dynamic Programming works when the subproblems have similar
forms, and when the tiniest subproblems have very easy solutions.

Linguistics 285 (USC Linguistics) Lecture 25: Dynamic Programming: Matlab Code December 1, 2015 2/1
Dynamic Programming

B D G

A
E H J
C
F I

I So we now have 2 subproblems VBJ and


I Main Problem: Shortest path VCJ . If we could solve those subproblems,
from A to J: VAJ we could solve the main problem VAJ .
I DP Thinking: Let’s say I know I But now we can think of VBJ as its own
the best paths from B to J and problem, and then repeat the thinking: If I
C to J: VBJ and VCJ . Then we knew the solutions to VDJ , VEJ , and VFJ ,
would add VBJ to the cost from then we can solve VBJ . Same with VCJ .
A to B, VCJ to the cost from A I Continue thinking in this way till we get to:
to C, compare the two, and VGJ , VHJ , VIJ , which are easy to solve!
pick the least.
Linguistics 285 (USC Linguistics) Lecture 25: Dynamic Programming: Matlab Code December 1, 2015 3/1
Dynamic Programming

B 5
D 8
G
9
12
6 7 5
A 14
7
9
E 10 10
H 9
J
7
10
C 9
7 8
11
F 8
I
8 9
< 8 + VGJ =
8 9 VDJ = min 7 + VHJ
: ;
<5 + VDJ = 10 + VIJ
VBJ = min 6 + VEJ VGJ = 5
: ; 8 9
⇢ 9 + VF J < 9 + VGJ =
12 + VBJ VHJ = 9
VAJ = min VEJ = min 7 + VHJ
7 + VCJ 8 9 : ;
<14 + VDJ = 9+V IJ
VCJ = min 10 + VEJ VIJ = 8
: ; 8 9
11 + VF J <10 + VGJ =
VF J = min 7 + VHJ
: ;
8+V IJ

Linguistics 285 (USC Linguistics) Lecture 25: Dynamic Programming: Matlab Code December 1, 2015 4/1
Dynamic Programming solution in Matlab
2, 1 5
3, 1 8
4, 1
9
12
6 7 5
1, 1 14
7
9
3, 2 10 10
4, 2 9
5, 1
7
10
2, 2 9
7 8
11
3, 3 8
4, 3
8 9
< 8 + VGJ =
8 9 VDJ = min 7 + VHJ
: ;
<5 + VDJ = 10 + VIJ
VBJ = min 6 + VEJ VGJ = 5
: ; 8 9
⇢ 9+V FJ < 9 + VGJ =
12 + VBJ VHJ = 9
VAJ = min VEJ = min 7 + VHJ
7 + VCJ 8 9 : ;
<14 + VDJ = 9 + VIJ
VCJ = min 10 + VEJ VIJ = 8
: ; 8 9
11 + VF J <10 + VGJ =
VF J = min 7 + VHJ
: ;
8 + VIJ

Linguistics 285 (USC Linguistics) Lecture 25: Dynamic Programming: Matlab Code December 1, 2015 5/1
Dynamic Programming notation: distances

I cost (or distance) of going from stage 1, state 1 to stage 2, state 1


I d(1, 1, 2, 1)
I cost (or distance) of going from stage 1, state 1 to stage 2, state 2
I d(1, 1, 2, 2)
I cost (or distance) of going from stage 2, state 1 to stage 3, state 2
I d(2, 1, 3, 2)
I cost (or distance) of going from stage k, state i to stage k+1, state j
I d(k, i, k + 1, j)

Linguistics 285 (USC Linguistics) Lecture 25: Dynamic Programming: Matlab Code December 1, 2015 6/1
Dynamic Programming notation: Minimum cost from a
node to the end

I minimum cost of going from stage 4, state 1 to end


I V (4, 1)
I minimum cost of going from stage 3, state 2 to end
I V (3, 2)
I Evaluating:
I V (4, 1) = 5
 
d(3, 2, 4, 1) + V (4, 1)
I V (3, 2) = min d(3, 2, 4, 2) + V (4, 2)
 
d(3, 2, 4, 3) + V (4, 3)
I Generalizing:
I V (k, i) = minj (d(k, i, k + 1, j) + V (k + 1, j))

Linguistics 285 (USC Linguistics) Lecture 25: Dynamic Programming: Matlab Code December 1, 2015 7/1
Matlab code for DP: Defining the network

costs(1,1,2,1) = 12;
costs(1,1,2,2) = 7;
2, 1 5
3, 1 8
4, 1 costs(2,1,3,1) = 5;
costs(2,1,3,2) = 6;
costs(2,1,3,3) = 9;
9 costs(2,2,3,1) = 14;
12 costs(2,2,3,2) = 10;
6 7 5 costs(2,2,3,3) = 11;
1, 1 14
costs(3,1,4,1) = 8;
7 costs(3,1,4,2) = 7;

9
3, 2 10 10
4, 2 9
5, 1 costs(3,1,4,3) = 10;
7 costs(3,2,4,1) = 9;
costs(3,2,4,2) = 7;
10
2, 2 9 costs(3,2,4,3) = 9;
costs(3,3,4,1) = 10;
7 8 costs(3,3,4,2) = 7;
11 costs(3,3,4,3) = 8;
costs(4,1,5,1) = 5;
3, 3 8
4, 3 costs(4,2,5,1) = 9;
costs(4,3,5,1) = 8;
num_states = [1 2 3 3 1];

Linguistics 285 (USC Linguistics) Lecture 25: Dynamic Programming: Matlab Code December 1, 2015 8/1
Matlab code for DP
I Using this generalized form, we can write a Matlab program, using
nested loops, that will start at the end and compute V (k, i) for every
node recursively.
I The last one we compute will be V (1, 1) which is the length of the
minimum path from beginning to end.
d = costs;
V(5,1)=0;
for k=4:-1:1
for i=1:num_states(k)
for j = 1:num_states(k+1)
path_length(j)= d(k,i,k+1,j)+V(k+1,j);
end
V(k,i)=min(path_length);
clear path_length
end
end
V(1,1)

Linguistics 285 (USC Linguistics) Lecture 25: Dynamic Programming: Matlab Code December 1, 2015 9/1
Matlab code for DP
I Why is this incomplete?
I We know the minimum length path, but we don’t know which states
it passes through.
I Now start at the begining. Add the cost of going from stage k to
each of the nodes at stage k + 1.
I Find which total is minimal and choose the corresponding state in
stage k + 1.
path = 0;
index = 1;
for k=1:4
for j=1:num_states(k+1)
path_length(j)= d(k,index,k+1,j)+V(k+1,j);
end
[minval, index] = min(path_length);
path(k) = index;
clear path_length
end
path
Linguistics 285 (USC Linguistics) Lecture 25: Dynamic Programming: Matlab Code December 1, 2015 10 / 1

You might also like