Lecture 25: Dynamic Programming: Matlab Code: University of Southern California
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
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
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
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
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