artificial intelligence in game design

33
Artificial Intelligence in Game Design Lecture 11: Path Planning Algorithms

Upload: shel

Post on 31-Jan-2016

20 views

Category:

Documents


0 download

DESCRIPTION

Artificial Intelligence in Game Design. Lecture 11: Path Planning Algorithms. Path Planning Algorithms. Dijkstra’s shortest path algorithm Guaranteed to find shortest path Not fast ( O ( n 2 ) ) A* path algorithm Requires estimation heuristic Much faster - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Artificial Intelligence in Game Design

Artificial Intelligence in Game Design

Lecture 11: Path Planning Algorithms

Page 2: Artificial Intelligence in Game Design

Path Planning Algorithms

• Dijkstra’s shortest path algorithm– Guaranteed to find shortest path– Not fast (O (n2) )

• A* path algorithm– Requires estimation heuristic– Much faster – Not guaranteed to find shortest path depending on heuristic

Page 3: Artificial Intelligence in Game Design

Dijkstra Algorithm Example

• Example:

2

1

1

8

6

5

3

7

6

Page 4: Artificial Intelligence in Game Design

Dijkstra Algorithm Example

• Example as a graph:

2

1

1

8

6

5

3

7

6B

A

E

D

G

C

Goal: find shortest path from A to G

Page 5: Artificial Intelligence in Game Design

Dijkstra Algorithm Components

• Tree– Contains all explored nodes– Initially start node– Assumption: know shortest path from

start to all nodes in rest of tree

• Fringe– All nodes adjacent to some tree node– Assumption : know shortest path from

start to all fringe nodes using only nodes in tree

• Unknown– All nodes not in tree or fringe

Page 6: Artificial Intelligence in Game Design

Dijkstra Algorithm Components

• Data structure for each node stores:– State of that node (tree, fringe, or unknown)– Shortest path (so far) from start to node– Total cost of that path– Initially:

• Fringe nodes have just edge from start• Unknown nodes have no path

State Best Path Path Cost

A

B

C

D

E

G

Page 7: Artificial Intelligence in Game Design

Dijkstra Algorithm

At each cycle:• Add fringe node n with shortest path to tree• Recheck all nodes f in fringe to see if now shorter path

using n– If current best path to f > path to n + edge from n to f

new path to f = path to n + edge from n to f

• Add unknown nodes u adjacent to n to fringe– u’s path = path to n + edge from n to u

• Done when goal node in tree– At that point, have shortest path to goal from start

Page 8: Artificial Intelligence in Game Design

Dijkstra Algorithm Example

Initially:• Start node A in tree• Adjacent nodes B, C, and E in fringe• Paths to nodes = edge from start

2

1

1

8

6

5

3

7

6B

A

E

D

G

C

Page 9: Artificial Intelligence in Game Design

Dijkstra Algorithm Example

State Best Path Path Cost

A Tree A

B Fringe A B 6

C Fringe A C 8

D Unknown

E Fringe A E 2

G Unknown

Page 10: Artificial Intelligence in Game Design

Dijkstra Algorithm Example

Next step:• Add E to tree (shortest path of B, C, and E)• Check whether creates shorter path to B (no)• Check whether adjacent to unknown nodes (no)

2

1

1

8

6

5

3

7

6B

A

E

D

G

C

Page 11: Artificial Intelligence in Game Design

Dijkstra Algorithm Example

State Best Path Path Cost

A Tree A

B Fringe A B 6

C Fringe A C 8

D Unknown

E Tree A E 2

G Unknown

Check whether A E B shorter

Cost of A E B = 7, so no change

Page 12: Artificial Intelligence in Game Design

Dijkstra Algorithm Example

Next step:• Add B to tree (shortest path of B and C)• Check whether creates shorter path to B (yes)• Check whether adjacent to unknown nodes (yes)

2

1

1

8

6

5

3

7

6B

A

E

D

G

C

Page 13: Artificial Intelligence in Game Design

Dijkstra Algorithm Example

State Best Path Path Cost

A Tree A

B Tree A B 6

C Fringe A C A B C 8 7

D Fringe A B D 12

E Tree A E 2

G Fringe A B G 13

