topic 16: graphs in game ai - university of western...

29
Topic 16: Graphs in Game AI © CSSE CITS4242 Game Design and Multi-Media

Upload: others

Post on 11-Mar-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Topic 16: Graphs in Game AI - University of Western Australiateaching.csse.uwa.edu.au/units/CITS4242/16-graphs.pdf · •Each node in a navgraph usually represents the position of

Topic 16:

Graphs in Game AI

© CSSE CITS4242 Game Design and Multi-Media

Page 2: Topic 16: Graphs in Game AI - University of Western Australiateaching.csse.uwa.edu.au/units/CITS4242/16-graphs.pdf · •Each node in a navgraph usually represents the position of

Graphs in Game AI

• When developing the AI for games, one of the most common uses of graphs is to represent a network of paths an agent can use to navigate around its environment.

Page 3: Topic 16: Graphs in Game AI - University of Western Australiateaching.csse.uwa.edu.au/units/CITS4242/16-graphs.pdf · •Each node in a navgraph usually represents the position of

Navigation Graph

• A navigation graph, or navgraph, is an abstraction of all the locations in a game environment the game agents may visit and of all the connections between those points.

• Consequently, a well-designed navgraph is a data structure embodying all the possible paths through the game environment and is therefore extremely handy for helping your game agents decide how to get from A to B.

• Each node in a navgraph usually represents the position of a key area or object within the environment and each edge represents the connections between those points.

Page 4: Topic 16: Graphs in Game AI - University of Western Australiateaching.csse.uwa.edu.au/units/CITS4242/16-graphs.pdf · •Each node in a navgraph usually represents the position of

Euclidian Graph

• Furthermore, each edge will have an associated cost, which in the simplest case represents the distance between the nodes it connects. This type of graph is known to mathematicians as a Euclidian graph.

• This Figure shows a navigation graph created for a small walled environment and highlights a paththrough that graph.

Page 5: Topic 16: Graphs in Game AI - University of Western Australiateaching.csse.uwa.edu.au/units/CITS4242/16-graphs.pdf · •Each node in a navgraph usually represents the position of

Cost-based Graph in Games

• RTS/RPG type games, for instance, are often based upon a grid of tiles or cells, where each tile represents a different type of terrain such as grass, road, mud, etc.

• Therefore, it's convenient to create a graph using the center points of each tile and assigning edge costs based upon the distance between cells weighted for the terrain type the edge moves over.

• This approach enables game agents to easily calculate paths that avoid water, prefer traveling on roads to mud, and meander around mountains.

Page 6: Topic 16: Graphs in Game AI - University of Western Australiateaching.csse.uwa.edu.au/units/CITS4242/16-graphs.pdf · •Each node in a navgraph usually represents the position of

Example

Page 7: Topic 16: Graphs in Game AI - University of Western Australiateaching.csse.uwa.edu.au/units/CITS4242/16-graphs.pdf · •Each node in a navgraph usually represents the position of

Dependency Graphs

• Dependency graphs are used in resource management type games to describe the dependencies between the various buildings, materials, units, and technologies available to a player.

• This kind of graph makes it easy to see what prerequisites are required for the creation of each type of resource.

• Dependency graphs are invaluable when designing an AI for this type of genre because the AI can use them to decide on strategies, predict the future status of an opponent, and assign resources effectively.

Page 8: Topic 16: Graphs in Game AI - University of Western Australiateaching.csse.uwa.edu.au/units/CITS4242/16-graphs.pdf · •Each node in a navgraph usually represents the position of

Example

• Techniques:

– AI Planning– Logic Deduction

Page 9: Topic 16: Graphs in Game AI - University of Western Australiateaching.csse.uwa.edu.au/units/CITS4242/16-graphs.pdf · •Each node in a navgraph usually represents the position of

Scenarios of using Dependency Graphs

• If the AI is preparing for battle and ascertains that archers are going to be advantageous, it can examine the dependency graph to conclude that before it can produce archers, it must first make sure it has a barracks and the technology of arrows. It also knows that in order to produce arrows it must have a lumber mill producing wood.

– if the AI already has a lumber mill it can assign resources to building a barracks or vice versa.

– If the AI has neither a barracks nor a lumber mill it can inspect the technology graph further to determine that it's probably advantageous to build the barracks before the lumber mill. Why? Because the barracks is a prerequisite for three different kinds of fighting unit, whereas a lumber mill is only a prerequisite for producing wood.

Page 10: Topic 16: Graphs in Game AI - University of Western Australiateaching.csse.uwa.edu.au/units/CITS4242/16-graphs.pdf · •Each node in a navgraph usually represents the position of

Scenarios of using Dependency Graphs

• If an enemy foot soldier carrying a gun comes into the AI's territory, the AI can work backward through the graph to conclude that:

