Top Banner
Fuzzy Logic Driven Agent Behavior Prof. Dr. Taoufik Nouri Nouri@ Nouri.ch 29.03.2014 The primary text for the course is Ref.[1]
29

Fuzzy Logic Driven Agent Behavior Prof. Dr. Taoufik Nouri Nouri@[email protected] 29.03.2014 The primary text for the course is Ref.[1]

Dec 16, 2015

Download

Documents

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: Fuzzy Logic Driven Agent Behavior Prof. Dr. Taoufik Nouri Nouri@Nouri@Nouri.ch 29.03.2014 The primary text for the course is Ref.[1]

Fuzzy Logic Driven Agent Behavior

Prof. Dr. Taoufik [email protected]

29.03.2014

The primary text for the course is

Ref.[1]

Page 2: Fuzzy Logic Driven Agent Behavior Prof. Dr. Taoufik Nouri Nouri@Nouri@Nouri.ch 29.03.2014 The primary text for the course is Ref.[1]

Fuzzy Logic

Goal: Master How the raven is using fuzzy logic.

Prof. Dr. Lotfi Zadeh father of Fuzzy Logic

Page 3: Fuzzy Logic Driven Agent Behavior Prof. Dr. Taoufik Nouri Nouri@Nouri@Nouri.ch 29.03.2014 The primary text for the course is Ref.[1]

Fuzzy Sets

Fuzzy Variable IQ

Ryan with IQ=115 Є Clever with = 0.75 Є Average with = 0.25

is membership coefficient

Fuzzy Variable: Distance to Target

Page 4: Fuzzy Logic Driven Agent Behavior Prof. Dr. Taoufik Nouri Nouri@Nouri@Nouri.ch 29.03.2014 The primary text for the course is Ref.[1]

Fuzzy Set Operators

AND = *

OR = +

NOT

Average AND Clever

Average OR Clever

NOT Clever

Page 5: Fuzzy Logic Driven Agent Behavior Prof. Dr. Taoufik Nouri Nouri@Nouri@Nouri.ch 29.03.2014 The primary text for the course is Ref.[1]

Definition A fuzzy set is defined by a membership function. These functions can be any arbitrary shape but are typically triangular or trapezoidal.

Notice how they define a gradual transition from regions completely outside the set to regions completely within the set, thereby enabling a value to have partial membership to a set.

Page 6: Fuzzy Logic Driven Agent Behavior Prof. Dr. Taoufik Nouri Nouri@Nouri@Nouri.ch 29.03.2014 The primary text for the course is Ref.[1]

Hedges

Page 7: Fuzzy Logic Driven Agent Behavior Prof. Dr. Taoufik Nouri Nouri@Nouri@Nouri.ch 29.03.2014 The primary text for the course is Ref.[1]

Fuzzy Rules

Fuzzy rules are comprised of an antecedent and a consequent in the form:

IF antecedent THEN consequent = new FuzzyRule(antecedent, consequence)

The antecedent represents a condition and the consequent describes the consequence if that condition is satisfied.

The difference with fuzzy rules is that unlike conventional rules where the consequent either fires or not, in fuzzy systems the consequent can fire to a matter of degree.

Here are some examples of fuzzy rules:

IF Target_isFarRight THEN Turn_QuicklyToRight IF VERY(Enemy_BadlyInjured) THEN Behavior_Aggressive IF Target_isFarAway AND Allegiance_isEnemy THEN Shields_OnLowPower IF Ball_isCloseToHole AND Green_isLevel THEN HitBall_Gently AND HitBall_DirectlyAtHole IF (Bend_HairpinLeft OR Bend_HairpinRight) AND Track_SlightlyWet THEN Speed_VerySlow

Page 8: Fuzzy Logic Driven Agent Behavior Prof. Dr. Taoufik Nouri Nouri@Nouri@Nouri.ch 29.03.2014 The primary text for the course is Ref.[1]

How Raven Uses the Fuzzy Logic

Each Raven weapon owns an instance of a fuzzy module, with rules specific to the weapon. All weapons have the method GetDesirability, which updates the fuzzy module and returns a crisp desirability score.

The desirability of selecting a particular weapon is dependent on two factors: the distance to the target and

the amount of ammo.

Page 9: Fuzzy Logic Driven Agent Behavior Prof. Dr. Taoufik Nouri Nouri@Nouri@Nouri.ch 29.03.2014 The primary text for the course is Ref.[1]

Fuzzy Variable by Raven

Page 10: Fuzzy Logic Driven Agent Behavior Prof. Dr. Taoufik Nouri Nouri@Nouri@Nouri.ch 29.03.2014 The primary text for the course is Ref.[1]

