graphs, goals and npcs

99
Graphs, Goals and NPCs Graphs, Goals and NPCs Advanced Programming for Advanced Programming for 3D Applications 3D Applications CE00383-3 CE00383-3

Upload: baris

Post on 12-Jan-2016

49 views

Category:

Documents


0 download

DESCRIPTION

Graphs, Goals and NPCs. Advanced Programming for 3D Applications CE00383-3. Path Planning and Search. Solution Path. Spatial Environment. discretisation. Grid of cells mapped on the environment. Search Tree. search. Path. Overview. Alternative Search Space Representations Grids - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Graphs, Goals and NPCs

Graphs, Goals and NPCsGraphs, Goals and NPCs

Advanced Programming for Advanced Programming for 3D Applications3D Applications

CE00383-3CE00383-3

Page 2: Graphs, Goals and NPCs

Path Planning and Path Planning and SearchSearch

Spatial Environment

Grid of cells mapped on the environment

Search Tree

Path

discretisation

search

SolutionPath

Page 3: Graphs, Goals and NPCs

OverviewOverview

• Alternative Search Space RepresentationsAlternative Search Space Representations– GridsGrids– GraphsGraphs– Meshes…Meshes…

• When we know what the world looks like, When we know what the world looks like, how can we move through it?how can we move through it?– A*A*– Precomputed Pathfinding with Navigation SetsPrecomputed Pathfinding with Navigation Sets– Potential FieldsPotential Fields

Page 4: Graphs, Goals and NPCs

IntroductioIntroductionn

• In many video In many video games, artificial games, artificial characters have to characters have to find their way find their way through spatial through spatial layouts:layouts:– rooms (DOOM-like rooms (DOOM-like

games)games)– terrain models, maps, terrain models, maps,

etc. (strategy games)etc. (strategy games)

Page 5: Graphs, Goals and NPCs

Search & Path Search & Path PlanningPlanning

• A significant number of games involve A significant number of games involve moving in buildings, battlefields, etc. moving in buildings, battlefields, etc.

• Choosing the best path within a Choosing the best path within a determined space is a classic AI problemdetermined space is a classic AI problem

• Algorithms have been developed that Algorithms have been developed that “construct” the best path by exploring “construct” the best path by exploring possible directions on the basis of possible directions on the basis of individual cells’ propertiesindividual cells’ properties

Page 6: Graphs, Goals and NPCs

DiscretisationDiscretisation

grid cells hexagonal cells

S

G

3D discretisation

Page 7: Graphs, Goals and NPCs

Regular GridsRegular Grids

Page 8: Graphs, Goals and NPCs

Regular GridsRegular Grids• How do we generate?How do we generate?• Advantages Advantages

– Random access lookup (O(1)) to determine Random access lookup (O(1)) to determine what tile lies at any coordinatewhat tile lies at any coordinate

– CompleteComplete

• NegativesNegatives– Usually requires large number of nodes to Usually requires large number of nodes to

accurately represent worldaccurately represent world– Path QualityPath Quality

- Agent can only walk in four cardinal directions? Agent can only walk in four cardinal directions? That’s no fun.That’s no fun.

- Let them walk diagonals! Still not much funLet them walk diagonals! Still not much fun

Page 9: Graphs, Goals and NPCs

Regular GridsRegular Grids

NWSE Diagonals

String-PullingCatmull-Rom Spline

Page 10: Graphs, Goals and NPCs

Grids as GraphsGrids as Graphs• Everything else we look at will be a Everything else we look at will be a

graphgraph

• Grids are graphs tooGrids are graphs too– Each cell is a node and edges are Each cell is a node and edges are

adjoining cellsadjoining cells

• Maybe we should just handle grid as Maybe we should just handle grid as a graph a graph – Undirected graph could tell us about Undirected graph could tell us about

topography, etc, whereas array can’ttopography, etc, whereas array can’t

Page 11: Graphs, Goals and NPCs

Grids as GraphsGrids as Graphs

Page 12: Graphs, Goals and NPCs

Corner GraphsCorner Graphs

• Waypoints around obstaclesWaypoints around obstacles

Page 13: Graphs, Goals and NPCs

Corner GraphsCorner Graphs

• How do we generate?How do we generate?– Identify convex corners, can character walk in straight Identify convex corners, can character walk in straight

line between? Add edge if possibleline between? Add edge if possible

• AdvantagesAdvantages– Less memoryLess memory– Faster generationFaster generation

• Negatives Negatives – Character will “walk on a rail” hugging edges of obstacles Character will “walk on a rail” hugging edges of obstacles

