artificial intelligence in game design lecture 6: fuzzy logic and fuzzy state machines
TRANSCRIPT
Artificial Intelligence in Game Design
Lecture 6: Fuzzy Logic and
Fuzzy State Machines
Problems with Discrete States• Characters may be in multiple states
simultaneously– Particularly emotional states
• Hit angry• Low HP frightened
• May indicate contradictory responses– Frightened run– Angry attack
• Need “intermediate” states and actions– “Somewhat angry”– “Somewhat frightened”
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
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
Problems with Discrete States
• Bad solution (based on discrete states)– Distance to curve > 10 120 mph– Distance to curve <= 10 60 mph
Sudden transition!
Problems with Discrete States
• Best solution:– As get closer to curve, gradually slow down– As get further from curve, gradually speed up
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
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
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)
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
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
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?
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)
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
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
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
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
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!
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
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
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
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
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
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!
Fuzzy State Machines
• Fuzzy membership in states– May be in multiple states– Transitions between states may be fuzzy– Resulting actions may be fuzzy
Example:
– May be confident and fearful to some degree– Transitions based on fuzzy measures “high” and “low” HP– Action (% chance of attack) is continuous valued
ConfidentAttack 100%
HP lowFearful
Attack 20%HP high
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
Fuzzy State Machine Example
• Current state memberships
– µ conf (Oswald) = 0.8
– µ fear (Oswald) = 0.4
ConfidentAttack 100%
HP lowFearful
Attack 40%HP high
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
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
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%