the a* and applications to games
DESCRIPTION
The A* and applications to Games. Sources : My own Joseph Siefers presentation 2008. Case Study—The 8-Puzzle. Case Study: The 8-Puzzle. http://www.permadi.com/java/puzzle8/. Case Study: The 8-Puzzle. Left. Up. Down. Rules of the game: - PowerPoint PPT PresentationTRANSCRIPT
The A* and applications to Games
Sources:• My own• Joseph Siefers presentation 2008
CASE STUDY—THE 8-PUZZLE
Case Study: The 8-Puzzle
• Rules of the game:– Move free space {left, up, down, right} within boundaries of
puzzle.1 2 3
4 6
7 8 5
1 2 3
4 6
7 8 5
1 3
4 2 6
7 8 5
Left Up
1 2 3
4 8 6
7 5
1 2 3
4 6
7 8 5
Down Right
SEARCH TERMINOLOGY
Search Terminology
• Search Tree• Node
1 2 3
4 6
7 8 5
1 2 3
4 6
7 8 5
1 3
4 2 6
7 8 5
Left Up
1 2 3
4 8 6
7 5
1 2 3
4 6
7 8 5
Down
Right
Search Terminology
1 2 3
4 6
7 8 5
1 2 3
4 6
7 8 5
1 3
4 2 6
7 8 5
Left Up
1 2 3
4 8 6
7 5
1 2 3
4 6
7 8 5
Down
Right
“Initial State”
Search Terminology
• Successor Function
1 2 3
4 6
7 8 5
1 2 3
4 6
7 8 5
1 3
4 2 6
7 8 5Left
Up
1 2 3
4 8 6
7 5
1 2 3
4 6
7 8 5
DownRight
“Successors”
Search Terminology
• Branching Factor (b)
1 2 3
4 6
7 8 5
1 2 3
4 6
7 8 5
1 3
4 2 6
7 8 5Left
Up
1 2 3
4 8 6
7 5
1 2 3
4 6
7 8 5
DownRight
b=4
Search Terminology
• Step Cost 1 2 34 67 8 5
1 2 34 6
7 8 5
1 34 2 67 8 5
1 2 34 8 67 5
1 2 34 67 8 5
+1+1 +1
+1
+1 +1 +1 +1 +1 +1 +1+1
2 31 4 67 8 5
1 2 37 4 6
8 5
1 34 2 67 8 5
(…)(…) (…)
(…) (…)(…)
Search Terminology
• Path Cost g(n) 1 2 34 67 8 5
1 2 34 6
7 8 5
1 34 2 67 8 5
1 2 34 8 67 5
1 2 34 67 8 5
+1+1 +1
+1
+1 +1 +1 +1 +1 +1 +1+1
2 31 4 67 8 5
1 2 37 4 6
8 5
1 34 2 67 8 5
(…)(…) (…)
(…) (…)(…)
g(n) = 1
Search Terminology
• Path Cost g(n) 1 2 34 67 8 5
1 2 34 6
7 8 5
1 34 2 67 8 5
1 2 34 8 67 5
1 2 34 67 8 5
+1+1 +1
+1
+1 +1 +1 +1 +1 +1 +1 +12 3
1 4 67 8 5
1 2 37 4 6
8 5
1 34 2 67 8 5
(…)(…) (…)
(…) (…)(…)
g(n) = 2
Search Terminology
• Goal State 3 1 86 47 2 5
(…)(…)(…)
(…)
(…)(…)(…)
(…)(…)
(…)(…)(…)1 2 3
4 5 67 8
1 2 34 5 67 8
The Search Problem http://www.youtube.com/watch?v=EH0JRFkpnU0&feature=related
Starting from a node n find the shortest path to a goal node g
1015
20 ?
2015
518
25
33
Greedy Algorithm
Greedy algorithm: from the candidate nodes select the one that has a path with minimum cost from the starting node
1015
20 ?
2015
518
25
33Two problems:• O(b*n), n = # of edges (so bad)• Not optimal (might be OK)• Caution: Enemy Nations story
Djikstra Algorithm
T
Given a Graph G = (V,E) and T a subset of V, the fringe of T, is defined as:
Fringe(T) = { (w,x) in E : w T and x V − T}
Djikstra’s algorithm pick the edge v in Fringe(T) that has minimum distance to the starting node g(v) is minimum
Example
What does Djikstra’s algorithm will do? (minimizing g(n))Problem: Visit too many nodes, some clearly out of the question
Complexity• Actual complexity is O(|E|log2 |E|)
• Is this good?Actually it is bad for very large graphs!
Branching factor: b
….
….# nodes = b(# levels)
Another Example: think of the search space in chess
Uninformed Search Strategies
• estimated with b= 10, a search rate of 105 nodes/sec, and 1 KiB/node• 1 mebibyte is 220 bytes• 1 tebibyte = 240 bytes• 1 pebibyte = 250 bytes• 1 exbibyte = 260 bytes
Depth(Breadth First)
Nodes Time Memory
2 110 1 sec 1MiB
4 11,100 11 sec 106 MiB
6 107 19 minutes 10 GiB
8 109 31 hours 1 TiB
10 1011 129 days 101 TiB
12 1013 35 years 10 PiB
14 1015 3,523 years 1 EiB
Credit: AIMA fig 3.11, pg 74
Better Solution: Take a ‘hunch”!
• Use heuristics to guide the search– Heuristic: estimation or “hunch” of how to search for a
solution• We define a heuristic function:
h(n) = “estimate of the cost of the cheapest path from the starting node to the goal node”
Lets Try A Heuristic
Heuristic: minimize h(n) = “Euclidean distance to destination” Problem: not optimal (through Rimmici Viicea and Pitesti is shorter)
The A* Search
• Constraint: we want to still be able to generate the path with minimum cost
• A* is an algorithm that:– Uses heuristic to guide search– While ensuring that it will compute a path with
minimum cost
• A* computes the function f(n) = g(n) + h(n)
“actual cost”
“estimated cost”
A*
• f(n) = g(n) + h(n)– g(n) = “cost from the starting node to reach n”– h(n) = “estimate of the cost of the cheapest path from n
to the goal node”
1015
20
2015
518
25
33
ng(n)
h(n)
Example
A*: minimize f(n) = g(n) + h(n)
A* Map Problem Heuristics
• Manhattan vs. Euclidean distance comparison
Image Credit: Wikimedia Commons
http://www.vision.ee.ethz.ch/~cvcourse/astar/AStar.html
Uninformed Search Strategies
• Breadth-First in Action
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* in Games
• Path finding– Frequently used– Though sometimes even A* speed improvements are not
sufficient– Additional improvements are required
• A* can be used for planning moves computer-controlled player (e.g., chess)
• F.E.A.R. uses A* to plan its search
A* Optimizations
• Some games don’t pathfind Quick straight line check
E.g., Diablo 2• “Flood Approaches”
– Measure flooding• Visually on map
A* Optimizations
• Visual representation of flooding
Image Credit: AIGPW Figure 3.4.1
A* Optimizations
• Memory Pooling – pre-allocating a number of memory blocks with the same
size– Allocating memory CPU intensive– How much memory?
• Specific to each game• Find “perfect balance”
– Statistical Analysis– Current technology makes pooling practical
How to Create Good Heuristics
• Relax the conditions of the problem– This will result in admissible heuristics!
• Lets look at an 8-puzzle game:
http://www.permadi.com/java/puzzle8/
• Possible heuristics?
Example: Good Heuristics in 8-Puzzle Game
• Heuristic: a tile A can be moved to any tile B– h1(n) = “number of misplaced tiles in board n”
• Heuristic: a tile A can be moved to a tile B if B is adjacent to A– h2(n) = “sum of distances of misplaced tiles to goal positions
in board n”
• Some experimental results reported in Russell & Norvig (2002):– A* with h2 performs up to 10 times better than A* with h1– A* with h2 performs up to 36,000 times better than a classical
uninformed search algorithm (iterative deepening)
Properties of Heuristics• The heuristic h must be admissible (tree search):
– h(n) cost(n) • where cost(n) = “actual cost to reach goal”• Is h1 admissible? Is h2 admissible?
• The heuristic h must be consistent (graph search)– For each child p of n: h(n) cost(n,p) + h(p)– h(G) = 0 // G is a goal– Is h1 consistent? Is h2 consistent?
• Property: If h is consistent then h is admissible– But the opposite is not necessarily true
What about a game in real time?
• The Super Mario competition
• Remake of game classic
• API was provided to control Mario
• Use whatever means to control the AI
• Winner of 2009 competition used A*