instead of walking through open spaceinstead of walking through open space– And what about different sized characters?And what about different sized characters?– Lookup is O(nLookup is O(n22), have to check every node in graph ), have to check every node in graph

against every other. Imagine a world with a boat load or against every other. Imagine a world with a boat load or corners!corners!

Page 14: Graphs, Goals and NPCs

Corner GraphsCorner Graphs

Page 15: Graphs, Goals and NPCs

Corner GraphsCorner Graphs

Page 16: Graphs, Goals and NPCs

Waypoint GraphsWaypoint Graphs• Place nodes in middle of rooms instead of Place nodes in middle of rooms instead of

at convex cornersat convex corners

Page 17: Graphs, Goals and NPCs

• How do we generate? How do we generate? – Place nodes wherever we want (suits 3-D worlds Place nodes wherever we want (suits 3-D worlds

But requires hand tuning to be effective But requires hand tuning to be effective ))

• AdvantagesAdvantages– Reduce memory footprint from regular grids, Reduce memory footprint from regular grids,

reduce wall hugging from corner graphsreduce wall hugging from corner graphs– Work well in “human” architecturesWork well in “human” architectures

• NegativesNegatives– Still O(nStill O(n22))– Path Quality vs. SimplicityPath Quality vs. Simplicity– Works poorly in open areasWorks poorly in open areas

Waypoint GraphsWaypoint Graphs

Page 18: Graphs, Goals and NPCs

Waypoint GraphsWaypoint Graphs

Page 19: Graphs, Goals and NPCs

Circle-Based Waypoint Circle-Based Waypoint GraphsGraphs• Add radius parameter to indicate open Add radius parameter to indicate open

space near waypointspace near waypoint

Page 20: Graphs, Goals and NPCs

Circle-Based WaypointCircle-Based Waypoint

• AdvantagesAdvantages– Only look at overlapping circles, Only look at overlapping circles,

alleviating O(nalleviating O(n22) problem from before) problem from before– Easier to obtain optimal pathsEasier to obtain optimal paths– Works well in open terrainWorks well in open terrain

• NegativesNegatives– Doesn’t work as well in human Doesn’t work as well in human

architectures that aren’t circle friendlyarchitectures that aren’t circle friendly

Page 21: Graphs, Goals and NPCs

Space-Filling VolumesSpace-Filling Volumes

• Use rectangles or 3-D Boxes instead of circlesUse rectangles or 3-D Boxes instead of circles

Page 22: Graphs, Goals and NPCs

Space-Filling VolumesSpace-Filling Volumes

• How do we generate?How do we generate?– Seed and GrowSeed and Grow– Make Grid and MergeMake Grid and Merge

• Very similar to circle-based, but Very similar to circle-based, but handles angles betterhandles angles better

Page 23: Graphs, Goals and NPCs

Navigation MeshesNavigation Meshes

• Let’s try and cover walk able Let’s try and cover walk able surfaces with convex polygonssurfaces with convex polygons

• Character can travel between Character can travel between adjoining polygonsadjoining polygons

Page 24: Graphs, Goals and NPCs

Navigation MeshesNavigation Meshes

Page 25: Graphs, Goals and NPCs

Navigation MeshesNavigation Meshes

• How do we generate? How do we generate? – By hand (time consuming)By hand (time consuming)

•Automated tools to analyze and optimize Automated tools to analyze and optimize geometry of worldgeometry of world

•Too complex and not represented as a single Too complex and not represented as a single polygon mesh, instead may be overlapping, polygon mesh, instead may be overlapping, etc etc

Page 26: Graphs, Goals and NPCs

Navigation MeshesNavigation Meshes

• AdvantagesAdvantages– Quickly find optimal paths independent Quickly find optimal paths independent

of character shapes and capabilitiesof character shapes and capabilities– Handle indoor and outdoor terrains wellHandle indoor and outdoor terrains well

• NegativesNegatives– Can become complex and expensive Can become complex and expensive

memory wisememory wise– Difficult to generateDifficult to generate

Page 27: Graphs, Goals and NPCs

Problem with N-Sided Problem with N-Sided MeshesMeshes

Page 28: Graphs, Goals and NPCs

Interacting with PathfindingInteracting with Pathfinding• What about dynamic objects What about dynamic objects

in world?in world?• All the representations All the representations

discussed have been static discussed have been static and obviously can’t handle a and obviously can’t handle a dynamic world directlydynamic world directly

• These representations need These representations need to be able to provide to be able to provide information to system information to system determining pathdetermining path– Waypoint Graphs and Corner Waypoint Graphs and Corner