Check whether A B C shorterCost of A B C = 7, so use it as path

Paths to new nodes = path to B + edge from B

Page 14: Artificial Intelligence in Game Design

Dijkstra Algorithm Example

Next step:• Add C to tree (shortest path of C, D, and G)• Check whether creates shorter path to G (yes)

2

1

1

8

6

5

3

7

6B

A

E

D

G

C

Page 15: Artificial Intelligence in Game Design

Dijkstra Algorithm Example

State Best Path Path Cost

A Tree A

B Tree A B 6

C Tree A B C 7

D Fringe A B D 12

E Tree A E 2

G Fringe A B G A B C G 13 10

Check whether A C G shorterCost of A B C G = 10, so use it as path

Page 16: Artificial Intelligence in Game Design

Dijkstra Algorithm Example

Next step:• Add goal node G to tree (shortest path of D and G)• Algorithm finished

– No possibility of shorter path through D

2

1

1

8

6

5

3

7

6B

A

E

D

G

C

Page 17: Artificial Intelligence in Game Design

Dijkstra Algorithm Example

State Best Path Path Cost

A Tree A

B Tree A B 6

C Tree A B C 7

D Fringe A B D 12

E Tree A E 2

G Tree A B C G 10

Final path and path cost

Page 18: Artificial Intelligence in Game Design

Dijkstra Algorithm Analysis

• Worst case: may need to example all n nodes• For each step, must check all m nodes in fringe

– Worst case: all n nodes in fringe– Unlikely, since most levels far less interconnected

• Number of comparisons: O (nm) – O (n2) in worst case

Page 19: Artificial Intelligence in Game Design

The A* Algorithm

• Similar in structure to Dijkstra– Tree, fringe, and unknown nodes

– Store “best path so far” fro each node explored

– Choose next fringe node to add to tree

• Idea: Choose fringe node n believed to be part of “shortest path”

– Haven’t explored entire graph yet, so don’t know path length– Requires estimate of total path length

• Estimate is a heuristic H(n)

Page 20: Artificial Intelligence in Game Design

The A* Algorithm

• Estimated path length for path including n = length of path from start to n + estimated distance from n to goal

Known, since have explored graph from start to n

Requires heuristic H(n) to create an estimate

Start

Goal

Page 21: Artificial Intelligence in Game Design

A* Algorithm Example

100

5075

60

90

7080

YNG

Goal: find shortest path from YNG to COL

CLE

COL

CAN

WHE

PIT

70AKR

30

Page 22: Artificial Intelligence in Game Design

A* Algorithm Example

State Best Path Path Cost from Start

Heuristic H (n)

Total Estimated Path Cost

YNG

CLE 100

AKR 90

CAN 75

PIT 150

WHE 90

COL 0

Heuristic H (n) = “as crow flies” distance to COL

Page 23: Artificial Intelligence in Game Design

A* Algorithm Example

State Best Path Path Cost from Start

Heuristic H (n)

Total Estimated Path Cost

YNG Tree YNG

CLE Fringe YNG CLE 90 100 190

AKR Fringe YNG AKR 70 90 160

CAN Unknown 75

PIT Fringe YNG PIT 70 150 220

WHE Fringe YNG WHE 80 90 170

COL Unknown 0

Best path so far

Page 24: Artificial Intelligence in Game Design

A* Algorithm Example

State Best Path Path Cost from Start

Heuristic H (n)

Total Estimated Path Cost

YNG Tree YNG

CLE Fringe YNG CLE 90 100 190

AKR Tree YNG AKR 70 90 160

CAN Fringe YNG AKR CAN 100 75 175

PIT Fringe YNG PIT 70 150 220

WHE Fringe YNG WHE 80 90 170

COL Unknown 0

Add CAN to fringe

Page 25: Artificial Intelligence in Game Design

A* Algorithm Example

State Best Path Path Cost from Start

Heuristic H (n)

Total Estimated Path Cost

YNG Tree YNG

CLE Fringe YNG CLE 90 100 190

AKR Tree YNG AKR 70 90 160

CAN Fringe YNG AKR CAN 100 75 175

PIT Fringe YNG PIT 70 150 220

