15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466...

of 48 /48
15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms Maxim Likhachev Robotics Institute Carnegie Mellon University

Embed Size (px)

Transcript of 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466...

Page 1: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

15-466

Computer Game Programming

Intelligence I:

Basic Decision-Making Mechanisms

Maxim Likhachev

Robotics Institute

Carnegie Mellon University

Page 2: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 2

AI Architecture

from “Artificial Intelligence for Games” by I. Millington & J. Funge

Page 3: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 3

Decision-making Framework

from “Artificial Intelligence for Games” by I. Millington & J. Funge

e.g., health level, availability

of ammunition, …

e.g., map, see enemy, hear

sound, …

e.g., attack, flee,

explore the sound, …

Page 4: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 4

Decision-making Framework

from “Artificial Intelligence for Games” by I. Millington & J. Funge

e.g., health level, availability

of ammunition, …

e.g., map, see enemy, hear

sound, …

e.g., attack, flee,

explore the sound, …

Any ideas for how to

implement decision-making?

Page 5: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 5

Basic Decision-making Mechanisms for this Class

• Decision Trees

• Finite-state Machines

• Basic Behavior Trees

Page 6: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 6

Basic Decision-making Mechanisms for this Class

• Decision Trees

• Finite-state Machines

• Basic Behavior Trees

Page 7: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 7

Decision Trees

from “Artificial Intelligence for Games” by I. Millington & J. Funge

• Formalization of a set of nested if-then rules

• Very popular: easy-to-implement, intuitive (=easy-to-debug) and

fast

• Require careful manual design (theoretically, learning trees is

also possible)

Page 8: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 8

Decision Trees

from “Artificial Intelligence for Games” by I. Millington & J. Funge

• Formalization of a set of nested if-then rules

• Very popular: easy-to-implement, intuitive (=easy-to-debug) and

fast

• Require careful manual design (theoretically, learning trees is

also possible)

K-ary trees are

possible but binary

decision trees are

much more common

Page 9: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 9

Decision Trees

• Support for multi-valued input variables in binary decision-trees

Example:

Depending on the size of the enemy troops,

attack, stand ground or retreat

How to implement in a

binary decision trees?

Page 10: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 10

Decision Trees

• Support for multi-valued input variables in binary decision-trees

Example:

Depending on the size of the enemy troops,

attack, stand ground or retreat

Attack

Retreat

Stand Ground

Is N < 2?

N=size of the enemy troops

Yes No

Is N < 5?

Yes

No

Page 11: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 11

Decision Trees

• Support for continuous input variables in binary decision-trees

Example:

Depending on the distance to the enemy,

hand-to-hand combat, shoot, hide

How to implement in a

binary decision trees?

Page 12: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 12

Decision Trees

• Support for continuous input variables in binary decision-trees

Example:

Depending on the distance to the enemy,

hand-to-hand combat, shoot, hide

Hand-to-hand combat

Hide

Shoot

Is d < 5.0m?

d=distance to enemy

Yes No

Is d < 20.0m?

Yes

No

Page 13: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 13

Decision Trees

• Support for complex decision formulae

Example:

Attack whenever

enemy is close OR (low-on-health AND not too far)

Attack

Do nothing

Do nothing

Is enemy close?

Yes No

Is enemy low-on-health?

Yes No

Yes

Is enemy too far?

No

Attack

Page 14: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 14

Decision Trees

• Support for complex decision formulae

Example:

Attack whenever

enemy is close OR (low-on-health AND not too far)

Attack

Do nothing

Do nothing

Is enemy close?

Yes No

Is enemy low-on-health?

Yes No

Yes

Is enemy too far?

No

Attack

Can we represent any Boolean

formula with a binary decision tree?

Proof?

Page 15: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 15

Decision Trees

• Support for complex decision formulae

Example:

Attack whenever

enemy is close OR (low-on-health AND not too far)

Proof?

A OR (B AND C)

A B C Outcome

0 0 0 No

1 0 0 Yes

0 1 0 No

1 1 0 Yes

… … … …

Each row is a