Graphs don’t illustrate walk Graphs don’t illustrate walk able surfacesable surfaces

– Meshes and Grids do map Meshes and Grids do map every walk able surfaceevery walk able surface

– Space-filling volumes and Space-filling volumes and Circle Waypoints provide Circle Waypoints provide some representation of walk some representation of walk able areasable areas

Page 29: Graphs, Goals and NPCs

Path findingPath finding

• Now that we have world represented, Now that we have world represented, how do we plan movement?how do we plan movement?– A*, Depth-First, DijkstraA*, Depth-First, Dijkstra

•Dynamic path finding is expensiveDynamic path finding is expensive

– Precompiled solutions can eliminate Precompiled solutions can eliminate runtime cost, but memory expensiveruntime cost, but memory expensive•Article suggests technique of Navigation Set Article suggests technique of Navigation Set

HierarchyHierarchy

Page 30: Graphs, Goals and NPCs

Path findingPath finding

• Determine best paths leading from Determine best paths leading from source node to boundary of source setsource node to boundary of source set

• Determine best path from source set Determine best path from source set boundary to goal set boundaryboundary to goal set boundary

• Determine best path from goal set Determine best path from goal set boundary to goal nodeboundary to goal node

• Compile list of complete paths and Compile list of complete paths and choose one with least costchoose one with least cost

Page 31: Graphs, Goals and NPCs

Transition TableTransition Table• Main element in pre computed solutions is Main element in pre computed solutions is

a lookup tablea lookup table

• Each entry represents next step to take Each entry represents next step to take from one node to some goal nodefrom one node to some goal node

Page 32: Graphs, Goals and NPCs

Transition TableTransition Table

Page 33: Graphs, Goals and NPCs

Transition TableTransition Table

• Do not need full search of nodes at Do not need full search of nodes at run time, just series of lookupsrun time, just series of lookups

• Fast, but becomes memory Fast, but becomes memory expensive as size of world growsexpensive as size of world grows– How expensive? nHow expensive? n22

• ……solution to shrink transition tables?solution to shrink transition tables?

Hierarchy!Hierarchy!

Page 34: Graphs, Goals and NPCs

Navigation SetNavigation Set

• Self-contained collection of nodes Self-contained collection of nodes that requires no links to external that requires no links to external nodes to complete a pathnodes to complete a path

• Nodes can be members of more than Nodes can be members of more than one setone set

• Goal: Find someway to partition large Goal: Find someway to partition large Navigation Sets into smaller onesNavigation Sets into smaller ones

Page 35: Graphs, Goals and NPCs

Complete HierarchyComplete Hierarchy

Page 36: Graphs, Goals and NPCs

Interface Nodes and SetsInterface Nodes and Sets

• Need to account for paths that cross Need to account for paths that cross navigation setsnavigation sets

• Any node that connects to a node in Any node that connects to a node in another navigation set is an interface nodeanother navigation set is an interface node

• Have a second layer of nodes in addition to Have a second layer of nodes in addition to navigation sets, called interface setnavigation sets, called interface set

• Interface set itself is a navigation setInterface set itself is a navigation set– Therefore, can make transition table for it tooTherefore, can make transition table for it too

Page 37: Graphs, Goals and NPCs

Complete HierarchyComplete Hierarchy

• 21 nodes21 nodes– 1 Navigation Set = 441 Table Entries (21*21)1 Navigation Set = 441 Table Entries (21*21)– 4 Navigation Sets = 183 Table Entries (7*7 + 7*7 + 7*7 4 Navigation Sets = 183 Table Entries (7*7 + 7*7 + 7*7

+ 6*6)+ 6*6)

Page 38: Graphs, Goals and NPCs

Constructing the HierarchyConstructing the HierarchyTwo goals to processTwo goals to process

– How many tables to create?How many tables to create?• Amount of data needed is 1/n of original size + Amount of data needed is 1/n of original size +

interface setinterface set

• As size of navigation sets increase, cost of interface As size of navigation sets increase, cost of interface set becomes less a factorset becomes less a factor

– Where to place boundaries?Where to place boundaries?• Keep interface nodes as low as possibleKeep interface nodes as low as possible

Page 39: Graphs, Goals and NPCs

Constructing the HierarchyConstructing the Hierarchy

Page 40: Graphs, Goals and NPCs

Potential FieldsPotential Fields

• Reactive approach to path findingReactive approach to path finding– Setup virtual potential or force field Setup virtual potential or force field

