Presentation is loading. Please wait.

Presentation is loading. Please wait.

Artificial Intelligence in Game Design

Similar presentations


Presentation on theme: "Artificial Intelligence in Game Design"— Presentation transcript:

1 Artificial Intelligence in Game Design
Path Planning Algorithms

2 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 (on average) Not guaranteed to find shortest path depending on heuristic

3 Dijkstra Algorithm Example
5 2 6 6 1 7 1 8 3

4 Dijkstra Algorithm Example
Example as a graph: E 5 D 2 6 B 6 1 7 A 1 G 8 3 C Goal: find shortest path from A to G

5 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

6 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

7 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

8 Dijkstra Algorithm Example
Initially: Start node A in tree Adjacent nodes B, C, and E in fringe Paths to nodes in fringe (B, C, and E) = edge from start E 5 D 2 6 B 6 1 7 A 1 G 8 3 C

9 Dijkstra Algorithm Example
State Best Path Path Cost A Tree B Fringe A  B 6 C A  C 8 D Unknown E A  E 2 G

10 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) E 5 D 2 6 B 6 1 7 A 1 G 8 3 C

11 Dijkstra Algorithm Example
Check whether A  E  B shorter Cost of A  E  B = 7, so no change State Best Path Path Cost A Tree B Fringe A  B 6 C A  C 8 D Unknown E A  E 2 G

12 Dijkstra Algorithm Example
Next step: Add B to tree (shortest path of B and C) Check whether creates shorter path to C (yes) Check whether adjacent to unknown nodes (yes) E 5 D 2 6 B 6 1 7 A 1 G 8 3 C

13 Dijkstra Algorithm Example
Check whether A  B  C shorter Cost of A  B  C = 7, so use it as path State Best Path Path Cost A Tree B A  B 6 C Fringe A  C 8 D A  B  D 12 E A  E 2 G A  B  G 13 A  B  C 7 Paths to new nodes = path to B + edge from B

14 Dijkstra Algorithm Example
Next step: Add C to tree (shortest path of C, D, and G) Check whether creates shorter path to G (yes) E 5 D 2 6 B 6 1 7 A 1 G 8 3 C

15 Dijkstra Algorithm Example
Check whether A  C  G shorter Cost of A  B  C  G = 10, so use it as path State Best Path Path Cost A Tree B A  B 6 C A  B  C 7 D Fringe A  B  D 12 E A  E 2 G A  B  G 13 A  B  C  G 10

16 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 E 5 D 2 6 B 6 1 7 A 1 G 8 3 C 16

17 Dijkstra Algorithm Example
Final path and path cost State Best Path Path Cost A Tree B A  B 6 C A  B  C 7 D Fringe A  B  D 12 E A  E 2 G A  B  C  G 10 17

18 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

19 The A* Algorithm Similar in structure to Dijkstra
Tree, fringe, and unknown nodes Store “best path so far” for 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)

20 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

21 A* Algorithm Example 90 CLE YNG 60 70 AKR CAN 70 30 PIT 80 75 50 WHE COL 100 Goal: find shortest path from YNG to COL

22 A* Algorithm Example Heuristic H (n) = “as crow flies” distance to COL
State Best Path Path Cost from Start Heuristic H (n) Total Estimated Path Cost YNG CLE 100 AKR 90 CAN 75 PIT 150 WHE COL Heuristic H (n) = “as crow flies” distance to COL

23 A* Algorithm Example Best path so far State Best Path
Path Cost from Start Heuristic H (n) Total Estimated Path Cost YNG Tree CLE Fringe YNG  CLE 90 100 190 AKR YNG  AKR 70 160 CAN Unknown 75 PIT YNG  PIT 150 220 WHE YNG  WHE 80 170 COL Best path so far

24 A* Algorithm Example Add CAN to fringe State Best Path
Path Cost from Start Heuristic H (n) Total Estimated Path Cost YNG Tree CLE Fringe YNG  CLE 90 100 190 AKR YNG  AKR 70 160 CAN YNG  AKR  CAN 75 175 PIT YNG  PIT 150 220 WHE YNG  WHE 80 170 COL Unknown Add CAN to fringe

25 A* Algorithm Example Best path so far State Best Path
Path Cost from Start Heuristic H (n) Total Estimated Path Cost YNG Tree CLE Fringe YNG  CLE 90 100 190 AKR YNG  AKR 70 160 CAN YNG  AKR  CAN 75 175 PIT YNG  PIT 150 220 WHE YNG  WHE 80 170 COL Unknown Best path so far

26 A* Algorithm Example Add COL to fringe State Best Path
Path Cost from Start Heuristic H (n) Total Estimated Path Cost YNG Tree CLE Fringe YNG  CLE 90 100 190 AKR YNG  AKR 70 160 CAN YNG  AKR  CAN 75 175 PIT YNG  PIT 150 220 WHE YNG  WHE 80 170 COL YNG  WHE  COL 180 Add COL to fringe

27 A* Algorithm Example Best path so far State Best Path
Path Cost from Start Heuristic H (n) Total Estimated Path Cost YNG Tree CLE Fringe YNG  CLE 90 100 190 AKR YNG  AKR 70 160 CAN YNG  AKR  CAN 75 175 PIT YNG  PIT 150 220 WHE YNG  WHE 80 170 COL YNG  WHE  COL 180 Best path so far

28 A* Algorithm Example State Best Path Path Cost from Start Heuristic H (n) Total Estimated Path Cost YNG Tree CLE Fringe YNG  CLE 90 100 190 AKR YNG  AKR 70 160 CAN YNG  AKR  CAN 75 175 PIT YNG  PIT 150 220 WHE YNG  WHE 80 170 COL YNG  AKR  CAN  COL Like Dijkstra, reevaluate all other nodes in fringe to see if new tree node gives shorter path

29 A* Algorithm Example When goal node added to tree, algorithm done
State Best Path Path Cost from Start Heuristic H (n) Total Estimated Path Cost YNG Tree CLE Fringe YNG  CLE 90 100 190 AKR YNG  AKR 70 160 CAN YNG  AKR  CAN 75 175 PIT YNG  PIT 150 220 WHE YNG  WHE 80 170 COL YNG  AKR  CAN  COL When goal node added to tree, algorithm done Best path

30 A* Heuristics H (n) ≤ actual distance to goal
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 Actual 115 105

31 A* Heuristics A* guaranteed to find shortest path if heuristic admissible Goal in tree  have known path of length d(goal) to goal Would not have chosen this path unless length < estimated path length d(m) + h(m) for all other nodes m in fringe Estimated path length d(m) + h(m) < actual path length through m since h(m) < actual distance to goal from m Therefore, no other path can be shorter than the one found 31

32 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! B C Start “Transporter pad” between C and goal A B C D Estimated 4 3 6.5 Actual 11 A Goal D

33 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 150 240 400 210 420

34 A* Heuristics Heuristic underestimates can happen in levels with costly terrain Estimated distance = 4 squares Actual distance = = 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 Estimated distance = width of forest area * cost of forest + width of mountain area * cost of mountain Goal


Download ppt "Artificial Intelligence in Game Design"

Similar presentations


Ads by Google