1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See...

34
1 Fuzzy Logic Fuzzy Logic Artificial Intelligence Artificial Intelligence for Games for Games Scott Goodwin Scott Goodwin School of Computer Science School of Computer Science See Buckland, Chapter 10

Transcript of 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See...

Page 1: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

11

Fuzzy LogicFuzzy Logic

Artificial Intelligence for GamesArtificial Intelligence for GamesScott GoodwinScott Goodwin

School of Computer ScienceSchool of Computer Science

See Buckland, Chapter 10

Page 2: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

22

Crisp WordsCrisp Words

1.1. Cut two slices of bread Cut two slices of bread a half inch thicka half inch thick

2.2. Turn the heat on the griddle to Turn the heat on the griddle to 400 degrees400 degrees

3.3. Grill the slices on one side for Grill the slices on one side for 2 minutes2 minutes

4.4. Turn the slices over and add a Turn the slices over and add a 1 ounze1 ounze of cheese of cheese

5.5. Replace and grill for Replace and grill for 30 seconds30 seconds

6.6. Remove, sprinkle on a Remove, sprinkle on a ¼ gram¼ gram of black pepper, of black pepper, and eat.and eat.

7.7. When the enemy has come within 80 feet, if you When the enemy has come within 80 feet, if you have at least have at least 10 rockets10 rockets, fire the rocket launcher., fire the rocket launcher.

Page 3: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

33

Fuzzy WordsFuzzy Words

1.1. Cut two slices of bread Cut two slices of bread medium thickmedium thick2.2. Turn the heat on the griddle to Turn the heat on the griddle to highhigh3.3. Grill the slices on one side until Grill the slices on one side until golden browngolden brown4.4. Turn the slices over and add a Turn the slices over and add a generous generous

helpinghelping of cheese of cheese5.5. Replace and grill until the top of the cheese is Replace and grill until the top of the cheese is

slightly brownslightly brown6.6. Remove, sprinkle on a Remove, sprinkle on a small amountsmall amount of black of black

pepper, and eat.pepper, and eat.7.7. When the enemy has come within When the enemy has come within medium medium

rangerange, if ammo is , if ammo is plentifulplentiful, fire the rocket , fire the rocket launcher.launcher.

Page 4: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

44

Crisp SetsCrisp Sets Crisp sets are the kind of sets you Crisp sets are the kind of sets you

learn about in math classlearn about in math class A set contains elementsA set contains elements Elements are either in a set or not Elements are either in a set or not

in the set (e.g., set of even integers)in the set (e.g., set of even integers) Degree of membership is 0 or 1Degree of membership is 0 or 1 Operations are defined on sets Operations are defined on sets