around objectsaround objects– Must define field and agents reaction to Must define field and agents reaction to

fieldfield

• Interactions in general are explicitly Interactions in general are explicitly stated and movement “emerges”stated and movement “emerges”– Paths are not planned explicitlyPaths are not planned explicitly

Page 41: Graphs, Goals and NPCs

Potential FieldsPotential Fields

• Think of world as matrixThink of world as matrix– Each point tells has value to represent Each point tells has value to represent

strength of field under itstrength of field under it– Possibly assign potential based on Possibly assign potential based on

distance from goaldistance from goal•Might get stuck behind obstacleMight get stuck behind obstacle

– Result/Goal: “Glide down gradient/path Result/Goal: “Glide down gradient/path of least resistance”of least resistance”

Page 42: Graphs, Goals and NPCs

Potential FieldsPotential Fields

• Advantages?Advantages?– Works in continuous space! No need to Works in continuous space! No need to

make grid, place waypoints, etcmake grid, place waypoints, etc

• Disadvantages?Disadvantages?– Can be trapped in local minimaCan be trapped in local minima– It’s emergent, not sure what it will doIt’s emergent, not sure what it will do

Page 43: Graphs, Goals and NPCs

Path planning on a gridPath planning on a grid (1/2)(1/2)

• Going from A to B Going from A to B avoiding obstaclesavoiding obstacles

• It is only possible to It is only possible to move from one cell move from one cell to an adjacent cellto an adjacent cell

• How to find:How to find:– the shortest path?the shortest path?– In the shortest time?In the shortest time?– At the lesser cost?At the lesser cost?

A

B

Page 44: Graphs, Goals and NPCs

Path planning on a gridPath planning on a grid (2/2)(2/2)

• From each node of From each node of the grid, it is possible the grid, it is possible to go only to to go only to neighbouring nodesneighbouring nodes

• Path planning is the Path planning is the process by which a process by which a path is assembled path is assembled from A to B through from A to B through authorised movesauthorised moves

A

B

Page 45: Graphs, Goals and NPCs

Search Search TreesTrees• From a given node, it is possible to From a given node, it is possible to

generate a set of generate a set of neighboursneighbours. For . For instance a given node on the grid has instance a given node on the grid has for neighbours corresponding to the for neighbours corresponding to the N, S, E, W, NW, NE, SE, SW movesN, S, E, W, NW, NE, SE, SW moves

• The process of generating all the The process of generating all the neighbours of a given node is called neighbours of a given node is called node expansionnode expansion..

Page 46: Graphs, Goals and NPCs

Grid Moves and Search Grid Moves and Search TreesTrees

A

B

path

Page 47: Graphs, Goals and NPCs

Search Algorithms and Path Search Algorithms and Path PlanningPlanning

• Because the path on the grid is Because the path on the grid is formally equivalent to a path on the formally equivalent to a path on the tree, tree-searching algorithms can tree, tree-searching algorithms can be used for path planningbe used for path planning

• The path found on the tree does The path found on the tree does correspond to a path on the grid, correspond to a path on the grid, hence in the game environmenthence in the game environment

Page 48: Graphs, Goals and NPCs

Why Search?Why Search?

• Search is a key technique for AI, in Search is a key technique for AI, in particular real-time AIparticular real-time AI

• Applications in computer games Applications in computer games include:include:– Path planningPath planning– Moving Target SearchMoving Target Search– Two-player games (othello, chess, etc.)Two-player games (othello, chess, etc.)– Search-based planning for animation or Search-based planning for animation or

intelligent actor behaviourintelligent actor behaviour

Page 49: Graphs, Goals and NPCs

SearchSearch

• Search consists of a set of technique Search consists of a set of technique that explore graphs and extract that explore graphs and extract solutions, i.e. sub-graphs satisfying solutions, i.e. sub-graphs satisfying some desirable propertiessome desirable properties

• Solutions can be:Solutions can be:– Nodes (e.g. n-queens)Nodes (e.g. n-queens)– Paths (e.g. n-puzzle)Paths (e.g. n-puzzle)

Page 50: Graphs, Goals and NPCs

State-space SearchState-space Search• In the state space representation of a In the state space representation of a

problem, the nodes correspond to problem, the nodes correspond to partial problem solution states and the partial problem solution states and the arcs correspond to steps in the arcs correspond to steps in the problem solving process (or operators)problem solving process (or operators)

• This makes possible to apply the same This makes possible to apply the same graph-search algorithms for finding a graph-search algorithms for finding a solution to these problems (various solution to these problems (various optimisation problems, games, etc.)optimisation problems, games, etc.)

