prof. dr. taoufik nouri nouri@nouri.ch 06-11-2014traveling salesman problem: what is the best order...

Post on 10-Mar-2020

11 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Pathfinding

Prof. Dr. Taoufik NouriNouri@Nouri.ch

06-11-2014

Pathfinding

• Depth First Search• Breadth First Search• Dijkstra• A*

Pathfinding

• Pathfinding

AI-Game Engine

• Navigation 1:Soccer

Navigation 2: Raven

Pathfinding 1

• Pathfinding is the path between two nodesof a graph.

• A graph is a set of points called nodesconnected to each other throughconnections.

• There is an infinite number of paths fromone node to another.

• Usually pathfinding is the shortest waybetween two nodes

Pathfinding 2

• Graphs can have a cost associated to theconnections like distances etc.

• This kind of graphs is called weighted graph.• There is also, directional graphs: paths between

nodes is considered one way.• Graphs can be more complicated: Weighted

directional graphs, you can not go back.• A graph can be of any dimensions and any

shapes

PathfindingVery important for real world problems:

1. The airport system is a graph. What is the best flightfrom one city to another?

2. Traffic flow can be modeled with a graph. What arethe shortest routes?

3. Traveling Salesman Problem: What is the best orderto visit a list of cities in a graph?

Graph Representation

v1 v2

v3 v4 v5

v6 v7 v1v2v3v4v5v6v7

v1 v2 v3 v4 v5 v6 v70 1 1 1 0 0 00 0 0 1 1 0 0

We can use an “adjacencymatrix” representation.

For each edge (u,v) we set A[u][v] to true;else it is false. If there are weights associatedwith the edges, insert those instead.

v1 v2

v3 v4 v5

v6 v7 v1 v2 v4 v3v2 v4 v5v3 v6v4 v6 v7 v3v5 v4 v7v6v7 v6

We can use an “adjacencylist” representation.

For each vertex we keep a list of adjacent vertices.If there are weights associated with the edges,that information must be stored as well.

Adjacency List

Graph

GraphNodes are indexed nodes see class GraphNode GraphNodeTypes.h

class GraphEdge{protected:

//An edge connects two nodes. Valid node indices are always positive.int m_iFrom; int m_iTo;//the cost of traversing the edgedouble m_dCost;

public://ctorsGraphEdge(int from, int to, double cost):m_dCost(cost),

m_iFrom(from),m_iTo(to)

…}see GraphEdgeTypes.h

The graph can be a matrix or a lists

Pathfinding 3

• This is a weighted graph• Cities are nodes connected through roads-

distances-Relief-Terrainkante(saintgall, zurich, 80, Terrain etc).kante(zurich, basel, 85).kante(basel, bern, 100).kante(lausanne,fribourg,31).kante(lausanne , geneve, 100).kante(zurich , bern, 120).kante(bern, fribourg, 30).kante(bern , neuchatel, 48).kante(neuchatel , lausanne, 78).kante(zurich , solothurn, 99).kante(solothurn , biel, 27).kante(biel , neuchatel, 29).

Pathfinding 4• Usually, we define a graph as regularly spaced grid where we can travel to:

N, NW, NE, S, SW, SE etc.• Green is start position, Red is Target• Note: There is no obstacle in our grid

Pathfinding 5• Here is a graph with obstacle

Graph is a powerfull representation tool. But this power can be realizedwhen they are operated upon by algorithms designed to explore them,either to find a specific node or to find a path berween nodes. this is whatwe will do next

General search algorithm• Start with

– "frontier" = { (v,v) }• Until frontier is empty

– remove an edge (n,m) from the frontier set– mark n as parent of m– mark m as visited– if m = w,

• return– otherwise

• for each edge <i,j> from m– add (i, j) to the frontier

» if j not previously visited

Questions

Answer the following questions:• What is pathfinding?• Why is pathfinding useful or necessary• what is a graph• What is a weighted graph• What are the traditional types of

pathfinding methods

Pathfinding

