Artificial Intelligence for Games Lecture 5 1 Minor Games Programming
Artificial Intelligence for Games 2 Theory: Short introduction to Fuzzy Logic Jan Verhoeven
Theory: Fuzzy Logic, (see study book: chapter 10) 3 Crisp and Fuzzy Sets with Operators Fuzzy Rules Fuzzy Inference Procedure Coding a Fuzzy Logic Module Fuzzy Logic Classes in Raven The Combs Method
Fuzzy Logic: how to make perfect cheese on toast: 4 1. Cut two slices of bread medium thick. 2. Turn the heat on the griddle on high. 3. Grill the slices on one side until golden brown. 4. Turn the slices over and add a generous helping of cheese. 5. Replace and grill until the top of the cheese is slightly brown. 6. Remove, sprinkle on a small amount of black pepper, and eat.
intermezzo 5 What is medium thick ? 1 cm? And what about 0,999 cm? Discrete intervals are sometimes to rigid.
Fuzzy Logic 6 Fuzzy logic, invented by a man named Lotfi Zadeh in the mid-sixties, enables a computer to reason about linguistic terms and rules in a way similar to humans
Fuzzy rule-based inference 7
Crisp Sets, an object belongs to a set or it doesn’t 8
Crisp Sets, a graphical way of showing your IQ 9 Dumb = {70, 71, 72, … 89} Average = {90, 91, 92, … 109} Clever = {110, 111, 112, … 129}
Fuzzy Sets 10 Dumb = {60, 61, …, 100} Average = {80, 81, … 120} Clever = {100, 101, … 140}
Defining Fuzzy Boundaries with Membership Functions 11 DOM (degree of membership)
fuzzy set, Membership value, DOM 12 The dotted line shows the IQ of 115, which is a member of two sets: m(115) = { Average/0.25, Clever/0.75 } Another example: m(80) = {Dumb/1.0, Average/0.0} = {Dumb/1.0} Note: 0.25 = membership degree (DOM)
Exercise Time Student X owns a IQ of 110 What are his degrees of membership in Dumb, Average and Clever?
Figure 10.6: The set of people who are Average AND Clever 14
Figure 10.7: The set of people who are Average OR Clever 15
Figure 10.8: The complement of Clever 16
Exercise Time -2-:Draw Fuzzy Sets The fuzzy set A = NOT (Average OR Dumb) 2. The fuzzy set: B = (Average AND Clever) OR (NOT Dumb)
Exercise Time What is DOM of student X with an IQ of 110 in fuzzy set A? 2. And in fuzzy set B? 3. Which IQ belongs to a DOM value of 0.5 in fuzzy set A? 4. And in fuzzy set B?
Hedges 19 Hedges are unary operators that can be employed to modify the meaning of a fuzzy set. Two commonly used hedges are VERY and FAIRLY
Fuzzy Modifiers 20 Possible fuzzy definitions very(a) = a 2 extremely(a) = a 3 moreorless(a) = a 1/2 slightly(a) = a 1/3 exactly(a) = a + always(a) = a 0 Example: DOM(90) in fuzzy set: Very(Dumb) is (0.5) 2
Fuzzy Linguistic Variables (or FLV) 21 A fuzzy linguistic variable (or FLV) is the composition of one or more fuzzy sets to represent a concept or domain qualitatively. Given our earlier example, the sets Dumb, Average, and Clever are members of the fuzzy linguistic variable IQ. This can be written in set notation as: IQ ={Dumb, Average, Clever}
Designing FLV’s 22 There are a couple of important guidelines to adhere to when designing FLVs. They are: For any vertical line (representing an input value) drawn through the FLV, the sum of the DOMs in each of the fuzzy sets it intersects with should be approximately 1. This ensures a smooth transition between values over the FLV's fuzzy manifold (the combined shape of all membership sets). For any vertical line drawn through the FLV, it should only intersect with two or fewer fuzzy sets.
Badly designed FLV’s: 23
Fuzzy Rules 24 Fuzzy rules are comprised of an antecedent and a consequent in the form: IF antecedent THEN consequent The antecedent represents a condition and the consequent describes the consequence if that condition is satisfied. This type of rule is familiar to all programmers.
Fuzzy Rules Examples 25 IF Target_isFarRight THEN Turn_QuicklyToRight IF VERY(Enemy_BadlyInjured) THEN Behavior_Aggressive IF Target_isFarAway AND Allegiance_isEnemy THEN Shields_OnLowPower IF MOREORLESS(Coffee_Break_Time) AND EXTREMELY(Sleepy) THEN Take_A_Cup
Fuzzy Inference 26 This is where we present the system with some values to see which rules fire and to what degree. Fuzzy inference follows these steps: 1. For each rule, a) For each antecedent, calculate the degree of membership of the input data. b) Calculate the rule's inferred conclusion based upon the values determined in 1a. 2. Combine all the inferred conclusions into a single conclusion (a fuzzy set). 3. For crisp values, the conclusion from 2 must be defuzzified.
Fuzzy rule-based inference 27
An Example: Weapon Selection in RAVEN 28 Steps: 1. Designing FLVs for Weapon Selection 2. Designing the Rule Set for Weapon Selection 3. Fuzzy Inference To keep the example simple, we'll say the desirability of selecting a particular weapon from the inventory is dependent on two factors: the distance to the target and the amount of ammo
Example Raven: FLV Desirability (consequent) 29
Example Raven: antecedent: Distance to Target 30
Example Raven: Designing the Ammo Status FLV (for each weapon type) 31
Example Raven: Designing the Rule Set for Weapon Selection To cover all the possibilities, a rule must be created for each possible combination of antecedent sets. The FLVs Ammo Status and Distance to Target each contain three member sets, so to cover every combination nine rules must be defined. The rules must be defined by an expert user!
Example Raven: Designing the Rule Set for Weapon Selection 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
Example Raven: Fuzzy Inference 34 Let's now work through these steps using some of the rules we've created for weapon selection and some crisp input values. Let's say the target is at a distance of 200 pixels and the amount of ammo remaining is 8 rockets. One rule at a time then…
Example Raven: Fuzzy Inference, Rule One 35 IF Target_Far AND Ammo_Loads THEN Desirable
Example Raven: Fuzzy Inference, Rule Two 36 IF Target_Far AND Ammo_Okay THEN Undesirable
Example Raven: Fuzzy Inference, Rule Three 37 IF Target_Far AND Ammo_Low THEN Undesirable
EXERCISE TIME 38 Fuzzy Inference, Rule 5: IF Target_Medium AND Ammo_Okay THEN VeryDesirable Draw and compute the inferred conclusion for VeryDesirable
Example Raven: Rules one to nine in fuzzy associative matrix (FAM) 39
Inferred conclusions 40 Undesirable0.33 (maximum value of 0.33 and 0.2) Desirable0.2 Very Desirable0.67
The inferred results of processing the rule set for weapon selection 41
Combining the conclusions 42
Defuzzification 43 Now that we have a composite fuzzy set representing the inferred conclusion of all the rules in the rule base, it's time to turn the process around and convert this output set into a single crisp value. This is achieved by a process called defuzzification. Many techniques are possible (see study book): Mean of Maximum (MOM) Centroid Average of Maxima (MaxAv)
Example Raven: Defuzzification 44 Mean of Maximum (MOM) 83 Centroid 62 Average of Maxima (MaxAv)
Example Raven: Selection 45 If this whole 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.
Homework 46 Homework Study Chapter 10 Fuzzy Logic, pages 415 to 437.
Practice (next TUESDAY) 47 Program a fuzzy (console) application in C++. You must use the FuzzyModule Class (and others) of Buckland, see study book pages 437 to 452. The sources will be on Blackboard. For an example see weapon selection code in Raven. TIP: Use your study book during next practice!