A* Path Finding

45
A* PATH FINDING ALGORITHM Presented by Daniel Natapov

description

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

Transcript of A* Path Finding

Page 1: A* Path Finding

A* PATH FINDING ALGORITHMPresented by Daniel Natapov

Page 2: A* Path Finding

PROBLEM DEFINITION

Find the shortest (weighted) path from a start node to a goal node in a graph (or grid).

What if we are “informed” with heuristics? Can we “look-ahead” and direct our search?

Page 3: A* Path Finding

APPLICATION

Games – NPC movement. Needs to be smart and fast.

Games use grids to describe the environment. These slides do too.

Many other applications: Network routing Image processing A.I. Path finding ...

Page 4: A* Path Finding

GRID = GRAPH

Grid allows movement between adjacent cells in 4 or 8 possible directions.

Each direction may have a different cost.

=

Page 5: A* Path Finding

EXAMPLE – GET FROM S TO T

S

T

Page 6: A* Path Finding

EXAMPLE – EDGE WEIGHTS

In a game, edge weights depend on various factors, ie travel on road vs. grass.

For simplicity: lets say all horizontal and vertical costs are the same.

Also assume no diagonal paths.

Page 7: A* Path Finding

WWDD? – WHAT WOULD DIJKSTRA’S DO?

S

T

Found it! (finally)

S

Page 8: A* Path Finding

WWDD?

Dijkstra’s algorithm guarantees shortest path.

But searches a lot of unneeded area. We know where the destination node is, (just

not how to get there). We can try to direct the search with greedy

Best-First-Search.

Page 9: A* Path Finding

BEST-FIRST-SEARCH

Similar to Dijkstra’s, but is informed. Has some estimate of how far from the goal

each vertex is: “look-ahead”. This estimate is a heuristic. It prioritizes vertices which it believes to be

closets to the goal, as opposed to vertices closest to the start.

Page 10: A* Path Finding

BEST-FIRST-SEARCH EXAMPLE

S

T

S

Page 11: A* Path Finding

HOW TO BREAK IT – OBSTACLES!

S

T

S

Found it! (could’ve

taken a better route)

Page 12: A* Path Finding

BEST-FIRST-SEARCH

Best-First-Search works faster than Dijkstra’s. But does not guarantee an optimal-path. We want some combination of Dijkstra’s and

Best-First-Search. Enter A*!

Page 13: A* Path Finding

A* ALGORITHM

Prioritizes its search based on: The distance traveled (Dijkstra’s) The distance remaining (Best-First-Search)

g(n) = Distance traveled from the start to a cell.

h(n) = Estimated distance from a cell to the target.

Value of a cell is f(n) = g(n) + h(n). The algorithm prioritizes cells whose f(n) is

lowest.

Page 14: A* Path Finding

WHAT’S ALL THIS TALK ABOUT ESTIMATES?

An estimate of the distance between a cell and a target is a heuristic.

May be able to estimate distance between two cells.

Choosing a good heuristic is important, and can be difficult.

In our simplified case it is easy: the Manhattan Distance:

h(n) = |cell.x – goal.x| + |cell.y – goal.y|

Page 15: A* Path Finding

MANHATTAN DISTANCE

Good for our case. Actual distance can never be less (more on this later).

Lets go through a complete example.

Page 16: A* Path Finding

A* EXAMPLE

S T

Page 17: A* Path Finding

A* EXAMPLE

g = 1h = 6f = 7

g = 1h = 6f = 7

S g = 1h = 4f = 5

T

g = 1h = 6f = 7

Page 18: A* Path Finding

A* EXAMPLE

g = 1h = 6f = 7

g = 1h = 6f = 7

S g = 1h = 4f = 5

T

g = 1h = 6f = 7

Page 19: A* Path Finding

A* EXAMPLE

g = 1h = 6f = 7

g = 2h = 5f = 7

g = 1h = 6f = 7

S g = 1h = 4f = 5

T

g = 1h = 6f = 7

g = 2h = 5f = 7

g = 2h = 3f = 5

Page 20: A* Path Finding

A* EXAMPLE

g = 1h = 6f = 7

g = 2h = 5f = 7

g = 1h = 6f = 7

S g = 1h = 4f = 5

T

g = 1h = 6f = 7

g = 2h = 5f = 7

Page 21: A* Path Finding

A* EXAMPLE

g = 1h = 6f = 7

g = 2h = 5f = 7

g = 1h = 6f = 7

S g = 1h = 4f = 5

T

g = 1h = 6f = 7

g = 2h = 5f = 7

g = 3h = 6f = 9

g = 3h = 6f = 9

Page 22: A* Path Finding

A* EXAMPLE

g = 1h = 6f = 7

g = 2h = 5f = 7