WHE Fringe YNG WHE 80 90 170

COL Unknown 0

Best path so far

Page 26: Artificial Intelligence in Game Design

A* Algorithm Example

State Best Path Path Cost from Start

Heuristic H (n)

Total Estimated Path Cost

YNG Tree YNG

CLE Fringe YNG CLE 90 100 190

AKR Tree YNG AKR 70 90 160

CAN Fringe YNG AKR CAN 100 75 175

PIT Fringe YNG PIT 70 150 220

WHE Tree YNG WHE 80 90 170

COL Fringe YNG WHE COL 180 0 180

Add COL to fringe

Page 27: Artificial Intelligence in Game Design

A* Algorithm Example

State Best Path Path Cost from Start

Heuristic H (n)

Total Estimated Path Cost

YNG Tree YNG

CLE Fringe YNG CLE 90 100 190

AKR Tree YNG AKR 70 90 160

CAN Fringe YNG AKR CAN 100 75 175

PIT Fringe YNG PIT 70 150 220

WHE Tree YNG WHE 80 90 170

COL Fringe YNG WHE COL 180 0 180

Best path so far

Page 28: Artificial Intelligence in Game Design

A* Algorithm Example

State Best Path Path Cost from Start

Heuristic H (n)

Total Estimated Path Cost

YNG Tree YNG

CLE Fringe YNG CLE 90 100 190

AKR Tree YNG AKR 70 90 160

CAN Tree YNG AKR CAN 100 75 175

PIT Fringe YNG PIT 70 150 220

WHE Tree YNG WHE 80 90 170

COL Fringe YNG AKR CAN COL

175 0 175

Like Dijkstra, reevaluate all other nodes in fringe to see if new tree node gives shorter path

Page 29: Artificial Intelligence in Game Design

A* Algorithm Example

State Best Path Path Cost from Start

Heuristic H (n)

Total Estimated Path Cost

YNG Tree YNG

CLE Fringe YNG CLE 90 100 190

AKR Tree YNG AKR 70 90 160

CAN Tree YNG AKR CAN 100 75 175

PIT Fringe YNG PIT 70 150 220

WHE Tree YNG WHE 80 90 170

COL Tree YNG AKR CAN COL 175 0 175

When goal node added to tree, algorithm doneBest path

Page 30: Artificial Intelligence in Game Design

A* Heuristics

• A* heuristic admissible if always underestimates distance to goal

H (n) ≤ actual distance to goal

– True for example– Usually true for “as crow flies” estimates

CLE ARK CAN PIT WHE

Estimated 100 90 75 150 90

Actual 115 105 75 150 100

A* Heuristics

Page 31: Artificial Intelligence in Game Design

A* Heuristics

• Heuristic may not be valid if “short cuts” in level

– Estimated path using C has length 12.5– Actual path has length 6 (using transporter pad)– Path using A has estimated length 7– Goal added to tree before C ever explored

• However, non-optimal behavior may be more plausible– Character may not know about transporter pad!

Start

GoalA

CB

D

A B C D

Estimated 4 3 6.5 4

Actual 4 11 0 4

“Transporter pad” between C and goal

Page 32: Artificial Intelligence in Game Design

A* Heuristics

• Algorithm can be inefficient if heuristic severely underestimates distance

– PIT and other obviously implausible nodes (CHI, PHI, NY, TOR, etc.) not explored

• If all estimated distances low (1 for example) then all will be explored

– Adjacent nodes (YNG PIT PHIL) explored before paths with closer but more nodes (YNG AKR CAN COL)

CLE ARK CAN WHE PIT CHI PHI TOR NY …

Estimated 100 90 75 90 150 240 400 210 420

Page 33: Artificial Intelligence in Game Design

A* Heuristics

• Heuristic underestimates can happen in levels with costly terrain– Estimated distance = 4 squares– Actual distance = 2 + 5 + 5 = 9 due

to desert, mountains

• No perfect solution• Potential hierarchical solution:

– Create high-level map with terrain type of large general areas

– Determine which areas direct path to goal would cross

– Factor in terrain costs

Start

GoalEstimated distance = width of forest area * cost of forest +width of mountain area * cost of mountain