chapter 15 artificial intelligence © 2008 cengage learning emea

32
CHAPTER 15 CHAPTER 15 Artificial Intelligence Artificial Intelligence © 2008 Cengage Learning EM

Post on 22-Dec-2015

229 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: CHAPTER 15 Artificial Intelligence © 2008 Cengage Learning EMEA

CHAPTER 15CHAPTER 15

Artificial IntelligenceArtificial Intelligence

© 2008 Cengage Learning EMEA

Page 2: CHAPTER 15 Artificial Intelligence © 2008 Cengage Learning EMEA

LEARNING OBJECTIVESLEARNING OBJECTIVES In this chapter you will learn about:

– History of AI in games– Pre-programmed AI– Scripting– Modeling AI by means of finite state machines– Autonomous behaviour: memory and planning– Pathfinding– The A* algorithm– Depth-first search– Breadth-first search– Waypoint pathfinding– More advanced AI: neural networks, expert

systems and genetic algorithms

Page 3: CHAPTER 15 Artificial Intelligence © 2008 Cengage Learning EMEA

HISTORY OF AI IN GAMESHISTORY OF AI IN GAMES Artificial intelligence (AI) is the design and

application of methods modeled after the behaviour of humans and/or animals to solve complicated problems.

More formally, AI can be defined as the design and application of intelligent agents.

An intelligent agent is a piece of software that intelligently supports the actions of a user.

These agents are used to monitor and intelligently act upon certain triggers present in an environment.

Page 4: CHAPTER 15 Artificial Intelligence © 2008 Cengage Learning EMEA

HISTORY OF AI IN GAMESHISTORY OF AI IN GAMES

So what is intelligence? – It is a collection of logic, mathematics,

probability, and memory, all applied in an interconnected manner that maximizes the chances of a successful outcome.

AI in games is a variation of the above-described technique to bring to ‘life’ non-player characters, thus creating the illusion of a life-like computer controlled player that is plotting, thinking and planning as a human would.

Page 5: CHAPTER 15 Artificial Intelligence © 2008 Cengage Learning EMEA

HISTORY OF AI IN GAMESHISTORY OF AI IN GAMES

Game AI differs substantially from theoretical academic AI in the sense that, even though numerous techniques exist, there is no right or wrong way of doing something – the end result is what matters. – For example, if a non-player character

or ‘bot’ (an abbreviation for the word robot) is indistinguishable from a real-life opponent then the AI of that character can be described as ideal.

Page 6: CHAPTER 15 Artificial Intelligence © 2008 Cengage Learning EMEA

HISTORY OF AI IN GAMESHISTORY OF AI IN GAMES Developers implementing AI as

showcased by the games released today, attempt to meet as many of the following criteria as possible (varying depending on the type of game and its genre):

Page 7: CHAPTER 15 Artificial Intelligence © 2008 Cengage Learning EMEA

HISTORY OF AI IN GAMESHISTORY OF AI IN GAMES 1 Cognitive model based non-player 1 Cognitive model based non-player

character AI (no way-point system).character AI (no way-point system). 2 ‘Intelligent’ non-combat (allies) and combat 2 ‘Intelligent’ non-combat (allies) and combat

(enemies) non-player character interaction.(enemies) non-player character interaction. 3 Interactive communication system, for 3 Interactive communication system, for

example, the player can instruct an ally to example, the player can instruct an ally to help out in a fight or to defend some other help out in a fight or to defend some other location.location.

4 Non-player characters make automatic 4 Non-player characters make automatic decision to fight, dodge, flee, hide, burrow, decision to fight, dodge, flee, hide, burrow, etc. based on player resistance, for example, etc. based on player resistance, for example, enemies can make decision to fall back to enemies can make decision to fall back to regroup if resistance is overwhelming.regroup if resistance is overwhelming.

Page 8: CHAPTER 15 Artificial Intelligence © 2008 Cengage Learning EMEA

PRE-PROGRAMMED AIPRE-PROGRAMMED AI Pre-programmed or deterministic AI is

the simplest form of AI found in games. Such algorithms add predetermined behaviour to an object and are characterized as minimal goal algorithms. – For example, an object can move from