• Depth First Search• Breadth First Search• Dijkstra• A*

Depth First Search DFSStack Parent

5-5 1->?2->?3->?4->?5->?6->?

Firt In Last Out

DFS

Stack Parent

5-25-45-6

1->?2->?3->?4->?5->56->?

The edges leading from node 5 are placed on the Stack

Node 5 is marked Visited

DFS

Stack Parent

2-15-45-6

1->?2->53->?4->?5->56->?

Node 2 is pushed on the Stack, Node 5 is parent of 2

Node 2 is marked Visited

DFS

Stack Parent

1-35-45-6

1->22->53->?4->?5->56->?

Node 1 is pushed on the stack, 2 is parent of 1

Node 1 is marked Visited

DFS

Stack Parent

5-45-6

1->22->53->14->?5->56->?

Node 1 is parent of 3, Edge 1-3 is poped fromthe stack

Node 3 is marked Visited, OHH Node 3 is the Target, the algorithm exit

•The path from S to T is stored in a vector m-Route inthe table parent.•A method getPathtoTarget will extract this informationand return a vector 5-2-1-3.•What about the path 5-4-3

DFS

• Depth limitation is the major problem of DFS• The path is not allways the shortest• It is easy to implement using recursion• Some situation DFS will make spagethi if not

restricted in depth• see GraphAlgorithms.h implementation

DFS in Action

Breadth First Search BFSThe BFS fans out from the source node and examines each of thenodes ist edges lead to before fanning out from those nodes andexamining all the edges they connect to and so on. look at that!!!

BFSQueue Parent

5-5

1->?2->?3->?4->?5->?6->?

First In First Out

BFS

Queue Parent

5-25-45-6

1->?2->?3->?4->?5->56->?

The edges faning from node 5 are placed on the Queue

Node 6, 4 and 2 are marked Visited

BFS

Queue Parent

4-35-2

5-6

1->?2->?3->?4->55->56->5

Node 6 pointed to node 4(visited),Node 6 is not placed on the Queue

Node 6 is marked Visited

BFS

Queue Parent

2-14-3

5-2

1->?2->53->?4->55->56->5

The edges leading from node 4 to 3 are placed on the Queue

Node 4 is marked Visited

BFS

Queue Parent

2-1

4-3

1->?2->53->44->55->56->5

Edge 4-3 is next. Node 4 is noted to be node 3's parent.Node 3 is Target. BFS exits.m_Routes Vector work back through the parents from T to S andwe get 3-4-5.

Node 3 is marked Visited

BFS in Action

•BFS returns allways the shortest path ifexist.•Simple to implement

•Very Slow on large graphe.•Doesn't consider path cost!

•see GraphAlgorithms.h implementation

DFS-BFS Summary

Depth first search begins by diving down as quickly as possible to the leaf nodes of the tree (or graph).Traversal can be done by:visiting the node first, then its children (pre-order traversal): a b d h e i j c f k gvisiting the children first, then the node (post-order traversal): h d i j e b k f g c avisiting some of the children, then the node, then the other children (in-order traversal) h d b i e j a f k c g

Breadth-first search traverses the tree or graph level-by-level, visiting the nodes of the tree above in theorder a b c d e f g h i j k.

Exercices

• How are the traditional types of pathfindingmethods implemented

• Implement in a pseudo-code the followingpathfinding techniques:BFS, DFS

Shortest Path Trees SPTDijkstra's Algorithm

This is a weighted graph.Weight is distance, cost etc.

How to find the shortest pathfrom a source node to a Targetnode?

Dijkstra's Algorithm

SPT

Starting from node 5, source.

S

TT

5

Dijkstra's Algorithm

SPT

The algorithm take edge 2 and 6.Added the shortest 2 (1.9) to the SPT

S

TT

5

2

Dijkstra's Algorithm

SPT

The algorithm examines edge 2-3, thecost to node 3 is 5(1.9+3.1).The cost to from node 5 to 6 is 3.0.Node 6 is added to SPT

S

TT