Fuzzy ModuleFuzzyVariable& DistToTarget = fm.CreateFLV("DistToTarget");

FzSet Target_Close = DistToTarget.AddLeftShoulderSet("Target_Close", 0, 25, 150);

FzSet Target_Medium = DistToTarget.AddTriangularSet("Target_Medium", 25, 150, 300);

FzSet Target_Far = DistToTarget.AddRightShoulderSet("Target_Far", 150, 300, 500);

Page 11: Fuzzy Logic Driven Agent Behavior Prof. Dr. Taoufik Nouri Nouri@Nouri@Nouri.ch 29.03.2014 The primary text for the course is Ref.[1]

How Raven Uses the Fuzzy Logic

Weapons & Projectiles:Weapon BlasterWeapon RailgunWeapon Rocket LuncherWeapon Shotgun

Fuzzy logic decides which weapon can be used for the next shot based on the distance to the ennemy, the AmmoStatus and the Desirability.

The weapon have specific rules

Page 12: Fuzzy Logic Driven Agent Behavior Prof. Dr. Taoufik Nouri Nouri@Nouri@Nouri.ch 29.03.2014 The primary text for the course is Ref.[1]

Weapon Blastervoid Blaster::InitializeFuzzyModule(){ FuzzyVariable& DistToTarget = m_FuzzyModule.CreateFLV("DistToTarget");

FzSet& Target_Close = DistToTarget.AddLeftShoulderSet("Target_Close",0,25,150); FzSet& Target_Medium = DistToTarget.AddTriangularSet("Target_Medium",25,150,300); FzSet& Target_Far = DistToTarget.AddRightShoulderSet("Target_Far",150,300,1000);

FuzzyVariable& Desirability = m_FuzzyModule.CreateFLV("Desirability"); FzSet& VeryDesirable = Desirability.AddRightShoulderSet("VeryDesirable", 50, 75, 100); FzSet& Desirable = Desirability.AddTriangularSet("Desirable", 25, 50, 75); FzSet& Undesirable = Desirability.AddLeftShoulderSet("Undesirable", 0, 25, 50);

m_FuzzyModule.AddRule(Target_Close, Desirable); m_FuzzyModule.AddRule(Target_Medium, FzVery(Undesirable)); m_FuzzyModule.AddRule(Target_Far, FzVery(Undesirable));}

Page 13: Fuzzy Logic Driven Agent Behavior Prof. Dr. Taoufik Nouri Nouri@Nouri@Nouri.ch 29.03.2014 The primary text for the course is Ref.[1]

Weapon Railgunvoid RailGun::InitializeFuzzyModule(){ FuzzyVariable& DistanceToTarget = m_FuzzyModule.CreateFLV("DistanceToTarget"); FzSet& Target_Close = DistanceToTarget.AddLeftShoulderSet("Target_Close", 0, 25, 150); FzSet& Target_Medium = DistanceToTarget.AddTriangularSet("Target_Medium", 25, 150, 300); FzSet& Target_Far = DistanceToTarget.AddRightShoulderSet("Target_Far", 150, 300, 1000);

FuzzyVariable& Desirability = m_FuzzyModule.CreateFLV("Desirability"); FzSet& VeryDesirable = Desirability.AddRightShoulderSet("VeryDesirable", 50, 75, 100); FzSet& Desirable = Desirability.AddTriangularSet("Desirable", 25, 50, 75); FzSet& Undesirable = Desirability.AddLeftShoulderSet("Undesirable", 0, 25, 50);

FuzzyVariable& AmmoStatus = m_FuzzyModule.CreateFLV("AmmoStatus"); FzSet& Ammo_Loads = AmmoStatus.AddRightShoulderSet("Ammo_Loads", 15, 30, 100); FzSet& Ammo_Okay = AmmoStatus.AddTriangularSet("Ammo_Okay", 0, 15, 30); FzSet& Ammo_Low = AmmoStatus.AddTriangularSet("Ammo_Low", 0, 0, 15);

m_FuzzyModule.AddRule(FzAND(Target_Close, Ammo_Loads), FzFairly(Desirable)); m_FuzzyModule.AddRule(FzAND(Target_Close, Ammo_Okay), FzFairly(Desirable)); m_FuzzyModule.AddRule(FzAND(Target_Close, Ammo_Low), Undesirable);

m_FuzzyModule.AddRule(FzAND(Target_Medium, Ammo_Loads), VeryDesirable); m_FuzzyModule.AddRule(FzAND(Target_Medium, Ammo_Okay), Desirable); m_FuzzyModule.AddRule(FzAND(Target_Medium, Ammo_Low), Desirable);

m_FuzzyModule.AddRule(FzAND(Target_Far, Ammo_Loads), FzVery(VeryDesirable)); m_FuzzyModule.AddRule(FzAND(Target_Far, Ammo_Okay), FzVery(VeryDesirable)); m_FuzzyModule.AddRule(FzAND(Target_Far, FzFairly(Ammo_Low)), VeryDesirable);}

