a rtificial i ntelligence for games

61
Artificial Intelligence for Games Lecture 3 1 Minor Games Programming

Upload: swain

Post on 15-Feb-2016

46 views

Category:

Documents


0 download

DESCRIPTION

A rtificial I ntelligence for Games. Minor Games Programming. Lecture 3 . Artificial Intelligence for Games. Theory: Graphs(Chapter 5) Path Planning(Chapter 8). Jan Verhoeven [email protected]. Theory: Graphs, (see study book: chapter 5). Graphs Graphs in Game AI - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: A rtificial   I ntelligence  for Games

Artificial Intelligence for Games

Lecture 3

1

Minor Games Programming

Page 2: A rtificial   I ntelligence  for Games

Artificial Intelligence for GamesTheory:

◦Graphs (Chapter 5)◦Path Planning (Chapter 8)

2

Jan [email protected]

Page 3: A rtificial   I ntelligence  for Games

Theory: Graphs,(see study book: chapter 5)

GraphsGraphs in Game AIGraph Search Algorithms

◦(DFS, BFS, Dijkstra) self study◦A*

3

Page 4: A rtificial   I ntelligence  for Games

Examples of graphs

4

Which figures are?

A graph

A tree

A forest

A connected graph

An unconnected graph

Page 5: A rtificial   I ntelligence  for Games

A navigation graph (navgraph)

5

Page 6: A rtificial   I ntelligence  for Games

Finding (shortest) path in a Maze

6

Page 7: A rtificial   I ntelligence  for Games

Finding (shortest) path in a Maze

7

Page 8: A rtificial   I ntelligence  for Games

State Graphs

8

Page 9: A rtificial   I ntelligence  for Games

9

Page 10: A rtificial   I ntelligence  for Games

A sliding puzzle

10

Page 12: A rtificial   I ntelligence  for Games

Graph search algorithmsVisit every node in a graphFind any path between two nodesFind the best path between two

nodes

12

Page 13: A rtificial   I ntelligence  for Games

Self study or previous knowledgeBFSDFSDijkstra

READ THE FOLLOWING SLIDES

13

Page 14: A rtificial   I ntelligence  for Games

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

Page 15: A rtificial   I ntelligence  for Games

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

Page 16: A rtificial   I ntelligence  for Games

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

Page 17: A rtificial   I ntelligence  for Games

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

Page 18: A rtificial   I ntelligence  for Games

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)

Page 19: A rtificial   I ntelligence  for Games

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

Page 20: A rtificial   I ntelligence  for Games

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

Page 21: A rtificial   I ntelligence  for Games

DijkstraEdgar Dijkstra (most famous

Dutch computer scientist)Dijkstra’s algorithm builds a

shortest path.

21

Page 22: A rtificial   I ntelligence  for Games

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

Page 23: A rtificial   I ntelligence  for Games

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

Page 24: A rtificial   I ntelligence  for Games

Manhattan Distance

24

Page 25: A rtificial   I ntelligence  for Games

Graphs:Pathfinderdemo

25

Page 26: A rtificial   I ntelligence  for Games

DEMOPlease do it now:

Run the pathfinder demo and experiment the different search algorithms. Also insert obstacles, mud and water ….

26

Page 27: A rtificial   I ntelligence  for Games

Theory: Path Planning,(see study book: chapter 8) Navigation Graph ConstructionRaven Navigation GraphCreating a Path Planner ClassSticky Situations

27

Page 28: A rtificial   I ntelligence  for Games

Navigation Graph ConstructionPoints of Visibility

(POV) navigation graph:

Created by handorUsing expanded

geometry techniques

28

Page 29: A rtificial   I ntelligence  for Games

Creating a POV using expanded geometry

29

Page 30: A rtificial   I ntelligence  for Games

NavMesh = network of convex polygons

30

Page 31: A rtificial   I ntelligence  for Games

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

Page 32: A rtificial   I ntelligence  for Games

Effect 1: Unsightly pathsThe path of

an agent moving from its current position to the one marked by the X. Nasty.

32

Page 33: A rtificial   I ntelligence  for Games

Effect 2: Invisible positions

33

Page 34: A rtificial   I ntelligence  for Games

Run the CourseGraph demo

34

Page 35: A rtificial   I ntelligence  for Games

Finely Grained Graphs

35

Can be generated using the Flood Fill Algorithm

Page 36: A rtificial   I ntelligence  for Games

Flood Fill Algorithm

36

Page 37: A rtificial   I ntelligence  for Games

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

Page 38: A rtificial   I ntelligence  for Games

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

Page 39: A rtificial   I ntelligence  for Games

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

Page 40: A rtificial   I ntelligence  for Games

Planning a path to an item type

40

When many similar item types are present, Dijkstra's algorithm is the better choice

Page 41: A rtificial   I ntelligence  for Games

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?

Page 42: A rtificial   I ntelligence  for Games

Path Smoothing, Rough but Quick

42

Page 43: A rtificial   I ntelligence  for Games

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

Page 44: A rtificial   I ntelligence  for Games

RUN PathSmoothing Demo

44

Page 45: A rtificial   I ntelligence  for Games

Methods for Reducing CPU Overhead

Precalculated PathsPrecalculated CostsHierarchical PathfindingAnd:Time-Sliced Path Planning (you

can skip the code of pages 363-372)

45

Page 46: A rtificial   I ntelligence  for Games

Precalculated Paths

46

Page 47: A rtificial   I ntelligence  for Games

Precalculated Costs

47

Page 48: A rtificial   I ntelligence  for Games

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

Page 49: A rtificial   I ntelligence  for Games

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

Page 50: A rtificial   I ntelligence  for Games

50

Page 51: A rtificial   I ntelligence  for Games

Result after smoothing

51

Please run demo Raven_TimeSlicing

Page 52: A rtificial   I ntelligence  for Games

Hierarchical Pathfinding

52

Page 53: A rtificial   I ntelligence  for Games

Getting Out of Sticky Situations -1-

An example: agent Jan is following the path A to B to C:

53

Page 54: A rtificial   I ntelligence  for Games

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

Page 55: A rtificial   I ntelligence  for Games

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

Page 56: A rtificial   I ntelligence  for Games

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

Page 57: A rtificial   I ntelligence  for Games

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

Page 58: A rtificial   I ntelligence  for Games

Run the BotsGettingStuck demo

58

Page 59: A rtificial   I ntelligence  for Games

HomeworkThe homework will be:

Study the A* algorithm.Make a (generic) A*

implementation (in C#).

59

Page 60: A rtificial   I ntelligence  for Games

PracticeThe practice will be:

Solve a given puzzle by using the path finding algorithm A* you realized for your homework.

60

Page 61: A rtificial   I ntelligence  for Games

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