AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# –...
Transcript of AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# –...
CS 4730
AI and Pathfinding
CS 4730 – Computer Game Design
Some slides courtesy Tiffany Barnes, NCSU
CS 4730
AI Strategies • ReacAon vs. DeliberaAon • When having the NPC make a decision, how much thought goes into the next move?
• How is the AI different in: – Frozen Synapse – Kingdom Hearts – CivilizaAon – Halo
2
CS 4730
AI Strategies • ReacAon-‐Based
– Fast, but limited capabiliAes
• ImplementaAons – Finite-‐State Machines – Rule-‐Based Systems – Set PaVern
3
CS 4730
AI Strategies • DeliberaAon-‐Based
– Much slower, but more adaptable
• ImplementaAons – A* / Dijkstra – Roadmaps – GeneAc Algorithms
4
CS 4730
DeliberaAon • Goal is to emphasize making the best possible decision by searching across all possibiliAes
• Thus, deliberaAon tends to use various search algorithms across data structures that contain the opAon space
5
CS 4730
“Sense – Plan – Act” • Sense (or perceive) a sufficiently complete model of the world
• Plan by searching over possible future situaAons that would result from taking various acAons
• Act by execuAng the best course of acAon • Each possible outcome is effecAvely scored by a “metric of success” that indicates whether the choice should be taken or not
6
CS 4730
DeliberaAve vs. ReacAve • These are NOT mutually exclusive! • You can have reacAve policies for immediate threats – Incoming PC fire – Environmental destrucAon
• And you can have deliberaAve policies for long-‐term planning – Build orders and posiAoning
7
CS 4730
Core QuesAons • How do you represent knowledge about the current task, environment, PC, etc?
• How do you find acAons that allow the goal to be met?
8
CS 4730
Knowledge RepresentaAon • A decision tree is a common way to represent knowledge
• The root node is the current state of the game state WRT the NPC / AI
• Thus, deliberaAve AI techniques are effecAvely versions of search and shortest-‐path algorithms!
9
CS 4730
Tic-‐Tac-‐Toe • What is the decision tree for Tic-‐Tac-‐Toe?
10
CS 4730
The Goal State • The objecAve of the decision tree model is to move from the iniAal game state (root of the tree) to the goal state of the NPC – What might a goal state be?
• When searching through the decision tree space, which path do we take?
11
CS 4730
Cost and Reward • Every choice has a cost
– Ammo – Movement – Time – Increase vulnerability to aVack
• Every choice has a reward – Opportunity to hit PC – Capture a strategic point – Gain new resources
12
CS 4730
Minimax Algorithm • Find the path through the decision tree that yields the best outcome for one player, assuming the other player always makes a decision that would lead to the best outcome for themselves
13
CS 4730
Naïve Search Algorithms • Breadth-‐First Search
– At each depth, explore every opAon at the next depth
• Depth-‐First Search – Fully explore one possible path to its “conclusion”, then backtrack to check other opAons
• What are the problems with these techniques in gaming?
14
CS 4730
Breadth-‐First Search • Expand Root node
– Expand all Root node’s children • Expand all Root node’s grandchildren
• Problem: Memory size
Root Root
Child1 Child2 Root
Child1 Child2
GChild1 GChild2 GChild3 GChild4
CS 4730
Uniform Cost Search • Modify Breadth-‐First by expanding cheapest nodes first
• Minimize g(n) cost of path so far
Root
Child1 Child2
GChild1 9
GChild2 5
GChild3 3
GChild4 8
CS 4730
Depth First Search • Always expand the node that is deepest in the tree
Root
Child1
GChild1 GChild2
Root
Child1 Root
Child1
GChild1
CS 4730
Adding a HeurisAc • Simple definiAon: a heurisAc is a “mental shortcut” to ignore non-‐useful states to limit the search space and make the decision tree more reasonable
• What metrics might we use to determine “the value” of a potenAal opAon?
• What metrics might we use to determine “the cost” of a potenAal opAon?
18
CS 4730
Adding a HeurisAc • CreaAng an AI heurisAc forms the basis of how the NPCs will behave – Will they ignore enemies that are farther than X away?
– Will they avoid water? – Will they always move in the straightest path to the PC?
19
CS 4730
Cheaper Distance First!
20
CS 4730
Greedy Search • Expand the node that yields the minimum cost
– Expand the node that is closest to target – Depth first – Minimize the funcAon h(n) the heurisAc cost funcAon
CS 4730
Greedy Search
22
CS 4730
Greedy Search
23
CS 4730
Greedy Search • Greedy gives us (ojen) a sub-‐opAmal path, but it’s really cheap to calculate!
• How can we improve on this? • Add another aspect to the funcAon – the cost of the node + the heurisAc distance
24
CS 4730
A* • A best-‐first search (using heurisAcs) to find the least-‐cost path from the iniAal state to the goal state
• The algorithm follows the path of lowest expected cost, keeping a priority queue of alternate path segments along the way
25
CS 4730
A* Search • Minimize sum of costs • g(n) + h(n)
– Cost so far + heurisAc to goal • Guaranteed to work
– If h(n) does not overesBmate cost • Examples
– Euclidean distance
CS 4730
A*
27
CS 4730
A*
28
CS 4730
A*
29
CS 4730
NavigaAon Grid
30
CS 4730
Pathfinding in “real life” • These algorithms work great when the game is grid based – Square grid – Hex grid
• For more “open” games, like FPSs: – Path nodes are placed on the map that NPCs can reach
– NavigaAon mesh layers are added over the terrain – Ojen done automaAcally in advanced engines
31
CS 4730
NavigaAon Mesh • Instead of using discrete node locaAons, a node in this instance is a convex polygon
• Every point inside a valid polygon can be considered “fair game” to move into
• NavigaAon meshes can be auto generated by the engine, so easier to manage than nodes
• Can also handle different sized NPCs by checking collisions
32
CS 4730
NavigaAon Mesh
33
CS 4730
Groups • Groups stay together
– All units move at same speed – All units follow the same general path – Units arrive at the same Ame
ObstrucAon Goal
CS 4730
Groups • Need a hierarchical movement system • Group structure
– Manages its own prioriAes – Resolves its own collisions – Elects a commander that traces paths, etc
• Commander can be an explicit game feature
CS 4730
FormaAons • Groups with unit layouts
– Layouts designed in advance • AddiAonal States
– Forming – Formed – Broken
• Only formed formaAons can move
CS 4730
FormaAons • Schedule arrival into posiAon
– Start at the middle and work outwards – Move one unit at a Ame into posiAon – Pick the next unit with
• Least collisions • Least distance
– Formed units have highest priority • Forming units medium priority • Unformed units lowest
CS 4730
FormaAons
1 2
3
4
5 6
7
8
9
1 2 3
4 5
6 7 8 9
1 2
3
4
5 6
7
8
9
Not so good…
Better…
CS 4730
FormaAons: Wheeling • Only necessary for non-‐symmetric formaAons
1 2 3 4 5
1
2
3
4
5
Break formaAon here Stop moAon temporarily
Set re-‐formaAon point here
CS 4730
FormaAons: Obstacles
1 2 3 4 5 Scale formaAon layout to fit through gaps
1 2 3 4 5 1 2 3 4 5
1 2 3 4 5 Subdivide formaAon around small obstacles
CS 4730
FormaAons • Adopt a hierarchy of paths to simplify path-‐planning problems
• High-‐level path considers only large obstacles – Perhaps at lower resoluAon – Solves problem of gross formaAon movement – Paths around major terrain features
CS 4730
AI That Learns • Imagine a player in Madden calls a parAcular play on offense over and over and over
• The heurisAc values for certain states should change to reflect a more opAmal strategy
• Now, the adjustment of heurisAc values represents long-‐term strategy (to a degree)
42
CS 4730
AI That Evolves • Neural networks add in a mutaAon mechanic • Bayesian networks can also learn and add inferences
• How much processing power can we use for this?
• Is it beVer to truly have a “learning” AI, or should we just adjust some game parameters?
43