55
Video Game Development: Fundamental AI Technologies A. Babadi 1 of 55 In The Name Of God Video Game Development Amin Babadi Department of Electrical and Computer Engineering Isfahan University of Technology Spring 2015 Fundamental AI Technologies

10. Fundamental AI Technologies

Embed Size (px)

Citation preview

Page 1: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 1 of 55

In The Name Of God

Video Game Development

Amin Babadi

Department of Electrical and Computer Engineering

Isfahan University of Technology

Spring 2015

Fundamental AI Technologies

Page 2: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 2 of 55

Outline

What is artificial intelligence?

Types of AI

Structure of an AI system

Specific technologies

Page 3: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 3 of 55

Artificial Intelligence

What is AI?

Page 4: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 4 of 55

Artificial Intelligence

What is AI? o "AI is the computer simulation of intelligent behavior.“

Page 5: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 5 of 55

Artificial Intelligence

What is AI? o "AI is the computer simulation of intelligent behavior.“

What does “intelligence” mean? o “Behavior that exhibits great ability to adapt and solve complex

problems,“ or

o "behavior that is close to that of humans"?

Do we want artificial intelligence or artificial stupidity?!

Game AI consists of techniques used to produce the illusion of intelligence in the behavior of non-playable characters (NPCs).

Page 6: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 6 of 55

Artificial Intelligence

Take, for example, one of the classic AI problems-finding a route from point A to point B that avoids obstacles.

Many algorithms exist to solve this problem with varying degrees of success.

Comparison between A* and a human path finder. Left: initial problem. Middle: human. Right: A*

Page 7: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 7 of 55

Artificial Intelligence

So clearly, game AI is a balance between generating behavior that is both highly evolved and sophisticated, and behavior that is more or less human.

Several techniques can be used to ensure that our AIs are not just "problem solving robots“.

AIs must be lifelike entities that provide just the right amount of complexity to be challenging and engaging, but not more than that.

Page 8: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 8 of 55

Structure of an AI System

Let’s looking inside a single AI entity.

Fundamentally, AI systems come in two flavors. 1. Virtual characters

2. Abstract controllers

Page 9: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 9 of 55

1. Virtual Characters

These are usually enemies, but can also be non-playable characters.

Page 10: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 10 of 55

2. Abstract Controllers

Take a strategy game, for example.

Who provides the tactical reasoning?

Page 11: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 11 of 55

2. Abstract Controllers

Take a strategy game, for example.

Who provides the tactical reasoning? o A strategy game needs an additional entity that acts like the master

controller of the CPU side of the battle.

Page 12: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 12 of 55

Structure of an AI System

What are main elements of an AI system?

Page 13: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 13 of 55

Structure of an AI System

What are main elements of an AI system? 1. A sensor or input system

2. A working memory

3. A reasoning/analysis core

4. An action/output system

Let's briefly discuss each element of the structure.

Page 14: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 14 of 55

1. Sensing the World

All AIs need to be aware of their surroundings.

What should be sensed and how?

Page 15: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 15 of 55

1. Sensing the World

All AIs need to be aware of their surroundings.

What should be sensed and how? o It largely depends on the type of game you are creating.

let's compare the individual-level AI for a game like Quake to the abstract controller from Age of Empires.

Page 16: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 16 of 55

Quake

Page 17: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 17 of 55

Quake

In Quake, an individual enemy needs to know: o Where is the player and where is he looking?

o What is the geometry of the surroundings?

o Sometimes, which weapons am I using and which is he using?

In such a game, the visual system is a gross simplification of the human one.

Page 18: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 18 of 55

Age of Empires

Page 19: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 19 of 55

Age of Empires

In Age of Empires, the master controller needs to know: o What is the balance of power in each subarea of the map?

o How much of each type of resource do I have?

o What is the breakdown of unit types: infantry, cavalry, and so on?

o What is my status in terms of the technology tree?

o What is the geometry of the game world?

These are not simple tests at all!

What’s the solution?

Page 20: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 20 of 55

2. Memory

Storing AI data is often complex! (Why?)

Page 21: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 21 of 55

2. Memory

Storing AI data is often complex! (Why?) o The concepts being stored are not straightforward.

Page 22: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 22 of 55

2. Memory

Storing AI data is often complex! (Why?) o The concepts being stored are not straightforward.

Which one is easier? An individual AI or a master controller?

Page 23: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 23 of 55

