A* Path Finding

1,390 views

Published on

An explanation of the A* Path Finding Algorithm
http://www.cse.yorku.ca/~dnatapov

Published in: Education, Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,390
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
95
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

A* Path Finding

  1. 1. A* PATH FINDING ALGORITHM Presented by Daniel Natapov
  2. 2. PROBLEM DEFINITION <ul><li>Find the shortest (weighted) path from a start node to a goal node in a graph (or grid). </li></ul><ul><li>What if we are “informed” with heuristics? Can we “look-ahead” and direct our search? </li></ul>
  3. 3. APPLICATION <ul><li>Games – NPC movement. Needs to be smart and fast. </li></ul><ul><li>Games use grids to describe the environment. These slides do too. </li></ul><ul><li>Many other applications: </li></ul><ul><ul><li>Network routing </li></ul></ul><ul><ul><li>Image processing </li></ul></ul><ul><ul><li>A.I. Path finding </li></ul></ul><ul><ul><li>... </li></ul></ul>
  4. 4. GRID = GRAPH <ul><li>Grid allows movement between adjacent cells in 4 or 8 possible directions. </li></ul><ul><li>Each direction may have a different cost. </li></ul><ul><ul><li> = </li></ul></ul>
  5. 5. EXAMPLE – GET FROM S TO T S T
  6. 6. EXAMPLE – EDGE WEIGHTS <ul><li>In a game, edge weights depend on various factors, ie travel on road vs. grass. </li></ul><ul><li>For simplicity: lets say all horizontal and vertical costs are the same. </li></ul><ul><li>Also assume no diagonal paths. </li></ul>
  7. 7. WWDD? – WHAT WOULD DIJKSTRA’S DO? Found it! (finally) S S T
  8. 8. WWDD? <ul><li>Dijkstra’s algorithm guarantees shortest path. </li></ul><ul><li>But searches a lot of unneeded area. </li></ul><ul><li>We know where the destination node is, (just not how to get there). </li></ul><ul><li>We can try to direct the search with greedy Best-First-Search. </li></ul>
  9. 9. BEST-FIRST-SEARCH <ul><li>Similar to Dijkstra’s, but is informed. </li></ul><ul><li>Has some estimate of how far from the goal each vertex is: “look-ahead”. </li></ul><ul><li>This estimate is a heuristic. </li></ul><ul><li>It prioritizes vertices which it believes to be closets to the goal, as opposed to vertices closest to the start. </li></ul>
  10. 10. BEST-FIRST-SEARCH EXAMPLE S S T
  11. 11. HOW TO BREAK IT – OBSTACLES! S Found it! (could’ve taken a better route) S T
  12. 12. BEST-FIRST-SEARCH <ul><li>Best-First-Search works faster than Dijkstra’s. </li></ul><ul><li>But does not guarantee an optimal-path. </li></ul><ul><li>We want some combination of Dijkstra’s and Best-First-Search. </li></ul><ul><li>Enter A*! </li></ul>
  13. 13. A* ALGORITHM <ul><li>Prioritizes its search based on: </li></ul><ul><ul><li>The distance traveled (Dijkstra’s) </li></ul></ul><ul><ul><li>The distance remaining (Best-First-Search) </li></ul></ul><ul><li>g(n) = Distance traveled from the start to a cell. </li></ul><ul><li>h(n) = Estimated distance from a cell to the target. </li></ul><ul><li>Value of a cell is f(n) = g(n) + h(n) . </li></ul><ul><li>The algorithm prioritizes cells whose f(n) is lowest. </li></ul>
  14. 14. WHAT’S ALL THIS TALK ABOUT ESTIMATES? <ul><li>An estimate of the distance between a cell and a target is a heuristic . </li></ul><ul><li>May be able to estimate distance between two cells. </li></ul><ul><li>Choosing a good heuristic is important, and can be difficult. </li></ul><ul><li>In our simplified case it is easy: the Manhattan Distance: </li></ul><ul><ul><li>h(n) = |cell.x – goal.x| + |cell.y – goal.y| </li></ul></ul>
  15. 15. MANHATTAN DISTANCE <ul><li>Good for our case. Actual distance can never be less (more on this later). </li></ul><ul><li>Lets go through a complete example. </li></ul>
  16. 16. A* EXAMPLE S T
  17. 17. A* EXAMPLE g = 1 h = 6 f = 7 g = 1 h = 6 f = 7 S g = 1 h = 4 f = 5 T g = 1 h = 6 f = 7
  18. 18. A* EXAMPLE g = 1 h = 6 f = 7 g = 1 h = 6 f = 7 S g = 1 h = 4 f = 5 T g = 1 h = 6 f = 7
  19. 19. A* EXAMPLE g = 2 h = 3 f = 5 g = 1 h = 6 f = 7 g = 2 h = 5 f = 7 g = 1 h = 6 f = 7 S g = 1 h = 4 f = 5 T g = 1 h = 6 f = 7 g = 2 h = 5 f = 7
  20. 20. A* EXAMPLE g = 1 h = 6 f = 7 g = 2 h = 5 f = 7 g = 1 h = 6 f = 7 S g = 1 h = 4 f = 5 T g = 1 h = 6 f = 7 g = 2 h = 5 f = 7
  21. 21. A* EXAMPLE g = 3 h = 6 f = 9 g = 1 h = 6 f = 7 g = 2 h = 5 f = 7 g = 1 h = 6 f = 7 S g = 1 h = 4 f = 5 T g = 1 h = 6 f = 7 g = 2 h = 5 f = 7 g = 3 h = 6 f = 9
  22. 22. A* EXAMPLE g = 1 h = 6 f = 7 g = 2 h = 5 f = 7 g = 1 h = 6 f = 7 S g = 1 h = 4 f = 5 T g = 1 h = 6 f = 7 g = 2 h = 5 f = 7 g = 3 h = 6 f = 9
  23. 23. A* EXAMPLE g = 1 h = 6 f = 7 g = 2 h = 5 f = 7 g = 1 h = 6 f = 7 S g = 1 h = 4 f = 5 T g = 2 h = 7 f = 9 g = 1 h = 6 f = 7 g = 2 h = 5 f = 7 g = 2 h = 7 f = 9 g = 3 h = 6 f = 9
  24. 24. A* EXAMPLE g=2 h=7 f=9 g=2 h=7 f=9 g=2 h=7 f=9 g=3 h=8 f=11 g=3 h=8 f=11 g=3 h=8 f=11 g=3 h=8 f=11 g = 3 h = 6 f = 9 g = 1 h = 6 f = 7 g = 2 h = 5 f = 7 g = 1 h = 6 f = 7 S g = 1 h = 4 f = 5 T g = 2 h = 7 f = 9 g = 1 h = 6 f = 7 g = 2 h = 5 f = 7 g = 2 h = 7 f = 9 g = 3 h = 6 f = 9
  25. 25. A* EXAMPLE g = 3 h = 8 f = 11 g = 2 h = 7 f = 9 g = 3 h = 6 f = 9 g = 3 h = 8 f = 11 g = 2 h = 7 f = 9 g = 1 h = 6 f = 7 g = 2 h = 5 f = 7 g = 2 h = 7 f = 9 g = 1 h = 6 f = 7 S g = 1 h = 4 f = 5 T g = 3 h = 8 f = 11 g = 2 h = 7 f = 9 g = 1 h = 6 f = 7 g = 2 h = 5 f = 7 g = 3 h = 8 f = 11 g = 2 h = 7 f = 9 g = 3 h = 6 f = 9 g = 4 h = 5 f = 9
  26. 26. A* EXAMPLE g = 3 h = 8 f = 11 g = 2 h = 7 f = 9 g = 3 h = 6 f = 9 g = 3 h = 8 f = 11 g = 2 h = 7 f = 9 g = 1 h = 6 f = 7 g = 2 h = 5 f = 7 g = 2 h = 7 f = 9 g = 1 h = 6 f = 7 S g = 1 h = 4 f = 5 T g = 3 h = 8 f = 11 g = 2 h = 7 f = 9 g = 1 h = 6 f = 7 g = 2 h = 5 f = 7 g = 3 h = 8 f = 11 g = 2 h = 7 f = 9 g = 3 h = 6 f = 9 g = 4 h = 5 f = 9 g = 5 h = 4 f = 9
  27. 27. A* EXAMPLE g = 3 h = 8 f = 11 g = 2 h = 7 f = 9 g = 3 h = 6 f = 9 g = 3 h = 8 f = 11 g = 2 h = 7 f = 9 g = 1 h = 6 f = 7 g = 2 h = 5 f = 7 g = 2 h = 7 f = 9 g = 1 h = 6 f = 7 S g = 1 h = 4 f = 5 T g = 3 h = 8 f = 11 g = 2 h = 7 f = 9 g = 1 h = 6 f = 7 g = 2 h = 5 f = 7 g = 6 h = 3 f = 9 g = 3 h = 8 f = 11 g = 2 h = 7 f = 9 g = 3 h = 6 f = 9 g = 4 h = 5 f = 9 g = 5 h = 4 f = 9 g = 6 h = 3 f = 9
  28. 28. A* EXAMPLE g = 3 h = 8 f = 11 g = 2 h = 7 f = 9 g = 3 h = 6 f = 9 g = 3 h = 8 f = 11 g = 2 h = 7 f = 9 g = 1 h = 6 f = 7 g = 2 h = 5 f = 7 g = 2 h = 7 f = 9 g = 1 h = 6 f = 7 S g = 1 h = 4 f = 5 g = 7 h = 2 f = 9 T g = 3 h = 8 f = 11 g = 2 h = 7 f = 9 g = 1 h = 6 f = 7 g = 2 h = 5 f = 7 g = 6 h = 3 f = 9 g = 7 h = 2 f = 9 g = 3 h = 8 f = 11 g = 2 h = 7 f = 9 g = 3 h = 6 f = 9 g = 4 h = 5 f = 9 g = 5 h = 4 f = 9 g = 6 h = 3 f = 9
  29. 29. A* EXAMPLE g = 3 h = 8 f = 11 g = 2 h = 7 f = 9 g = 3 h = 6 f = 9 g = 3 h = 8 f = 11 g = 2 h = 7 f = 9 g = 1 h = 6 f = 7 g = 2 h = 5 f = 7 g = 2 h = 7 f = 9 g = 1 h = 6 f = 7 S g = 1 h = 4 f = 5 g = 7 h = 2 f = 9 g = 8 h = 1 f = 9 T g = 3 h = 8 f = 11 g = 2 h = 7 f = 9 g = 1 h = 6 f = 7 g = 2 h = 5 f = 7 g = 6 h = 3 f = 9 g = 7 h = 2 f = 9 g = 8 h = 1 f = 9 g = 3 h = 8 f = 11 g = 2 h = 7 f = 9 g = 3 h = 6 f = 9 g = 4 h = 5 f = 9 g = 5 h = 4 f = 9 g = 6 h = 3 f = 9
  30. 31. MORE ABOUT HEURISTICS <ul><li>Depending heuristic, A* can be admissible . </li></ul><ul><li>This guarantees an optimal solution, despite using an estimate. </li></ul><ul><li>For A* to be admissible and guarantee an optimal solution we need: </li></ul><ul><ul><li> n, h(n) ≤ h*(n) </li></ul></ul><ul><li>h*(n) is the actual distance. </li></ul><ul><li>If the heuristic overestimates the actual distance, an optimal solution is not guaranteed. </li></ul>
  31. 32. MORE ABOUT HEURISTICS CONT’D <ul><li>For admissibility we also need monotonicity. </li></ul><ul><li>Satisfy triangle inequality h(n1) ≤ c(n1 -> n2) + h(n2) </li></ul>n1 goal h(n1) n2 c(n1->n2) h(n2)
  32. 33. FIDDLING WITH THE HEURISTIC <ul><li>Use the heuristic to balance speed vs. accuracy. </li></ul><ul><li>If h(n) = 0, then f(n) = g(n). </li></ul><ul><ul><li>In other words, A* becomes Dijkstra’s. </li></ul></ul><ul><li>If h(n) >> g(n), g(n) can be ignored. </li></ul><ul><ul><li>f(n) ≈ h(n). A* becomes Best-First-Search. </li></ul></ul><ul><li>In general: </li></ul><ul><ul><li>The bigger g(n) is, the more it expands, which makes it slower. </li></ul></ul><ul><ul><li>The bigger h(n) is, the more direct the search is, but better paths could be missed. </li></ul></ul>
  33. 34. FIDDLING WITH THE HEURISTIC 2 <ul><li>If h(n) = h*(n), then A* will find the optimal solution, and not expand anything unnecessary. </li></ul><ul><ul><li>Straight to the target. </li></ul></ul><ul><li>Only possible with good heuristic and no obstacles. </li></ul><ul><li>Can ‘fiddle’ with the heuristic and set it depending on the need. </li></ul><ul><li>Sometimes okay to get an approximate solution at the cost of a speed-up. </li></ul>
  34. 35. SPEED-ACCURACY SEE-SAW g(n) h(n) Speed Accuracy
  35. 36. FORMAL DEFINITION <ul><li>preCond : Input a grid/graph G with positive edge weights, a source node s, a target node t . </li></ul><ul><ul><li>Also given a admissible heuristic for estimating distances. </li></ul></ul><ul><li>postCond : Finds a shortest weighted path from s to t. </li></ul><ul><li>Loop Invariant: So far, the nodes have been handled in order of f(n), where f(n) = g(n) + h(n). </li></ul>
  36. 37. FORMAL DEFINITION CONT’D <ul><li>Step: Handle the found (not handled) node with min f(n). </li></ul><ul><ul><li>Store the parent for each cell – the cell through which the shortest path from s came. </li></ul></ul><ul><li>Exit: Stop when t has been found. </li></ul><ul><li>Obtaining the post condition: LI + Exit + Code => PostCond. </li></ul><ul><li>Proving the path we traced back is shortest: </li></ul><ul><ul><li>Prove there is a path of this length: We have one. </li></ul></ul><ul><ul><li>Prove there is no shorter path: ... </li></ul></ul>
  37. 38. PROOF THAT THERE IS NO BETTER PATH <ul><li>We know our heuristic is admissible, h(n) ≤ h*(n). </li></ul><ul><li>By LI, our path handles cells in order of the minimum of f(n) = g(n) + h(n). </li></ul><ul><li>All unhandled paths have a larger f(n) than ours. </li></ul><ul><li>We found t. So h(n)=0. f(n) becomes our actual - g(n). In other words, our actual cost is lower than the actual+ estimated of any other found node. </li></ul><ul><li>The estimated cost is always less than the actual cost. Meaning our actual cost is less than any other actual cost. </li></ul>COMPLICATED SLIDE. PAY ATTENTION!
  38. 39. CONFUSED? f(n) ≤ f(any) = g(any) + h(any) ≤ g(any) + actual(any) Our actual cost Any other found estimate Any other ‘actual’
  39. 40. WHAT IT ALL MEANS <ul><li>If heuristic is admissible, A* returns the shortest path. </li></ul><ul><li>It will find it by (likely) expanding and searching less cells than Dijkstra’s. </li></ul><ul><li>But if the condition that h(n) ≤ h*(n) is violated, we can no longer ensure optimality. </li></ul><ul><li>Should it always be optimal? </li></ul>
  40. 41. RUNNING TIME <ul><li>Well.... </li></ul><ul><li>Dijkstra’s O( | E | + | V |log | V | ) </li></ul><ul><ul><li>V = number of vertices, E = number of edges. </li></ul></ul><ul><li>Obviously it is possible for A* to search every edge as well, so we have no savings in the worst case. </li></ul><ul><li>Lets focus on the nodes instead: </li></ul><ul><li>Dijkstra’s O(V 2 ) </li></ul>
  41. 42. RUNNING TIME – DIJKSTRA’S S T Area of circle is O(L 2 )
  42. 43. RUNNING TIME – A* S T Area of half ellipse: O(L∙H)
  43. 44. RUNNING TIME – A* S T In total: O((L/n)∙H∙n) = O(L∙H)
  44. 45. ALL DONE! <ul><li>Thank you. Questions? </li></ul><ul><li>The algorithm was first described in 1968 by Peter Hart, Nils Nilsson, and Bertram Raphael </li></ul><ul><li>References & Resources: </li></ul><ul><ul><li>http://theory.stanford.edu/~amitp/GameProgramming/ (Great source) </li></ul></ul><ul><ul><li>http://www.policyalmanac.org/games/aStarTutorial.htm </li></ul></ul><ul><ul><li>http://en.wikipedia.org/wiki/A*_search_algorithm </li></ul></ul><ul><ul><li>http://www.cse.yorku.ca/course_archive/2008-09/W/3402/slides/Week3.pdf </li></ul></ul>

×