pathfinding - part 1: Α* heuristic search
DESCRIPTION
These slides are part of a course about interactive objects in games. The lectures cover some of the most widely used methodologies that allow smart objects and non-player characters (NPCs) to exhibit autonomy and flexible behavior through various forms of decision making, including techniques for pathfinding, reactive behavior through automata and processes, and goal-oriented action planning. More information can be found here: http://tinyurl.com/sv-intobj-2013TRANSCRIPT
INTERACTIVE OBJECTS IN
GAMING APPLICATIONS
Basic principles and practical scenarios in Unity
June 2013 Stavros Vassos Sapienza University of Rome, DIAG, Italy [email protected]
Interactive objects in games 2
Pathfinding
Part 1: A* heuristic search on a grid
Part 2: Examples in Unity
Part 3: Beyond the basics
Action-based decision making
AI Architectures
Pathfinding 3
Preview
Interactive objects in games 4
Pathfinding
Part 1: A* heuristic search on a grid
Part 2: Examples in Unity
Part 3: Beyond the basics
Action-based decision making
AI Architectures
Pathfinding 5
Find a path that connects two points in the game world
Pathfinding 6
Find a path that connects two points in the game world
Fundamental requirement in most video games
Traditionally considered as “AI for games”
Typically dealt as separate component of the game
that is used “as a service” by other AI components,
e.g., the decision making component of characters
Pathfinding 7
Find a path that connects two points in the game world
Fundamental requirement in most video games
Traditionally considered as “AI for games”
Typically dealt as separate component of the game
that is used “as a service” by other AI components,
e.g., the decision making component of characters
More complicated than it looks!
Pathfinding 8
Funny video with bugs by Paul Tozour (2008)
youtube link
Pathfinding: A* heuristic search 9
Let’s start with a textbook approach
Game-world as a grid, A* heuristic search
Pathfinding: A* heuristic search 10
Let’s start with a textbook approach
Using tool from http://www.policyalmanac.org/
Pathfinding: A* heuristic search 11
Let’s start with a textbook approach
Game-world as a grid, A* heuristic search
Simple forward search
Explore until the target is found
Open list
Possible nodes to visit next
Closed list
Visited nodes
Choose next node using
A cost function g(n) – cost so far
A heuristic function h(n) – remaining
Pathfinding: A* heuristic search 12
Let’s start with a textbook approach
Game-world as a grid, A* heuristic search
Open list
Possible nodes to visit next
Closed list in blue
Visited nodes
Pathfinding: A* heuristic search 13
Let’s start with a textbook approach
Game-world as a grid, A* heuristic search
Open list in green
Possible nodes to visit next
Closed list
Visited nodes
Pathfinding: A* heuristic search 14
Let’s start with a textbook approach
Game-world as a grid, A* heuristic search
Open list in green
Possible nodes to visit next
Closed list in blue
Visited nodes
Here exactly one node is
expanded (i.e., the starting node)
Pathfinding: A* heuristic search 15
Let’s start with a textbook approach
Game-world as a grid, A* heuristic search
Use f(n) = g(n) + h(n) for each
node in the open list to pick the
next one to expand
f(n) is computed when node n is
added to the open list, when
node n’ is expanded, e.g.:
g(n) = g(n’) + 10 if straight
g(n) = g(n’) + 14 if diagonal
h(n) = manhattan distance to target
Pathfinding: A* heuristic search 16
Estimated remaining cost h(n) guides the search
Cost so far g(n) balances wrt weak estimates
g(n) = 0+10
h(n) = 80
f(n) = 90
Pathfinding: A* heuristic search 17
Manhattan distance: x2-x1 + y2-y1
Here it is an accurate estimate as the example is trivial
g(n) = 0+10
h(n) = 80
f(n) = 90
Pathfinding: A* heuristic search 18
Expanding the node with the minimal value of f(n)..
Pathfinding: A* heuristic search 19
Expanding the node with the minimal value of f(n)..
Pathfinding: A* heuristic search 20
Expanding the node with the minimal value of f(n)..
Pathfinding: A* heuristic search 21
Things become more interesting when there are
obstacles in the way
Pathfinding: A* heuristic search 22
What percentage of the search space will be
expanded (i.e., explored) in this case?
Pathfinding: A* heuristic search 23
We expect that the search would go directly toward the destination expanding only nodes to the right, until it hits the wall and then go toward up and down in parallel
Then, as the g(n) cost of each expanded node increases though, it forces the search method to look back and expand also in the other directions away from the goal
This way, following a dead-end for too long is avoided, but also more nodes are explored
Pathfinding: A* heuristic search 24
Pathfinding: A* heuristic search 25
Pathfinding: A* heuristic search 26
Pathfinding: A* heuristic search 27
All four nodes are equally
promising
Pathfinding: A* heuristic search 28
f(n1) = 54+50 = 104
f(n2) = 44+60 = 104
Pathfinding: A* heuristic search 29
f(n1) = 54+50 = 104
f(n2) = 44+60 = 104
But why not
f(n1) = 60+50 ?
f(n2) = 50+60 ?
Pathfinding: A* heuristic search 30
Some important details
What happens when a node is already visited
The closed list keeps track about visited nodes
Also keeps track of the best way so far to get there
When a node n is expanded by node n’, and f(n) is
better than the value stored in the closed list, then we
update the f(n) value
Pathfinding: A* heuristic search 31
This node is considered both when
the two other nodes are expanded
Pathfinding: A* heuristic search 32
Some important details
What happens when a node is already visited
The closed list keeps track about visited nodes
Also keeps track of the best way so far to get there
When a node n is expanded by node n’, and f(n) is
better than the value stored in the closed list, then we
update the f(n) value
We also need to update the best path that leads there
Pathfinding: A* heuristic search 33
Keeping track of the best path that leads to a node
In general we could store with each node also the path
that takes us there (we will see this in heuristic search A*
planning later)
Here we just need to store the parent of the node n,
i.e., the node n’ that we expanded to get n with the
best f(n) value
Pathfinding: A* heuristic search 34
Keeping track of the best path that leads to a node
In general we could store with each node also the path
that takes us there (we will see this in heuristic search A*
planning later)
Here we just need to store the parent of the node n,
i.e., the node n’ that we expanded to get n with the
best f(n) value
This is what these little arrows do in the images we saw
Pathfinding: A* heuristic search 35
Pathfinding: A* heuristic search 36
Pathfinding: A* heuristic search 37
f(n1) = 58+60 = 118
In fact more costly than the other
f(n2) = 10+100 = 110
Pathfinding: A* heuristic search 38
f(n1) = 58+60 = 118
In fact more costly than the other
f(n2) = 10+100 = 110
Put it differently: a
lot of cost g(n1) has
been invested, but
the estimated cost
h(n1) is not small
enough to make the
overall cost f(n1) the
best promising
Pathfinding: A* heuristic search 39
Pathfinding: A* heuristic search 40
Pathfinding: A* heuristic search 41
Pathfinding: A* heuristic search 42
f(n1) = 146
h(n2) = 146
Pathfinding: A* heuristic search 43
Important point about A*!
Does A* provide the shortest path?
Pathfinding: A* heuristic search 44
Important point about A*!
Does A* provide the shortest path?
It depends on the heuristic
One particular class of heuristics that ensure optimal
solutions are the admissible heuristics
These are optimistic heuristics that always
underestimate the remaining cost
Pathfinding: A* heuristic search 45
Important point about A*!
Does A* provide the shortest path?
It depends on the heuristic
One particular class of heuristics that ensure optimal
solutions are the admissible heuristics
These are optimistic heuristics that always
underestimate the remaining cost
Intuition1: Think of a heuristic that hugely overestimates
the optimal path and is zero for all other nodes.
Pathfinding: A* heuristic search 46
Important point about A*!
Does A* provide the shortest path?
It depends on the heuristic
One particular class of heuristics that ensure optimal
solutions are the admissible heuristics
These are optimistic heuristics that always
underestimate the remaining cost
Intuition2: When heuristic is zero for all nodes, the best
node is based on total cost so the optimal path is found.
Pathfinding: A* heuristic search 47
Important point about A*!
Does A* provide the shortest path?
It depends on the heuristic
One particular class of heuristics that ensure optimal
solutions are the admissible heuristics
These are optimistic heuristics that always
underestimate the remaining cost
(Understanding heuristics will become very important
when we talk about heuristic search A* planning later)
Pathfinding: A* heuristic search 48
Basic options to consider for pathfinding problems
Forward/backward/bi-directional search
A*/best-first/weighted A*/…
Domain-dependent/independent heuristics
Manhattan, Chebyshev, Euclidian
Heuristics on Amit's A* pages
Diagonal movement: 4-connected vs 8-connected grid
A* playground to try out different combinations
http://qiao.github.io/PathFinding.js/visual/
Pathfinding: A* heuristic search 49
Pathfinding: A* heuristic search 50
h(n): Euclidean
Grid: 4-connected
Pathfinding: A* heuristic search 51
h(n): Euclidean
Grid: 8-connected
Pathfinding: A* heuristic search 52
h(n): Manhattan
Grid: 4-connected
Pathfinding: A* heuristic search 53
h(n): Manhattan
Grid: 8-connected
Pathfinding: A* heuristic search 54
In many cases this is all you need to handle
pathfinding in a game (modulo tuning your
implementation to be efficient)
In AAA modern games though, the game-world is
more complicated both in terms of features and in
terms of size, and tricks (or research) is needed!
Pathfinding: A* heuristic search 55
More complicated game-worlds
Different types of terrain different cost
Road, path, water, terrain, mud, …
Different type of characters different cost/ability
Walking units, vehicles, air-units, scouters, …
More than one level
Stairs, elevators, hills, slopes, …
“Nicer” paths are needed!
Smoother curves, more “organic”, with variation, …
The game-world is huge!
Need for memory and CPU efficiency
Pathfinding: A* heuristic search 56
More complicated game-worlds
Different types of terrain different cost
Different type of characters different cost/ability
More than one level
“Nicer” paths are needed!
The game-world is huge!
We can deal with some of these with simple tricks
There is a lot of applied and research work!
E.g., AI Programming Wisdom book series
Also in top AI/Robotics conferences, e.g., AAAI, ICAPS,
AIIDE, IROS
Pathfinding: A* heuristic search 57
Commercial game grid-world benchmarks
http://www.movingai.com/benchmarks/
Pathfinding: A* heuristic search 58
GPPC: Grid-Based Path Planning Competition
http://movingai.com/GPPC/cfp.html
Maps
Size at most 2048x2048
Static (unchanging)
8-connected
Diagonal actions in an optimal path cost sqrt(2)
Pathfinding: A* heuristic search 59
Many neat tricks for efficient/nice pathfinding
Alternative game-world representations
Better heuristics
Beyond A*
But first let’s see how this simple grid representation
and A* heuristic works in a game setting in Unity
Pathfinding Part 2: Examples in Unity 60