(intersection, union, complement(intersection, union, complement

Page 5: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

55

Fuzzy SetsFuzzy Sets

IQ 85 is kind of dumb but almost averageIQ 85 is kind of dumb but almost average How can we make crisp sets fuzzy?How can we make crisp sets fuzzy? How do we blur (fuzzify) the boundaries between sets?How do we blur (fuzzify) the boundaries between sets?

Dumb Average Clever Dumb Average Clever

Fuzzify

Page 6: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

66

FuzzificationFuzzification We can define We can define partialpartial

membership in a set membership in a set using a functionusing a function

The function could have The function could have one of many different one of many different shapesshapes

Gradual or abrupt Gradual or abrupt transitiontransition

Degree of membershipDegree of membership

Page 7: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

77

Degree of Membership (DOM)Degree of Membership (DOM)

Let’s suppose Brian has an IQ of 115Let’s suppose Brian has an IQ of 115 DOMDOMcleverclever(115) = 0.75(115) = 0.75

DOMDOMaverageaverage(115) = 0.25(115) = 0.25

DOMDOMdumbdumb(115) = 0(115) = 0

Page 8: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

88

Fuzzy AND OperatorFuzzy AND Operator

Fuzzy AND is the intersection of two (or more) fuzzy sets Fuzzy AND is the intersection of two (or more) fuzzy sets which is itself a fuzzy setwhich is itself a fuzzy set

DOMDOMaverage ^ cleveraverage ^ clever(x) = (x) = min{DOMmin{DOMaverageaverage(x), DOM(x), DOMcleverclever(x)}(x)}

DOMDOMaverage ^ cleveraverage ^ clever(115) = (115) = min{DOMmin{DOMaverageaverage(115), DOM(115), DOMcleverclever(115)} =(115)} =min{0.25, 0.75} = 0.25min{0.25, 0.75} = 0.25

Page 9: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

99

Fuzzy OR OperatorFuzzy OR Operator

Fuzzy OR is the union of two (or more) fuzzy sets which is Fuzzy OR is the union of two (or more) fuzzy sets which is itself a fuzzy setitself a fuzzy set

DOMDOMaverage V cleveraverage V clever(x) = (x) =

max{DOMmax{DOMaverageaverage(x), DOM(x), DOMcleverclever(x)}(x)} DOMDOMaverage V cleveraverage V clever(115) = (115) =

max {DOMmax {DOMaverageaverage(115), DOM(115), DOMcleverclever(115)} =(115)} =

max {0.25, 0.75} = 0.75max {0.25, 0.75} = 0.75

Page 10: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

1010

Fuzzy NOT OperatorFuzzy NOT Operator

Fuzzy NOT is complement of a fuzzy set which Fuzzy NOT is complement of a fuzzy set which is itself a fuzzy setis itself a fuzzy set

DOMDOM ¬¬cleverclever(x) = 1 - DOM(x) = 1 - DOMcleverclever(x)(x) DOMDOM ¬¬cleverclever(115) =(115) =

1 - DOM1 - DOMcleverclever(115)} =(115)} =

1 - 0.75 = 0.251 - 0.75 = 0.25

Page 11: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

1111

Fuzzy HedgesFuzzy Hedges

Unary operators that modify the membership function of Unary operators that modify the membership function of a fuzzy seta fuzzy set

DOMDOMvery(clever)very(clever)(x) = DOM(x) = DOMcleverclever(x)(x)22

DOMDOMvery(clever)very(clever)(115) = DOM(115) = DOMcleverclever(115)(115)2 2 = 0.75= 0.7522 = 0.5625 = 0.5625

DOMDOMfairly(clever)fairly(clever)(x) = DOM(x) = DOMcleverclever(x)(x)0.50.5

DOMDOMfairly(clever)fairly(clever)(115) = DOM(115) = DOMcleverclever(115)(115)0.5 0.5 = 0.75= 0.750.5 0.5 = 0.866= 0.866

Page 12: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

1212

Fuzzy Identity LawsFuzzy Identity Laws DeMorgan’s LawDeMorgan’s Law

AssociativityAssociativity

CommutativityCommutativity

DistributivityDistributivity

Page 13: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

1313

Fuzzy Linguistic VariablesFuzzy Linguistic Variables

A fuzzy linguistic variable (FLV) is a A fuzzy linguistic variable (FLV) is a composition of one or more fuzzy sets to composition of one or more fuzzy sets to represent a domain or concept qualitativelyrepresent a domain or concept qualitatively

Dumb, Average, Clever are members of the Dumb, Average, Clever are members of the fuzzy linguistic variable IQfuzzy linguistic variable IQ

IQ = {Dumb, Average, Clever}IQ = {Dumb, Average, Clever} Speed = {Slow, Medium, Fast}Speed = {Slow, Medium, Fast} Allegiance = {Friend, Neutral, Foe}Allegiance = {Friend, Neutral, Foe} Target Heading = {Far left, Left, Center, Target Heading = {Far left, Left, Center,

Right, Far right}Right, Far right}

Page 14: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

1414

FLV Target HeadingFLV Target Heading

The membership functions of the member sets The membership functions of the member sets can have different shapescan have different shapes

The collection of shapes (membership functions) The collection of shapes (membership functions) that comprise the FLV are called a fuzzy that comprise the FLV are called a fuzzy manifold or fuzzy surfacemanifold or fuzzy surface

Page 15: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

1515

Fuzzy RulesFuzzy Rules

IF IF antecedentantecedent THEN THEN consequentconsequent The DOM of the antecedent defines the The DOM of the antecedent defines the

degree to which the consequent firesdegree to which the consequent fires The consequents that fire are combined The consequents that fire are combined

