Top Banner
Artificial Intelligence in Game Design Lecture 6: Fuzzy Logic and Fuzzy State Machines
30

Artificial Intelligence in Game Design Lecture 6: Fuzzy Logic and Fuzzy State Machines.

Jan 16, 2016

Download

Documents

Franklin Norris
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Artificial Intelligence in Game Design Lecture 6: Fuzzy Logic and Fuzzy State Machines.

Artificial Intelligence in Game Design

Lecture 6: Fuzzy Logic and

Fuzzy State Machines

Page 3: Artificial Intelligence in Game Design Lecture 6: Fuzzy Logic and Fuzzy State Machines.

Problems with Discrete States

• Abrupt transitions between states

– Exact behavior from 100 down to 10 HP– Sudden behavior change at 9 HP

• More realistic if behavior gradually changes as HP approaches 10

AngrySnarling

Moving forward

FrightenedWincing

Backing away

HP < 10

Page 4: Artificial Intelligence in Game Design Lecture 6: Fuzzy Logic and Fuzzy State Machines.

Problems with Discrete States

• Serious problem if action is continuous value– Example: Racing games

Actions:• Speed of car• Amount to steer• …

– Basic idea:• Near curve slow speed• Far from curve fast speed

Page 5: Artificial Intelligence in Game Design Lecture 6: Fuzzy Logic and Fuzzy State Machines.

Problems with Discrete States

• Bad solution (based on discrete states)– Distance to curve > 10 120 mph– Distance to curve <= 10 60 mph

Sudden transition!

Page 6: Artificial Intelligence in Game Design Lecture 6: Fuzzy Logic and Fuzzy State Machines.

Problems with Discrete States

• Best solution:– As get closer to curve, gradually slow down– As get further from curve, gradually speed up

Page 7: Artificial Intelligence in Game Design Lecture 6: Fuzzy Logic and Fuzzy State Machines.

Fuzzy Logic

• Membership function µ S (x)– “Degree” to which entity X belongs in state S

– Discrete states: µ S (x) = 1 X is in state S

or 0 X is not in state S

• Fuzzy states:

µ S (x) may be any number between 0 and 1

– X may be “somewhat” in state S

Page 8: Artificial Intelligence in Game Design Lecture 6: Fuzzy Logic and Fuzzy State Machines.

Fuzzy Logic and Language

• Often used to quantify “natural language” hedges• Example:

– “Not at all” µ S (x) = 0

– “A little” µ S (x) = 0.2

– “Kind of” µ S (x) = 0.4

– “Pretty” µ S (x) = 0.6

– “Very” µ S (x) = 0.8

– “Absolutely” µ S (x) = 1.0

Page 9: Artificial Intelligence in Game Design Lecture 6: Fuzzy Logic and Fuzzy State Machines.

Fuzzy Logic Example

• Example: Oswald the Orc– Very angry

µ angry (Oswald) = 0.8

– Kind of frightened

µ frightened (Oswald) = 0.4

– Not at all confident

µ confident (Oswald) = 0.0

Note that fuzzy memberships don’t have to add up to 1 (unlike probabilities)

Page 10: Artificial Intelligence in Game Design Lecture 6: Fuzzy Logic and Fuzzy State Machines.

Fuzzification

• Converting a continuous valued attribute to a fuzzy state membership

• Racing example:– Attribute = car’s distance from curve

– Fuzzy states:• Near curve• Far from curve

– Goal: determine membership of car in these sets– First step to determine desired speed of car

Start of curve

Page 11: Artificial Intelligence in Game Design Lecture 6: Fuzzy Logic and Fuzzy State Machines.

Fuzzification• Often represent as simple graphs

– X axis: continuous attribute– Y axis: membership in set(s)– Example: current distance = 60

distance to curve0 20 40 60 80 100 120

Near Far

0.4

0.6

µ near (car) = 0.6 µ far (car) = 0.4

Page 12: Artificial Intelligence in Game Design Lecture 6: Fuzzy Logic and Fuzzy State Machines.

Fuzzy Rules

