# Tune Fuzzy Trees

This example shows how to tune the parameters of a FIS tree using the following two-step process. For more information about a similar two-step process, see Tuning Fuzzy Inference Systems.

1. Learn and tune the rules of the FISs in the tree.

2. Learn the MF parameters of the FISs in the tree.

Tuning FIS trees is not supported in the Fuzzy Logic Designer app.

Create a FIS tree to model $\frac{\mathrm{sin}\left(\mathit{x}\right)+\mathrm{cos}\left(\mathit{x}\right)}{\mathrm{exp}\left(\mathit{x}\right)}$, as shown in the following figure. For more information on creating FIS trees, see Fuzzy Trees.

Create fis1 as a Sugeno-type FIS, which results in a faster tuning process compared to a Mamdani system, due to its computationally efficient defuzzification method. Add two inputs, both with range [0, 10] and with three MFs each. Use a smooth, differentiable MF, such as gaussmf, to match the characteristics of the data type you are modeling.

fis1 = sugfis("Name","fis1");

Add an output with the range [–1.5, 1.5] having nine MFs corresponding to the nine possible input MF combinations. Doing so provides maximum granularity for the FIS rules. Set the output range according to the possible values of $\mathrm{sin}\left(\mathit{x}\right)+\mathrm{cos}\left(\mathit{x}\right)$.

Create fis2 as a Sugeno-type FIS. Add two inputs. Set the range of the first input to [–1.5, 1.5], which matches the range of the output of fis1. The second input is the same as the inputs of fis1. Therefore, use the same input range, [0, 10]. Add three MFs for each of the inputs.

fis2 = sugfis("Name","fis2");

Add an output with range [0, 1] and nine MFs. The output range is set according to the possible values of $\frac{\mathrm{sin}\left(\mathit{x}\right)+\mathrm{cos}\left(\mathit{x}\right)}{\mathrm{exp}\left(\mathit{x}\right)}$.

Connect the inputs and the outputs as shown in the diagram. The first output of fis1, output1, connects to the first input of fis2, input1. The inputs of fis1 connect to each other, and the second input of fis1 connects to the second input of fis2.

con1 = ["fis1/output1" "fis2/input1"];
con2 = ["fis1/input1" "fis1/input2"];
con3 = ["fis1/input2" "fis2/input2"];

Finally, create a FIS tree using the specified FISs and connections.

fisT = fistree([fis1 fis2],[con1;con2;con3]);

Add an additional output to the FIS tree to access the output of fis1.

fisT.Outputs = ["fis1/output1";fisT.Outputs];

Generate input and output training data.

x = (0:0.1:10)';
y1 = sin(x)+cos(x);
y2 = y1./exp(x);
y = [y1 y2];

Tune the FIS tree parameters in two steps. First, learn the rules of the FIS tree using a global optimization method. For this example, use particle swarm.

options = tunefisOptions("Method","particleswarm","OptimizationType","learning");

This tuning step uses a small number of iterations to learn a rule base without overfitting the training data. The rule base provides an educated initial condition that the second step can use to optimize all the FIS tree parameters together. Set the maximum iteration number to 5, and learn the rule base.

options.MethodOptions.MaxIterations = 5;
rng("default")  % for reproducibility
fisTout1 = tunefis(fisT,[],x,y,options);
Best            Mean     Stall
Iteration     f-count            f(x)            f(x)    Iterations
0             100          0.6682          0.9395        0
1             200          0.6682           1.023        0
2             300          0.6652          0.9308        0
3             400          0.6259           0.958        0
4             500          0.6259           0.918        1
5             600          0.5969          0.9179        0
Optimization ended: number of iterations exceeded OPTIONS.MaxIterations.

Next, to tune all the FIS tree parameters at once, use a local optimization method. For this example, use pattern search. Local optimization is generally faster than global optimization and can produce better results when the input fuzzy system parameters are already consistent with the training data.

Use the patternsearch method for optimization. Set the number of iterations to 25.

options.Method = "patternsearch";
options.MethodOptions.MaxIterations = 25;

Use getTunableSettings to obtain input, output, and rule parameter settings from the FIS tree.

[in,out,rule] = getTunableSettings(fisTout1);

Tune the FIS tree parameters.

rng("default") % for reproducibility
fisTout2 = tunefis(fisTout1,[in;out;rule],x,y,options);
Iter     Func-count       f(x)      MeshSize     Method
0           1       0.596926             1
1           8       0.594989             2     Successful Poll
2          14       0.580893             4     Successful Poll
3          14       0.580893             2     Refine Mesh
4          36       0.580893             1     Refine Mesh
5          43       0.577757             2     Successful Poll
6          65       0.577757             1     Refine Mesh
7          79        0.52794             2     Successful Poll
8         102        0.52794             1     Refine Mesh
9         120       0.524443             2     Successful Poll
10         143       0.524443             1     Refine Mesh
11         170        0.52425             2     Successful Poll
12         193        0.52425             1     Refine Mesh
13         221       0.524205             2     Successful Poll
14         244       0.524205             1     Refine Mesh
15         329       0.508752             2     Successful Poll
16         352       0.508752             1     Refine Mesh
17         434       0.508233             2     Successful Poll
18         457       0.508233             1     Refine Mesh
19         546       0.506136             2     Successful Poll
20         569       0.506136             1     Refine Mesh
21         659       0.505982             2     Successful Poll
22         682       0.505982             1     Refine Mesh
23         795       0.505811             2     Successful Poll
24         818       0.505811             1     Refine Mesh
25         936       0.505811           0.5     Refine Mesh
26         950       0.504362             1     Successful Poll
Maximum number of iterations exceeded: increase options.MaxIterations.