Page 14: Fuzzy Logic Driven Agent Behavior Prof. Dr. Taoufik Nouri Nouri@Nouri@Nouri.ch 29.03.2014 The primary text for the course is Ref.[1]

RocketLaunchervoid RocketLauncher::InitializeFuzzyModule(){ FuzzyVariable& DistToTarget = m_FuzzyModule.CreateFLV("DistToTarget");

FzSet& Target_Close = DistToTarget.AddLeftShoulderSet("Target_Close",0,25,150); FzSet& Target_Medium = DistToTarget.AddTriangularSet("Target_Medium",25,150,300); FzSet& Target_Far = DistToTarget.AddRightShoulderSet("Target_Far",150,300,1000);

FuzzyVariable& Desirability = m_FuzzyModule.CreateFLV("Desirability"); FzSet& VeryDesirable = Desirability.AddRightShoulderSet("VeryDesirable", 50, 75, 100); FzSet& Desirable = Desirability.AddTriangularSet("Desirable", 25, 50, 75); FzSet& Undesirable = Desirability.AddLeftShoulderSet("Undesirable", 0, 25, 50);

FuzzyVariable& AmmoStatus = m_FuzzyModule.CreateFLV("AmmoStatus"); FzSet& Ammo_Loads = AmmoStatus.AddRightShoulderSet("Ammo_Loads", 10, 30, 100); FzSet& Ammo_Okay = AmmoStatus.AddTriangularSet("Ammo_Okay", 0, 10, 30); FzSet& Ammo_Low = AmmoStatus.AddTriangularSet("Ammo_Low", 0, 0, 10);

m_FuzzyModule.AddRule(FzAND(Target_Close, Ammo_Loads), Undesirable); m_FuzzyModule.AddRule(FzAND(Target_Close, Ammo_Okay), Undesirable); m_FuzzyModule.AddRule(FzAND(Target_Close, Ammo_Low), Undesirable);

m_FuzzyModule.AddRule(FzAND(Target_Medium, Ammo_Loads), VeryDesirable); m_FuzzyModule.AddRule(FzAND(Target_Medium, Ammo_Okay), VeryDesirable); m_FuzzyModule.AddRule(FzAND(Target_Medium, Ammo_Low), Desirable);

m_FuzzyModule.AddRule(FzAND(Target_Far, Ammo_Loads), Desirable); m_FuzzyModule.AddRule(FzAND(Target_Far, Ammo_Okay), Undesirable); m_FuzzyModule.AddRule(FzAND(Target_Far, Ammo_Low), Undesirable);}

Page 15: Fuzzy Logic Driven Agent Behavior Prof. Dr. Taoufik Nouri Nouri@Nouri@Nouri.ch 29.03.2014 The primary text for the course is Ref.[1]

ShotGunvoid ShotGun::InitializeFuzzyModule(){ FuzzyVariable& DistanceToTarget = m_FuzzyModule.CreateFLV("DistanceToTarget");

FzSet& Target_Close = DistanceToTarget.AddLeftShoulderSet("Target_Close", 0, 25, 150); FzSet& Target_Medium = DistanceToTarget.AddTriangularSet("Target_Medium", 25, 150, 300); FzSet& Target_Far = DistanceToTarget.AddRightShoulderSet("Target_Far", 150, 300, 1000);

FuzzyVariable& Desirability = m_FuzzyModule.CreateFLV("Desirability"); FzSet& VeryDesirable = Desirability.AddRightShoulderSet("VeryDesirable", 50, 75, 100); FzSet& Desirable = Desirability.AddTriangularSet("Desirable", 25, 50, 75); FzSet& Undesirable = Desirability.AddLeftShoulderSet("Undesirable", 0, 25, 50);

FuzzyVariable& AmmoStatus = m_FuzzyModule.CreateFLV("AmmoStatus"); FzSet& Ammo_Loads = AmmoStatus.AddRightShoulderSet("Ammo_Loads", 30, 60, 100); FzSet& Ammo_Okay = AmmoStatus.AddTriangularSet("Ammo_Okay", 0, 30, 60); FzSet& Ammo_Low = AmmoStatus.AddTriangularSet("Ammo_Low", 0, 0, 30);

m_FuzzyModule.AddRule(FzAND(Target_Close, Ammo_Loads), VeryDesirable); m_FuzzyModule.AddRule(FzAND(Target_Close, Ammo_Okay), VeryDesirable); m_FuzzyModule.AddRule(FzAND(Target_Close, Ammo_Low), VeryDesirable);

m_FuzzyModule.AddRule(FzAND(Target_Medium, Ammo_Loads), VeryDesirable); m_FuzzyModule.AddRule(FzAND(Target_Medium, Ammo_Okay), Desirable); m_FuzzyModule.AddRule(FzAND(Target_Medium, Ammo_Low), Undesirable);

m_FuzzyModule.AddRule(FzAND(Target_Far, Ammo_Loads), Desirable); m_FuzzyModule.AddRule(FzAND(Target_Far, Ammo_Okay), Undesirable); m_FuzzyModule.AddRule(FzAND(Target_Far, Ammo_Low), Undesirable);}