• Fuzzy antecedent Fuzzy conclusion

• Fuzzy antecedent created from fuzzification• Requires fuzzy definitions of logical connectives

AND, OR, and NOT.

Given fuzzy membership in this

What is fuzzy membership in this?

Page 13: Artificial Intelligence in Game Design Lecture 6: Fuzzy Logic and Fuzzy State Machines.

Fuzzy Connectives

Usual measures:

• AND: µ S and T (x) = min (µ S (x), µ T (x))

• OR: µ S or T (x) = max (µ S (x), µ T (x))Note: this also applies if combining separate rules

• NOT: µ not T (x) = 1 - µ T (x)

Page 14: Artificial Intelligence in Game Design Lecture 6: Fuzzy Logic and Fuzzy State Machines.

Fuzzy Rule Example

Rules:• IF near curve exit AND going slow THEN accelerate• IF far from curve exit AND going medium THEN

accelerate

Question:• If near, far, slow, medium all fuzzy measures

What is resulting membership in accelerate?

Speed: 100 mph

Distance from exit: 80

Page 15: Artificial Intelligence in Game Design Lecture 6: Fuzzy Logic and Fuzzy State Machines.

Fuzzy Rule Example

distance to curve0 20 40 60 80 100 120

Near Far

0.4

0.6

µ near (car) = 0.4 µ far (car) = 0.6

speed0 30 60 90 120 150 180

Slow Fast

0.3

1.0

µ slow (car) = 0.3 µ medium (car) = 1.0

Medium

Page 16: Artificial Intelligence in Game Design Lecture 6: Fuzzy Logic and Fuzzy State Machines.

Fuzzy Rule Example

• Rule 1:IF near curve exit AND going slow THEN accelerate

µ accelerate (car) = µ near and slow (car) = min (µ near (car), µ slow (car))

= min(0.4, 0.3) = 0.3• Rule 2:

IF far curve exit AND going medium THEN accelerate

µ accelerate (car) = µ far and medium (car) = min (µ far (car), µ medium (car))

= min(0.6, 1.0) = 0.6

Page 17: Artificial Intelligence in Game Design Lecture 6: Fuzzy Logic and Fuzzy State Machines.

Fuzzy Rule Example

• Combining the rules:

µ accelerate (car) = µ accelerate1 or accelerate2 (car) = max (µ accelerate1 (car), µ accelerate2 (car))

= max(0.3, 0.6) = 0.6

Membership of the car in accelerate = 0.6

Page 18: Artificial Intelligence in Game Design Lecture 6: Fuzzy Logic and Fuzzy State Machines.

Defuzzification

• Converting fuzzy state membership to appropriate actions – Higher membership in state greater degree of

action implied by state

• Possible problems– Conflicts if multiple actions indicated– Some actions not fuzzy (attack vs. no attack)– Many open problems!

Page 19: Artificial Intelligence in Game Design Lecture 6: Fuzzy Logic and Fuzzy State Machines.

Simple Defuzzification

• Create fuzzy set for action• Map membership in set back to value

– Assumes no conflicts with other actions based on same value– Assumes monotonic function for fuzzy set

• Example:

µ accelerate (car) = 0.6 acceleration = 16 feet/second2

feet/second2

0 5 10 15 20 25 30

Acceleration 0.6

Page 20: Artificial Intelligence in Game Design Lecture 6: Fuzzy Logic and Fuzzy State Machines.

Conflicts in Defuzzification

• Example rule:IF near curve entrance AND going fast THEN brake

– Distance: 90 µ near (car) = 0.2

– Speed: 80 µ fast (car) = 0.15

– µ brake (car) = min (0.2, 0.15) = 0.15

Page 21: Artificial Intelligence in Game Design Lecture 6: Fuzzy Logic and Fuzzy State Machines.

Conflicts in Defuzzification

• Conflicting fuzzy actions:

– µ accelerate (car) = 0.6

– µ brake (car) = 0.15

• Intuitive idea:– Should accelerate since its membership is larger– Membership in brake should decrease acceleration to some

degree

