Path Planning LAB: Dynamic Programming
Path Planning LAB: Dynamic Programming
LAB
Dynamic Programming
Submitted by:
Introduction:
Dynamic programming is a widely used mathematical technique for solving problems
that can be divided into stages and where decisions are required in each stage. dynamic
programming is a method for solving a complex problem by breaking it down into a
collection of simpler subproblems, solving each of those subproblems just once, and storing
their solutions. The next time the same subproblem occurs, instead of recomputing its
solution, one simply looks up the previously computed solution, thereby saving computation
time at the expense of (it is hoped) a modest expenditure in storage space. (Each of the
subproblem solutions is indexed in some way, typically based on the values of its input
parameters, so as to facilitate its lookup.
Dynamic programming algorithms are often used for optimization. A dynamic
programming algorithm will examine the previously solved sub-problems and will combine
their solutions to give the best solution for the given problem. In comparison, a greedy
algorithm treats the solution as some sequence of steps and picks the locally optimal choice at
each step. Using a greedy algorithm does not always guarantee an optimal solution, whereas a
dynamic programming algorithm does, because picking locally optimal choices may result in
a bad global solution
Flow Chart:
Decisions
Making decisions at one stage transforms one state of the current stage into a state in
the next stage.
In a geographical example, it could be a decision to go from one city to another.
In resource allocation problems, it might be a decision to create or spend a certain
amount of a resource.
States
Each stage has a number of states associated with it. Depending on what decisions are
made in one stage, the system might end up in different states in the next stage.
If a geographical region corresponds to a stage then the states associated with it could
be some particular locations (cities, warehouses, etc.) in that region.
In other situations, a state might correspond to amounts of certain resources, which
are essential for optimizing the system.
Exercise
Develop a MATLAB® program to solve the given problem using dynamic programming.
After the program is complete, make necessary changes in the code so that every input
required will be entered by the user.
Code:
%A=1;B=2;C=3;D=4;E=5;F=6;G=7;H=8;I=9;J=10;
clc;
clear all;
Links = [2 3 4;
5 6 7;
5 6 7;
5 6 7;
8 9 0;
8 9 0;
8 9 0;
10 0 0;
10 0 0];
Distances = [2 4 2;
7 4 8;
3 2 4;
4 1 5;
1 4 0;
6 3 0;
3 3 0;
3 0 0;
4 0 0];
stages = 5;
stage_indices = [1 0 0;
2 3 4;
5 6 7;
8 9 0;
10 0 0];
selectedNode = 1;
total_distance = 0;
List = [];
%Creating Nodes
for i = 0:stages-1
nodes_in_stage = stage_indices(stages-i,stage_indices(stages-i,:)>0);
[~,l_size] = size(nodes_in_stage);
nodeUnderCons = nodes_in_stage(selectedNode);
List = [List nodeUnderCons];
[x,y] = find(Links>nodeUnderCons-1 & Links<nodeUnderCons+1);
if (x>0 & y>0)
for k = 1:max(size(x))
if k==1
d = Distances(x(k),y(k));
else
d = [d Distances(x(k),y(k))];
end
end
else
selectedNode = 1;
d = 0;
end
[~,selectedNode] = min(d);
total_distance = total_distance + min(d);
end
List = sort(List);
disp("Path:")
disp(List);
disp("Total Distance:");
disp(total_distance);
Output:
Conclusion
We conclude that by using dynamic programming on given problem we find a
combination of decisions that optimizes a certain amount associated with a system. It takes a
decision and find the optimal path along with different path.