FUZZY LOGIC Fuzzy Logic Lotfi Zadeh (professor at UC Berkeley) wrote his original paper on fuzzy set theory. In various occasions, this is what he said… “Fuzzy logic is a means of presenting problems to computers in a way akin to the way humans solve them” “The essence of fuzzy logic is that everything is a matter of degree” What do these statements really mean? Fuzzy Logic Very often, we humans analyze situations and solve problems in a rather imprecise manner Do not have all the facts Facts might be uncertain Maybe we only generalize facts without having the precise data or measurements… Real-life example: Playing a game of basketball Everything is a matter of degree? Is your basketball opponent tall, or average or short? (use of linguistic terms to measure degree) Is 7 feet tall? Is 6 feet 10 inches tall? Are they both considered tall? (overlapping degrees) Problem with traditional Boolean logic You are forced to define a point above which we will consider the guy to be tall or just average, e.g. > 7 ft Fuzzy Logic allows gray areas or degrees of being considered “tall” The degree of truth So…you can think of fuzzy logic as classifying something as being TRUE, but to varying degrees Real-life control applications (air-conditioning, household appliances): Traditional Boolean logic will result in abrupt switching of response functions Fuzzy logic alleviates this problem Responses will vary smoothly given the degree of truth or strength of the input conditions Fuzzy logic for games A previous game AI example… An AI character makes his decision to chase (using FSM or DT) based on traditional Boolean logic, e.g. distance of player < 20 units, and player health < 50% In fuzzy logic, we can represent these input conditions using a few “membership” degrees of measure Distance: (“Far”, “Average”, “Near”) Health: (“Good”, “Normal”, “Poor”) The output actions can also be represented with different membership degrees (“Chase Fast”, “Chase Slow”) How to use Fuzzy Logic in Games? 3 possible ways how fuzzy logic can be used in games Control Modulating steering forces, travelling/moving towards target Threat Assessment Assessing player’s strengths/weaknesses for deploying units and making moves Classification Identifying the combat prowess of characters in the game based on a variety of factors in order to choose opponent There are many other possibilities… Fuzzy Logic Basics Fuzzy control or fuzzy inference process – 3 basic steps Step 1: Fuzzification Fuzzification: Process of mapping/converting crisp data (real numbers) to fuzzy data Find degree of membership of the crisp input in predefined fuzzy sets E.g. given a character’s health, determine the degree to which it is “Good”, “Fair” or “Poor”. Mapping is achieved using membership functions Membership Functions Membership Functions Map input variables to a degree of membership, in a fuzzy set, between 0 and 1. Degree 1 absolutely true, degree 0 absolutely false, any degree in between true or false to a certain extent “Boolean logic membership function” Membership Functions Fuzzy Membership Functions Enables us to transition gradually from false to true Grade membership function Membership Functions Triangular m/f Reverse grade m/f Equations are just the inverse of the grade m/f Membership Functions Trapezoid m/f Other nonlinear m/f Gaussian or Sigmoid ‘S’-shaped curves Membership Functions Typically, we are interested in the degree of which an input variable falls within a number of qualitative sets Membership Functions Setting up collections of fuzzy sets for an input variable is a matter of judgment and trial-and-error not uncommon to “tune” the sets While tuning, one can try different membership functions, increase or decrease number of sets Some fuzzy practitioners recommend 7 fuzzy sets to fully define a practical working range (?!?!?) Membership Functions One rule of thumb for ensuring smooth transitions (in later steps) is to enforce overlapping between neighboring sets Hedge Functions Hedge functions are sometimes used to modify the degree of membership Provide additional linguistic constructs that you can use in conjunction with other logical operations. Two common hedges: = Truth(A)2 NOT_VERY(Truth(A)) = Truth(A)0.5 (Truth(A) is the degree of membership of A in some fuzzy set) VERY(Truth(A)) Step 2: Fuzzy Rules Next, construct a set of rules, combining the input in some logical manner, to yield some output If-then style rules (if A then B) – A being the antecedent/premise and B being the consequent/conclusion Fuzzy input variables are combined logically to form premise Conclusion will be the degree of membership of some output fuzzy set Fuzzy Axioms Since we are writing “logical” rules with fuzzy input, we need a way to apply logical operators to fuzzy input (just like with Boolean input) Logical OR (disjunction) Truth(A Logical AND (conjunction) Truth(A OR B) = MAX(Truth(A), Truth(B)) AND B) = MIN(Truth(A), Truth(B)) Logical NOT (negation) Truth(NOT A) = 1 – Truth(A) Fuzzy Axioms Example, given a person is overweight to the degree of 0.7 and tall to the degree of 0.3: Overweight AND tall = MIN(0.7, 0.3) = 0.3 Overweight OR tall = MAX(0.7, 0.3) = 0.7 NOT overweight = 1 – 0.7 = 0.3 NOT tall = 1 – 0.3 = 0.7 NOT(overweight AND tall) = 1 – MIN(0.7, 0.3) = 0.7 There are other definitions for these logical operators… Rule Evaluation Unlike traditional Boolean logic, Rules in fuzzy logic can evaluate into any number between 0 and 1 (not just 0 or 1) All rules are evaluated in parallel (not in series that the first one that is true gets fired). Each rule always fires, to various degrees The strength of each rule represents the degree of membership in the output fuzzy set Rule Evaluation Example: Evaluating whether an AI should attack player Rules can be written like: If (in melee range AND uninjured) AND NOT hard then attack Set up as many rules to handle all possibilities in the game Rule Evaluation Given specific degrees for the input variables, you might get outputs (conclusions of the rules) that look something like this: Attack to degree: 0.2 Do nothing to degree: 0.4 Flee to degree: 0.7 The most straightforward way to interpret these outputs is to take the action associated with the highest degree (in this case, the action will be flee) Step 3: Defuzzification In some cases, you might want to use the fuzzy output degree to determine a crisp value (real number), which can be useful for further calculations Defuzzification: Process of converting the results from the fuzzy rules to get a crisp number as an output Opposite of fuzzification (you can say that, although the purpose and methods are different!) Step 3: Defuzzification Previous example: Instead of determining some finite action (do nothing, flee, attack), we also want to use the output to determine the speed to take the action To get a crisp number, aggregate the output strengths on the predefined output membership functions Step 3: Defuzzification With the numerical output from the earlier example (0.2 degree attack, 0.4 degree do nothing, 0.7 degree flee), we have the composite membership function below Defuzzifying composite m/f Truncate each output set to the output degree of membership for that set. Then combine all output sets by disjunction A crisp number can be arrived from such an output fuzzy set in many ways Geometric centroid of the area under the output fuzzy set, taking its horizontal axis coordinate as the crisp output Using “predefuzzified” output A less computationally expensive method is the use of singleton output membership function or a “predefuzzified” output function Instead of doing lots of calculation, assign speeds to each output action (-10 for flee, 1 for do nothing, 10 for attack). E.g. The resulting speed for flee is simply the preset value of -10 times the degree to which the output action flee is true (-10 x 0.7 = -7) Using “predefuzzified” output Aggregate of all outputs with a simple weighted average In our example, we might have: Output = [(0.7)(-10) + (0.4)(1) + (0.3)(10)] / (0.7+0.4+0.3) = -2.5 This output would result in the creature fleeing, but not earnestly in full extent Naturally, we can obtain various output (crisp) values depending on the different input conditions Further Examples There are 2 good examples in the textbook, showing the full process of using fuzzy logic to model game AI characters Using Fuzzy Logic in FSMs? If we want to add some fuzzy logic into FSMs, how can that be accomplish? Is it possible? Remember: Each state defines a behavior or action, and each state is reached by transition from another state on the basis of fulfilling some input conditions… Conditions for transition are normally in Boolean logic, how do we accommodate fuzzy logic? Fuzzy State Machines Different AI developers regard Fuzzy State Machines differently State machine with fuzzy states State transitions that use fuzzy logic to trigger Both Find out more about how these different variations can be worked out and implemented (refer to Millington book) … Next up Homework 2 (due in Week 10, submission via mail) Milestone #2 (due in Week 11, 11.00am 23/8, Thurs) Upcoming lectures Probabilities and Uncertainty Techniques Tactical and Strategic AI