Page 51: Graphs, Goals and NPCs

Search ProblemsSearch Problems

• Many problems can be described as Many problems can be described as search problems (though this is not search problems (though this is not always the best description)always the best description)

• Some traditional search problems Some traditional search problems are used to teach search algorithms are used to teach search algorithms and assess the performance of and assess the performance of search methodssearch methods

Page 52: Graphs, Goals and NPCs

Search AlgorithmsSearch Algorithms

• These algorithms are based on graph These algorithms are based on graph traversal through node expansion, i.e. the traversal through node expansion, i.e. the generation of successors of a given nodegeneration of successors of a given node

• There are several formulations for search There are several formulations for search algorithms (e.g., recursive vs. non-algorithms (e.g., recursive vs. non-recursive ones)recursive ones)

Page 53: Graphs, Goals and NPCs

Search Algorithms (2)Search Algorithms (2)

• Traditionally, search algorithms are Traditionally, search algorithms are described using two specific data described using two specific data structures, OPEN and CLOSEDstructures, OPEN and CLOSED

• OPEN is the list of candidate nodes OPEN is the list of candidate nodes

• CLOSED is the list of explored nodesCLOSED is the list of explored nodes

Page 54: Graphs, Goals and NPCs

Uninformed SearchUninformed Search

• ““Brute force” searchBrute force” search

• … … could take more than the age of could take more than the age of universe on some problems (e.g. Chess)universe on some problems (e.g. Chess)

• One solutions is to use heuristics to One solutions is to use heuristics to prune the search space: this is called prune the search space: this is called informed search or heuristic searchinformed search or heuristic search

• All the search algorithms we will All the search algorithms we will describe are heuristic search algorithmsdescribe are heuristic search algorithms

Page 55: Graphs, Goals and NPCs

Breadth-first SearchBreadth-first Search

OPEN

a

b c d e

c d e f g h

d e f g h i j

1

2

a

b c d e

f g h i j k l m

n o p

34 5

6 7

1

2

a

b c d e

f g h i j k l m

n o p

34 5

6 7

Page 56: Graphs, Goals and NPCs

Breadth-first SearchBreadth-first Search

• Put S (the Start node) on OPENPut S (the Start node) on OPEN

• While OPEN is not empty:While OPEN is not empty:– Select the first node from OPENSelect the first node from OPEN– If n is a goal node, exit on success, elseIf n is a goal node, exit on success, else

•Expand node nExpand node n

•Put children at the end of OPEN Put children at the end of OPEN (eliminating those already in OPEN or (eliminating those already in OPEN or CLOSED)CLOSED)

• Note: this is a FIFO strategyNote: this is a FIFO strategy

Page 57: Graphs, Goals and NPCs

Depth-first SearchDepth-first Search

1

2

3

4 5

6

7

a

b c d e

f g h i j k l m

n o p

1

2

3

4 5

6

7

a

b c d e

f g h i j k l m

n o p

a

b c d e

f g h c d e

n o g h c d e

p h c d e

OPEN

Page 58: Graphs, Goals and NPCs

Depth-first SearchDepth-first Search

• Put S on OPENPut S on OPEN

• While OPEN is not empty do:While OPEN is not empty do:– Select first node from OPENSelect first node from OPEN– If n is a goal node, exit with solutionIf n is a goal node, exit with solution

•Else expand node n, put children of n Else expand node n, put children of n at the at the beginningbeginning of OPEN (eliminating those of OPEN (eliminating those already on OPEN or CLOSED)already on OPEN or CLOSED)

• Note: this is a LIFO strategyNote: this is a LIFO strategy

Page 59: Graphs, Goals and NPCs

Heuristic SearchHeuristic Search

• General idea: to use some form of General idea: to use some form of information to reduce the number of information to reduce the number of nodes explored by the search nodes explored by the search processprocess

• This saves time and space (memory)This saves time and space (memory)

• introduction to heuristic search: A*introduction to heuristic search: A*

Page 60: Graphs, Goals and NPCs

Heuristic SearchHeuristic Search

• Principle: using Principle: using information to guide the information to guide the search of the space state, search of the space state, reducing the number of reducing the number of nodes actually explorednodes actually explored

• This information This information characterises how a given characterises how a given node might lead to a node might lead to a solutionsolution

Page 61: Graphs, Goals and NPCs

Heuristic SearchHeuristic Search

• Depth-first search and breadth-first search Depth-first search and breadth-first search select the first node from the OPEN list as the select the first node from the OPEN list as the next node to be explorednext node to be explored

