1 / 39

The A* and applications to Games

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:

pete
Download Presentation

The A* and applications to Games

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The A* and applications to Games • Sources: • My own • Joseph Siefers presentation 2008

  2. Case Study—The 8-Puzzle

  3. Case Study: The 8-Puzzle • http://www.permadi.com/java/puzzle8/

  4. Case Study: The 8-Puzzle Left Up Down • Rules of the game: • Move free space {left, up, down, right} within boundaries of puzzle. Right

  5. Search Terminology

  6. Search Terminology Left Up Down Right Search Tree Node

  7. Search Terminology “Initial State” Left Up Down Right

  8. Search Terminology “Successors” Right Left Up Down Successor Function

  9. Search Terminology b=4 Right Left Up Down Branching Factor (b)

  10. Search Terminology +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 (…) (…) (…) (…) (…) (…) Step Cost

  11. Search Terminology +1 g(n) = 1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 (…) (…) (…) (…) (…) (…) Path Cost g(n)

  12. Search Terminology +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 (…) (…) (…) g(n) = 2 (…) (…) (…) Path Cost g(n)

  13. Search Terminology (…) (…) (…) (…) (…) (…) (…) (…) (…) (…) (…) (…) Goal State

  14. 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

  15. 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

  16. 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}

  17. Example What does Djikstra’s algorithm will do? (minimizing g(n)) Problem: Visit too many nodes, some clearly out of the question

  18. 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

  19. 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

  20. 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”

  21. Lets Try A Heuristic Heuristic: minimize h(n) = “Euclidean distance to destination” Problem: not optimal (through RimmiciViicea and Pitesti is shorter)

  22. “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)

  23. 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

  24. Example A*: minimize f(n) = g(n) + h(n)

  25. A* Map Problem Heuristics http://www.vision.ee.ethz.ch/~cvcourse/astar/AStar.html • Manhattan vs. Euclidean distance comparison Image Credit: Wikimedia Commons

  26. Uninformed Search Strategies • Breadth-First in Action

  27. A* Search: Compare and Contrast Manhattan distance: Weight 0

  28. A* Search: Compare and Contrast Manhattan distance: Weight 1

  29. A* Search: Compare and Contrast Manhattan distance: Weight 2

  30. A* Search: Compare and Contrast Manhattan distance: Weight 3

  31. A* Search: A Look Back… 75% LESS NODES! Breadth-First Search A*, Manhattan, W=3

  32. 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

  33. A* Optimizations • Some games don’t pathfind • Quick straight line check • E.g., Diablo 2 • “Flood Approaches” • Measure flooding • Visually on map

  34. A* Optimizations Visual representation of flooding Image Credit: AIGPW Figure 3.4.1

  35. 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

  36. 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?

  37. 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)

  38. 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

  39. 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*

More Related