Page 16: Fuzzy Logic Driven Agent Behavior Prof. Dr. Taoufik Nouri Nouri@Nouri@Nouri.ch 29.03.2014 The primary text for the course is Ref.[1]

Worked Example• Rule 1. IF Target_Far AND Ammo_Loads THEN Desirable • Rule 2. IF Target_Far AND Ammo_Okay THEN Undesirable • Rule 3. IF Target_Far AND Ammo_Low THEN Undesirable • Rule 4. IF Target_Medium AND Ammo_Loads THEN VeryDesirable • Rule 5. IF Target_Medium AND Ammo_Okay THEN VeryDesirable • Rule 6. IF Target_Medium AND Ammo_Low THEN Desirable • Rule 7. IF Target_Close AND Ammo_Loads THEN Undesirable • Rule 8. IF Target_Close AND Ammo_Okay THEN Undesirable • Rule 9. IF Target_Close AND Ammo_Low THEN Undesirable

Page 17: Fuzzy Logic Driven Agent Behavior Prof. Dr. Taoufik Nouri Nouri@Nouri@Nouri.ch 29.03.2014 The primary text for the course is Ref.[1]

Worked ExampleLet's say the target is at a distance of 200 pixels and the amount of ammo remaining is 8 rockets.

Rule 1

IF Target_Far AND Ammo_Loads THEN Desirable

The degree of membership of the value 200 to the set Target_Far is 0.33. The degree of membership of the value 8 in the set Ammo_Loads is 0. The AND operator results in the minimum of these values so the inferred conclusion for Rule 1 is Desirable =0. In other words, the rule doesn't fire.

Page 18: Fuzzy Logic Driven Agent Behavior Prof. Dr. Taoufik Nouri Nouri@Nouri@Nouri.ch 29.03.2014 The primary text for the course is Ref.[1]

Worked ExampleRule 2

IF Target_Far AND Ammo_Okay THEN Undesirable

For the second rule the degree of membership of the value 200 to the set Target_Far is 0.33. The degree of membership of the value 8 in the set Ammo_Okay is 0.78. The inferred conclusion for Rule 2 therefore is Undesirable = 0.33.

Page 19: Fuzzy Logic Driven Agent Behavior Prof. Dr. Taoufik Nouri Nouri@Nouri@Nouri.ch 29.03.2014 The primary text for the course is Ref.[1]

Worked Example

Rule 3IF Target_Far AND Ammo_Low THEN Undesirable

Appling the same values to the third rule, the degree of membership of the value 200 to the set Target_Far is 0.33. The degree of membership of the value 8 in the set Ammo_Low is 0.2. The inferred conclusion for Rule 3 therefore is Undesirable = 0.2.

Page 20: Fuzzy Logic Driven Agent Behavior Prof. Dr. Taoufik Nouri Nouri@Nouri@Nouri.ch 29.03.2014 The primary text for the course is Ref.[1]

Worked ExampleWe can continue inferencing the rules. Here is summurized result in fuzzy associative matrix, or FAM

The FAM for the weapon selection rule base given the input values target distance =

200 and ammo status = 8. The shaded cells highlight rules that have fired.

Note that VeryDesirable has fired once to a degree of 0.67.

Desirable has fired once to a degree of 0.2, and Undesirable has fired twice with the degrees 0.2 and 0.33.

One way to think of these values is as confidence levels.

Page 21: Fuzzy Logic Driven Agent Behavior Prof. Dr. Taoufik Nouri Nouri@Nouri@Nouri.ch 29.03.2014 The primary text for the course is Ref.[1]