• The first node might not be the bestThe first node might not be the best• How to extract the best node from OPEN?How to extract the best node from OPEN?• An evaluation function, An evaluation function, f(n),f(n), measures the measures the

quality of each nodequality of each node• A heuristic is an estimate of the cost to the A heuristic is an estimate of the cost to the

goal nodegoal node• It is not “mathematically accurate”, it is a It is not “mathematically accurate”, it is a

guessguess

Page 62: Graphs, Goals and NPCs

Best-first SearchBest-first Search

• Put S on OPENPut S on OPEN• While OPEN is not empty:While OPEN is not empty:

– Select the node n from OPEN for which f(n) Select the node n from OPEN for which f(n) is minimal, put n in CLOSED; Expand n, is minimal, put n in CLOSED; Expand n, generating all its successors ngenerating all its successors nii

– For all nFor all ni i do:do:•Compute f(nCompute f(nii))• If nIf nii is neither in OPEN or CLOSED, put it in is neither in OPEN or CLOSED, put it in

OPENOPEN• If nIf nii is already in OPEN or CLOSED, compare is already in OPEN or CLOSED, compare

the new value of f(nthe new value of f(nii) to the old one. If the new ) to the old one. If the new one is lower (= better), substitute the new one is lower (= better), substitute the new value to the old, redirecting its pointers to nvalue to the old, redirecting its pointers to n

Page 63: Graphs, Goals and NPCs

Best-first SearchBest-first Search

• The problem with BF search is that is The problem with BF search is that is has “no memory”has “no memory”

• It can wander in the graph without It can wander in the graph without keeping track of the cost of the path it keeping track of the cost of the path it generated so fargenerated so far

Page 64: Graphs, Goals and NPCs

The A* AlgorithmThe A* Algorithm• A* is a heuristic search algorithm A* is a heuristic search algorithm

whose evaluation function, f, takes whose evaluation function, f, takes into account both the value of a node into account both the value of a node for reaching the goal and the cost: for reaching the goal and the cost: f(n) = g(n) + h(n)f(n) = g(n) + h(n)

• If n’ is a successor of n:If n’ is a successor of n:

g(n’) = c(n, n’) + g(n)g(n’) = c(n, n’) + g(n)

n’c(n, n’)

n

Page 65: Graphs, Goals and NPCs

The The ff function in function in A*A*

f’(n) = h’(n) + g(n)

“Best-first” component “Breadth-first” component

(breadth-first search if h(n) = 0 for all n)

Page 66: Graphs, Goals and NPCs

The A* Algorithm (1/3)The A* Algorithm (1/3)(adapted from J. Pearl) (adapted from J. Pearl)

1. Put initial node in OPEN1. Put initial node in OPEN

2. if OPEN is empty, exit on failure2. if OPEN is empty, exit on failure

3. Take out of OPEN the node n for which f is 3. Take out of OPEN the node n for which f is minimum and put it in CLOSEDminimum and put it in CLOSED

4. If n is a goal node, exit returning the path 4. If n is a goal node, exit returning the path obtained by tracing back pointers from n to obtained by tracing back pointers from n to ss

Page 67: Graphs, Goals and NPCs

A* Algorithm (2/3) A* Algorithm (2/3)

5. Otherwise expand n, generating all 5. Otherwise expand n, generating all successors nodes n’, and keep pointers successors nodes n’, and keep pointers from these to n. For each n’ (successor of from these to n. For each n’ (successor of n):n):

– if n’ is neither in OPEN or CLOSED, compute if n’ is neither in OPEN or CLOSED, compute h(n’) and f(n’) = h(n’) + g(n’)h(n’) and f(n’) = h(n’) + g(n’)

– if n’ is already either in OPEN or CLOSED, re-if n’ is already either in OPEN or CLOSED, re-direct its pointers along the path giving direct its pointers along the path giving minimal g(n’)minimal g(n’)

Page 68: Graphs, Goals and NPCs

A* Algorithm (3/3) A* Algorithm (3/3)

6. If n’ has required changing pointers 6. If n’ has required changing pointers and was previously on CLOSED, put it and was previously on CLOSED, put it back in OPENback in OPEN

7. Go to step 27. Go to step 2

Page 69: Graphs, Goals and NPCs

The A* Algorithm (pseudo-The A* Algorithm (pseudo-code)code)

While OPEN is not empty{ pop node n from OPEN for which f is minimal if n is a goal node return successful path for each successor n’ of n

{ if n’ is already in OPEN or CLOSED with a lower cost g(n’), consider next successor compute f(n’)

if n’ is in CLOSED, remove it from CLOSED if n’ is not yet in OPEN, push n’ on OPEN }

push n onto CLOSED }Return failure if no solution found

