%% Tune Fuzzy Rules and Membership Function Parameters
% When tuning 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 Tune 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)

% 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

% For each parameter value of an input/output MF, you can specify whether it is available 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]. Make MF 1 of input 1 nontunable.

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

%  Similarly, make the first parameter of MF 2 of input 1 nontunable.

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

% Set minimum ranges for the second and third parameters of MF 3 of input 1 to 0.

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

% Set maximum ranges for second and third parameters of MF 3 of input 1 to 15.

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

% The default minimum and maximum range values of tunable MF parameters are set to corresponding input/output ranges in the tuning process.
% Finally, make the output nontunable.

out = setTunable(out,false);

% Specify input and output training data. For this example, generate training data using the following function.
% y=\left|\frac{\sin \left(2x\right)}{e^{x/5} }\right|

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.

options = tunefisOptions("Method","ga");

% Specify a maximum of five generations for optimization.

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.
% By default, tunefis uses 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);

% 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.tuningOutputs

% You can optionally tune fis using either the just the input or output parameter settings. Since the output parameter settings are set 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);
optimout
optimout.tuningOutputs

%
%% 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)

% Each rule parameter setting includes the FIS name, index of the rule in the FIS, and parameter settings for the rule antecedent and consequent (the rule clauses).
% The parameter settings for a rule clause include three options:
% Whether the input/output MF indices are available for tuning. By default, clause parameters are free for tuning.
% Whether the clause allows use of NOT logic, in other words, whether it allows negative MF indices. By default, rules do not allow NOT logic.
% Whether the clause allows the absence of input/output variables, in other words, if it allows zero MF indices. By default, the absence of a variable is allowed.

rule(1).Antecedent(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);

% 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);

%
%% 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 three.

options.NumMaxRules = 3;

% The size of the tuned rule base may 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);

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

fisout.Rules

% 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);
fisout.Rules

% You can also tune the antecedents and consequents of existing rules and learn new rules. To do so, obtain the rule tunable 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);
fisout.Rules

%
%% 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);

% For a large fuzzy system, tuning all FIS parameters in the same tuning process can take several iterations to obtain the expected results. To improve the tuning time, you can tune parameters using the following two steps.
% Tune or learn rule parameters only.
% Tune both MF and rule parameters.
% The learning and tuning rules is less computationally expensive 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
% To generate an initial rule base for tuning, you can generate a FIS from your training data using the genfis function. 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);

% You can increase the number of iterations to further optimize the cost.
% Copyright 2019 The MathWorks, Inc.