5

2 6

Dijkstra's Algorithm

SPT

The process is repeated once more.Cost to node 4 is less(3.0+1.1contra 1.9+3.1 to node 3.Node 4 is added to SPT

S

TT

5

2 6

Dijkstra's Algorithm

SPT

The process is repeated once more.Cost to node 3 is more(3.0+1.1+3.7contra 1.9+3.1 to node 3.Node 3 is added to SPT through 5-2-3

S

TT

5

26

43

The shortest path is:5-2-3 with the cost 5.0

Djikstra's Algorithm• Use a priority queue

– a data structure in which the item with the smallest "value" isalways first

– items can be added in any order• Use the "value" of an edge as the total cost of the path through

that edge– always expand the node with the least cost so far

• If an edge leads to a previously expanded node– compare costs

• if greater, ignore edge• if lesser, replace path and estimate at node with new value

• "Greedy" algorithm

Dijskra's Algoritm in Action

In this case the cost is the distance,from source to target

see GraphAlgorithms.h implementation

Best First Search A*• Dijkstra's algorithm searches by minimizing the cost of

the path in a directed weighet graph.• In some situations, we have partial knowledge of the

structure of the search space that can be applied toguide search.

• It can be improved by estimating the cost to the target.This estimate is usually referred to as a heuristic and thealgorithm is called Best-first search (A*).

• We can inspect all the currently-available transitions, andrank them on the basis of our partial knowledge. Herehigh rank means that the transition looks promising inrelation to the goal.

Best First Search A*• If good Heuristics are used,A* is guaranteed to

give the optimal paths.• Heuristics: any cost estimate to the goal, ex.

Euclidean distance, Block distance, it can becoupled with penalties or bonuses for traveling ona specific type of terrain(jungle, desert,forest,plain…).

• Performance of A* is based on the value ofHeuristics

• Poor heuristics provides bad performance with A*

Best First Search A*

Heuristic estimates• Max(dx, dy)• Manhatten or Blocks (dx+dy)• Euclidean sqrt(dx*dx + dy*dy)

Best First Search A*• You are at A going to Target T.• f(T) = g(A) + h(A).• f(T) total cost, g(A) cost until A, h(A) estimate cost from A to T.• We continue our search with the lowest f(T).

A* in Action

•see GraphAlgorithms.himplementation

Comparison No walls

Comparison with walls

Questions about A*

• What is A*?• What are the advantages of A*?• What are the disadvantages of A*?• How can heuristics be used to help A*?

Exercices

• Implement in a pseudo-code the followingpathfinding techniques: DJ,A*

Summary

• We introduced the concepts of : node,edge and graph.

• Pathfinding: BFS, BFS, DJ and best-firstsearch A*

• Their Advantages/Desadvantages• Demo and comparison• For source code/exe files s. [1]

Exercices

1. Solve with hand some path finder usingthese four algorithms.

2. Implement the DFS Method3. Implement the A* search Method4. Create a weighted Graph and walk

through it step by step with Dijkstra's5. Try Dijkstra's on graph with negative

weights6. Experiment with pathfinder chapt. 5 [1]

Exercices

7. Implement or read the implementations ofDFS, BFS, Dijkstra and A*

8. Read the solution of exercice 7 in chap. 5[1]

Other Implementations

In the next slide, you have the best search first using prolog.It is compact and efficient.

Demo--Vortrag

/* to use this programme, write:go(saintgall,geneve,Route).

The answer look like this: Route =[saintgall,zurich,bern,fribourg,lausanne,geneve]

*/

kante(saintgall, zurich, 80).kante(zurich, basel, 85).kante(basel, bern, 100).kante(lausanne,fribourg,31).kante(lausanne , geneve, 100).kante(zurich , bern, 120).kante(bern, fribourg, 30).kante(bern , neuchatel, 48).kante(neuchatel , lausanne, 178).kante(zurich , solothurn, 99).kante(solothurn , biel, 27).kante(biel , neuchatel, 29).

pfad(X, X).pfad(AnfP, EndP):- (kante(AnfP, ZP,Dist); kante(ZP, AnfP,Dist) ), pfad(ZP,

EndP).

pfad_ohne_zyklus(AnfP, EndP):- weg(AnfP, EndP, [AnfP]).

weg(X, X, Kantenzug) .weg(AnfP, EndP, Kantenzug):- (kante(AnfP, ZP, Dist);

kante(ZP, AnfP, Dist)),not(element(ZP, Kantenzug)),weg(ZP, EndP, [ZP | Kantenzug]).

element(E, [E | Rest]).element(E, [Kopf | Rest]):- element(E, Rest).

pfad_mit_knotenliste(AnfP, EndP, Pfad):- weg(AnfP, EndP, [], P),reverse(P, Pfad).

weg(X, X, Kantenzug, [X|Kantenzug]).weg(AnfP, EndP, T, Kantenzug) :- moeglicher_Knoten(AnfP, T, ZP),

weg(ZP, EndP, [AnfP|T], Kantenzug).

moeglicher_Knoten(AnfP, Kantenzug, ZP) :- (kante(AnfP, ZP,Dist);kante(ZP, AnfP,Dist)),not(element(ZP, Kantenzug)) .

reverse([], []).reverse([K|Rest], Neueliste):-reverse( Rest, TL),append(TL , [K], Neueliste).

append([], Liste , Liste).append([Kopf|Rest], Liste, [Kopf|Ergebnis]) :-append( Rest, Liste, Ergebnis).

pfad_mit_breitensuche(AnfP, EndP, Route) :- weg([[AnfP]], EndP, R), reverse(R, Route ).

moeglicher_Knoten( AnfP, Kantenzug, ZP) :- (kante(AnfP, ZP,Dist);kante(ZP, AnfP,Dist)),not(element(ZP, Kantenzug)).

collect_found(S, L) :-getnext(X), !,collect_found([X | S], L).collect_found(L, L).getnext(X) :- retract(found(X)), !, X \== mark.

append([], Liste, Liste).append([Kopf|Rest], Liste, [Kopf|Ergebnis]):- append( Rest, Liste, Ergebnis).

% Best first searchkurz_pfad(Routes, Dest, Route) :- shortest(Routes, Shortest, RestRoutes),

proceed(Shortest, Dest, RestRoutes, Route).% proceed finds all the legal extensions to the path and adds them to the list

proceed(r(Dist, Route), Dest,_, Route):- Route =[Dest|_].% r(M, P) M is the total length of the path and P is the list of places visitedproceed(r(Dist, [Last|Trail]), Dest, Routes, Route):-

findall(r(D1, [Z, Last|Trail]),legalnode(Last, Trail, Z, Dist, D1), List),

% write(Route),write(Dist),nl,% write(Routes),nl,% write([List]),nl,% write([D1,Z,Dist,D1,Last,Trail]),nl,

append(List, Routes, NewRoutes),kurz_pfad(NewRoutes, Dest, Route).

% shortest returns the shortest path on the listshortest([Route|Routes], Shortest, [Route|Rest]):-

shortest(Routes, Shortest, Rest),shorter(Shortest, Route),!.

shortest([Route|Rest], Route, Rest).shorter(r(M1,_), r(M2,_)):- M1 < M2.

% legal node adds the distance to the next town

legalnode(X, Trail, Y, Dist, NewDist) :- (kante(X, Y, Z); kante(Y, X, Z)),legal(Y, Trail), NewDist is Dist + Z.

legal(X, []). % legal ist gleich not member or not elementlegal(X, [H|T]) :- X \== H, legal(X, T).

% To start write: go(zurich,geneve,J).go(Start, Dest, Route) :- kurz_pfad([r(0, [Start])], Dest, R), reverse(R, Route),!.

Exercice: Best search using prolog

Pseudo-code of DFS

Pseudo-code of BFS

Pseudo-code of DJ

Pseudo-code of A*

Pseudo-code of Trace

top related