Page 70: Graphs, Goals and NPCs

Distance as Heuristic Distance as Heuristic Function Function

• Cell “x” is closer Cell “x” is closer to B than cell “z”to B than cell “z”

• As the goal is to As the goal is to find the shortest find the shortest path, path, distancedistance can be used to can be used to select the most select the most promising nodespromising nodes

A

B

x

z

Euclidean distance

“Manhattan distance”

Page 71: Graphs, Goals and NPCs

ExampleExample

S

G

12 3 4

5

3 2 4 1 5

S

f = g + h

f’ = g + h’ (estimate)

h’ = d (n, G)

c =1c

g(n’) = g(n) + c(n, n’)

Page 72: Graphs, Goals and NPCs

Example (2)Example (2)

S

G

12 3 4

5

3

2 4 1 5

S

3.1 3.2 3.3

3.13.2 3.3

g

Page 73: Graphs, Goals and NPCs

Example (3)Example (3)

2

4 1 5 3

S

2.22.3 2.1

S

G

12 3 4

5

3.1 3.2 3.3

2.12.22.3

1

1 1 1

1

2

2

2 2 2 2 2

2

2

3

3

3

3

3

4

4

4

4

4

4

5

5 5 5

6 6 66

6

64 4 5

55

666

Page 74: Graphs, Goals and NPCs

ExampleExample

S

G

• g(n) = steps

• h(n) = Manhattan distance

S

G

Page 75: Graphs, Goals and NPCs

ExampleExample

1+3=4

S1+5=6

GS

G

Page 76: Graphs, Goals and NPCs

ExampleExample

1+3=4

S1+5=6

GS

G

Page 77: Graphs, Goals and NPCs

ExampleExample

2+4=6

1+3=4

S1+5=6

2+2=4 GS

G

Page 78: Graphs, Goals and NPCs

ExampleExample

2+4=6

1+3=4

S1+5=6

2+2=4 GS

G

Page 79: Graphs, Goals and NPCs

ExampleExample

2+4=6

1+3=4

S1+5=6

3+3=6

2+2=4 G

Could choose any one of the 6’s next

S

G

Page 80: Graphs, Goals and NPCs

ExampleExample

2+4=6

1+3=4

S1+5=6

3+3=6

2+2=4 G

Let’s choose this one …

S

G

Page 81: Graphs, Goals and NPCs

ExampleExample

2+4=6

1+3=4

S1+5=6

3+3=6

2+2=4 G

4+4=8Could choose either of the 6’s

S

G

Page 82: Graphs, Goals and NPCs

ExampleExample

2+4=6

1+3=4

S1+5=6

3+3=6

2+2=4 G

4+4=8Let’s choose this one …

S

G

Page 83: Graphs, Goals and NPCs

ExampleExample

2+4=6

1+3=4

S1+5=6

3+3=6

2+2=4 G

3+5=8 4+4=8

S

G

Page 84: Graphs, Goals and NPCs

ExampleExample

2+4=6

1+3=4

S1+5=6

3+3=6

2+2=4 G

3+5=8 4+4=8

S

G

Page 85: Graphs, Goals and NPCs

ExampleExample

2+4=6

1+3=4

S1+5=6

3+3=6

2+2=4

2+4=6

G

3+5=8 4+4=8

S

G

Page 86: Graphs, Goals and NPCs

ExampleExample

2+4=6

1+3=4

S1+5=6

3+3=6

2+2=4

2+4=6

G

3+5=8 4+4=8

S

G

Page 87: Graphs, Goals and NPCs

ExampleExample

2+4=6

1+3=4

S1+5=6

3+3=6

2+2=4

2+4=6 3+3=6

G

3+5=8 4+4=8

S

G

Page 88: Graphs, Goals and NPCs

ExampleExample

2+4=6

1+3=4

S1+5=6

3+3=6

2+2=4

2+4=6 3+3=6

G

3+5=8 4+4=8

S

G

Page 89: Graphs, Goals and NPCs

ExampleExample

2+4=6

1+3=4

S1+5=6

3+3=6

2+2=4

2+4=6 3+3=6

G

4+2=6

3+5=8 4+4=8

S

G

Page 90: Graphs, Goals and NPCs

ExampleExample

2+4=6

1+3=4

S1+5=6

3+3=6

2+2=4

2+4=6 3+3=6

G

4+2=6

