390 likes | 530 Views
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:
E N D
The A* and applications to Games • Sources: • My own • Joseph Siefers presentation 2008
Case Study: The 8-Puzzle • http://www.permadi.com/java/puzzle8/
Case Study: The 8-Puzzle Left Up Down • Rules of the game: • Move free space {left, up, down, right} within boundaries of puzzle. Right
Search Terminology Left Up Down Right Search Tree Node
Search Terminology “Initial State” Left Up Down Right
Search Terminology “Successors” Right Left Up Down Successor Function
Search Terminology b=4 Right Left Up Down Branching Factor (b)
Search Terminology +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 (…) (…) (…) (…) (…) (…) Step Cost
Search Terminology +1 g(n) = 1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 (…) (…) (…) (…) (…) (…) Path Cost g(n)
Search Terminology +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 (…) (…) (…) g(n) = 2 (…) (…) (…) Path Cost g(n)
Search Terminology (…) (…) (…) (…) (…) (…) (…) (…) (…) (…) (…) (…) Goal State
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 20 15 10 5 15 ? 18 20 25 33
Greedy Algorithm Greedy algorithm: from the candidate nodes select the one that has a path with minimum cost from the starting node 20 15 10 5 15 ? 18 20 25 • Two problems: • O(b*n), n = # of edges (so bad) • Not optimal (might be OK) • Caution: Enemy Nations story 33
T Djikstra’s algorithm pick the edge v in Fringe(T) that has minimum distance to the starting node g(v) is minimum Djikstra Algorithm 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}
Example What does Djikstra’s algorithm will do? (minimizing g(n)) Problem: Visit too many nodes, some clearly out of the question
Branching factor: b …. …. # nodes = b(# levels) Complexity • Actual complexity is O(|E|log2 |E|) • Is this good? Actually it is bad for very large graphs! 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 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 RimmiciViicea and Pitesti is shorter)
“estimated cost” “actual cost” 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)
h(n) g(n) 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” 20 15 n 10 5 15 18 20 25 33
Example A*: minimize f(n) = g(n) + h(n)
A* Map Problem Heuristics http://www.vision.ee.ethz.ch/~cvcourse/astar/AStar.html • Manhattan vs. Euclidean distance comparison Image Credit: Wikimedia Commons
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… 75% LESS NODES! Breadth-First Search A*, Manhattan, W=3
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*