branch in the tree

True?

Page 16: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 16

Decision Trees

• Making the decision tree traversal fast is important

vs.

Which decision tree is better for

decision-making?

from “Artificial Intelligence for Games” by I. Millington & J. Funge

Page 17: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 17

Decision Trees

• Making the decision tree traversal fast is important

vs.

Which decision tree is better for

decision-making?

What does it depend on?

from “Artificial Intelligence for Games” by I. Millington & J. Funge

Page 18: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 18

Decision Trees

• Making the decision tree traversal fast is important

vs.

Which decision tree is better for

decision-making?

What does it depend on?

• Frequency (probability) of outcome (e.g., what if A happens 99% of the time)

• The computational complexity of the test (e.g., what if testing for G is very

expensive)

from “Artificial Intelligence for Games” by I. Millington & J. Funge

Page 19: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 19

Decision Trees

• Making the decision tree traversal fast is important

Merging the branches:

from “Artificial Intelligence for Games” by I. Millington & J. Funge

Page 20: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 20

Decision Trees

• How to deal with the predictability of AI?

Any ideas?

Page 21: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 21

Decision Trees

• How to deal with the predictability of AI?

Random Decision Trees:

from “Artificial Intelligence for Games” by I. Millington & J. Funge

To avoid switching every

frame: use hysteresis

(memory)

Page 22: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 22

Basic Decision-making Mechanisms for this Class

• Decision Trees

• Finite-state Machines

• Basic Behavior Trees

Page 23: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 23

Finite State Machines

• Basic FSM

from “Artificial Intelligence for Games” by I. Millington & J. Funge

Initial State

Page 24: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 24

Finite State Machines

• Basic FSM

from “Artificial Intelligence for Games” by I. Millington & J. Funge

Advantages over

Decision Trees?

Disadvantages over

Decision Trees?

Page 25: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 25

Finite State Machines

• Basic FSM

from “Artificial Intelligence for Games” by I. Millington & J. Funge

How to introduce

unpredictability?

Page 26: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 26

Finite State Machines

• Hierarchical FSM

from “Artificial Intelligence for Games” by I. Millington & J. Funge

How it changes to react to

“re-charge now” alarm?

Page 27: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 27

Finite State Machines

• Hierarchical FSM

from “Artificial Intelligence for Games” by I. Millington & J. Funge

Page 28: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 28

Finite State Machines

• Hierarchical FSM

from “Artificial Intelligence for Games” by I. Millington & J. Funge

Upper bound on # of

states for N alarms?

What if an additional alarm?

Page 29: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 29

Finite State Machines

• Hierarchical FSM: strict hierarchy with only global alarms

from “Artificial Intelligence for Games” by I. Millington & J. Funge

Its own FSM

State: [State at Level i, … State at Level 1] (for i active FSMs)

All triggers get acted upon by FSM at level i

Whenever FSM at Level i exits, FSM at level i-1 becomes dominant

Page 30: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 30

Finite State Machines

• Hierarchical FSM: strict hierarchy with additional direct transitions

from “Artificial Intelligence for Games” by I. Millington & J. Funge

Direct transitions between levels allow to leave the source state

Page 31: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 31

Finite State Machines

• Hierarchical FSM: strict hierarchy with additional direct transitions

from “Artificial Intelligence for Games” by I. Millington & J. Funge

Direct transitions between levels allow to leave the source state Search state is left

When GetPower is done,

Clean Up is entered from scratch

Page 32: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 32

Finite State Machines

• Hierarchical FSM: strict hierarchy with additional direct transitions

from “Artificial Intelligence for Games” by I. Millington & J. Funge

More complex example:

Page 33: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 33

Finite State Machines

• Combining FSM and decision trees

from “Artificial Intelligence for Games” by I. Millington & J. Funge

Why bother?

Page 34: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 34

Basic Decision-making Mechanisms for this Class

• Decision Trees

• Finite-state Machines

• Basic Behavior Trees

Page 35: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 35

Basic Behavior Trees

• Became very popular after Halo 2 game [2004]

Page 36: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 36

