# Tune Fuzzy Rules and Membership Function Parameters

This example shows how to tune a fuzzy inference system (FIS). Using the `tunefis` function, you can:

• Tune membership function parameters for input and output variables.

• Learn fuzzy rules.

• Tune the antecedent and consequent parameters of fuzzy rules.

For more information on tuning a FIS, see Tuning Fuzzy Inference Systems.

### Tune Membership Function Parameters

For both type-1 and type-2 FISs, you can specify tunable parameter settings for the input and output MFs and tune the values of the selected parameters. You can tune the parameters for any combination of input and output MFs. This example shows an example workflow using a type-1 FIS. For an example that tunes a type-2 FIS, see Predict Chaotic Time Series Using Type-2 FIS.

Create a FIS.

```fis = mamfis; fis = addInput(fis,[0 10],'NumMFs',3); fis = addOutput(fis,[0 1],'NumMFs',3); fis = addRule(fis,[1 1 1 1;1 1 1 1;1 1 1 1]);```

Extract input and output parameter settings from the FIS.

`[in,out] = getTunableSettings(fis)`
```in = VariableSettings with properties: Type: "input" VariableName: "input1" MembershipFunctions: [1×3 fuzzy.tuning.MembershipFunctionSettings] FISName: "fis" ```
```out = VariableSettings with properties: Type: "output" VariableName: "output1" MembershipFunctions: [1×3 fuzzy.tuning.MembershipFunctionSettings] FISName: "fis" ```

The parameter settings are represented by `VariableSettings` objects that include the FIS name, variable type, variable name, and MF parameter settings. Examine the parameter settings of MF 1 of input 1.

`in(1).MembershipFunctions(1).Parameters`
```ans = NumericParameters with properties: Minimum: [-Inf -Inf -Inf] Maximum: [Inf Inf Inf] Free: [1 1 1] ```

For each parameter value of an input or output MF, you can specify its availability for tuning and its minimum and maximum values. By default, all MF parameters are free for tuning and their ranges are set to [`-Inf`, `Inf`].

You can specify the tunability of all parameters in an MF using the `setTunable` function. For example, make MF 1 of input 1 nontunable.

`in(1).MembershipFunctions(1) = setTunable(in(1).MembershipFunctions(1),false);`

You can also specify the tunability of individual MF parameters. Make the first parameter of MF 2 of input 1 nontunable.

`in(1).MembershipFunctions(2).Parameters.Free(1) = false;`

For each parameter, you can specify the range of possible values. For example, set the minimum values for the second and third parameters of MF 3 of input 1 to 0.

`in(1).MembershipFunctions(3).Parameters.Minimum(2:3) = 0;`

Similarly, set the maximum values for second and third parameters of MF 3 of input 1 to `15`.

`in(1).MembershipFunctions(3).Parameters.Maximum(2:3) = 15;`

The tuning process sets the default minimum and maximum range values of tunable MF parameters to their corresponding input or output ranges.

Finally, make the MF parameters of all output membership functions nontunable.

`out = setTunable(out,false);`

Specify input and output training data. For this example, generate training data using the following function.

`$\mathit{y}=|\frac{\mathrm{sin}\left(2\mathit{x}\right)}{{\mathit{e}}^{\mathit{x}/5}}|$`

```x = (0:0.1:10)'; y = abs(sin(2*x)./exp(x/5));```

Specify options for `tunefis`. For this example, use the genetic algorithm tuning method. Use a maximum of five generations for optimization.

```options = tunefisOptions("Method","ga"); options.MethodOptions.MaxGenerations = 5;```

If you have Parallel Computing Toolbox™ software, you can improve the speed of the tuning process by setting `options.UseParallel` to `true`. If you do not have Parallel Computing Toolbox software, set `options.UseParallel` to `false`, which is the default value.

By default, `tunefis` uses the root mean squared error (RMSE) for cost calculation. You can change the cost function to `norm1` or `norm2` by setting `options.DistanceMetric`.

`options.DistanceMetric = "norm1";`