one position to another based on some randomly calculated velocity (the only goal here is to move in a pre-defined path).

Page 9: CHAPTER 15 Artificial Intelligence © 2008 Cengage Learning EMEA

PRE-PROGRAMMED AIPRE-PROGRAMMED AI

[see the textbook for an example and detailed discussion].

Page 10: CHAPTER 15 Artificial Intelligence © 2008 Cengage Learning EMEA

SCRIPTINGSCRIPTING Pre-programmed AI is great for adding basic

predetermined behaviour to an object, but the associated randomness doesn’t serve us well in situations where an object is required to perform a specific sequence of tasks or steps. – For instance, a bot might be required to patrol an

area between two points; using deterministic AI we can place two objects at the ends of this path with the bot doing simple pathtracing between them.

– This works well but becomes tedious and computationally intensive when numerous paths need to be traced or when additional tasks such as the activation of a switch are required.

Page 11: CHAPTER 15 Artificial Intelligence © 2008 Cengage Learning EMEA

SCRIPTINGSCRIPTING

A deterministic algorithm would not be able to cope with a complicated sequence of steps such as the ones given here:1 If the player is within MAX_ATTACK_DISTANCE:

a Break into room through air vent.b Attack player using a combination of the following:

i Throw foreign objects such as barrels at the player.ii Use projectile-based weaponry.iii Use melee weapons after launching at player.

c Fight, dodge, flee, hide, or burrow based on player resistance.

2 If the player is not within MAX_ATTACK_DISTANCE:

a Wait.

Page 12: CHAPTER 15 Artificial Intelligence © 2008 Cengage Learning EMEA

SCRIPTINGSCRIPTING This is the reason for scripted AI; it provides us

with a convenient and accurate way of controlling the behaviour of non-player characters.

Returning to our example, we can now script its actions using the following scripted sequence:

1 Move forward.2 If END_OF_BRIDGE is reached, stop.

a Turn around (180-degree rotation).b Move forward.c If START_OF_BRIDGE is reached, stop.

i Turn around (180-degree rotation)

[see the textbook for an example and detailed discussion].

Page 13: CHAPTER 15 Artificial Intelligence © 2008 Cengage Learning EMEA

MODELING AI BY MEANS OF MODELING AI BY MEANS OF FINITE STATE MACHINESFINITE STATE MACHINES

Using FSMs, we can model the actions of AI controlled computer opponents through a number of states.

Page 14: CHAPTER 15 Artificial Intelligence © 2008 Cengage Learning EMEA

MODELING AI BY MEANS OF MODELING AI BY MEANS OF FINITE STATE MACHINESFINITE STATE MACHINES

As an example of developing an FSA for a bot, as found in common first-person shooter games such as Quake III and Unreal Tournament, we can start with a number of master states as listed here:1 Attack the player and other bots in sight.2 Hide when under heavy attack.3 Roam the game level if no target in sight.4 Collect game items like health boosters,

weapons, and armour.5 Select an attack pattern based on the

movement of the player.6 Stop the current action.

[see the textbook for an example and detailed discussion].

Page 15: CHAPTER 15 Artificial Intelligence © 2008 Cengage Learning EMEA

AUTONOMOUS BEHAVIOUR: AUTONOMOUS BEHAVIOUR: MEMORY AND PLANNINGMEMORY AND PLANNING

Up to this point we have not really dealt with any real AI.

Pre-programmed AI and scripting are great ways to create seemingly intelligent opponents but a truly intelligent opponent must have the ability to learn, remember, and adapt in much the same way as expected from a human being.

Page 16: CHAPTER 15 Artificial Intelligence © 2008 Cengage Learning EMEA

AUTONOMOUS BEHAVIOUR: AUTONOMOUS BEHAVIOUR: MEMORY AND PLANNINGMEMORY AND PLANNING

The simplest solution to the memory problem involves creating tables with information about the items stored at a specific location.

These tables can then be updated by the bot on an area-by-area basis.

Page 17: CHAPTER 15 Artificial Intelligence © 2008 Cengage Learning EMEA

AUTONOMOUS BEHAVIOUR: AUTONOMOUS BEHAVIOUR: MEMORY AND PLANNINGMEMORY AND PLANNING

The next step towards the goal of creating more realistic AI is to implement planning and decision trees.

Planning is used to solve problems where a series of actions must be performed to reach some goal.

An action is a specific step performed towards the successful completion of a goal.

Actions are in turn governed by conditionals, i.e. states that must be reached before a particular action can be executed.

[see the textbook for an example and detailed discussion].

Page 18: CHAPTER 15 Artificial Intelligence © 2008 Cengage Learning EMEA

AUTONOMOUS BEHAVIOUR: AUTONOMOUS BEHAVIOUR: MEMORY AND PLANNINGMEMORY AND PLANNING

Page 19: CHAPTER 15 Artificial Intelligence © 2008 Cengage Learning EMEA

PATHFINDINGPATHFINDING

Pathfinding is an AI technique used to determine a route from point A to point B, as shown in Figure 15-12.

Page 20: CHAPTER 15 Artificial Intelligence © 2008 Cengage Learning EMEA

PATHFINDINGPATHFINDING

If obstacle avoidance wasn’t a primary concern, we could simply have calculated a vector from the start point to end point. – For example, Figure 15-13

shows two points (A and B) in three-dimensional space with the AI technique of vectoring being used to determine the path.

Page 21: CHAPTER 15 Artificial Intelligence © 2008 Cengage Learning EMEA

The A* AlgorithmThe A* Algorithm The A* algorithm (pronounced ‘A star’) is one of

the most commonly used path finding algorithms. It is basically a tree-based search algorithm that

calculates the path from one node to some pre-specified goal node.

The A* algorithm starts at the root node, progressively writing the nodes to a list in order of accessibility. – Written nodes are each assigned a heuristic which is

used to sort them in a manner that should reveal the optimal route to the goal node – i.e. the algorithm continuously extends the path by moving to the node that seems to be closest to the goal.

The A* algorithm will thus not only find the path, but it will find the shortest path if it exists.

Page 22: CHAPTER 15 Artificial Intelligence © 2008 Cengage Learning EMEA

The A* AlgorithmThe A* Algorithm

Page 23: CHAPTER 15 Artificial Intelligence © 2008 Cengage Learning EMEA

The A* AlgorithmThe A* Algorithm The following pseudocode example details the A* algorithm’s

basic operation:1 Initialize the goal state node.2 Initialize the start state node.3 Add the start state node to the open list.4 While the open list has items in it:

a Determine the node with the lowest f(node) weight and initialize it as the current node – this node is removed from the open list.

b Generate all the nodes possible from the current node.c For each of these generated nodes:

i Calculate the cost of the generated node by taking the cost of the current node plus the cost to go from the generated node to the current node.

ii Search for the next generated node on the open list. If the weight of the current generated node is as good as or better

than this located node, then disregard the found node and continue.iii Search for the next generated node on the closed list.

If the weight of the current generated node is as good as or better than this located node, then disregard the found node and continue.

iv Remove all instances of the generated node from the open and closed list.v Set the generated node as a child node of the current node.vi Calculate the traversal cost from the current node to the goal and add the

generated node to the open list.d Add the current node to the closed list

Page 24: CHAPTER 15 Artificial Intelligence © 2008 Cengage Learning EMEA

Depth-First SearchDepth-First Search Depth-first search is a commonly used search

algorithm that follows each path to its greatest depth before moving on to the next.

This algorithm is extremely rudimentary when compared to A*; for example, it needs a stopping limit to halt the search if a goal isn’t found in say the first 200 nodes.

Depth-first search is an example of brute-force search/exhaustive search, that is, all nodes are traversed until the goal node is found.

Page 25: CHAPTER 15 Artificial Intelligence © 2008 Cengage Learning EMEA

Depth-First SearchDepth-First Search

The following pseudocode example details the depth-first search algorithm’s basic operation:1 Start at the given node (initially at root):1 Start at the given node (initially at root):

a Mark it as visited.a Mark it as visited.

2 For all the vertices adjacent to the initial node:2 For all the vertices adjacent to the initial node:

a If the vertex is unvisited:a If the vertex is unvisited:

i Recursively jump to step 1 with this node as i Recursively jump to step 1 with this node as input.input.

