# Fuzzy Logic Driven Agent Behavior

## Presentation on theme: "Fuzzy Logic Driven Agent Behavior"— Presentation transcript:

Fuzzy Logic Driven Agent Behavior
Prof. Dr. Taoufik Nouri 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

Fuzzy Sets Fuzzy Variable IQ Ryan with IQ=115 Є Clever with m = 0.75
Є Average with m = 0.25 m is membership coefficient Fuzzy Variable: Distance to Target

Fuzzy Set Operators Average AND Clever OR = + Average OR Clever NOT
NOT Clever

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.

Hedges

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

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.

Fuzzy Variable by Raven

Fuzzy Module FuzzyVariable& 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);

How Raven Uses the Fuzzy Logic
Weapons & Projectiles: Weapon Blaster Weapon Railgun Weapon Rocket Luncher Weapon 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

Weapon Blaster void Blaster::InitializeFuzzyModule() {

Weapon Railgun void RailGun::InitializeFuzzyModule() {

RocketLauncher void RocketLauncher::InitializeFuzzyModule() {

ShotGun void ShotGun::InitializeFuzzyModule() {

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

Worked Example Let'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 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.

Worked Example Rule 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 The degree of membership of the value 8 in the set Ammo_Okay is The inferred conclusion for Rule 2 therefore is Undesirable = 0.33.

Worked Example Rule 3 IF 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 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.

Worked Example We 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.

Worked Example: Combining the conclusions
The next step is to combine the inferred results into a single fuzzy manifold

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 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.

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.

Defuzzification: Centroid
The 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 20 30 0.2 0.53 40 50 60 0.4 0.6 70 0.67 0.87 80 90 100

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:

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 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.

Fuzzy Module Class

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

Exercice Draw the fuzzy variables of the Rocket Launcher, apply the fuzzy rules and make the inference. 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));