g = 1h = 6f = 7

S g = 1h = 4f = 5

T

g = 1h = 6f = 7

g = 2h = 5f = 7

g = 3h = 6f = 9

Page 23: A* Path Finding

A* EXAMPLE

g = 1h = 6f = 7

g = 2h = 5f = 7

g = 1h = 6f = 7

S g = 1h = 4f = 5

T

g = 2h = 7f = 9

g = 1h = 6f = 7

g = 2h = 5f = 7

g = 2h = 7f = 9

g = 3h = 6f = 9

Page 24: A* Path Finding

A* EXAMPLE

g = 3h = 6f = 9

g = 1h = 6f = 7

g = 2h = 5f = 7

g = 1h = 6f = 7

S g = 1h = 4f = 5

T

g = 2h = 7f = 9

g = 1h = 6f = 7

g = 2h = 5f = 7

g = 2h = 7f = 9

g = 3h = 6f = 9

g=2h=7f=9

g=2h=7f=9

g=2h=7f=9

g=3h=8f=11

g=3h=8f=11

g=3h=8f=11

g=3h=8f=11

Page 25: A* Path Finding

A* EXAMPLE

g = 3h = 8f = 11

g = 2h = 7f = 9

g = 3h = 6f = 9

g = 3h = 8f = 11

g = 2h = 7f = 9

g = 1h = 6f = 7

g = 2h = 5f = 7

g = 2h = 7f = 9

g = 1h = 6f = 7

S g = 1h = 4f = 5

T

g = 3h = 8f = 11

g = 2h = 7f = 9

g = 1h = 6f = 7

g = 2h = 5f = 7

g = 3h = 8f = 11

g = 2h = 7f = 9

g = 3h = 6f = 9

g = 4 h = 5f = 9

Page 26: A* Path Finding

A* EXAMPLE

g = 3h = 8f = 11

g = 2h = 7f = 9

g = 3h = 6f = 9

g = 3h = 8f = 11

g = 2h = 7f = 9

g = 1h = 6f = 7

g = 2h = 5f = 7

g = 2h = 7f = 9

g = 1h = 6f = 7

S g = 1h = 4f = 5

T

g = 3h = 8f = 11

g = 2h = 7f = 9

g = 1h = 6f = 7

g = 2h = 5f = 7

g = 3h = 8f = 11

g = 2h = 7f = 9

g = 3h = 6f = 9

g = 4 h = 5f = 9

g = 5h = 4f = 9

Page 27: A* Path Finding

A* EXAMPLE

g = 3h = 8f = 11

g = 2h = 7f = 9

g = 3h = 6f = 9

g = 3h = 8f = 11

g = 2h = 7f = 9

g = 1h = 6f = 7

g = 2h = 5f = 7

g = 2h = 7f = 9

g = 1h = 6f = 7

S g = 1h = 4f = 5

T

g = 3h = 8f = 11

g = 2h = 7f = 9

g = 1h = 6f = 7

g = 2h = 5f = 7

g = 6 h = 3f = 9

g = 3h = 8f = 11

g = 2h = 7f = 9

g = 3h = 6f = 9

g = 4 h = 5f = 9

g = 5h = 4f = 9

g = 6h = 3f = 9

Page 28: A* Path Finding

A* EXAMPLE

g = 3h = 8f = 11

g = 2h = 7f = 9

g = 3h = 6f = 9

g = 3h = 8f = 11

g = 2h = 7f = 9

g = 1h = 6f = 7

g = 2h = 5f = 7

g = 2h = 7f = 9

g = 1h = 6f = 7

S g = 1h = 4f = 5

g = 7h = 2f = 9

T

g = 3h = 8f = 11

g = 2h = 7f = 9

g = 1h = 6f = 7

g = 2h = 5f = 7

g = 6 h = 3f = 9

g = 7h = 2f = 9

g = 3h = 8f = 11

g = 2h = 7f = 9

g = 3h = 6f = 9

g = 4 h = 5f = 9

g = 5h = 4f = 9

g = 6h = 3f = 9

Page 29: A* Path Finding

A* EXAMPLE

g = 3h = 8f = 11

g = 2h = 7f = 9

g = 3h = 6f = 9

g = 3h = 8f = 11

g = 2h = 7f = 9

g = 1h = 6f = 7

g = 2h = 5f = 7

g = 2h = 7f = 9

g = 1h = 6f = 7

S g = 1h = 4f = 5

g = 7h = 2f = 9

g = 8h = 1f = 9

T

g = 3h = 8f = 11

g = 2h = 7f = 9

g = 1h = 6f = 7

g = 2h = 5f = 7

g = 6 h = 3f = 9

g = 7h = 2f = 9

g = 8h = 1f = 9

g = 3h = 8f = 11