and and defuzzifieddefuzzified to give a crisp result to give a crisp result

Page 16: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

1616

Designing FLVsDesigning FLVsfor Weapon Selectionfor Weapon Selection

Suppose the desirability of selecting a weapon Suppose the desirability of selecting a weapon from inventory depends on the distance to target from inventory depends on the distance to target and the amount of ammoand the amount of ammo

Each weapon class owns an instance of a Fuzzy Each weapon class owns an instance of a Fuzzy Module which contains Module which contains FLVs represent DistanceToTarget and AmmoStatus FLVs represent DistanceToTarget and AmmoStatus

(antecedents) (antecedents) FLV representing Desirability (consequent)FLV representing Desirability (consequent) Fuzzy rules relating antecedents to the consequentFuzzy rules relating antecedents to the consequent

FLVs DistanceToTarget and Desirability are the FLVs DistanceToTarget and Desirability are the same for each weapon typesame for each weapon type

FLV AmmoStatus and the Fuzzy rules depend FLV AmmoStatus and the Fuzzy rules depend on the weapon typeon the weapon type

Page 17: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

1717

Designing FLVsDesigning FLVs Guidelines:Guidelines:

The sum of the DOMs of the fuzzy sets at each point The sum of the DOMs of the fuzzy sets at each point in an FLV should sum to 1. (This ensures smooth in an FLV should sum to 1. (This ensures smooth transition between values over the FLV’s fuzzy transition between values over the FLV’s fuzzy manifold.)manifold.)

A vertical line drawn at any point in the FLV should A vertical line drawn at any point in the FLV should intersect no more than two fuzzy setsintersect no more than two fuzzy sets

The examples below violate one of these rulesThe examples below violate one of these rules

Page 18: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

1818

Designing the Desirability FLVDesigning the Desirability FLV

Desirability scores range from 0 to 100Desirability scores range from 0 to 100 FLV contains three fuzzy setsFLV contains three fuzzy sets

Undesirable: Left-shouldered fuzzy setUndesirable: Left-shouldered fuzzy set Desirable: Triangular fuzzy setDesirable: Triangular fuzzy set VeryDesirable: Right-shouldered fuzzy setVeryDesirable: Right-shouldered fuzzy set

Page 19: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

1919

Designing the Designing the DistanceToTarget FLVDistanceToTarget FLV

The distance scale is in pixels and assumes The distance scale is in pixels and assumes a RavenBot is about 10 pixels widea RavenBot is about 10 pixels wide

Page 20: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

2020

Designing the AmmoStatus FLVDesigning the AmmoStatus FLV

Depends on weapon type, say rocket Depends on weapon type, say rocket launcher which fires 2 rockets per secondlauncher which fires 2 rockets per second

So 10 rockets is Okay, 30 is Loads, etcSo 10 rockets is Okay, 30 is Loads, etc

Page 21: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

2121

Designing the Fuzzy RulesDesigning the Fuzzy Rulesfor Weapon Selectionfor Weapon Selection

Need a rule for each combination of the antecedents Need a rule for each combination of the antecedents DistanceToTarget (3) x AmmoStatus (3) = 9 combinationsDistanceToTarget (3) x AmmoStatus (3) = 9 combinations

For the rocket launcher, it is best at medium distance. At For the rocket launcher, it is best at medium distance. At close range, you may get caught in the blast radius. At close range, you may get caught in the blast radius. At long range, the target can easily dodge slow rockets.long range, the target can easily dodge slow rockets.

Page 22: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

2222

Fuzzy InferenceFuzzy Inference

We pass the system the input values for We pass the system the input values for the antecedents and apply the above steps.the antecedents and apply the above steps.

Let’s run through an example with the Let’s run through an example with the target at distance=200 and ammo=8target at distance=200 and ammo=8

Page 23: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

2323

Rule1: IF Target_Far AND Rule1: IF Target_Far AND Ammo_Loads THEN DesirableAmmo_Loads THEN Desirable

DOMDOMfarfar(200)=0.33 DOM(200)=0.33 DOMloadsloads(8)=0(8)=0

AND: min is 0AND: min is 0 Conclusion: Desirable = 0Conclusion: Desirable = 0