Basic Behavior Trees

• Became very popular after Halo 2 game [2004]

• Especially, when coupled with graphical interfaces to edit them

Screenshot of GameBrains GUI

Page 37: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 37

Basic Behavior Trees

• Collection of simple tasks arranged in a tree structure

• One of the main advantages: Tasks and sub-trees can be reused!!!

Screenshot of GameBrains GUI

Page 38: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 38

Basic Behavior Trees

• Type of tasks: Conditions, Actions and Composites

• Each returning either success or failure

“Condition” task tests for a condition

Door open? Enemy close-by? Health level OK? …

Examples of behavior trees that consist of Conditions tasks only:

Page 39: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 39

Basic Behavior Trees

• Type of tasks: Conditions, Actions and Composites

• Each returning either success or failure

“Action” task alters the state of the game

Move to room Play audio sound Find a path …

Examples of behavior trees that consist of Actions tasks only:

Talk to the player

Page 40: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 40

Basic Behavior Trees

• Type of tasks: Conditions, Actions and Composites

• Each returning either success or failure

Condition and Action tasks are always at the leafs of the tree

“Composite” task sequences through them

Page 41: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 41

Basic Behavior Trees

• Type of tasks: Conditions, Actions and Composites

• Each returning either success or failure

Condition and Action tasks are always at the leafs of the tree

“Composite” task sequences through them

Two types of Composite tasks:

Selector returns as soon as

the first leaf task is successful

?

Attack Flee Declare defeat

Page 42: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 42

Basic Behavior Trees

• Type of tasks: Conditions, Actions and Composites

• Each returning either success or failure

Condition and Action tasks are always at the leafs of the tree

“Composite” task sequences through them

Two types of Composite tasks:

Selector returns as soon as

the first leaf task is successful

?

Attack Flee Declare defeat

Sequencer returns as soon as

the first leaf task fails

Door open? Move (into room)

Page 43: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 43

Basic Behavior Trees

• Type of tasks: Conditions, Actions and Composites

• Each returning either success or failure

Condition and Action tasks are always at the leafs of the tree

“Composite” task sequences through them

Two types of Composite tasks:

Selector returns as soon as

the first leaf task is successful

?

Attack Flee Declare defeat

Sequencer returns as soon as

the first leaf task fails

Door open? Move (into room)

Tasks are often

parameterized

Page 44: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 44

Basic Behavior Trees

• Type of tasks: Conditions, Actions and Composites

• Each returning either success or failure

Example:

Door open? Move

(into room)

Move

(to door)

Move

(into room) Open door

? What does it do?

Page 45: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 45

Basic Behavior Trees

• Type of tasks: Conditions, Actions and Composites

• Each returning either success or failure

Example:

Move

(to door)

Move

(into room)

? What does it do?

Door open? Move

(into room) ?

Door

unlocked?

Open

door

Door

locked?

Barge

door

Page 46: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 46

Basic Behavior Trees

• Type of tasks: Conditions, Actions and Composites

• Each returning either success or failure

Example:

Move

(to door)

Move

(into room)

? Can the tree always

be structured so that

sequence and selector

levels alternate?

Door open? Move

(into room) ?

Door

unlocked?

Open

door

Door

locked?

Barge

door

Page 47: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 47

Basic Behavior Trees

• Type of tasks: Conditions, Actions and Composites

• Each returning either success or failure

Example:

Move

(to door)

Move

(into room)

?

Door open? Move

(into room) ?

Door

unlocked?

Open

door

Door

locked?

Barge

door

How to reduce

predictability of the

behavior?

Page 48: 15-466 Computer Game Programming Intelligence I: …maxim/classes/CIS15466_Fall11/lectures/...15-466 Computer Game Programming Intelligence I: Basic Decision-Making Mechanisms ...

Maxim Likhachev Carnegie Mellon University 48

Basic Behavior Trees

• Behavior trees with Order Randomization for some Sequencers

and Selectors

Example:

How to reduce

predictability of the

behavior?

from “Artificial Intelligence for Games” by I. Millington & J. Funge