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");
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_learned = constructFuzzyInferenceSystem()
%% Returns a fuzzy inference system.

% Construct FIS
fis_learned = mamfis(Name="mpgInitialFIS_learned");
% Input 1
fis_learned = addInput(fis_learned,[3 8],Name="Cylinder");
fis_learned = addMF(fis_learned,"Cylinder","trimf",[-1.16667 3 7.16667], ...
    Name="mf1",VariableType="input");
fis_learned = addMF(fis_learned,"Cylinder","trimf",[3.83333 8 12.1667], ...
    Name="mf2",VariableType="input");
% Input 2
fis_learned = addInput(fis_learned,[68 455],Name="Disp");
...
% Input 3
fis_learned = addInput(fis_learned,[46 230],Name="Power");
...
% Input 4
fis_learned = addInput(fis_learned,[1613 5140],Name="Weight");
...
% Input 5
fis_learned = addInput(fis_learned,[8 24.8],Name="Acceler");
...
% Input 6
fis_learned = addInput(fis_learned,[70 82],Name="Year");
...
% Output 1
fis_learned = addOutput(fis_learned,[9 46.6],Name="MPG");
fis_learned = addMF(fis_learned,"MPG","trimf",[8.50265 9 9.49735], ...
    Name="mf1",VariableType="output");
...
fis_learned = addMF(fis_learned,"MPG","trimf",[46.1026 46.6 47.0974], ...
    Name="mf64",VariableType="output");
% Rules
fis_learned = addRule(fis_learned,[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, fisOut1 to the parameter-tuning stage. Configure each stage using their respective tuning options and tunable settings local functions.

function [fisOut1,fisOut2,optOut1,optOut2] = tuneFuzzyInferenceSystem(x,y)
%% Tunes a fuzzy inference system by 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();
[fisOut1,optOut1] = tunefis(fis,[ins;outs;rules],x,y,options1);

% Tune MF and rule parameters
[ins,outs,rules] = constructTunableSettingsTuning(fisOut1);
options2 = constructTuningOptions();
[fisOut2,optOut2] = tunefis(fisOut1,[ins;outs;rules],x,y,options2);
end

function mpgInitialFIS = constructFuzzyInferenceSystem()
...
end

function [ins,outs,rules] = constructTunableSettingsLearning(fis)
...
end

function [ins,outs,rules] = constructTunableSettingsTuning(fis)
...
end

function options = constructLearningOptions()
...
end

function options = constructTuningOptions()
...
end

See Also

Related Topics