Page 24: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

2424

Rule 2: IF Target_Far AND Rule 2: IF Target_Far AND Ammo_Okay THEN UndesirableAmmo_Okay THEN Undesirable

DOMDOMfarfar(200)=0.33 DOM(200)=0.33 DOMokayokay(8)=0.78(8)=0.78

AND: min is 0.33AND: min is 0.33 Conclusion: Undesirable = 0.33Conclusion: Undesirable = 0.33

Page 25: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

2525

Rule 3: IF Target_Far AND Rule 3: IF Target_Far AND Ammo_Low THEN UndesirableAmmo_Low THEN Undesirable

DOMDOMfarfar(200)=0.33 DOM(200)=0.33 DOMlowlow(8)=0.2(8)=0.2 AND: min is 0.2AND: min is 0.2 Conclusion: Undesirable = 0.2Conclusion: Undesirable = 0.2

Page 26: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

2626

Fuzzy Associative MatrixFuzzy Associative Matrix

VeryDesirable fired once with confidence 0.67VeryDesirable fired once with confidence 0.67 Desirable fired once with confidence 0.2Desirable fired once with confidence 0.2 Undesirable fired twice with confidences 0.2 and 0.33Undesirable fired twice with confidences 0.2 and 0.33 How can we handle multiple confidences?How can we handle multiple confidences? Sum (with max=1) or OR the results (take the max)Sum (with max=1) or OR the results (take the max)

The results for the 9 rules The results for the 9 rules given distance=200 and given distance=200 and ammo=8 are summarized in ammo=8 are summarized in the fuzzy associative matrixthe fuzzy associative matrix

The shaded cells highlight The shaded cells highlight rules that fired.rules that fired.

Page 27: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

2727

Clip the consequentClip the consequent

The confidence of The confidence of each consequent each consequent is then used to clip is then used to clip the membership the membership functionsfunctions

Page 28: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

2828

Combine the conclusionsCombine the conclusions The result of The result of

combining the combining the conclusions is conclusions is a composite a composite fuzzy setfuzzy set

Next, we need Next, we need to determine a to determine a final crisp value final crisp value

Page 29: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

2929

DefuzzificationDefuzzification

Defuzzification determines a crisp value Defuzzification determines a crisp value from the inferred composite fuzzy setfrom the inferred composite fuzzy set

There are many defuzzification techniquesThere are many defuzzification techniques Three of the most common areThree of the most common are

Mean of Maximum (MOM)Mean of Maximum (MOM) CentroidCentroid Average of Maxima (MaxAv)Average of Maxima (MaxAv)

Page 30: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

3030

Mean of MaximumMean of Maximum

The maximum confidence value was 0.67The maximum confidence value was 0.67 It occurs between 66 and 100. The mean is 83.It occurs between 66 and 100. The mean is 83. This is the crisp value of desirabilityThis is the crisp value of desirability Doesn’t take into account the grayed out setsDoesn’t take into account the grayed out sets

Page 31: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

3131

CentroidCentroid

Most accurate and most difficult to calculateMost accurate and most difficult to calculate Essentially, find the balance point of the fuzzy Essentially, find the balance point of the fuzzy

manifoldmanifold Take s samples, sum the contribution of the Take s samples, sum the contribution of the

DOM at each point and divide by the sum of the DOM at each point and divide by the sum of the DOMsDOMs

Page 32: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

3232

Centroid CalculationCentroid Calculation

Page 33: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

3333

Average of MaximaAverage of Maxima

Maximum or representative Maximum or representative value is when DOM in the value is when DOM in the fuzzy set is 1fuzzy set is 1

For triangular fuzzy set, at For triangular fuzzy set, at the peakthe peak

For trapizoidal or For trapizoidal or shouldered, average of the shouldered, average of the endpoints of the plateauendpoints of the plateau

Result is close to the more Result is close to the more accurate but costlier accurate but costlier CentroidCentroid

Page 34: 1 Fuzzy Logic Artificial Intelligence for Games Scott Goodwin School of Computer Science See Buckland, Chapter 10.

3434

Raven Fuzzy SystemRaven Fuzzy System