The optimization cost reduces from 0.60 to 0.40 in the second step.

Alternatively, you can tune the specific fuzzy systems separately within a FIS tree. For this example, after learning the rule base of the FIS tree, separately tune the fis1 and fis2 parameters.

To obtain parameter settings of a FIS within the FIS tree, use getTunableSettings, specifying the FIS name. First, get the parameter settings for fis1.

[in,out,rule] = getTunableSettings(fisTout1,"FIS","fis1");

Tune the parameters of fis1.

rng("default")
fisTout2 = tunefis(fisTout1,[in;out;rule],x,y,options);
Iter     Func-count       f(x)      MeshSize     Method
0           1       0.596926             1
1          14       0.548082             2     Successful Poll
2          32       0.548082             1     Refine Mesh
3          48        0.54804             2     Successful Poll
4          66        0.54804             1     Refine Mesh
5         109       0.547504             2     Successful Poll
6         127       0.547504             1     Refine Mesh
7         207       0.547504           0.5     Refine Mesh
8         227       0.535549             1     Successful Poll
9         307       0.535549           0.5     Refine Mesh
10         334       0.458199             1     Successful Poll
11         414       0.458199           0.5     Refine Mesh
12         466       0.457367             1     Successful Poll
13         546       0.457367           0.5     Refine Mesh
14         622       0.449427             1     Successful Poll
15         702       0.449427           0.5     Refine Mesh
16         802        0.43661             1     Successful Poll
17         847       0.436555             2     Successful Poll
18         867       0.436555             1     Refine Mesh
19         947       0.436555           0.5     Refine Mesh
20        1046       0.430626             1     Successful Poll
21        1126       0.430626           0.5     Refine Mesh
22        1181       0.430585             1     Successful Poll
23        1261       0.430585           0.5     Refine Mesh
24        1383       0.430585          0.25     Refine Mesh
25        1386       0.379851           0.5     Successful Poll
26        1507       0.379851          0.25     Refine Mesh
Maximum number of iterations exceeded: increase options.MaxIterations.

In this case, the optimization cost is improved by tuning only the fis1 parameter values.

Next, obtain the parameter settings for fis2 and tune the fis2 parameters.

[in,out,rule] = getTunableSettings(fisTout2,"FIS","fis2");
rng("default")
fisTout3 = tunefis(fisTout2,[in;out;rule],x,y,options);
Iter     Func-count       f(x)      MeshSize     Method
0           1       0.379851             1
1           7        0.36047             2     Successful Poll
2          25        0.36047             1     Refine Mesh
3          36       0.360464             2     Successful Poll
4          54       0.360464             1     Refine Mesh
5          66       0.356372             2     Successful Poll
6          83       0.356372             1     Refine Mesh
7          97       0.356325             2     Successful Poll
8         114       0.356325             1     Refine Mesh
9         127       0.356322             2     Successful Poll
10         144       0.356322             1     Refine Mesh
11         195       0.352944             2     Successful Poll
12         212       0.352944             1     Refine Mesh
13         269       0.347813             2     Successful Poll
14         286       0.347813             1     Refine Mesh
15         350       0.344695             2     Successful Poll
16         367       0.344695             1     Refine Mesh
17         434       0.344464             2     Successful Poll
18         451       0.344464             1     Refine Mesh
19         493       0.344381             2     Successful Poll
20         510       0.344381             1     Refine Mesh
21         579       0.344051             2     Successful Poll
22         596       0.344051             1     Refine Mesh
23         616       0.344051             2     Successful Poll
24         633       0.344051             1     Refine Mesh
25         711       0.344051           0.5     Refine Mesh
26         789       0.343802             1     Successful Poll
Maximum number of iterations exceeded: increase options.MaxIterations.

The optimization cost is further reduced by tuning the fis2 parameter values. To avoid overfitting of individual FIS parameter values, you can further tune both the fis1 and fis2 parameters together.

[in,out,rule] = getTunableSettings(fisTout3);
rng("default")
fisTout4 = tunefis(fisTout3,[in;out;rule],x,y,options);
Iter     Func-count       f(x)      MeshSize     Method
0           1       0.343802             1
1          19       0.304764             2     Successful Poll
2          46       0.304764             1     Refine Mesh
3          88       0.304733             2     Successful Poll
4         115       0.304733             1     Refine Mesh
5         197       0.304375             2     Successful Poll
6         224       0.304375             1     Refine Mesh
7         313       0.252524             2     Successful Poll
8         340       0.252524             1     Refine Mesh
9         412       0.236664             2     Successful Poll
10         415       0.236587             4     Successful Poll
11         415       0.236587             2     Refine Mesh
12         429       0.236511             4     Successful Poll
13         429       0.236511             2     Refine Mesh
14         456       0.236511             1     Refine Mesh
15         471       0.232892             2     Successful Poll
16         497       0.232892             1     Refine Mesh
17         572       0.203613             2     Successful Poll
18         596        0.20355             4     Successful Poll
19         596        0.20355             2     Refine Mesh
20         621       0.203488             4     Successful Poll
21         621       0.203488             2     Refine Mesh
22         646       0.203488             1     Refine Mesh
23         721       0.201124             2     Successful Poll
24         724       0.201107             4     Successful Poll
25         724       0.201107             2     Refine Mesh
26         738       0.201091             4     Successful Poll
Maximum number of iterations exceeded: increase options.MaxIterations.

Overall, the optimization cost is smaller after using three tuning steps than after using only one.