– The enemy must have already built a forge and a lumber mill.– The enemy must have developed the technology of gunpowder.– The enemy must be producing the resources of wood and iron.

• Further examination of the graph would indicate that the enemy probably either has cannons or is currently building them. Nasty!

Page 11: Topic 16: Graphs in Game AI - University of Western Australiateaching.csse.uwa.edu.au/units/CITS4242/16-graphs.pdf · •Each node in a navgraph usually represents the position of

Scenarios of using Dependency Graphs

• The AI can use this information to decide on the best plan of attack. For example, the AI would know that to prevent any more gun-toting enemies from reaching its territory, it should target the enemy's forge and lumber mill. It can also infer that sending an assassin to hit the enemy blacksmith would weaken its foe considerably, and perhaps devote resources toward creating an assassin for this purpose.

• If the costs of building each resource are assigned to the dependency graph's edges, then the AI can use this information to calculate the most efficient route to produce that resource.

Page 12: Topic 16: Graphs in Game AI - University of Western Australiateaching.csse.uwa.edu.au/units/CITS4242/16-graphs.pdf · •Each node in a navgraph usually represents the position of

State Graph

• A state graph is a representation of every possible state a system can be in and the transitions between those states. This collection of a system's potential states is known as its state space.

• A graph of this type can be searched to see if a particular state is possible or to find the most efficient route to a specific state.

Page 13: Topic 16: Graphs in Game AI - University of Western Australiateaching.csse.uwa.edu.au/units/CITS4242/16-graphs.pdf · •Each node in a navgraph usually represents the position of

State Graph

• A state graph can easily be searched to find a goal state. In this example, the goal state is one where all the pieces are positioned on peg C in the correct order. By searching the state space it's possible to not only find a single solution, but to find every possible solution or the solution requiring the fewest moves (or the most moves if that's what you are looking for).

• The average number of child nodes radiating from each parent node is known as a graph's branching factor.

Page 14: Topic 16: Graphs in Game AI - University of Western Australiateaching.csse.uwa.edu.au/units/CITS4242/16-graphs.pdf · •Each node in a navgraph usually represents the position of

Branching Factor and Complexity

• For some problems, such as the “Towers of Hanoi” puzzle example we have discussed here, the branching factor is low — on the order of one to three branches per node — making it possible to represent the entire state space in memory.

• For many domains though, the branching factor is much higher and the number of potential states grows enormously as the distance from the root node (the depth of the graph) increases. To complete a search will take a long time.

• Consequently, these types of graphs are created and searched by expanding a few nodes at a time, typically (but not always) using algorithms that direct the search toward the goal state.

Page 15: Topic 16: Graphs in Game AI - University of Western Australiateaching.csse.uwa.edu.au/units/CITS4242/16-graphs.pdf · •Each node in a navgraph usually represents the position of

How Graphs are used?

• Visit every node in a graph, effectively mapping out the graph's topology.

• Find any path between two nodes. This is useful if you want to find a node but you don't really care how you get there. For example, this type of search can be used to find one (or more) of the solutions to the Towers of Hanoi puzzle.

• Find the best path between two nodes. What is "best" depends on the problem. If the graph to be searched is a navgraph, the best path may be the shortest path between two nodes, the path that takes an agent between two points in the fastest time, the path that avoids enemy line of sight, or the most quiet path (à la Thief). If the graph is a state graph such as that for the Towers of Hanoi puzzle, then the best path will be the one reaching the solution in the fewest steps.

Page 16: Topic 16: Graphs in Game AI - University of Western Australiateaching.csse.uwa.edu.au/units/CITS4242/16-graphs.pdf · •Each node in a navgraph usually represents the position of

Graph Search Algorithms

• Uninformed Graph Searches

– Often called blind searches. Search a graph without regard to any associated edge costs.

– Depth First Search– Breath First Search

• Cost-based Graph Search

– Dijkstra Search– A* Search

Page 17: Topic 16: Graphs in Game AI - University of Western Australiateaching.csse.uwa.edu.au/units/CITS4242/16-graphs.pdf · •Each node in a navgraph usually represents the position of

Exploratory – Depth First Search

• Meet little Billy. Billy is standing at the entrance to a typical theme park: a conglomeration of rides and other attractions and the paths meandering through the park that connect them. Billy doesn't have a map but he's eager to discover what rides and other entertainment the park has to offer.

• The depth first search is so named because it searches by moving as deep into the graph as possible. When it hits a dead end it backtracks to a shallower node where it can continue its exploration.

Page 18: Topic 16: Graphs in Game AI - University of Western Australiateaching.csse.uwa.edu.au/units/CITS4242/16-graphs.pdf · •Each node in a navgraph usually represents the position of

DFS

• From the entrance to the park (the source node), Billy makes a note of its description and of the paths that extend outward from it (the edges) on a slip of paper. Next, he chooses one of the paths to walk down. It makes no difference which — he can choose one at random provided it's a path he hasn't already explored. Every time a path leads Billy to a new attraction he makes a note of its name and of the paths connected to it.

