Pathfinding and mazes

Pathfinding and mazes
and mazes
Pathfinding – problem statement
problem statement
• Finding the shortest route between two points
• How to represent the environment
• Lots of algorithms
Lots of algorithms
Environment representation
Environment representation
• Agents need to know where they can move
g
y
• Search space should represent either
– Clear routes that can be traversed
– Or the entire walkable surface
• Search space typically doesn’t represent:
– Small obstacles or moving objects
Small obstacles or moving objects
• Most common search space representations:
– Grids
– Waypoint graphs
– Navigation meshes
Grids
Grids
Grids as Graphs
Grids as Graphs
Distance metrics
Distance metrics
Qx, Qy
Px, Py
|P Q | |P Q |
|Px‐Qx| + |Py‐Qy|
Qx, Qy
Px, Py
05
[(Px‐Qx)
[(P
Q )2+(Py‐Qy)
(P Q )2]0.5
Waypoint graph
Waypoint graph
Pathfinding algorithms
•
•
•
•
•
•
Random walk
Random trace
Random trace
Breadth first search
B t fi t
Best first search
h
Dijkstra’s algorithm
A* algorithm
Evaluating graph search algorithms
Evaluating graph search algorithms
• Quality of final path
• Resource consumption during search
i d i
h
– CPU and memory
• Whether it is a complete algorithm
– A complete algorithm guarantees to find a path if one exists
Random walk
Random walk
• Agent takes a random step
– If goal reached, then done
• Repeat procedure until goal reached
• Add intelligence
– Only
Only step in cases where distance to goal is smaller
step in cases where distance to goal is smaller
– Stuck? With a certain probability, allow a step where distance to goal is greater
Random trace
Random trace
• Agent moves towards goal
– If goal reached, then done
If
l
h d th d
– If obstacle
• TTurn around the obstacle clockwise or counter‐
d h b
l l k i
clockwise (pick randomly) until free path towards goal
• Repeat procedure until goal reached
Repeat procedure until goal reached
Random walk and trace properties
Random walk and trace properties
• Not complete algorithms
• Found paths are unlikely to be optimal
• Consume very little memory
Consume very little memory
Random walk and trace performance
Random walk and trace performance
Search algorithms
Search algorithms
•
•
•
•
Breadth‐First
Breadth
First
Best‐First
Dijkstra’s
ijk
’
A* (combination of Best‐First and Dijkstra)
• Nodes represent candidate paths
Nodes represent candidate paths
• Keep track of numerous paths simultaneously
Breath First Search
A
Closed List (Those visited)
l d
( h
d)
A (Path A);
B
C
D
E
F
G
H
Open List (To be visited)
Open
List (To be visited)
B (Path A‐>B)
C (Path A‐>C)
Breath First Search
Closed List (Those visited)
A (P h A)
A (Path A);
B (Path A‐>B);
A
B
C
D
E
F
G
H
Open List (To be visited)
C (Path A‐>C);
C (Path A
>C);
E (Path A‐>B‐>E);
D (Path A‐>B‐>D);
Breath First Search
A
B
C
D
E
F
G
H
Closed List (Those visited)
A (P h A)
A (Path A);
B (Path A‐>B);
C (Path A‐>C);
Open List (To be visited)
E (Path A‐>B‐>E);
E (Path A
>B >E);
D (Path A‐>B‐>D);
Breath First Search
A
B
C
D
E
F
G
H
Closed List (Those visited)
A (P h A)
A (Path A);
B (Path A‐>B);
C (Path A‐>C);
E (Path A‐>B‐>E);
Open List (To be visited)
Open
List (To be visited)
D (Path A‐>B‐>D);
Breath First Search
A
B
C
D
E
F
G
H
Closed List (Those visited)
A (P h A)
A (Path A);
B (Path A‐>B);
C (Path A‐>C);
E (Path A‐>B‐>E);
D (Path A‐>B‐>D);
Open List (To be visited)
G (Path A‐>B‐>D‐>G)
F (P th A B D F)
F (Path A‐>B‐>D‐>F)
Breath First Search
A
B
C
D
E
F
G
H
Closed List (Those visited)
A (P h A)
A (Path A);
B (Path A‐>B);
C (Path A‐>C);
E (Path A‐>B‐>E);
D (Path A‐>B‐>D);
G (Path A‐>B‐>D‐>G)
G (Path A
>B >D >G)
Open List (To be visited)
F (P th A B D F)
F (Path A‐>B‐>D‐>F)
H (Path A‐>B‐>D‐>G‐>H)
Breath First Search
A
B
C
D
E
F
Closed List (Those visited)
A (P h A)
A (Path A);
B (Path A‐>B);
C (Path A‐>C);
E (Path A‐>B‐>E);
D (Path A‐>B‐>D);
G (Path A‐>B‐>D‐>G)
G (Path A
>B >D >G)
F (Path A‐>B‐>D‐>F)
Open List (To be visited)
O
Li t (T b i it d)
H (Path A‐>B‐>D‐>G‐>H)
G
H
Breath First Search
A
B
C
D
E
F
Closed List (Those visited)
A (P h A)
A (Path A);
B (Path A‐>B);
C (Path A‐>C);
E (Path A‐>B‐>E);
D (Path A‐>B‐>D);
G (Path A‐>B‐>D‐>G)
G (Path A
>B >D >G)
F (Path A‐>B‐>D‐>F)
H (Path A‐>B‐>D‐>G‐>H) … Found!
G
H
Open List (To be visited)
Graph search algorithms
Graph search
• Two lists: open and closed
• Open list keeps track of unexplored nodes
• Examine a node on the open list:
– Check to see if it is the goal
– If not, check its edges to add more nodes to the open list
If t h k it d
t dd
d t th
li t
• Each added node contains a reference to the node that explored it
– Place current node on closed list (mark a field as visited)
• Closed list are those that have been explored/processed and are not the goal
Node selection
Node selection
• Which open node becomes closed first?
Which open node becomes closed first?
– Breadth‐First processes the node that has been waiting the longest
waiting the longest
– Best‐First processes the one that is closest to the goal
– Dijkstra’sprocesses the one that is the cheapest according to some weight
g
g
– A* chooses a node that is cheap and close to the goal
Breadth first search
Breadth first search
Best first search
Best first search
Suboptimal best first search
Suboptimal best first search
Dijkstra’ss algorithm
Dijkstra
• Disregards distance to goal
Disregards distance to goal
– Keeps track of the cost of every path
– No guessing / no search heuristic
No guessing / no search heuristic
• Computes accumulated cost paid to reach a node from the start
d f
th t t
• Uses the cost (called the given cost) as a priority value to determine the next node that should be brought out of the open list
Dijkstra ssearch
Dijkstra’ssearch
A* algorithm
A
algorithm
• Admissible
Admissible heuristic function that never heuristic function that never
overestimates the true cost
• To order the open list, use
To order the open list use
– Heuristic cost – estimated cost to reach the goal
– Given cost –
Gi
cost to reach node from the start
h d f
h
Final Cost = Given Cost + (Heuristic Cost * Heuristic Weight)
Heuristic weight
Heuristic weight
• Final Cost = Given Cost + (Heuristic Cost * Heuristic Weight)
(
g )
• Heuristic weight controls the emphasis on the g
p
heuristic cost versus the given cost
• Controls the behavior of A
Controls the behavior of A*
– If hw=0, final cost will be the given cost ‐>Dijkstra
– As hw
As hw increases, it behaves more like Best‐First
increases it behaves more like Best First
A* search
A
search
More reading on pathfinding
More reading on pathfinding
• Amit’s
Amit s A
A* Pages Pages
http://theory.stanford.edu/~amitp/GameProg
ramming/
• Beginner’s guide to pathfindinghttp://ai‐
depot com/Tutorial/PathFinding html
depot.com/Tutorial/PathFinding.html
• Waypoint graphs vs. navigation meshes h //
http://www.ai‐blog.net/archives/000152.html
i bl
/ hi /000152 h l
Mazes
Maze taken from Image‐guided maze construction, Xu and Kaplan 2007, Siggraph
Simple mazes – binary grid
Simple mazes binary grid
• Matrix of booleans
ex: 21 x 21
• ex: 21 x 21
• Arbitrary mapping:
Arbitrary mapping:
– True = black pixel = wall
– False False = white pixel white pixel = open space
Game mazes– enumerated grid
Game mazes
enumerated grid
• Matrix
Matrix of enumerated of enumerated
values : ints
• Arbitrary mapping
– 0 = white pixel = open p
p
space
– 1 = black pixel = wall
– 2 = health = red plus
– Etc…
Procedural maze initial values
Procedural maze initial values
All open space – add walls
All open space –
add walls
All walls All
walls – add open space
add open space
(usually preferred)
Advanced algorithms can also start with artist input or an image
Maze generation – start with all walls
Maze generation start with all walls
• Randomized depth first search
– Mark current node as visited
M k
t d
i it d
– Make list of neighbors, pick one randomly
– If new node is not marked as visited, remove the wall between it and its parent
– Recursive
R
i
Maze generation – start with all walls
Maze generation start with all walls
• Randomized Kruskal
Randomized Kruskal'ss algorithm
– Randomly select a wall that joins two unconnected open areas, and merge them
– For an n x n grid there are initially n2open areas, one per grid cell
• Randomized Prim's algorithm
– Randomly pick a starting point
– Keep track of walls on the boundary of the maze
– Randomly select a wall that “grows” the maze
Examples
Animation examples from
http://en wikipedia org/wiki/Maze generation algorithm
http://en.wikipedia.org/wiki/Maze_generation_algorithm
Image‐based Mazes
Image‐based Mazes