1 co2301 - games development 1 week 8 a* gareth bellaby

21
1 CO2301 - Games Development 1 Week 8 A* Gareth Bellaby

Upload: august-peters

Post on 18-Jan-2018

231 views

Category:

Documents


0 download

DESCRIPTION

3 A* algorithm Combines cost with a prediction of how close the goal is. A* is an efficient pathfinding algorithm.

TRANSCRIPT

Page 1: 1 CO2301 - Games Development 1 Week 8 A* Gareth Bellaby

1

CO2301 - Games Development 1Week 8

A*

Gareth Bellaby

Page 2: 1 CO2301 - Games Development 1 Week 8 A* Gareth Bellaby

2

Best first searchesBest first searches

•Best first algorithm is estimate of distance to goal

•Dijkstra's algorithm is cost

•A* algorithm is cost + estimate

Page 3: 1 CO2301 - Games Development 1 Week 8 A* Gareth Bellaby

3

A* algorithmA* algorithm

Combines cost with a prediction of how close the goal is.

A* is an efficient pathfinding algorithm.

Page 4: 1 CO2301 - Games Development 1 Week 8 A* Gareth Bellaby

4

A* algorithmA* algorithm

As with Dijkstra's Algorithm the algorithm itself is on the web site in "algorithms.doc" since it is too long to put up in a PowerPoint presentation.

Note that the open list is a priority queue.

Also note that the algorithm removes items from the closed list if a better route to a node is found during the course of the search.

Page 5: 1 CO2301 - Games Development 1 Week 8 A* Gareth Bellaby

5

A* algorithmA* algorithm

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

f(n) is the evaluation function, i.e. the score assigned to node.

g(n) is the actual cost of getting to n from the start.

h(n) is the heuristic estimate of getting from n to the goal.

Page 6: 1 CO2301 - Games Development 1 Week 8 A* Gareth Bellaby

6

A* example - heuristicA* example - heuristic

Use a simple heuristic: count the total number of squares to the destination.

This is called the Manhattan distance.

Simply sum together the number of squares horizontally and vertically.

start

1

1

2

2

3

2 3 4

Page 7: 1 CO2301 - Games Development 1 Week 8 A* Gareth Bellaby

7

A* example - costA* example - cost

Need to include the actual cost incurred for moving to a particular square.

Cost is 1 for a white square, 2 for a green square and the wall is impassable.

Page 8: 1 CO2301 - Games Development 1 Week 8 A* Gareth Bellaby

8

A* example 1A* example 1Red: start/endBlack: wallGreen: forest

Apply the rules in the order N, E, S, W

Page 9: 1 CO2301 - Games Development 1 Week 8 A* Gareth Bellaby

9

A* example 1 - accumulated A* example 1 - accumulated costcost

5

Cost of the square + cost of the route taken to the square.

Obviously the accumulated cost depends on the exact route taken.3

7

3

4

5

6

5

6

6

1

2

0

1

Page 10: 1 CO2301 - Games Development 1 Week 8 A* Gareth Bellaby

10

A* example 1 - heuristicA* example 1 - heuristic

1

Red: start/endBlack: wallGreen: forest

Apply the rules in the order N, E, S, W

(So the cost to the square above the wood is 4 because the route was through the wood - a lower cost route does exist)

3

1

2

3

2

1

2

2

3

0

2

3

3

4

2

3

4

3

4

2

3

4

Page 11: 1 CO2301 - Games Development 1 Week 8 A* Gareth Bellaby

11

A* example 1A* example 1Red: start/endBlack: wallGreen: forest

Apply the rules in the order N, E, S, W

(So the cost to the square above the wood is 4 because the route was through the wood - a lower cost route does exist)

1+3

4

1+3

4

2+4

63+2

5

2+4

6

5+1

6

4+3

7

6+0

6

Page 12: 1 CO2301 - Games Development 1 Week 8 A* Gareth Bellaby

12

A* example 2A* example 2Red: start/endBlack: wallGreen: forest

Apply the rules in the order N, E, S, W

Page 13: 1 CO2301 - Games Development 1 Week 8 A* Gareth Bellaby

13

A* example 2A* example 2Red: start/endBlack: wallGreen: forest

Apply the rules in the order N, E, S, W

1+4

5

2+3

5

1+2

3

3+3

6

2+5

7

3+2

5

4+1

5

5+0

5

Page 14: 1 CO2301 - Games Development 1 Week 8 A* Gareth Bellaby

14

A* example 3A* example 3Red: start/endBlack: wallGreen: forest

Apply the rules in the order N, E, S, W

Page 15: 1 CO2301 - Games Development 1 Week 8 A* Gareth Bellaby

15

A* example 3A* example 3Red: start/endBlack: wallGreen: forest

Apply the rules in the order N, E, S, W

1+4

5

2+3

5

1+2

3

2+5

72+3

5

3+4

73+2

54+1

5

4+3

7

4+3

7

5+0

5

Page 16: 1 CO2301 - Games Development 1 Week 8 A* Gareth Bellaby

16

AdmissibilityAdmissibility

If a grid is used, and the only allowed movement is horizontally or vertically, then the Manhattan distance is:

• admissible, i.e. it never overestimates the distance.

• the best possible heuristic that can be used.

Page 17: 1 CO2301 - Games Development 1 Week 8 A* Gareth Bellaby

17

AdmissibilityAdmissibility

In A* proper the heuristic estimate must be admissible, i.e. it never uses a heuristic that overestimates the distance.

A* is guaranteed to find the optimal solution, but only if the heuristic estimate is admissible.

Why?

Page 18: 1 CO2301 - Games Development 1 Week 8 A* Gareth Bellaby

18

A* algorithmA* algorithm

If A* uses a heuristic that overestimates the distance then the value calculated by the heuristic can be larger than the cost.

Consider the boundary conditions.

At the extreme, overestimating the heuristic is equivalent to ignoring the cost.

If the cost is ignored then the algorithm is just based on the heuristic. This is the best-first search algorithm.

Page 19: 1 CO2301 - Games Development 1 Week 8 A* Gareth Bellaby

19

A* algorithmA* algorithm

Now, consider the other extreme.

If the heuristic estimation is ignored then the algorithm is just based on cost. This is Dijkstra's algorithm.

The best situation is one where the heuristic evaluation approaches the cost but is never actually overestimated.

Page 20: 1 CO2301 - Games Development 1 Week 8 A* Gareth Bellaby

20

The graceful decay of The graceful decay of admissibilityadmissibility

• In A* proper the heuristic estimate must be admissible, i.e. it never uses a heuristic that overestimates the distance.

• A* is guaranteed to find the optimal solution, but only if the heuristic estimate is admissible.

• Games pathfinding often ignores this: emphasising speed over optimality.

Page 21: 1 CO2301 - Games Development 1 Week 8 A* Gareth Bellaby

21

The graceful decay of The graceful decay of admissibilityadmissibility

• If your heuristic rarely over-estimates the real distance to goal by more than a certain value (lets call it d) then the A* algorithm will rarely find a solution which costs more than d over the cost of the optimal solution.

• Graceful decay of admissability. Be strict about admissability at the beginning of the search but as the search is assumed to get closer to the destination relax the admissability criterion.