Algorithms¶
Note
- This page contains the pseudo code outlining the algorithms implemented.
- An effort has been made to implement the algorithm in a way that allows easy correspondence with the pseudo code, facilitating learning.
Important
- The pseudo code is written based on the original papers from which the algorithms are derived; slight modifications might be made, while retaining the essence of the algorithm.
- Minor modifications have been made to the implementation of Dijkstra to highlight it’s similarity of A*.
- Using the grid on which the algorithm runs to maintain cost would make the speed up the algorithms significantly for both A* and Dijkstra, and remove the necessity for using the closed list, maintaining cost in the open list.
Todo
- Highlight changes between pseudocode and implementation
Contents
Dijkstra¶
procedure main()
OL = 0 //Initialize the open list. Priority queue sorted by cost
CL = 0 //Initialize the closed list
OL <— sstart
while(!OL.empty())
q <— OL.pop()
for all s in Succ(q)
if(s=sgoal) return path found
if(s is obstacle) continue
s.parent <— q
s.cost = q.cost + distance between successor and q
if(s` = s in OL && s`.cost < s.cost)
continue
if(s` = s in OL && s`.cost < s.cost)
continue
OL <— s
CL <— q
return no path
A*¶
procedure main()
OL = 0 //Initialize the open list. Priority queue sorted by cost + heuristic cost value
CL = 0 //Initialize the closed list
OL <— sstart
while(!OL.empty())
q <— OL.pop()
for all s in Succ(q)
if(s=sgoal) return path found
if(s is obstacle) continue
s.parent <— q
s.cost = q.cost + distance between successor and q
s.h_cost = heuristic(s, sgoal)
if(s` = s in OL && s`.cost + s`.h_cost < s.cost + s.h_cost)
continue
if(s` = s in CL && s`.cost + s`.h_cost < s.cost + s.h_cost)
continue
OL <— s
CL <— q
return no path
LPA*:¶
procedure CalculateKey(s)
return [min(g(s), rhs(s)) + h(sstart, s); min(g(s),rhs(s))];
procedure Initialize()
U = 0;
for all s in S rhs(s) = g(s) = inf
rhs(sstart) = 0;
U.insert(sstart, CalculateKey(sstart));
procedure UpdateVertex()
if(u!=sstart) rhs(s) = minfor all s’ in Succ(u)c(u, s’) + g(s’)
if(u in U) U.remove(u)
if(g(u)!=rhs(u)) U.insert(u, CalculateKey(u));
procedure ComputeShortestPath()
while(U.TopKey() < CalculateKey(sgoal) OR rhs(sgoal) != g(sgoal))
u = U.Pop();
if(g(u) > rhs(u))
g(u) = rhs(u);
for all s in Succ(u) UpdateVertex(s);
else
g(u) = inf;
for all s in Succ(u) (union) {u} UpdateVertex(s)
procedure Main()
Initialize();
forever: // This runs for n times by default in current implementation
ComputeShortestPath();
Scan graph for changed edge costs;
if any edge cost changed // this occurs with probability 1/n for every iteration
for all directed edges (u,v) with changed edge costs
Update the edge cost c(u,v);
UpdateVertex(u)
D* Lite:¶
procedure CalculateKey(s)
return [min(g(s), rhs(s)) + h(sstart, s) + km; min(g(s),rhs(s))];
procedure Initialize()
U = 0;
km= 0;
for all s in S rhs(s) = g(s) = inf
rhs(sgoal) = 0;
U.insert(sgoal, CalculateKey(sgoal));
procedure UpdateVertex()
if(u!=sgoal) rhs(s) = minfor all s’ in Succ(u)c(u, s’) + g(s’)
if(u in U) U.remove(u)
if(g(u)!=rhs(u)) U.insert(u, CalculateKey(u));
procedure ComputeShortestPath()
while(U.TopKey() < CalculateKey(sstart) OR rhs(sstart) != g(sstart))
kold= U.TopKey();
u = U.Pop();
if(kold< CalculateKey(u))
U.insert(u, CalculateKey(u))
else if(g(u) > rhs(u))
g(u) = rhs(u);
for all s in Pred(u) UpdateVertex(s);
else
g(u) = inf;
for all s in Pred(u) (union) {u} UpdateVertex(s)
procedure Main()
slast= sstart
Initialize();
ComputeShortestPath();
while(sstart!=sgoal)
/ if(g(sstart)=inf) there is no known path /
sstart= arg minfor all s’ in Succ(s:sub:`start)`(c(sstart,s’)+g(s’)
Move to start
Scan graph for changed edge costs;
if any edge cost changed
km= km+ h(slast,sstart);
slast= sstart
for all directed edges (u,v) with changed edge costs
Update the edge cost c(u,v);
UpdateVertex(u)
ComputeShortestPath()
RRT¶
procedure main()
T = 0; // Initialize list/tree
for k = 1 to K
qrand← GenerateRandomNode()
qparent← FindNearestPoint(qrand, T, threshold) // Returns closest node based on distance
if(qparentexists)
qparent<— qparent
if(qrand= goal) return path found
return no path
RRTStar¶
procedure rewire()
for all node in close_node_list != qparent
if(qrand.cost + dist(node ,qrand) < node.cost)
node.parent <— qrand
node.cost <— qrand.cost + distance(node, qrand)
procedure main()
T = 0; // Initialize list/tree
for k = 1 to K
qrand← GenerateRandomNode()
qparent, close_node_list ← FindNearestPoint(qrand, T, threshold) // Returns node that results in lowest total cost and list of all close nodes // Updates qrand.cost to qparent.cost + distance(qrand, qparent)
if(qparentexists)
qparent<— qparent
if(qrand= goal) return path found
rewire()
return no path
Ant Colony Optimization¶
procedure main()
Initialize ant colony
for each iterations
initialize ants
for each ant
while ant position != goal and number of steps < max steps
calculate probabilities of possible next edges to traverse
choose next position based on probability
if not next positions possible, break
if reached revisited points, remove loop and reduce number of steps taken
update pheromone trail
update ant in ant colony
update pheromone trail by evaporation equation
update pheromone trails based on ants that reached the goal
if path found return last best path
else return no path
Genetic Algorithm¶
procedure main()
Initialize paths
for each generation
choose paths based on criteria
mutate paths (using CrossoverMutation()) to find new paths if number of paths less than set number
for each path
calculate cost and validity of path
if path valid add to paths list
if path found return last best path
else return no path
procedure CrossoverMutation()
randomly choose 2 chromosomes (paths) in path list
initialize new chromosome
for each gene in chromosome
choose either the gene from the first chromosome, form the second chromosome or a random gene based on probabilities set
add this gene to new chromosome
return new chromosome