Download - Pathfinding - Part 1: Α* heuristic search

Transcript
Page 1: Pathfinding - Part 1: Α* heuristic search

INTERACTIVE OBJECTS IN

GAMING APPLICATIONS

Basic principles and practical scenarios in Unity

June 2013 Stavros Vassos Sapienza University of Rome, DIAG, Italy [email protected]

Page 2: Pathfinding - Part 1: Α* heuristic search

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

Page 3: Pathfinding - Part 1: Α* heuristic search

Pathfinding 3

Preview

Page 4: Pathfinding - Part 1: Α* heuristic search

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

Page 5: Pathfinding - Part 1: Α* heuristic search

Pathfinding 5

Find a path that connects two points in the game world

Page 6: Pathfinding - Part 1: Α* heuristic search

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

Page 7: Pathfinding - Part 1: Α* heuristic search

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!

Page 9: Pathfinding - Part 1: Α* heuristic search

Pathfinding: A* heuristic search 9

Let’s start with a textbook approach

Game-world as a grid, A* heuristic search

Page 10: Pathfinding - Part 1: Α* heuristic search

Pathfinding: A* heuristic search 10

Let’s start with a textbook approach

Using tool from http://www.policyalmanac.org/

Page 11: Pathfinding - Part 1: Α* heuristic search

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

Page 12: Pathfinding - Part 1: Α* heuristic search

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

Page 13: Pathfinding - Part 1: Α* heuristic search

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

Page 14: Pathfinding - Part 1: Α* heuristic search

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)

Page 15: Pathfinding - Part 1: Α* heuristic search

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

Page 16: Pathfinding - Part 1: Α* heuristic search

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

Page 17: Pathfinding - Part 1: Α* heuristic search

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

Page 18: Pathfinding - Part 1: Α* heuristic search

Pathfinding: A* heuristic search 18

Expanding the node with the minimal value of f(n)..

Page 19: Pathfinding - Part 1: Α* heuristic search

Pathfinding: A* heuristic search 19

Expanding the node with the minimal value of f(n)..

Page 20: Pathfinding - Part 1: Α* heuristic search

Pathfinding: A* heuristic search 20

Expanding the node with the minimal value of f(n)..

Page 21: Pathfinding - Part 1: Α* heuristic search

Pathfinding: A* heuristic search 21

Things become more interesting when there are

obstacles in the way

Page 22: Pathfinding - Part 1: Α* heuristic search

Pathfinding: A* heuristic search 22

What percentage of the search space will be

expanded (i.e., explored) in this case?

Page 23: Pathfinding - Part 1: Α* heuristic search

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

Page 24: Pathfinding - Part 1: Α* heuristic search

Pathfinding: A* heuristic search 24

Page 25: Pathfinding - Part 1: Α* heuristic search

Pathfinding: A* heuristic search 25

Page 26: Pathfinding - Part 1: Α* heuristic search

Pathfinding: A* heuristic search 26

Page 27: Pathfinding - Part 1: Α* heuristic search

Pathfinding: A* heuristic search 27

All four nodes are equally

promising

Page 28: Pathfinding - Part 1: Α* heuristic search

Pathfinding: A* heuristic search 28

f(n1) = 54+50 = 104

f(n2) = 44+60 = 104

Page 29: Pathfinding - Part 1: Α* heuristic search

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 ?

Page 30: Pathfinding - Part 1: Α* heuristic search

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

Page 31: Pathfinding - Part 1: Α* heuristic search

Pathfinding: A* heuristic search 31

This node is considered both when

the two other nodes are expanded

Page 32: Pathfinding - Part 1: Α* heuristic search

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

Page 33: Pathfinding - Part 1: Α* heuristic search

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

Page 34: Pathfinding - Part 1: Α* heuristic search

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

Page 35: Pathfinding - Part 1: Α* heuristic search

Pathfinding: A* heuristic search 35

Page 36: Pathfinding - Part 1: Α* heuristic search

Pathfinding: A* heuristic search 36

Page 37: Pathfinding - Part 1: Α* heuristic search

Pathfinding: A* heuristic search 37

f(n1) = 58+60 = 118

In fact more costly than the other

f(n2) = 10+100 = 110

Page 38: Pathfinding - Part 1: Α* heuristic search

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

Page 39: Pathfinding - Part 1: Α* heuristic search

Pathfinding: A* heuristic search 39

Page 40: Pathfinding - Part 1: Α* heuristic search

Pathfinding: A* heuristic search 40

Page 41: Pathfinding - Part 1: Α* heuristic search

Pathfinding: A* heuristic search 41

Page 42: Pathfinding - Part 1: Α* heuristic search

Pathfinding: A* heuristic search 42

f(n1) = 146

h(n2) = 146

Page 43: Pathfinding - Part 1: Α* heuristic search

Pathfinding: A* heuristic search 43

Important point about A*!

Does A* provide the shortest path?

Page 44: Pathfinding - Part 1: Α* heuristic search

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

Page 45: Pathfinding - Part 1: Α* heuristic search

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.

Page 46: Pathfinding - Part 1: Α* heuristic search

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.

Page 47: Pathfinding - Part 1: Α* heuristic search

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)

Page 48: Pathfinding - Part 1: Α* heuristic search

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/

Page 49: Pathfinding - Part 1: Α* heuristic search

Pathfinding: A* heuristic search 49

Page 50: Pathfinding - Part 1: Α* heuristic search

Pathfinding: A* heuristic search 50

h(n): Euclidean

Grid: 4-connected

Page 51: Pathfinding - Part 1: Α* heuristic search

Pathfinding: A* heuristic search 51

h(n): Euclidean

Grid: 8-connected

Page 52: Pathfinding - Part 1: Α* heuristic search

Pathfinding: A* heuristic search 52

h(n): Manhattan

Grid: 4-connected

Page 53: Pathfinding - Part 1: Α* heuristic search

Pathfinding: A* heuristic search 53

h(n): Manhattan

Grid: 8-connected

Page 54: Pathfinding - Part 1: Α* heuristic search

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!

Page 55: Pathfinding - Part 1: Α* heuristic search

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

Page 56: Pathfinding - Part 1: Α* heuristic search

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

Page 57: Pathfinding - Part 1: Α* heuristic search

Pathfinding: A* heuristic search 57

Commercial game grid-world benchmarks

http://www.movingai.com/benchmarks/

Page 58: Pathfinding - Part 1: Α* heuristic search

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)

Page 59: Pathfinding - Part 1: Α* heuristic search

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

Page 60: Pathfinding - Part 1: Α* heuristic search

Pathfinding Part 2: Examples in Unity 60