Page 22: Artificial Intelligence in Game Design Lecture 6: Fuzzy Logic and Fuzzy State Machines.

Conflicts in Defuzzification

Methods:

• Just use largest rule (µ accelerate (car) = 0.6)– Fast to compute– Leads to uneven behavior

• Weighted center of mass of sets– Commonly used in AI– Slow and costly to compute

-15 -10 -5 -0 5 10 15

Acceleration

0.6

feet/second2

Braking

0.15

Page 23: Artificial Intelligence in Game Design Lecture 6: Fuzzy Logic and Fuzzy State Machines.

Conflicts in Defuzzification

1. Choose one value for each set– Usually its maximum value

2. Weight by memberships in each set

3. Determine average value

Example:– Maximum acceleration = 30 feet/second2 – Maximum braking = -40 feet/second2

• Weighted by membership:– Acceleration = 0.6 * 30 feet/second2 = 18 feet/second2

– Braking = 0.15 * -40 feet/second2 = -6 feet/second2

• Resulting acceleration: 12 feet/second2

Page 24: Artificial Intelligence in Game Design Lecture 6: Fuzzy Logic and Fuzzy State Machines.

Defuzzifying Discrete Actions

• Example: Attack/No attack (must do one or the other)• Thresholding:

– If fuzzy membership > threshold take action

– Example: If µ angry (orc) > 0.7 attack

If µ angry (orc) <= 0.7 no attack

• Conflicts:Take action with highest membership– Angry attack, µ angry (Oswald) = 0.7

– Frightened run, µ frightened (Oswald) = 0.3

• Note: Fuzzy logic works best for continuous valued actions

Oswald attacks!

Page 26: Artificial Intelligence in Game Design Lecture 6: Fuzzy Logic and Fuzzy State Machines.

Fuzzy State Machines

• Transitions of formtreated like fuzzy and – Must be in state S1– Stimulus T must be present

• µ S2 (orc) = min( µ S1 (orc), µ T (orc) )

• Note that unless µ S2 (orc) = 1, it is still in S1 to some degree: µ S1 (orc) = 1 - µ S2 (orc)

• Note that state S2 may be indicated as a next state by many other states (including itself)– Will use max rule to determine final membership in S2

S1 S2T

Page 27: Artificial Intelligence in Game Design Lecture 6: Fuzzy Logic and Fuzzy State Machines.

Fuzzy State Machine Example

• Current state memberships

– µ conf (Oswald) = 0.8

– µ fear (Oswald) = 0.4

ConfidentAttack 100%

HP lowFearful

Attack 40%HP high

Page 28: Artificial Intelligence in Game Design Lecture 6: Fuzzy Logic and Fuzzy State Machines.

Fuzzy State Machine Example

• Current HP = 13 (just took heavy hit by player)

– µ low (Oswald) = 0.7

– µ high (Oswald) = 0.2

HP

Low High

0 5 10 15 20 25 30

Page 29: Artificial Intelligence in Game Design Lecture 6: Fuzzy Logic and Fuzzy State Machines.

Fuzzy State Machine Example

• Confident Fearful transition

– µ fear (Oswald) = min (µ conf (Oswald), µ low (Oswald))

= min (0.8, 0.7) = 0.7

– µ conf (Oswald) = 1 - µ fear (Oswald) = 0.3

• Fearful Confident transition

– µ conf (Oswald) = min(µ fear (Oswald), µ high (Oswald))

= min (0.4, 0.2) = 0.2

– µ fear (Oswald) = 1 - µ conf (Oswald) = 0.8

Page 30: Artificial Intelligence in Game Design Lecture 6: Fuzzy Logic and Fuzzy State Machines.

Fuzzy State Machine Example

• Combining effects of transitions

– µ conf (Oswald) = max (0.3, 0.2) = 0.3

– µ fear (Oswald) = max (0.7, 0.8) = 0.8

• Determining current action (defuzzification)– Attack % = ( µ conf (Oswald) * 100% +

µ fear (Oswald) * 20% ) / 2

= (0.4 * 100% + 0.7 * 20% ) / 2 = 46%