Artificial Intelligence for Games
Lecture 3
1
Minor Games Programming
Artificial Intelligence for GamesTheory:
◦Graphs (Chapter 5)◦Path Planning (Chapter 8)
2
Theory: Graphs,(see study book: chapter 5)
GraphsGraphs in Game AIGraph Search Algorithms
◦(DFS, BFS, Dijkstra) self study◦A*
3
Examples of graphs
4
Which figures are?
A graph
A tree
A forest
A connected graph
An unconnected graph
A navigation graph (navgraph)
5
Finding (shortest) path in a Maze
6
Finding (shortest) path in a Maze
7
State Graphs
8
9
A sliding puzzle
10
http://www.gizdic.com/freegames/gamespages/wolf_sheep_cabbage.htm
11
Graph search algorithmsVisit every node in a graphFind any path between two nodesFind the best path between two
nodes
12
Self study or previous knowledgeBFSDFSDijkstra
READ THE FOLLOWING SLIDES
13
14
Depth First SearchLast-in first-outWe continue expanding the most
recent edge until we run out of edges◦no edges out or◦all edges point to visited nodes
Then we "backtrack" to the next edge and keep going
15
CharacteristicsCan easily get side-tracked into non-optimal pathsVery sensitive to the order in which edges are addedGuaranteed to find a path if one existsLow memory costs
◦only have to keep track of current path◦nodes fully explored can be discarded
Typical Complexity◦Time: O(E/2)◦Space: O(1)
assuming paths are short relative to the size of the graph
16
OptimalityDFS does not find the shortest
path◦returns the first path it encounters
If we want the shortest path◦we have to keep going◦until we have expanded everything
17
Breadth-first searchFirst-in first-outExpand nodes in the order in
which they are added◦don't expand "two steps" away◦until you've expanded all of the "one
step" nodes
18
CharacteristicsWill find shortest pathWon't get lost in deep treesCan be memory-intensive
◦frontier can become very large◦especially if branching factor is high
Typical Complexity◦Time: O(p*b)◦Space: O(E)
19
What if edges have weight?
If edges have weight◦ then we might want the lowest weight path◦ a path with more nodes might have lower
weightExample
◦ a path around the lava pit has more steps◦ but you have more health at the end◦ compared to the path that goes through the
lava pit
20
Weighted graph
v1 v2
v3 v4 v5
v6 v7
1
1
1
2
21
5 3
3
23
1
v1v2v3v4v5v6v7
v1 v2 v3 v4 v5 v6 v70 1 1 5 0 0 00 0 0 3 1 0 00 0 0 0 0 1 00 0 1 0 0 3 2
DijkstraEdgar Dijkstra (most famous
Dutch computer scientist)Dijkstra’s algorithm builds a
shortest path.
21
A*Dijkstra with a Twist: A*, includes
an heuristic: the Euclidean distance
(variant: Manhattan Distance Heuristic).
See !! : http://en.wikipedia.org/wiki/A*_search_algorithm 22
A* in pseudo code …………;-) function A*(start,goal) closedset := the empty set // The set of
nodes already evaluated. openset := set containing the initial node // The set of tentative nodes to be evaluated. g_score[start] := 0 // Distance from start along optimal path. h_score[start] := heuristic_estimate_of_distance(start, goal) f_score[start] := h_score[start] // Estimated total distance from start to goal through y. while openset is not empty x := the node in openset having the lowest f_score[] value if x = goal return reconstruct_path(came_from,goal) remove x from openset add x to closedset foreach y in neighbor_nodes(x) if y in closedset continue tentative_g_score := g_score[x] + dist_between(x,y) if y not in openset add y to openset tentative_is_better := true elseif tentative_g_score < g_score[y] tentative_is_better := true else tentative_is_better := false if tentative_is_better = true came_from[y] := x g_score[y] := tentative_g_score h_score[y] := heuristic_estimate_of_distance(y, goal) f_score[y] := g_score[y] + h_score[y] return failure
23
Manhattan Distance
24
Graphs:Pathfinderdemo
25
DEMOPlease do it now:
Run the pathfinder demo and experiment the different search algorithms. Also insert obstacles, mud and water ….
26
Theory: Path Planning,(see study book: chapter 8) Navigation Graph ConstructionRaven Navigation GraphCreating a Path Planner ClassSticky Situations
27
Navigation Graph ConstructionPoints of Visibility
(POV) navigation graph:
Created by handorUsing expanded
geometry techniques
28
Creating a POV using expanded geometry
29
NavMesh = network of convex polygons
30
Coarsely Granulated Graphs
If a game restricts its agents to movement along the edges of a navigation graph only, such as the movement of the characters in the Pac-Man range of games ,then a coarsely granulated navgraph is the perfect choice.
31
Effect 1: Unsightly pathsThe path of
an agent moving from its current position to the one marked by the X. Nasty.
32
Effect 2: Invisible positions
33
Run the CourseGraph demo
34
Finely Grained Graphs
35
Can be generated using the Flood Fill Algorithm
Flood Fill Algorithm
36
Spatial Partitioning !!One of the most frequently used
methods of a path planning class is a function that determines the closest visible node to a given position.
The performance will be in O(n2) time.
As you saw in chapter 3, the efficiency of such searches can be improved by using a spatial partitioning technique such as cell-space partitioning.
37
Planning a path to a positionThe path planner must:1. Find the closest visible
unobstructed graph node to the bot's current location.
2. Find the closest visible unobstructed graph node to the target location.
3. Use a search algorithm to find the least cost path between the two.
38
Planning a path to a position
39
A* is the better algorithm to search for the least cost path from the bot's current position to a specific target position
Planning a path to an item type
40
When many similar item types are present, Dijkstra's algorithm is the better choice
Paths as Nodes or Paths as Edges?
41
The game design requires that agents must change to the "swim" behavior when traveling from A to B (or vice versa), so the nodes A and B are annotated to reflect this. Do you see the problem?
Path Smoothing, Rough but Quick
42
Path Smoothing(A) Rough and Quick:
◦See figures 8.13 thru 8.17 for an example of the algorithm
(B) Precise but Slow:◦See figure 8.18 and the
corresponding text
43
RUN PathSmoothing Demo
44
Methods for Reducing CPU Overhead
Precalculated PathsPrecalculated CostsHierarchical PathfindingAnd:Time-Sliced Path Planning (you
can skip the code of pages 363-372)
45
Precalculated Paths
46
Precalculated Costs
47
Time-Sliced Path PlanningAn alternative to precalculating
lookup tables to lighten the CPU load is to allocate a fixed amount of CPU resources per update step for all the search requests and to distribute these resources evenly between the searches.
This is achieved by dividing up the searches over multiple time steps, a technique known as time slicing 48
Preventing the Twiddling of ThumbsOne consequence of time-sliced
path planning is that there will be a delay from the time an agent requests a path to the time it receives notification that the search has been successful or unsuccessful.
49
50
Result after smoothing
51
Please run demo Raven_TimeSlicing
Hierarchical Pathfinding
52
Getting Out of Sticky Situations -1-
An example: agent Jan is following the path A to B to C:
53
Getting Out of Sticky Situations -2-
Jan has reached waypoint A so it’s removed from the list and B assigned as the next waypoint. Unfortunately, as this happens, other bots arrive …
54
Getting Out of Sticky Situations -3-
Jan has been pushed all the way back out of the doorway, but he still keeps seeking to waypoint B. Silly Jan.
55
Getting Out of Sticky Situations -4-
Finally Jan ends up wedged against the wall, struggling furiously, still hopelessly trying to seek to his next waypoint as shown …
56
Getting Out of Sticky Situations -5-
How to prevent this behavior?
One way is to calculate an expected arrival time for each waypoint and replan if the current time exceeds the expected. This is the approach adopted in Raven.
57
Run the BotsGettingStuck demo
58
HomeworkThe homework will be:
Study the A* algorithm.Make a (generic) A*
implementation (in C#).
59
PracticeThe practice will be:
Solve a given puzzle by using the path finding algorithm A* you realized for your homework.
60
Study Read Chapter 5
Graphs (most of it is previous knowledge…)
Study Chapter 8 Path Planning (except the code and details of time-sliced path planning, pages 363-372)
61