3+5=8 4+4=8

S

G

Page 91: Graphs, Goals and NPCs

ExampleExample

2+4=6

1+3=4

S1+5=6

3+3=6

2+2=4

2+4=6 3+3=6

G5+1=6

4+2=6

3+5=8 4+4=8

S

G

Page 92: Graphs, Goals and NPCs

ExampleExample

2+4=6

1+3=4

S1+5=6

3+3=6

2+2=4

2+4=6 3+3=6

G5+1=6

4+2=6

3+5=8 4+4=8

S

G

Page 93: Graphs, Goals and NPCs

ExampleExample

2+4=6

1+3=4

S1+5=6

3+3=6

2+2=4

2+4=6 3+3=6

G5+1=6

4+2=6

3+5=8 4+4=8

S

G

Page 94: Graphs, Goals and NPCs

Implementing Path Planning Implementing Path Planning (1)(1)

• Discretise your graphic environment to obtain Discretise your graphic environment to obtain a grid of cellsa grid of cells

• Determine the contents of each cell, this will Determine the contents of each cell, this will define the cost for each cell (whether it can define the cost for each cell (whether it can be traversed or not, and at which terrain cost)be traversed or not, and at which terrain cost)

• For a static environment, this can be done off-For a static environment, this can be done off-lineline

Page 95: Graphs, Goals and NPCs

Implementing Path Planning Implementing Path Planning (2)(2)

• Choose the data structures for OPEN and Choose the data structures for OPEN and CLOSED depending on estimated size of the CLOSED depending on estimated size of the problemproblem– get relevant source code for handling data get relevant source code for handling data

structures structures

• Implement the A* algorithm itself Implement the A* algorithm itself

• Map the resulting path to the animation Map the resulting path to the animation procedureprocedure

Page 96: Graphs, Goals and NPCs

Dynamic EnvironmentsDynamic Environments

• A* assumes that the goal is fixedA* assumes that the goal is fixed• Hence Hence it cannot be used in dynamic it cannot be used in dynamic

environmentsenvironments, i.e. environments in , i.e. environments in which the goal is movingwhich the goal is moving

• Solutions:Solutions:– A* to last known location + local searchA* to last known location + local search– Real-time A* (Algorithm versions of real-Real-time A* (Algorithm versions of real-

time search)time search)

Page 97: Graphs, Goals and NPCs

The A* “Family”The A* “Family”• RTA*: Real-Time A*. The algorithm RTA*: Real-Time A*. The algorithm

commits to a move every unit of timecommits to a move every unit of time

• LRTA*: Learning RTA* (variants can be LRTA*: Learning RTA* (variants can be used to chase moving targets)used to chase moving targets)

• AA*: *: -admissible A*-admissible A*

• IDA*: Iterative-Deepening A* (IDA*: Iterative-Deepening A* (ff threshold)threshold)

• D*: Dynamic A*, allows “replanning”D*: Dynamic A*, allows “replanning”

• RR*: A variant which uses information *: A variant which uses information on “how good” the heuristic function ison “how good” the heuristic function is

Page 98: Graphs, Goals and NPCs

A* searchA* search

• A* search is a heuristically guided graph searchA* search is a heuristically guided graph search• Under certain conditions, it is guaranteed to be Under certain conditions, it is guaranteed to be

optimal and no other search algorithm can find the optimal and no other search algorithm can find the solution while exploring fewer verticessolution while exploring fewer vertices

• For example, better than Dijkstra’s algorthim For example, better than Dijkstra’s algorthim because A* is guided by heuristicsbecause A* is guided by heuristics

• Inputs: graph representation, start vertex and goal Inputs: graph representation, start vertex and goal vertex, cost function, heuristic estimate functionvertex, cost function, heuristic estimate function

• Output: best path from start to goal, total costOutput: best path from start to goal, total cost

Page 99: Graphs, Goals and NPCs

Cost and heuristic functionsCost and heuristic functions• Cost function g(n)Cost function g(n)

– g(n) is the cost from the start vertex to vertex g(n) is the cost from the start vertex to vertex nn

– Cost could mean distance, resources, risk, etc.Cost could mean distance, resources, risk, etc.– Use distance as cost for shortest pathUse distance as cost for shortest path

• Heuristic function h(n)Heuristic function h(n)– h(n) is the estimated cost from n to the goalh(n) is the estimated cost from n to the goal– E.g., Euclidean distance, Manhattan distanceE.g., Euclidean distance, Manhattan distance

• Total cost f(n) = g(n) + h(n)Total cost f(n) = g(n) + h(n)