g = 2h = 7f = 9

g = 3h = 6f = 9

g = 4 h = 5f = 9

g = 5h = 4f = 9

g = 6h = 3f = 9

Page 30: A* Path Finding
Page 31: A* Path Finding

MORE ABOUT HEURISTICS

Depending heuristic, A* can be admissible. This guarantees an optimal solution, despite

using an estimate. For A* to be admissible and guarantee an

optimal solution we need:n, h(n) ≤ h*(n)

h*(n) is the actual distance. If the heuristic overestimates the actual

distance, an optimal solution is not guaranteed.

Page 32: A* Path Finding

MORE ABOUT HEURISTICS CONT’D

For admissibility we also need monotonicity. Satisfy triangle inequality h(n1) ≤ c(n1 → n2)

+ h(n2)n1

goal

h(n1)

n2

c(n1->n2)

h(n2)

Page 33: A* Path Finding

FIDDLING WITH THE HEURISTIC

Use the heuristic to balance speed vs. accuracy.

If h(n) = 0, then f(n) = g(n). In other words, A* becomes Dijkstra’s.

If h(n) >> g(n), g(n) can be ignored. f(n) ≈ h(n). A* becomes Best-First-Search.

In general: The bigger g(n) is, the more it expands, which

makes it slower. The bigger h(n) is, the more direct the search is,

but better paths could be missed.

Page 34: A* Path Finding

FIDDLING WITH THE HEURISTIC 2

If h(n) = h*(n), then A* will find the optimal solution, and not expand anything unnecessary. Straight to the target.

Only possible with good heuristic and no obstacles.

Can ‘fiddle’ with the heuristic and set it depending on the need.

Sometimes okay to get an approximate solution at the cost of a speed-up.

Page 35: A* Path Finding

SPEED-ACCURACY SEE-SAW

g(n) h(n)

SpeedAccuracy

Page 36: A* Path Finding

FORMAL DEFINITION

preCond: Input a grid/graph G with positive edge weights, a source node s, a target node t.

Also given a admissible heuristic for estimating distances.

postCond: Finds a shortest weighted path from s to t.

Loop Invariant: So far, the nodes have been handled in order of f(n), where f(n) = g(n) + h(n).

Page 37: A* Path Finding

FORMAL DEFINITION CONT’D

Step: Handle the found (not handled) node with min f(n).

Store the parent for each cell – the cell through which the shortest path from s came.

Exit: Stop when t has been found.

Obtaining the post condition: LI + Exit + Code => PostCond.

Proving the path we traced back is shortest: Prove there is a path of this length: We have one.

Prove there is no shorter path: ...

Page 38: A* Path Finding

PROOF THAT THERE IS NO BETTER PATH

We know our heuristic is admissible, h(n) ≤ h*(n).

By LI, our path handles cells in order of the minimum of f(n) = g(n) + h(n).

All unhandled paths have a larger f(n) than ours.

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.

The estimated cost is always less than the actual cost. Meaning our actual cost is less than any other actual cost.

COMPLICATED SLIDE. PAY

ATTENTION!

Page 39: A* Path Finding

CONFUSED?

f(n) ≤ f(any) = g(any) + h(any)

≤ g(any) + actual(any)

Our actual cost

Any other found estimate

Any other ‘actual’

Page 40: A* Path Finding

WHAT IT ALL MEANS

If heuristic is admissible, A* returns the shortest path.

It will find it by (likely) expanding and searching less cells than Dijkstra’s.

But if the condition that h(n) ≤ h*(n) is violated, we can no longer ensure optimality.

Should it always be optimal?

Page 41: A* Path Finding

RUNNING TIME

Well.... Dijkstra’s O(|E| + |V |log |V| )

V = number of vertices, E = number of edges. Obviously it is possible for A* to search every

edge as well, so we have no savings in the worst case.

Lets focus on the nodes instead: Dijkstra’s O(V2)

Page 42: A* Path Finding

RUNNING TIME – DIJKSTRA’S

S T

Area of circle is O(L2)

Page 43: A* Path Finding

RUNNING TIME – A*

S T

Area of half ellipse: O(L∙H)

Page 44: A* Path Finding

RUNNING TIME – A*

ST

In total: O((L/n)∙H∙n) = O(L∙H)

Page 45: A* Path Finding

ALL DONE! Thank you. Questions?

The algorithm was first described in 1968 by Peter Hart, Nils Nilsson, and Bertram Raphael

References & Resources: http://theory.stanford.edu/~amitp/GameProgrammi

ng/ (Great source)

http://www.policyalmanac.org/games/aStarTutorial.htm

http://en.wikipedia.org/wiki/A*_search_algorithm http://www.cse.yorku.ca/course_archive/2008-09/W/

3402/slides/Week3.pdf