• This process of moving forward through the graph as far as possible before backtracking to previously unexplored paths continues until the entire theme park has been mapped.

Page 19: Topic 16: Graphs in Game AI - University of Western Australiateaching.csse.uwa.edu.au/units/CITS4242/16-graphs.pdf · •Each node in a navgraph usually represents the position of

Building the map

Given a source node, the depth first search can only guarantee that all the nodes and edges will be visited in a connected graph.

Remember, a connected graph is one where any node can be reached from any other.

Page 20: Topic 16: Graphs in Game AI - University of Western Australiateaching.csse.uwa.edu.au/units/CITS4242/16-graphs.pdf · •Each node in a navgraph usually represents the position of

Implementation of DFS

LIFO Stack

Page 21: Topic 16: Graphs in Game AI - University of Western Australiateaching.csse.uwa.edu.au/units/CITS4242/16-graphs.pdf · •Each node in a navgraph usually represents the position of

Breath First Search

• The BFS algorithm fans out from the source node and examines each of the nodes its edges lead to before fanning out from those nodes and examining all the edges they connect to and so on.

• You can think of the search as exploring all the nodes that are one edge away from the source node, then all the nodes two edges away, then three edges, and so on until the target node is found.

• Therefore, as soon as the target is located, the path leading to it is guaranteed to contain the fewest edges possible.

Page 23: Topic 16: Graphs in Game AI - University of Western Australiateaching.csse.uwa.edu.au/units/CITS4242/16-graphs.pdf · •Each node in a navgraph usually represents the position of

Problem with BFS

• If we denote the branching factor as b and the number of edges the target node is away from the source as d (the depth), then the number of nodes examined is given by

• You can speed up the BFS (and many other graph search algorithms) by running two searches simultaneously, one started at the source node and the other at the target node, and stopping the search when they meet. This is called a bidirectional search.

Page 24: Topic 16: Graphs in Game AI - University of Western Australiateaching.csse.uwa.edu.au/units/CITS4242/16-graphs.pdf · •Each node in a navgraph usually represents the position of

Edge Relaxation

• The search algorithms discussed in the remainder of this lecture are based on a technique called edge relaxation.

• As an algorithm proceeds it gathers information about the best path found so far (BPSF) from the source node to any of the other nodes en route to the target.

• This information is updated as new edges are examined. If a newly examined edge infers that the path to a node may be made shorter by using it in place of the existing best path, then that edge is added and the path is updated accordingly.

Page 25: Topic 16: Graphs in Game AI - University of Western Australiateaching.csse.uwa.edu.au/units/CITS4242/16-graphs.pdf · •Each node in a navgraph usually represents the position of

Edsger Wybe Dijkstra

http://www.cs.utexas.edu/users/EWD/

Page 26: Topic 16: Graphs in Game AI - University of Western Australiateaching.csse.uwa.edu.au/units/CITS4242/16-graphs.pdf · •Each node in a navgraph usually represents the position of

Implementation of Dijkstra’s algorithm

• Shortest Path Tree (SPT)

Page 27: Topic 16: Graphs in Game AI - University of Western Australiateaching.csse.uwa.edu.au/units/CITS4242/16-graphs.pdf · •Each node in a navgraph usually represents the position of

A* - Dijkstra’s algorithm with a twist

• Dijkstra's algorithm searches by minimizing the cost of the path so far. It can be improved significantly by taking into account, when putting nodes on the frontier, an estimate of the cost to the target from each node under consideration. This estimate is usually referred to as a heuristic.

• A* proceeds in an almost identical fashion to Dijkstra's search algorithm. The only difference is in the calculation of the costs of the nodes on the search frontier. The adjusted cost, F, to the node when positioned on the priority queue (the search frontier), is calculated as: F = G+H

– where G is the cumulative cost to reach a node and H is the heuristic estimate of the distance to the target.

Page 28: Topic 16: Graphs in Game AI - University of Western Australiateaching.csse.uwa.edu.au/units/CITS4242/16-graphs.pdf · •Each node in a navgraph usually represents the position of

A* - Dijkstra’s algorithm with a twist

• For an edge E that has just come off the frontier and been added to the SPT, the pseudocode to calculate the cost to its destination node looks like this:

Cost = AccumulativeCostTo(E.From) + E.Cost + CostTo(Target)

• Utilizing a heuristic in this way, the modified costs direct the search toward the target node instead of radiating outward equally in all directions. This results in fewer edges needing to be examined, thereby speeding up the search and is the primary difference between A* and Dijkstra's algorithm.

Page 29: Topic 16: Graphs in Game AI - University of Western Australiateaching.csse.uwa.edu.au/units/CITS4242/16-graphs.pdf · •Each node in a navgraph usually represents the position of

A* - Example