Worked Example: Combining the conclusions

The next step is to combine the inferred results into a single fuzzy manifold

Page 22: Fuzzy Logic Driven Agent Behavior Prof. Dr. Taoufik Nouri Nouri@Nouri@Nouri.ch 29.03.2014 The primary text for the course is Ref.[1]

Defuzzification: Mean of Maximum (MOM)

The mean of maximum — MOM for short — method of defuzzification calculates the average of those output values that have the highest confidence degrees. Figure 10.21 shows how this technique can be used to determine a crisp value from the output distribution for Desirability calculated earlier.

The mean of maximum method results in a desirability score of 83. One problem with this method is it doesn't take into account those sets in the output that do not have confidences equal to the highest (like those shown in the figure in gray), which can bias the resultant crisp value to one end of the domain.

Page 23: Fuzzy Logic Driven Agent Behavior Prof. Dr. Taoufik Nouri Nouri@Nouri@Nouri.ch 29.03.2014 The primary text for the course is Ref.[1]

Defuzzification: Centroid

The centroid method is the most accurate but is also the most complex to calculate. It works by determining the center of mass of the output sets. If you imagine each member set of the output set cut out of card stock and then glued together to form the shape of the fuzzy manifold, the center of mass is the position where the resultant shape would balance if placed on a ruler.

Page 24: Fuzzy Logic Driven Agent Behavior Prof. Dr. Taoufik Nouri Nouri@Nouri@Nouri.ch 29.03.2014 The primary text for the course is Ref.[1]

Defuzzification: CentroidThe centroid of a fuzzy manifold is calculated by slicing up the manifold into s sample points and calculating the sum of the contribution of the DOM at each sample point to the total, divided by the sum of the DOMs of the samples.

Value Undesirable Desirable VeryDesirable Sum

10 0.33 0 0 0.33

20 0.33 0 0 0.33

30 0.33 0.2 0 0.53

40 0.33 0.2 0 0.53

50 0 0.2 0 0.2

60 0 0.2 0.4 0.6

70 0 0.2 0.67 0.87

80 0 0 0.67 0.67

90 0 0 0.67 0.67

100 0 0 0.67 0.67

Page 25: Fuzzy Logic Driven Agent Behavior Prof. Dr. Taoufik Nouri Nouri@Nouri@Nouri.ch 29.03.2014 The primary text for the course is Ref.[1]

Defuzzification: Average of Maxima (MaxAv)

The maximum or representative value of a fuzzy set is the value where membership in that set is 1. For triangular sets this is the simply the value at the midpoint; for sets containing plateaus — such as right shoulder, left shoulder, and trapezoidal sets — this value is the average of the values at the beginning and end of the plateau. The average of maxima (MaxAv for short) defuzzification method scales the representative value of each consequent by its confidence and takes the average, like so:

Page 26: Fuzzy Logic Driven Agent Behavior Prof. Dr. Taoufik Nouri Nouri@Nouri@Nouri.ch 29.03.2014 The primary text for the course is Ref.[1]

We started with crisp values (distance to target = 200, ammo status = 8) to fuzzy sets, to inference, and back to a crisp value representing the desirability of using the rocket launcher (83, 62, or 60.625 depending on the defuzzification method).

If this process is repeated for each weapon type a bot is carrying, it's a simple matter to select the one with the highest desirability score to be the weapon the bot should use given its current situation.

Page 27: Fuzzy Logic Driven Agent Behavior Prof. Dr. Taoufik Nouri Nouri@Nouri@Nouri.ch 29.03.2014 The primary text for the course is Ref.[1]

Fuzzy Module Class

Page 28: Fuzzy Logic Driven Agent Behavior Prof. Dr. Taoufik Nouri Nouri@Nouri@Nouri.ch 29.03.2014 The primary text for the course is Ref.[1]

Conclusion

• You should now have an understanding of the theory behind fuzzy logic and it's application.

Page 29: Fuzzy Logic Driven Agent Behavior Prof. Dr. Taoufik Nouri Nouri@Nouri@Nouri.ch 29.03.2014 The primary text for the course is Ref.[1]

Exercice

1. Draw the fuzzy variables of the Rocket Launcher, apply the fuzzy rules and make the inference.

2. How would you read the following rules:m_FuzzyModule.AddRule(Target_Close, Desirable);

m_FuzzyModule.AddRule(Target_Medium, FzVery(Undesirable));

m_FuzzyModule.AddRule(Target_Far, FzVery(Undesirable));