## Fuzzy logic in Scilab (sciFLT). Part 3 — Mamdani and Sugeno from the command line

In the previous parts of this tutorial (part 1 & part 2) I showed some examples of fuzzy inference computations using the Scilab’s graphical user interface.

One student in my fuzzy logic class, Kairat Bostanbekov, found how to do it from the command line.

I provide his code here, adding some explanations.

Here we solve the same tipping problem discussed in the previous parts of the tutorial.

The code for the Mamdani system is:

```myfls=newfls();
myfls.name="cltip";
myfls.comment="This is command line tip";
myfls.type="m";
myfls.SNorm="max";
myfls.TNorm="min";
myfls.Comp="one";
myfls.ImpMethod="prod";
myfls.AggMethod="max";
myfls.defuzzMethod="centroide";
"input",1,"rancid","trapmf",[-1 0 0 7]);
"input",1,"delicious","trapmf",[3 10 10 11]);
"input",2,"poor","trapmf",[-1 0 0 5]);
"input",2,"good","trimf",[0 5 10]);
"input",2,"excellent","trapmf",[5 10 10 11]);
"output",1,"cheap","trapmf",[-1 0 0 0.125]);
"output",1,"average","trimf",[0 0.125 0.25]);
"output",1,"generous","trapmf",...
[0.125 0.25 0.25 0.26]);
myfls=addrule(myfls,[1 1 1 0 1; 0 2 2 0 1; 2 3 3 0 1]);
scf();clf();
plotvar(myfls,"input",[1 2]);
scf();clf();
plotvar(myfls,"output",1);
scf();clf();
plotsurf(myfls,[1 2],1,0,21,2);
```

The first line starts a new fuzzy inference system, and stores it in the variable `myfls` (you can choose any name):

`myfls=newfls();`

Then, you can give a name for your system and add a comment

```myfls.name="cltip";
myfls.comment="This is command line tip";
```

Choose the FLS type with the command

`myfls.type="m";`

where `m` stands for Mamdani, `ts` for Sugeno.

The S-norm, T-norm, and the inverse are specified with the code

```myfls.SNorm="max";
myfls.TNorm="min";
myfls.Comp="one";
```

In this case, maximum is taken as an S-norm, minimum as a T-norm, and 1-x as an inverse.

We take the product implication method,

`myfls.ImpMethod="prod";`

the maximum aggregation method,

`myfls.AggMethod="max";`

and the centroid defuzzification method:

`myfls.defuzzMethod="centroide";`

Now we add an input variable “food” and specify its range (from 0 to 10):

```myfls=addvar(myfls,"input","food",[0 10]);
```

Then we add membership functions for the two fuzzy sets in which the linguistic variable food takes values: rancid and delicious.

```myfls=addmf(myfls,...
"input",1,"rancid","trapmf",[-1 0 0 7]);
"input",1,"delicious","trapmf",[3 10 10 11]);
```

Then we add a new input variable “service” and specify its range (from 0 to 10):

```myfls=addvar(myfls, "input","service",[0 10]);
```

The membership functions for the three fuzzy sets of the service variable (poor, good, excellent) are:

```myfls=addmf(myfls,...
"input",2,"poor","trapmf",[-1 0 0 5]);
"input",2,"good","trimf",[0 5 10]);
"input",2,"excellent","trapmf",[5 10 10 11]);
```

The output variable “tip” with its range 0 to 25%, and the membership functions for the three fuzzy sets in which the variable tip takes values (cheap, average, generous) are added in a similar way to the input variables:

```myfls=addvar(myfls, "output","tip",[0 0.25]);

"output",1,"cheap","trapmf",[-1 0 0 0.125]);
"output",1,"average","trimf",[0 0.125 0.25]);
"output",1,"generous","trapmf",...
[0.125 0.25 0.25 0.26]);
```

The rules are specified via a rule matrix:

```myfls=addrule(myfls,[1 1 1 0 1; 0 2 2 0 1; 2 3 3 0 1]);
```

In the rule matrix,

```    1    1    1    0    1
0    2    2    0    1
2    3    3    0    1
```

each row describes a single rule.

The first column of the rule matrix refers to the first linguistic variable (food in our case).

The second column refers to the second variable (service).

The third column refers to the output variable (tip).

The fourth column indicates connections (0 — or; 1 — and).

The fifth column indicates the weights of the rules.

The variable food is described by two integer values in the matrix: 1 (rancid) and 2 (delicious).

The variable service is described by three integer values in the matrix: 1 (poor), 2 (good), 3 (excellent).

The variable tip is described by three integer values in the matrix: 1 (cheap) 2 (average), 3 (generous).

Thus, the first row of the rule matrix means: If the food is rancid (1 in the first column) or (0 in the fourth column) service is poor (1 in the second column), then tip is cheap (1 in the third column).

The second row means: If service is good (2 in the second column), then tip is average (2 in the third column) whatever the quality of food is (0 in the first column).

There is no or/and connection in the second rule, therefore we can put either 0 or 1 into the fourth column.

The third row means: If food is delicious (3 in the first column) or (0 in the fourth column) service is excellent (3 in the second column), then tip is generous (3 in the third column).

Then we plot the results. Click the pictures to enlarge them and see the details.

Input variables:

```scf();clf();
plotvar(myfls,"input",[1 2]);
```

Output variable:

```scf();clf();
plotvar(myfls,"output",1);  -- draw output functions
```

The solution surface:

```scf();clf();
plotsurf(myfls,[1 2],1,0,21,2);
```

***

Now, let’s solve the same exercise with the Sugeno method of the zero’s order.

The code is

```myfls=newfls();
myfls.name="cltipsgn";
myfls.comment="This is comand line tip Takagi-Sugeno";
myfls.type="ts";
myfls.SNorm="max";
myfls.TNorm="min";
myfls.Comp="one";
myfls.defuzzMethod="wtsum";
"input",1,"rancid","trapmf",[-1 0 0 7]);
"input",1,"delicious","trapmf",[3 10 10 11]);
"input",2,"poor","trapmf",[-1 0 0 5]);
"input",2,"good","trimf",[0 5 10]);
"input",2,"excellent","trapmf",[5 10 10 11]);
myfls=addrule(myfls,[1 1 1 0 1; 0 2 2 0 1; 2 3 3 0 1]);
scf();clf();
plotvar(myfls,"input",[1 2]);
scf();clf();
plotvar(myfls,"output",1);
scf();clf();
plotsurf(myfls,[1 2],1,0,21,2);
```

Notice the value of the type:

```myfls.type="ts";
```

Here we use the weighted sum defuzzification method:

```myfls.defuzzMethod="wtsum";
```

The input variables and the rules are added in the same way as we did in the Mamdani method but the output is defined with fuzzy singletons “cheap”, “average”, and “generous”:

```myfls=addmf(myfls,"output",1,"cheap","constant",0.0625);
```

The figure with the input variables:

```scf();clf();
plotvar(myfls,"input",[1 2]);
```

The output singletons:

```scf();clf();
plotvar(myfls,"output",1);
```

The solution surface:

```scf();clf();
plotsurf(myfls,[1 2],1,0,21,2);
```

Next: Part 4

Published in: on 26/10/2013 at 13:15  Comments (2)
Tags: