the a* and applications to games

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

Upload: pete

Post on 24-Feb-2016

48 views

Category:

Documents


0 download

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 Presentation

TRANSCRIPT

Page 1: The A* and applications to Games

The A* and applications to Games

Sources:• My own• Joseph Siefers presentation 2008

Page 2: The A* and applications to Games

CASE STUDY—THE 8-PUZZLE

Page 3: The A* and applications to Games

Case Study: The 8-Puzzle

http://www.permadi.com/java/puzzle8/

Page 4: The A* and applications to Games

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

Page 5: The A* and applications to Games

SEARCH TERMINOLOGY

Page 6: The A* and applications to Games

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

Page 7: The A* and applications to Games

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”

Page 8: The A* and applications to Games

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”

Page 9: The A* and applications to Games

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

Page 10: The A* and applications to Games

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

(…)(…) (…)

(…) (…)(…)

Page 11: The A* and applications to Games

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

Page 12: The A* and applications to Games

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

Page 13: The A* and applications to Games

Search Terminology

• Goal State 3 1 86 47 2 5

(…)(…)(…)

(…)

(…)(…)(…)

(…)(…)

(…)(…)(…)1 2 3

4 5 67 8

1 2 34 5 67 8

Page 14: The A* and applications to Games

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

Page 15: The A* and applications to Games

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

Page 16: The A* and applications to Games

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

Page 17: The A* and applications to Games

Example

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

Page 18: The A* and applications to Games

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

Page 19: The A* and applications to Games

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

Page 20: The A* and applications to Games

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”

Page 21: The A* and applications to Games

Lets Try A Heuristic

Heuristic: minimize h(n) = “Euclidean distance to destination” Problem: not optimal (through Rimmici Viicea and Pitesti is shorter)

Page 22: The A* and applications to Games

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”

Page 23: The A* and applications to Games

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)

Page 24: The A* and applications to Games

Example

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

Page 25: The A* and applications to Games

A* Map Problem Heuristics

• Manhattan vs. Euclidean distance comparison

Image Credit: Wikimedia Commons

http://www.vision.ee.ethz.ch/~cvcourse/astar/AStar.html

Page 26: The A* and applications to Games

Uninformed Search Strategies

• Breadth-First in Action

Page 27: The A* and applications to Games

A* Search: Compare and Contrast

• Manhattan distance: Weight 0

Page 28: The A* and applications to Games

A* Search: Compare and Contrast

• Manhattan distance: Weight 1

Page 29: The A* and applications to Games

A* Search: Compare and Contrast

• Manhattan distance: Weight 2

Page 30: The A* and applications to Games

A* Search: Compare and Contrast

• Manhattan distance: Weight 3

Page 31: The A* and applications to Games

A* Search: A Look Back…

• Breadth-First Search • A*, Manhattan, W=3

75% LESS NODES!

Page 32: The A* and applications to Games

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

Page 33: The A* and applications to Games

A* Optimizations

• Some games don’t pathfind Quick straight line check

E.g., Diablo 2• “Flood Approaches”

– Measure flooding• Visually on map

Page 34: The A* and applications to Games

A* Optimizations

• Visual representation of flooding

Image Credit: AIGPW Figure 3.4.1

Page 35: The A* and applications to Games

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

Page 36: The A* and applications to Games

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?

Page 37: The A* and applications to Games

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)

Page 38: The A* and applications to Games

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

Page 39: The A* and applications to Games

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*