PATHFINDING WITH A* Presented by Joseph Siefers February 19 th, 2008
Outline Introduction to Pathfinding Search Terminology Search Methods Uninformed Search Informed Search A* Search: How it Works A* Optimizations Conclusion
Introduction Effective pathfinding is the key to a good game …unless your studio wants to be the laughing stalk of gamers worldwide: But it is difficult to do. Efficient search strategy/algorithms A* is the “star of the search”
Case Study—The 8-Puzzle
Case Study: The 8-Puzzle From the set of AI exercises know as “Toy Problems” Why? Easy to define and understand. Sufficiently complex to analyze search methods.
Case Study: The 8-Puzzle Rules of the game: Move free space {left, up, down, right} within boundaries of puzzle LeftUp DownRight
Search Terminology
Search Tree Node LeftUp DownRight
Search Terminology LeftUp DownRight “Initial State”
Search Terminology Successor Function Left Up Down Right “Successors”
Search Terminology Branching Factor (b) Left Up Down Right b=4
Search Terminology Step Cost (…)
Search Terminology Path Cost g(n) (…) g(n) = 1
Search Terminology Path Cost g(n) (…) g(n) = 2
Search Terminology Goal State (…)
Search Terminology “The Fringe” Open/Closed lists “Algorithms which ignore their history are doomed to repeat it.” –AIMA pg 82
Search Strategies
Uninformed Search Strategies Uninformed Search Blindly search tree “Driving without a map”
Uninformed Search Strategies Breadth First Search nodes in “hierarchical order” FIFO Image Credit: Wikimedia Commons
Uninformed Search Strategies Let’s see breadth-first search in action! “Pathdemo” by Bryan Stout
Uninformed Search Strategies Breadth-First in Action
Uninformed Search Strategy Overall Result:
Uninformed Search Strategies Depth First Repeatedly traverse one path to completion
Unformed Search Strategies Depth-First in Action? —“No Dice”
Uninformed Search Strategies Problems: Not practical: (estimated with b= 10, a search rate of 10 5 nodes/sec, and 1 KiB/node) Credit: AIMA fig 3.11, pg 74
Search Strategies Informed Search Traverse search tree “intelligently” “Driving with a map” A heuristic function = “the map”
Informed Search Strategies Heuristic function h(n) Estimate path cost to goal Key to improving search problems NEVER overestimate— “be optimistic”
Heuristics Admissibility “Relaxed” Problems Simplify constraints Reduces complexity Example: 8-Puzzle ? Initial StateGoal State
A* Search: Introduction A * search: Extremely fast Provably optimal (given an admissible heuristic) Flexible and Adaptable
A* Search: How it Works Estimated Overall Path Cost = How far I think I have to go + How far I have come
A* Search: How it Works Node evaluation function Heuristic function Current Path Cost
A* Search: How it works A* Algorithm Pseudocode 1. Let P= the starting point 2. Assign f, g and h values to P. 3. Add P to the Open list. 4. Let B = the best node (lowest f) from the Open list a. If B is the goal node, quit (path found) b. If the Open list is empty, quit (path impossible) A* Algorithm Pseudocode 1. Let P= the starting point 2. Assign f, g and h values to P. 3. Add P to the Open list. 4. Let B = the best node (lowest f) from the Open list a. If B is the goal node, quit (path found) b. If the Open list is empty, quit (path impossible)
A* Search: How it works 5. Expand B a. Generate f,g,h values for the successors of B b. Add successors to open list, appropriately 6. Repeat from step 4 5. Expand B a. Generate f,g,h values for the successors of B b. Add successors to open list, appropriately 6. Repeat from step 4
A* in Map Problems Example Problem—“The hurried traveler”
A* Map Problem Heuristics A* efficiency dependent on choice of heuristic Euclidean Distance Always admissible Manhattan Distance “City Blocks” Admissible for discrete map problems h = |dx-sx| + |dy-sy|
A* Map Problem Heuristics Manhattan vs. Euclidean distance comparison Image Credit: Wikimedia Commons
A* Search: Map Problem Testing Interesting parameters in “Pathdemo” Heuristic weight Sample heuristic functions Manhattan Distance Diagonal Shortcut Euclidean Max
A* Search: Compare and Contrast Manhattan distance: Weight 0
A* Search: Compare and Contrast Manhattan distance: Weight 1
A* Search: Compare and Contrast Manhattan distance: Weight 2
A* Search: Compare and Contrast Manhattan distance: Weight 3
A* Search: A Look Back… Breadth-First Search A*, Manhattan, W=3 75% LESS NODES
A* Optimizations Don’t pathfind Common solution Quick straight line check “Flood insurance” Measure flooding Visually on map
A* Optimizations Visual representation of flooding Image Credit: AIGPW Figure 3.4.1
A* Optimizations Don’t pathfind Common solution “Flood insurance” Measure flooding and reduce Visually on map Sometimes futile… Limit map designs?
A* Optimizations Memory Pooling Allocating memory CPU intensive How much memory? Specific to each game Find “perfect balance” Statistical Analysis Current technology makes pooling practical
A* Optimizations Open/Closed list implementation “Priority queue” Heap O(lg n) operations Even this might not be fast enough Hash Table O(1) operations Problems fine-tuning “Cheap List”
Optimization Tools Performance metric for search algorithms Intel VTune In-Code Timers Approximate execution time Multithreading Issues Other threads might be busy and slowing Pathfinding Valuable “Standard Path” No tool stands by itself
Conclusion A* solves difficult pathfinding problems Ubiquitous across gaming industry Shows up in every type of game A* is highly dependent on a good heuristic Breadth-first if bad heuristic A* can be resource-intensive A* is not a “one-size fits all” solution Plan on spending time optimizing Solved problem
Thank you! Thanks for listening!