Main Content

Generate MATLAB Code for Tuning Fuzzy Systems

Since R2024a

Once you interactively tune a fuzzy system using Fuzzy Logic Designer, you can generate MATLAB® code to programmatically tune that system. You can then use or modify the generated code for your applications.

Often, tuning a FIS is a two-stage process: Learn the rules for the FIS, then tune the membership function (MF) and rule parameters. For this example, you generate code for the two stages separately and combine the generated code for the full two-stage tuning process.

You can also generate code to simultaneously learn rules and tune MF parameters. Though, doing so can significantly increase the time required for tuning.

For this example, you generate code for tuning the FIS in Tune Fuzzy Inference System Using Fuzzy Logic Designer. For details on how to load the initial FIS structure and training data, see Load Example Data.

Create FIS Structure

To tune a FIS, first define the initial FIS structure. For this example, open the app and import the initial FIS structure.

fuzzyLogicDesigner(mpgInitialFIS)

Fuzzy Logic Designer app showing FIS tree plot for specified system.

Import Training Data

To select input and output data for tuning, on the Tuning tab:

  • In the Input Data drop-down list, under Workspace Data Sets, select trnX.

  • In the Output Data drop-down list, under Workspace Data Sets, select trnY.

Configure Options for Learning Rules

To learn the rules for your FIS, first specify the tuning options. On the Tuning tab, click Tuning Options.

In the Tuning Options dialog box, in the Optimization Type section, select Learning.

Configure the remaining tuning options as specified in Learn Rules. For more information on configuring tuning options, see Configure Tuning Options in Fuzzy Logic Designer.

To only learn rules without modifying the MF parameters, disable the input and output tunable parameter settings.

  • In the System Browser, select the FIS design.

  • In the Tunable Parameters pane, click Tune None for both the input and output tables.

Generate Code for Learning Rules

To generate MATLAB code for tuning this system, on the Tuning tab, under Export, click Generate Tuning Function.

The app exports the training data as a MAT file with the name tunefisData_<random_string>.mat

The MATLAB Editor opens a generated script with the following code for creating and learning the rules for the current active FIS. For clarity, some of the input and output MF configuration code is omitted.

function [fisOutput,optOutputs] = tuneFuzzyInferenceSystem(x,y)
%% Tunes a fuzzy inference system.
% Training data and Tunable parameter settings are closely coupled with the
% fuzzy system. As a result, updating the fuzzy system may cause unexpected
% errors during the tuning process.
if nargin==0
    data = load("tunefisData_tp3b05c3ba_42aa_49c7_a453_d79dabd2bbde.mat","x","y");
    x = data.x;
    y = data.y;
end

fis = constructFuzzyInferenceSystem();
[ins,outs,rules] = constructTunableSettings(fis);
options = constructTuningOptions();

rng("default")
[fisOutput,optOutputs] = tunefis(fis,[ins;outs;rules],x,y,options);
end

function mpgInitialFIS = constructFuzzyInferenceSystem()
%% Returns a fuzzy inference system.

% Construct FIS
mpgInitialFIS = mamfis(Name="mpgInitialFIS");
% Input 1
mpgInitialFIS = addInput(mpgInitialFIS,[3 8],Name="Cylinder");
mpgInitialFIS = addMF(mpgInitialFIS,"Cylinder","trimf",[-1.16667 3 7.16667], ...
    Name="mf1",VariableType="input");
mpgInitialFIS = addMF(mpgInitialFIS,"Cylinder","trimf",[3.83333 8 12.1667], ...
    Name="mf2",VariableType="input");
% Input 2
mpgInitialFIS = addInput(mpgInitialFIS,[68 455],Name="Disp");
...
% Input 3
mpgInitialFIS = addInput(mpgInitialFIS,[46 230],Name="Power");
...
% Input 4
mpgInitialFIS = addInput(mpgInitialFIS,[1613 5140],Name="Weight");
...
% Input 5
mpgInitialFIS = addInput(mpgInitialFIS,[8 24.8],Name="Acceler");
...
% Input 6
mpgInitialFIS = addInput(mpgInitialFIS,[70 82],Name="Year");
...
% Output 1
mpgInitialFIS = addOutput(mpgInitialFIS,[9 46.6],Name="MPG");
mpgInitialFIS = addMF(mpgInitialFIS,"MPG","trimf",[8.50265 9 9.49735], ...
    Name="mf1",VariableType="output");
...
mpgInitialFIS = addMF(mpgInitialFIS,"MPG","trimf",[46.1026 46.6 47.0974], ...
    Name="mf64",VariableType="output");
end

function [ins,outs,rules] = constructTunableSettings(fis)
%% Returns tunable parameter settings of the specified fuzzy system.

[ins,outs,rules] = getTunableSettings(fis);
ins = setTunable(ins,false);
outs = setTunable(outs,false);
end

function options = constructTuningOptions()
%% Returns tuning options.

