## onsdag 18 maj 2011

### Fuzzy logic - an introduction and an example in MATLAB

Conventional logic, also called “crisp logic”, is two-valued. In conventional logic you work with 0 and 1 or true or false. You can’t say that something is almost true. But in fuzzy logic you can! In fuzzy logic you work with the whole scale from 0 (completely false) to 1 (completely true), you can for example say that something is almost true and give that a value between 0 and 1. Fuzzy logic is multi-valued and uses linguistic variables. And in that way it’s a more human like logic because we often describe things on a sliding scale. If we want to describe a movie we don’t just use the words good or bad, we use a lot of different words and expressions. We can for example say that it was “extremely bad”, “mediocre” or “magnificent”. And if we would like to rate the movie on a scale from 0 to 10, then with conventional logic we might say that “extremely bad” is true between 0 and 3, “mediocre” is true between 3 and 7 and “magnificent” is true between 7 and 10. But that is a blunt and not very accurate translation. As humans we work with sliding scales, in our minds the movie doesn’t goes abruptly from “extremely bad” to “mediocre”. If we rate the movie as between “extremely bad” and “mediocre” then in fuzzy logic both are true at the same time but with different membership values. We can for example assign a membership value of 0.8 to “extremely bad” and a membership value of 0.2 to “mediocre”.

The picture above shows how our example with movie ratings works in the fuzzy world. On the Y-axis we have degree of membership and on the X-axis we have our rating from 0 to 10. In our example we have three fuzzy sets, “extremely bad”, “mediocre” and “magnificent”. Our fuzzy sets use linear functions but we could use other types such as sigmoid or Gaussian functions. In the picture above the vertical line represents our movie rating and we can see that it crosses all our fuzzy sets. Our movie rating gets the membership values 0.8 (extremely bad), 0.2 (mediocre) and 0.0 (magnificent).

We can modify the shape of our fuzzy sets with hedges. Hedges are adverbs such as slightly, very, quite and more or less. Hedges can reduce the degree of membership (e.g. very) or increase the degree of membership (e.g. more or less).

### MATLABs FIS Editor GUI

The FIS (Fuzzy Inference System) Editor GUI is part of the Fuzzy Logics Toolbox in MATLAB. To explain how it works I will implement the above example with movie ratings, but now with two input parameters, actor performance and script. And I will use “bad” instead of “extremely bad” and “good” instead of magnificent. Fuzzy logic uses IF THEN syntax when creating the rules. For my movie rating example I will use the following rules:

I give the script rating priority and rate the movie as good as long as the script is good. And I set up the start dialog as following:

I have two inputs (ActorPerformance and Script) and one output (MovieRating). And to map the input to the output I use Mamdani-style inference. The Mamdani-style inferences process has four steps:  fuzzification of the input, rule evaluation, aggregation of the rule outputs and defuzzification.

The picture above shows the membership function setup dialog. I use the Gaussian type for my inputs and triangular for my output. The range is from 0 to 10 because I can rate actor performance and script from 0 to 10. The MATLABs FIS Editor can display the results as at three-dimensional curve. In my case the mapping from ActorPerformance rating and Script rating to the MovieRating rating value as following picture shows:

Here we can for example see that the MovieRating value always is 10 if Script value is 10 and that correspond to the rule “IF Script is Good THEN MovieRating is Good”. If I change the rules the surface changes accordingly. Lets ad the rule “IF Script is Bad THEN MovieRating is Bad” and see what happens:

We can clearly see that our new rule changes the surface. ..