pathfinding prof. dr. taoufik nouri [email protected] 06-11-2014

60
Pathfinding Prof. Dr. Taoufik Nouri [email protected] 06-11-2014

Upload: jasmin-palmer

Post on 17-Jan-2016

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Pathfinding

Prof. Dr. Taoufik [email protected]

06-11-2014

Page 2: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Pathfinding

• Depth First Search

• Breadth First Search

• Dijkstra

• A*

Page 3: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Pathfinding

• Pathfinding

Page 4: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

AI-Game Engine

• Navigation 1:Soccer

Page 5: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Navigation 2: Raven

Page 6: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Pathfinding 1

• Pathfinding is the path between two nodes of a graph.

• A graph is a set of points called nodes connected to each other through connections.

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

• Usually pathfinding is the shortest way between two nodes

Page 7: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Pathfinding 2

• Graphs can have a cost associated to the connections 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

Page 8: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Pathfinding

Very important for real world problems:1. The airport system is a graph. What is the best flight

from one city to another?

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

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

Page 9: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Graph Representation

v1 v2

v3 v4 v5

v6 v7 v1v2v3

v4v5v6v7

v1 v2 v3 v4 v5 v6 v7

0 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.

Page 10: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

v1 v2

v3 v4 v5

v6 v7 v1 v2 v4 v3v2 v4 v5v3 v6

v4 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

Page 11: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

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 edge double m_dCost;public: //ctors GraphEdge(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

Page 12: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Pathfinding 3

• This is a weighted graph

• Cities are nodes connected through roads-distances-Relief-Terrain

kante(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).

Page 13: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

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

Page 14: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Pathfinding 5• Here is a graph with obstacle

Graph is a powerfull representation tool. But this power can be realized when 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 what we will do next

Page 15: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

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

Page 16: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

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

Page 17: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Pathfinding

• Depth First Search

• Breadth First Search

• Dijkstra

• A*

Page 18: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Depth First Search DFSStack Parent

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

Firt In Last Out

Page 19: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

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

Page 20: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

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

Page 21: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

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

Page 22: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

DFS

Stack Parent

5-45-6

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

Node 1 is parent of 3, Edge 1-3 is poped from the 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 in the table parent.•A method getPathtoTarget will extract this information and return a vector 5-2-1-3. •What about the path 5-4-3

Page 23: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

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

Page 24: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

DFS in Action

Page 25: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

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

Page 26: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

BFSQueue Parent

5-5

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

First In First Out

Page 27: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

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

Page 28: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

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

Page 29: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

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

Page 30: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

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 and we get 3-4-5.

Node 3 is marked Visited

Page 31: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

BFS in Action

•BFS returns allways the shortest path if exist.•Simple to implement

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

•see GraphAlgorithms.h implementation

Page 32: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

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 g visiting the children first, then the node (post-order traversal): h d i j e b k f g c a visiting 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 the order a b c d e f g h i j k.

Page 33: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Exercices

• How are the traditional types of pathfinding methods implemented

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

Page 34: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

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?

Page 35: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Dijkstra's Algorithm

SPT

Starting from node 5, source.

S

TT

5

Page 36: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Dijkstra's Algorithm

SPT

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

S

TT

5

2

Page 37: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Dijkstra's Algorithm

SPT

The algorithm examines edge 2-3, the cost 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

26

Page 38: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Dijkstra's Algorithm

SPT

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

S

TT

5

26

Page 39: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Dijkstra's Algorithm

SPT

The process is repeated once more.Cost to node 3 is more(3.0+1.1+3.7 contra 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

Page 40: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Djikstra's Algorithm• Use a priority queue

– a data structure in which the item with the smallest "value" is always 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

Page 41: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Dijskra's Algoritm in Action

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

see GraphAlgorithms.h implementation

Page 42: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

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 to guide search.

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

• We can inspect all the currently-available transitions, and rank them on the basis of our partial knowledge. Here high rank means that the transition looks promising in relation to the goal.

Page 43: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

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 be coupled with penalties or bonuses for traveling on a specific type of terrain(jungle, desert, forest,plain…).

• Performance of A* is based on the value of Heuristics

• Poor heuristics provides bad performance with A*

Page 44: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Best First Search A*

Heuristic estimates

• Max(dx, dy)

• Manhatten or Blocks (dx+dy)

• Euclidean sqrt(dx*dx + dy*dy)

Page 45: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

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).

Page 46: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

A* in Action

•see GraphAlgorithms.h implementation

Page 47: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Comparison No walls

Page 48: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Comparison with walls

Page 49: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

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*?

Page 50: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Exercices

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

Page 51: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Summary

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

• Pathfinding: BFS, BFS, DJ and best-first search A*

• Their Advantages/Desadvantages

• Demo and comparison

• For source code/exe files s. [1]

Page 52: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Exercices

1. Solve with hand some path finder using these 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]

Page 53: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Exercices

7. Implement or read the implementations of

DFS, BFS, Dijkstra and A*

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

Page 54: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Other Implementations

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

Demo--Vortrag

Page 55: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

/* 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

Page 56: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Pseudo-code of DFS

Page 57: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Pseudo-code of BFS

Page 58: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Pseudo-code of DJ

Page 59: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Pseudo-code of A*

Page 60: Pathfinding Prof. Dr. Taoufik Nouri Nouri@Nouri.ch 06-11-2014

Pseudo-code of Trace