2. Memory

Storing AI data is often complex! (Why?) o The concepts being stored are not straightforward.

Which one is easier? An individual AI or a master controller? o An individual AI

Page 24: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 24 of 55

2. Memory

Storing AI data is often complex! (Why?) o The concepts being stored are not straightforward.

Which one is easier? An individual AI or a master controller? o An individual AI

How do we store more abstract information, such as the balance of power? And how about a path?

Some of these data structures are nontrivial, and we will often end up with case-by-case solutions, especially when coding a master controller.

Page 25: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 25 of 55

3. Analysis/Reasoning Core

The analysis/reasoning core is what people often think about when they talk about AI.

Popular methods for such tasks are finite state machines and rule systems.

Luckily, many games require only simple decision-making processes.

A lot of games praised for their great AI have been built with relatively simple algorithms!

Page 26: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 26 of 55

4. Action/Output System

It is essential to couple our AI routines with clever action subroutines, so we get the feeling of real intelligence.

In fact, many games exaggerate this action system much like in a theater play, so the character's intentions are obvious and personality is conveyed.

Recall the Super Mario Bros game. o All types of crazy creatures filled the game world.

o But if you separate logic from the actual actions, you'll discover that these AIs were all very similar.

Page 27: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 27 of 55

Specific Technologies

In the rest of this lecture, we will explore the four main AI constructs most commonly used in game programming: 1. Finite state machines,

2. Rule systems,

3. State-space searches, and

4. Genetic algorithms.

Page 28: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 28 of 55

1. Finite State Machines (FSMs)

It is also called deterministic finite automata (DFA) or simply state machines or automata.

An FSM is a formalism consisting of two sets: o A set of states that represent the scenarios or configurations the AI

can be immersed in.

o A set of transitions that are conditions that connect two states in a directed way.

FSMs are the most popular technique used to create game AIs.

Page 29: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 29 of 55

Example 1. A Simple FSM

If you say something like the following, you will have created a state machine: o A dog is hungry.

o If you give him a bone, he will not be hungry anymore.

o He'll be quiet after eating the bone.

o And he'll become hungry after four hours of being quiet.

Which sentences are states?

Which sentences are transitions?

Page 30: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 30 of 55

Example 2. A Soldier FSM

Page 31: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 31 of 55

FSMs: Pros and Cons

Pros: o Intuitive to understand

o Easy to code

o Good performance

o Suitable for representing a broad range of behaviors

Cons: o Quick growth

o Scaling FSMs up is not always a trivial task.

Page 32: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 32 of 55

2. Parallel Automata

The core idea of parallel automata is to divide our complex behavior into different subsystems or layers, pretending that the entity being modeled has several brains.

Each sub-brain is assigned a simpler automata.

Now let’s give a gun to our soldier! o How many brains does he/she need?

Page 33: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 33 of 55

Example 3. Automata for Locomotion

Page 34: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 34 of 55

Example 3. Automata Controlling the Gun

Page 35: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 35 of 55

Example 3. Combined Automata

Page 36: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 36 of 55

Parallel Automata: Pros and Cons

Pros: o More control over complex AI systems

o Great to use to model those systems where we have simultaneous but largely independent behaviors going on

Cons: o Not suitable when we have interconnections

Page 37: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 37 of 55

3. Synchronized FSMs

Another addition to our FSM bag of tricks is implementing inter-automata communications, so several of our AIs can work together and, to a certain extent, cooperate.

By doing so we can have enemies that attack in squads. While one of them advances, the other provides coverage, and the third one calls for help.

Such an approach became very popular with the game Half-Life, which was considered a quantum leap in action AI, basically because of the team-based AI.

Page 38: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 38 of 55

3. Synchronized FSMs

Notice, however, that synchronizing AIs is no silver bullet.

The method we will use is not well suited for a full-blown strategic game with complete faction-based AIs involving dozens of entities. o Rule systems (explained in the next section) are much better suited

for that purpose.

Synchronizing AIs via FSMs is just a way to increase the expressive potential of our AI, so we can have small teams working in parallel.

Page 39: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 39 of 55

3. Synchronized FSMs

At the core, synchronizing AIs involves a shared memory area where all automata can write to and read data from.

Whenever an AI wants to say something to the rest, it posts it in the shared memory area.

So others can browse the information; and if it is relevant to them, they take it into consideration in their decision-making process.

Page 40: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 40 of 55

