%% Tune Fuzzy Trees
% You can tune the parameters of a FIS tree using a similar two-step process as shown in Tune Fuzzy Inference Systems.
% Learn and tune the rules of the FISs in the tree.
% Learn the MF parameters of the FISs in the tree.
% Create a FIS tree to model for \frac{\sin \left(x\right)+\cos \left(x\right)}{\exp \left(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 due to computationally efficient defuzzification method. Add two inputs, both with range the [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');
fis1 = addInput(fis1,[0 10],'NumMFs',3,'MFType','gaussmf');
fis1 = addInput(fis1,[0 10],'NumMFs',3,'MFType','gaussmf');

% 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 \sin \left(x\right)+\cos \left(x\right).

fis1 = addOutput(fis1,[-1.5 1.5],'NumMFs',9);

% 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');
fis2 = addInput(fis2,[-1.5 1.5],'NumMFs',3,'MFType','gaussmf');
fis2 = addInput(fis2,[0 10],'NumMFs',3,'MFType','gaussmf');

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

fis2 = addOutput(fis2,[0 1],'NumMFs',9);

% Connect the inputs and the outputs as shown in the diagram. Output 1 of fis1 connects to input 1 of fis2, inputs 1 and 2 of fis1 connect to each other, and input 2 of fis1 connects to input 2 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 (particle swarm for this example).

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 for the second step 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);

%
% Next, to tune all the FIS tree parameters, use a local optimization method (pattern search for this example). 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);

% The optimization cost reduces from 0.59 to 0.39 in the second step.
%
% Alternatively, you can tune the specific fuzzy systems within a FIS tree. For this example, after learning the rule base of the FIS tree, separately tune 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);

% In this case, the optimization cost is improved by tuning only 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);

% 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 fis1 and fis2 parameters together.

[in,out,rule] = getTunableSettings(fisTout3);
rng('default') 
fisTout4 = tunefis(fisTout3,[in;out;rule],x,y,options);

% Overall, the optimization cost is smaller after using the three tuning steps.
%
% Copyright 2019 The MathWorks, Inc.