Tune `fis` using the parameter settings, training data, and tuning options.

```rng('default') % for reproducibility [fisout,optimout] = tunefis(fis,[in;out],x,y,options);```
``` Best Mean Stall Generation Func-count f(x) f(x) Generations 1 100 32.84 32.84 0 2 147 32.84 32.84 1 3 194 32.84 32.84 2 4 241 32.84 32.84 3 5 288 32.84 32.84 4 Optimization terminated: maximum number of generations exceeded. ```

`fisout` includes the updated parameter values. `optimout` provides additional outputs of the optimization method and any error messages that are returned during the update process of the input fuzzy system using the optimized parameter values.

`optimout`
```optimout = struct with fields: tuningOutputs: [1×1 struct] totalFcnCount: 288 totalRuntime: 2.7104 errorMessage: [] ```
`optimout.tuningOutputs`
```ans = struct with fields: x: [5 9.1667 5.8333 10 14.1667] fval: 32.8363 exitflag: 0 output: [1×1 struct] population: [50×5 double] scores: [50×1 double] ```

You can optionally tune `fis` using either the input or output parameter settings only. In this example, since you set the output parameter settings to nontunable, tuning the FIS with just the input parameter settings produces the same results.

```rng('default') [fisout,optimout] = tunefis(fis,in,x,y,options);```
``` Best Mean Stall Generation Func-count f(x) f(x) Generations 1 100 32.84 32.84 0 2 147 32.84 32.84 1 3 194 32.84 32.84 2 4 241 32.84 32.84 3 5 288 32.84 32.84 4 Optimization terminated: maximum number of generations exceeded. ```
`optimout`
```optimout = struct with fields: tuningOutputs: [1×1 struct] totalFcnCount: 288 totalRuntime: 1.4074 errorMessage: [] ```
`optimout.tuningOutputs`
```ans = struct with fields: x: [5 9.1667 5.8333 10 14.1667] fval: 32.8363 exitflag: 0 output: [1×1 struct] population: [50×5 double] scores: [50×1 double] ```

### Tune Fuzzy Rules

In addition to tuning membership function parameters, you can tune the antecedent and consequent parameters of the rules in a fuzzy system.

Obtain rule parameter settings from a fuzzy system using `getTunableSettings`.

`[~,~,rule] = getTunableSettings(fis)`
```rule=3×1 RuleSettings array with properties: Index Antecedent Consequent FISName ```

Each rule parameter setting includes the FIS name, the index of the rule in the FIS, and parameter settings for the rule antecedent and consequent (the rule clauses).

For a rule clause, you can set the following parameters settings.

• `AllowNot` — Allow the use of NOT logic, that is, negative MF indices. By default, rules do not allow NOT logic.

• `Free` — Make the input/output MF indices available for tuning. By default, clause parameters are available for tuning.

• `AllowEmpty` — Allow the absence of input/output variables, that is, zero MF indices. By default, the absence of a variable is allowed.

`rule(1).Antecedent(1)`
```ans = ClauseParameters with properties: AllowNot: 0 AllowEmpty: 1 Free: 1 ```

Allow NOT logic in the antecedent of rule 1.

`rule(1).Antecedent.AllowNot = true;`

Make the consequent of rule 1 not available for tuning.

`rule(1).Consequent.Free = 0;`

Do not allow absence of a variable in the consequent of rule 2.

`rule(2).Consequent.AllowEmpty = false;`

Set rule 3 as nontunable.

`rule(3) = setTunable(rule(3),false);`

Set `options.DistanceMetric` to `norm2`.

`options.DistanceMetric = "norm2";`

Tune `fis` using the rule parameter settings.

```rng('default') % for reproducibility fisout = tunefis(fis,rule,x,y,options);```
``` Best Mean Stall Generation Func-count f(x) f(x) Generations 1 100 1.648 2.575 0 2 147 1.648 2.448 1 3 194 1.648 2.212 2 4 241 1.648 2.052 3 5 288 1.648 1.874 4 Optimization terminated: maximum number of generations exceeded. ```