3. Synchronized FSMs

There are two common approaches to this task: 1. One is to use a message passing architecture, which involves sending

messages to those AIs we want to synchronize to.

2. The alternative and preferred method is to use a bulletin board architecture, where we don't send dedicated messages, but instead post our sync messages on a shared space (the bulletin board).

Let's make a synchronized AI for a squad of three soldiers, designed to attack an enemy in a coordinated manner.

Page 41: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 41 of 55

Example 4. Automata with no Synchronization

Page 42: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 42 of 55

Example 4. Fully Synchronized Automata

Page 43: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 43 of 55

4. Nondeterministic Automata

Classic FSMs are deterministic. o So the behavior of our AI entity will be totally predictable.

This predictability gives the developer tight control, but is not always desirable. (Why?)

A limited degree of randomness can be added to the mix to ensure that the AI will never become predictable.

This kind of automata is usually called Nondeterministic Finite Automata (NDFA).

Page 44: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 44 of 55

Example 5. NDFA for the Soldier

Page 45: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 45 of 55

5. Rule Systems (RSs)

Finite state machines are a very convenient tool for designing AIs.

They can model different behaviors elegantly.

But some phenomena are not easy to describe in terms of states and transitions.

Page 46: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 46 of 55

Example 6. Simplified Automata for a Dog

For example, imagine the following specs for a virtual dog: o If I'm hungry and there's a bone nearby, I'll eat it.

o If I'm hungry (but there is no bone around), I will wander.

o If I'm not hungry, but I'm sleepy, I will sleep.

o If I'm not hungry and not sleepy, I'll bark and walk.

Page 47: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 47 of 55

5. Rule Systems (RSs)

FSMs are well suited for behaviors that are: o Local in nature (while we are in a certain state, only a few outcomes

are possible).

o Sequential in nature (we carry out tasks after other tasks depending on certain conditions)

Is the virtual dog local or not? What about sequential? o Not local, not sequential

All the dog actually does is act according to some priorities or rules. o Luckily, there is one way to model this kind of prioritized, global

behavior.

Page 48: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 48 of 55

5. Rule Systems (RSs)

RS allows us to model many behaviors that are tricky to model using FSMs.

At the core of an RS, there is a set of rules that drive our AI's behavior.

Each rule has the form:

Condition ⟹ Action

Page 49: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 49 of 55

Example 6. A More Formal Specification

1. (Hungry) AND (Bone nearby) ⟹ Eat it

2. (Hungry) AND (No bone nearby) ⟹ Wander

3. (Not hungry) AND (Sleepy) ⟹ Sleep

4. (Not hungry) AND (Not sleepy) ⟹ Bark and walk

The execution of an RS is really straightforward.

We test the LHS of each expression (the conditions) in order, and then execute the RHS (the action) of the first rule that is activated.

Page 50: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 50 of 55

5. Rule Systems (RSs)

RSs, as opposed to FSMs, provide a global model of behavior.

RSs provide a better tool when we need to model behavior that is based on guidelines.

We model directions as rules, placing the more important ones closer to the top of the list, so they are priority executed.

Page 51: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 51 of 55

Example 7. The Rule System for a Soldier

Now imagine that we need to create the AI for a soldier in a large squadron.

1. If in contact with an enemy ⟹ combat

2. If an enemy is closer than 10 meters and I'm stronger than him ⟹ chase him

3. If an enemy is closer than 10 meters ⟹ escape him

4. If we have a command from our leader pending ⟹ execute it

5. If a friendly soldier is fighting and I have a ranged weapon ⟹ shoot at the enemy

6. Stay still

Page 52: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 52 of 55

Planning and Problem Solving

FSMs and rule sets are very useful paradigms to model simple behavior.

However, both approaches are limited when it comes to modeling other, more complex behaviors.

How can we teach a computer how to play chess?

Page 53: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 53 of 55

6. State-space Search

State-space search is a very powerful paradigm that can be used to create strategies and plans.

We propagate each possible move into the future and evaluate its consequences.

Many exploration algorithms exist for state-space problems. o Some are blind search methods, some are heuristic search methods.

Page 54: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 54 of 55

Example 8. State-space Tree for Tic-tac-toe

Page 55: 10. Fundamental AI Technologies

Video Game Development: Fundamental AI Technologies A. Babadi 55 of 55

References

Sanchez-Crespo’s textbook,

Wikipedia, and

Some other sources on the Internet.