Page 26: CHAPTER 15 Artificial Intelligence © 2008 Cengage Learning EMEA

Breadth-First SearchBreadth-First Search

Breadth-first search is an alternative to the depth-first search path-finding algorithm that traverses a tree by breadth rather than depth.

The algorithm terminates the moment the goal state is reached and is much more efficient than depth-first search where the tree has very deep paths and particularly where the goal node is in a shallower part of the tree.

Page 27: CHAPTER 15 Artificial Intelligence © 2008 Cengage Learning EMEA

Breadth-First SearchBreadth-First Search The following pseudocode example details the

depth-first search algorithm’s basic operation:1 Start at the given node (initially at root):

a Mark it as visited.b Add it to a queue.

2 While the queue isn’t empty:a Access the node at the beginning of the queue:

i If this node is the goal node, quit and return this result.

ii Otherwise add all the subsequent unmarked child nodes of this node to the end of the queue.

3 If the queue is empty then no path has been found.4 Jump to step 2 and repeat.

Page 28: CHAPTER 15 Artificial Intelligence © 2008 Cengage Learning EMEA

Waypoint PathfindingWaypoint Pathfinding Searching and pathfinding algorithms are great

techniques for creating computer controlled opponents with enough intelligence to navigate complex environments themselves. – This isn’t, however, always necessary and by connecting

various points of interest we can utilize vectoring to produce accurate bot movement.

A waypoint system is thus a collection of nodes (points of interest) connected via directional links.

Each node or waypoint represents a spatial position that can be visited.

Each directional link consists of a vector and length to the next node in the network.

Page 29: CHAPTER 15 Artificial Intelligence © 2008 Cengage Learning EMEA

Waypoint PathfindingWaypoint Pathfinding

[see the textbook for an example and detailed discussion].

Page 30: CHAPTER 15 Artificial Intelligence © 2008 Cengage Learning EMEA

MORE ADVANCED AI: NEURAL MORE ADVANCED AI: NEURAL NETWORKS, EXPERT SYSTEMSNETWORKS, EXPERT SYSTEMS

AND GENETIC ALGORITHMSAND GENETIC ALGORITHMS A neural network is a mathematical

model based on human brain cells or neurons.

Page 31: CHAPTER 15 Artificial Intelligence © 2008 Cengage Learning EMEA

MORE ADVANCED AI: NEURAL MORE ADVANCED AI: NEURAL NETWORKS, EXPERT SYSTEMSNETWORKS, EXPERT SYSTEMS

AND GENETIC ALGORITHMSAND GENETIC ALGORITHMS Another technique often employed to facilitate the

storage of and access to human expertise, accumulated through training, is that of expert systems (also called ‘knowledge systems’).

An expert system is based on three concepts, namely, the knowledge of facts, data about the relationship among these facts and a method to store and access this data.

An expert system is created by extracting facts from human knowledge, such as routines, historic events, relationships, and transforming these data so that they can be used to influence the reasoning of a computer controlled opponent, for example.

The most basic expert system is defined by a set of production rules which is in turn used to analyse information.

This mathematical analysis yields a recommendation with regards to the course of action that should be taken by the user or bot.

Page 32: CHAPTER 15 Artificial Intelligence © 2008 Cengage Learning EMEA

MORE ADVANCED AI: NEURAL MORE ADVANCED AI: NEURAL NETWORKS, EXPERT SYSTEMSNETWORKS, EXPERT SYSTEMS

AND GENETIC ALGORITHMSAND GENETIC ALGORITHMS A genetic algorithm (GA) is an advanced search technique

based on the principles of genetics such as inheritance, crossover, mutation, and natural selection.

The following process outlines the execution of a genetic algorithm:1 Start by generating a population of chromosomes.2 Define some termination or goal criteria which can be used to

terminate the algorithm when satisfied, for example, by limiting the number of generations.

3 Test whether the termination criteria are satisfied: a If they are, terminate. b If they are not, jump to step 4.

4 Ascertain the fitness of all the chromosomes (for example how far a specific node is located from the goal node).

5 Generate a new population (the next generation) of chromosomes by selecting chromosomes based on their fitness and applying mutation and crossover to them.

6 Jump to step 3.