options = tunefisOptions(Method="particleswarm", ...
    NumMaxRules=64, ...
    OptimizationType="learning", ...
    UseParallel=1);
options.MethodOptions.MaxIterations = 20;
end

This code defines the following local functions:

  • constructFuzzyInferenceSystem — Create the initial FIS structure.

  • constructTunableSettings — Define the rule and MF tunable settings as they are defined in the app. For this generated code, the function sets the input and output MF parameters as nontunable.

  • constructTuningOptions — Define the tuning options as they are defined in the app.

The tuneFuzzyInferenceSystem function:

  1. Loads the training data from the saved MAT file.

  2. Configures the tuning using the local functions.

  3. Tunes the FIS using tunefis.

Before generating code for tuning the FIS parameters, run the rule-learning process in the app. For more information on running the tuning process, see Learn Rules.

Configure Options for Tuning FIS Parameters

To tune the MF and rule parameters for your FIS, first specify the tuning options. In the Tuning Options dialog box, in the Optimization Type section, select Tuning. Configure the remaining tuning options as specified in Tune MF and Rule Parameters.

You can then select the rule and MF parameters for tuning, as described in Select Rules and Parameters to Tune in Fuzzy Logic Designer. For this example, select all MF and rule parameters for tuning.

The tunable parameter settings are closely coupled with the structure of the fuzzy system. Therefore, modifying the FIS structure can cause unexpected errors.

Generate Code for Tuning FIS Parameters

On the Tuning tab, under Export, click Generate Tuning Function.

The MATLAB Editor opens a generated script with the following code for creating and tuning the current active FIS. For clarity, some of the input MF, output MF, and rule configuration code is omitted.

function [fisOutput,optOutputs] = tuneFuzzyInferenceSystem(x,y)
%% Tunes a fuzzy inference system.
% Training data and Tunable parameter settings are closely coupled with the
% fuzzy system. As a result, updating the fuzzy system may cause unexpected
% errors during the tuning process.
if nargin==0
    data = load("tunefisData_tpebacce13_2855_47ce_9179_a168bbb3b004.mat","x","y");
    x = data.x;
    y = data.y;
end

fis = constructFuzzyInferenceSystem();
[ins,outs,rules] = constructTunableSettings(fis);
options = constructTuningOptions();

rng("default")
[fisOutput,optOutputs] = tunefis(fis,[ins;outs;rules],x,y,options);
end

function mpgInitialFIS_tuned = constructFuzzyInferenceSystem()
%% Returns a fuzzy inference system.

% Construct FIS
mpgInitialFIS_tuned = mamfis(Name="mpgInitialFIS_tuned");
% Input 1
mpgInitialFIS = addInput(mpgInitialFIS,[3 8],Name="Cylinder");
mpgInitialFIS = addMF(mpgInitialFIS,"Cylinder","trimf",[-1.16667 3 7.16667], ...
    Name="mf1",VariableType="input");
mpgInitialFIS = addMF(mpgInitialFIS,"Cylinder","trimf",[3.83333 8 12.1667], ...
    Name="mf2",VariableType="input");
% Input 2
mpgInitialFIS = addInput(mpgInitialFIS,[68 455],Name="Disp");
...
% Input 3
mpgInitialFIS = addInput(mpgInitialFIS,[46 230],Name="Power");
...
% Input 4
mpgInitialFIS = addInput(mpgInitialFIS,[1613 5140],Name="Weight");
...
% Input 5
mpgInitialFIS = addInput(mpgInitialFIS,[8 24.8],Name="Acceler");
...
% Input 6
mpgInitialFIS = addInput(mpgInitialFIS,[70 82],Name="Year");
...
% Output 1
mpgInitialFIS_tuned = addOutput(mpgInitialFIS_tuned,[9 46.6],Name="MPG");
mpgInitialFIS_tuned = addMF(mpgInitialFIS_tuned,"MPG","trimf",[8.50265 9 9.49735], ...
    Name="mf1",VariableType="output");
...
mpgInitialFIS_tuned = addMF(mpgInitialFIS_tuned,"MPG","trimf",[46.1026 46.6 47.0974], ...
    Name="mf64",VariableType="output");
% Rules
mpgInitialFIS_tuned = addRule(mpgInitialFIS_tuned,[2 2 2 2 0 2 5 1 1; ...
    1 0 2 2 2 1 63 1 1; ...
...    
    2 2 1 2 1 1 28 1 1]);
end

function [ins,outs,rules] = constructTunableSettings(fis)
%% Returns tunable parameter settings of the specified fuzzy system.

[ins,outs,rules] = getTunableSettings(fis);
end

function options = constructTuningOptions()
%% Returns tuning options.
options = tunefisOptions(Method="patternsearch", ...
    NumMaxRules=64);
options.MethodOptions.UseCompletePoll = 1;
options.MethodOptions.MaxIterations = 60;
end