Since you specified rule 3 as nontunable, you can exclude rule 3 when you tune `fis`. Doing so produces the same tuning result.

```rng('default') % for reproducibility fisout = tunefis(fis,rule(1:2),x,y,options);```
``` Best Mean Stall Generation Func-count f(x) f(x) Generations 1 100 1.648 2.575 0 2 147 1.648 2.448 1 3 194 1.648 2.212 2 4 241 1.648 2.052 3 5 288 1.648 1.874 4 Optimization terminated: maximum number of generations exceeded. ```

### Learn Fuzzy Rules

You can configure `tunefis` to learn the rules of a fuzzy system. To do so, set the `OptimizationType` option of `tunefisOptions` to "`learning"`.

```fisin = fis; fisin.Rules = []; options.OptimizationType = "learning";```

Set the maximum number of rules in the tuned FIS to `3`.

`options.NumMaxRules = 3;`

The size of the tuned rule base might be less than `NumMaxRules`, because `tunefis` removes duplicate rules from the tuned FIS. If you do not specify `NumMaxRules`, then `tunefis` adds the maximum number of rules determined by the possible combinations of input MFs. The default input MF combinations include zero MF indices, which allow absence of variables. The default combinations exclude negative MF indices, so that NOT logic is not allowed.

Set `options.DistanceMetric` to `"rmse"` and tune the FIS.

```options.DistanceMetric = "rmse"; rng('default') % for reproducibility fisout = tunefis(fisin,[],x,y,options);```
``` Best Mean Stall Generation Func-count f(x) f(x) Generations 1 400 0.165 0.2956 0 2 590 0.165 0.2805 1 3 780 0.165 0.2578 2 4 970 0.165 0.2393 3 5 1160 0.165 0.2322 4 Optimization terminated: maximum number of generations exceeded. ```

During the tuning process, the FIS automatically learns rules after cost optimization with the training data. Examine the tuned rules.

`fisout.Rules`
```ans = 1×3 fisrule array with properties: Description Antecedent Consequent Weight Connection Details: Description ________________________________ 1 "input1==mf3 => output1=mf1 (1)" 2 "input1==mf1 => output1=mf2 (1)" 3 "input1==mf2 => output1=mf1 (1)" ```

You can remove some of the existing rules and learn additional rules.

```fisout.Rules(2:end) = []; rng('default') % for reproducibility fisout = tunefis(fisin,[],x,y,options);```
``` Best Mean Stall Generation Func-count f(x) f(x) Generations 1 400 0.165 0.2956 0 2 590 0.165 0.2805 1 3 780 0.165 0.2578 2 4 970 0.165 0.2393 3 5 1160 0.165 0.2322 4 Optimization terminated: maximum number of generations exceeded. ```
`fisout.Rules`
```ans = 1×3 fisrule array with properties: Description Antecedent Consequent Weight Connection Details: Description ________________________________ 1 "input1==mf3 => output1=mf1 (1)" 2 "input1==mf1 => output1=mf2 (1)" 3 "input1==mf2 => output1=mf1 (1)" ```

You can also tune the antecedents and consequents of existing rules and learn new rules. To do so, obtain the tunable rule parameter settings and pass them to the `tunefis` function.

```fisout.Rules(2:end) = []; fisout.Rules(1).Antecedent = 1; fisout.Rules(1).Consequent = 1; [~,~,rule] = getTunableSettings(fisout); rng('default') fisout = tunefis(fisin,rule,x,y,options);```
``` Best Mean Stall Generation Func-count f(x) f(x) Generations 1 400 0.165 0.3063 0 2 590 0.165 0.2845 1 3 780 0.165 0.2549 2 4 970 0.165 0.2344 3 5 1160 0.165 0.2153 4 Optimization terminated: maximum number of generations exceeded. ```
`fisout.Rules`
```ans = 1×3 fisrule array with properties: Description Antecedent Consequent Weight Connection Details: Description ________________________________ 1 "input1==mf1 => output1=mf2 (1)" 2 "input1==mf2 => output1=mf1 (1)" 3 "input1==mf3 => output1=mf1 (1)" ```

### Tune MF and Rule Parameters

You can tune all MF and rule parameters simultaneously. First, obtain all parameter settings for the FIS.

`[in,out,rule] = getTunableSettings(fis);`

Configure the tuning options.

```options = tunefisOptions('Method','ga'); options.MethodOptions.MaxGenerations = 5;```

Tune the MF and rule parameters of the FIS.

```rng('default') % for reproducibility fisout = tunefis(fis,[in;out;rule],x,y,options);```
``` Best Mean Stall Generation Func-count f(x) f(x) Generations 1 400 0.165 0.296 0 2 590 0.1638 0.2821 0 3 780 0.1625 0.2697 0 4 970 0.1625 0.2605 1 5 1160 0.1604 0.2546 0 Optimization terminated: maximum number of generations exceeded. ```

For a large fuzzy system, if you tune all FIS parameters in the same tuning process, obtaining the expected results can take several iterations. To improve the tuning time, you can tune parameters using the following two steps.

1. Tune or learn rule parameters only.

2. Tune both MF and rule parameters.

Learning and tuning rules is less computationally expensive that tuning the MF parameters, due to the small number of rule parameters. Therefore, the first step quickly converges to a fuzzy rule base during training. In the second step, using the rule base from the first step as an initial condition improves convergence of the parameter tuning process.

### Generate FIS from Data and Tune

When you manually create a FIS for tuning, you must either manually create an initial rule base or learn the initial rules. Alternatively, you can generate a FIS using the `genfis` function, which creates an initial rule base based on your training data. You can then optimize the FIS using `tunefis`. In this approach, the tuning process can employ a local optimization method because the rule base is derived from the training data.

This example uses the same training data as the preceding examples.

Create options for `genfis` that specify five MFs, a Gaussian MF for the input, and a constant MF for the output.

```goptions = genfisOptions('GridPartition','NumMembershipFunctions',5, ... 'InputMembershipFunctionType','gaussmf', ... 'OutputMembershipFunctionType','constant');```

Generate the initial FIS and get its parameter settings.

```fisin = genfis(x,y,goptions); [in,out,rule] = getTunableSettings(fisin);```

Use the pattern search method for optimization, setting the maximum number of iterations to 25, and tune the FIS.

```toptions = tunefisOptions('Method','patternsearch'); toptions.MethodOptions.MaxIterations = 25; rng('default') fisout = tunefis(fisin,[in;out],x,y,toptions);```
```Iter Func-count f(x) MeshSize Method 0 1 0.346649 1 1 19 0.346649 0.5 Refine Mesh 2 37 0.273812 1 Successful Poll 3 38 0.236413 2 Successful Poll 4 39 0.190794 4 Successful Poll 5 40 0.182142 8 Successful Poll 6 47 0.182142 4 Refine Mesh 7 49 0.162927 8 Successful Poll 8 56 0.162927 4 Refine Mesh 9 67 0.162927 2 Refine Mesh 10 69 0.159539 4 Successful Poll 11 80 0.159539 2 Refine Mesh 12 92 0.159539 1 Refine Mesh 13 94 0.159421 2 Successful Poll 14 106 0.159373 4 Successful Poll 15 117 0.159373 2 Refine Mesh 16 125 0.159185 4 Successful Poll 17 136 0.159185 2 Refine Mesh 18 151 0.159185 1 Refine Mesh 19 152 0.15914 2 Successful Poll 20 167 0.15914 1 Refine Mesh 21 170 0.158914 2 Successful Poll 22 185 0.158914 1 Refine Mesh 23 187 0.158839 2 Successful Poll 24 202 0.158839 1 Refine Mesh 25 206 0.158366 2 Successful Poll 26 215 0.158121 4 Successful Poll Maximum number of iterations exceeded: increase options.MaxIterations. ```

You can increase the number of iterations to further optimize the cost.