This code has a similar structure to the rule-learning code. The primary differences are:

  • The constructFuzzyInferenceSystem local function, adds rules to the FIS structure. These are the rules learned during the first tuning process.

  • The constructTuningOptions local function uses different tuning options.

  • All the tunable settings returned by the constructTunableSettings local function are set as tunable.

Combine Learning and Tuning Code

You can modify the generated code for your applications. As an example, create a single tuning function that performs both stages of the tuning process.

  1. Use the constructFuzzyInferenceSystem local function from the rule-learning stage.

  2. Use the constructTuningOptions local function from both stages:

    • For the rule-learning stage, rename constructTuningOptions to constructLearningOptions.

    • For the parameter-tuning stage, use constructTuningOptions without changes.

  3. Use the constructTunableSettings local function from both stages:

    • For the rule-learning stage, rename constructTunableSettings to constructTunableSettingsLearning.

    • For the parameter-tuning stage, rename constructTunableSettings to constructTunableSettingsTuning.

  4. Perform the rule-learning stage and pass the resulting FIS, fisOutput1 to the parameter-tuning stage. Configure each stage using their respective tuning options and tunable settings local functions.

function [fisOutput1,fisOutput2,optsOutput1,optOutputs2] = tuneFuzzyInferenceSystem(x,y)
%% Tunes a fuzzy inference system by first learning rules and then tuning parameters.
% Training data and Tunable parameter settings are closely coupled with the
% fuzzy system. As a result, updating the fuzzy system may cause unexpected
% errors during the tuning process.
if nargin==0
    data = load("tunefisData_tp3b05c3ba_42aa_49c7_a453_d79dabd2bbde.mat","x","y");
    x = data.x;
    y = data.y;
end

rng("default")

% Construct FIS structure.
fis = constructFuzzyInferenceSystem();

% Learn rules.
[ins,outs,rules] = constructTunableSettingsLearning(fis);
options1 = constructLearningOptions();
[fisOutput1,optOutputs1] = tunefis(fis,[ins;outs;rules],x,y,options1);

% Tune MF and rule parameters
[ins,outs,rules] = constructTunableSettingsTuning(fisOutput);
options2 = constructTuningOptions();
[fisOutput2,optOutputs2] = tunefis(fisOutput1,[ins;outs;rules],x,y,options2);
end

function mpgInitialFIS = constructFuzzyInferenceSystem()
%% Returns a fuzzy inference system.

% Construct FIS
mpgInitialFIS = mamfis(Name="mpgInitialFIS");
% Input 1
mpgInitialFIS = addInput(mpgInitialFIS,[3 8],Name="Cylinder");
mpgInitialFIS = addMF(mpgInitialFIS,"Cylinder","trimf",[-1.16667 3 7.16667], ...
    Name="mf1",VariableType="input");
mpgInitialFIS = addMF(mpgInitialFIS,"Cylinder","trimf",[3.83333 8 12.1667], ...
    Name="mf2",VariableType="input");
% Input 2
mpgInitialFIS = addInput(mpgInitialFIS,[68 455],Name="Disp");
...
% Input 3
mpgInitialFIS = addInput(mpgInitialFIS,[46 230],Name="Power");
...
% Input 4
mpgInitialFIS = addInput(mpgInitialFIS,[1613 5140],Name="Weight");
...
% Input 5
mpgInitialFIS = addInput(mpgInitialFIS,[8 24.8],Name="Acceler");
...
% Input 6
mpgInitialFIS = addInput(mpgInitialFIS,[70 82],Name="Year");
...
% Output 1
mpgInitialFIS = addOutput(mpgInitialFIS,[9 46.6],Name="MPG");
mpgInitialFIS = addMF(mpgInitialFIS,"MPG","trimf",[8.50265 9 9.49735], ...
    Name="mf1",VariableType="output");
...
mpgInitialFIS = addMF(mpgInitialFIS,"MPG","trimf",[46.1026 46.6 47.0974], ...
    Name="mf64",VariableType="output");
end

function [ins,outs,rules] = constructTunableSettingsLearning(fis)
%% Returns tunable parameter settings for the rule-learning stage.

[ins,outs,rules] = getTunableSettings(fis);
ins = setTunable(ins,false);
outs = setTunable(outs,false);
end

function [ins,outs,rules] = constructTunableSettingsTuning(fis)
%% Returns tunable parameter settings for the parameter-tuning stage.

[ins,outs,rules] = getTunableSettings(fis);
end

function options = constructLearningOptions()
%% Returns tuning options for the rule-learning stage.

options = tunefisOptions(Method="particleswarm", ...
    NumMaxRules=64, ...
    OptimizationType="learning", ...
    UseParallel=1);
options.MethodOptions.MaxIterations = 20;
end

function options = constructTuningOptions()
%% Returns tuning options for the parameter-tuning stage.
options = tunefisOptions(Method="patternsearch", ...
    NumMaxRules=64);
options.MethodOptions.UseCompletePoll = 1;
options.MethodOptions.MaxIterations = 60;
end

See Also

Related Topics