Main Content

Generate Code for Fuzzy System Using MATLAB Coder

You can generate code for evaluating a fuzzy systems using MATLAB® Coder™. For more information on generating code, see Code Generation (MATLAB Coder).

Create Fuzzy System

To generate code for evaluating fuzzy systems, you must first design a fuzzy inference system (FIS). For more information, see Build Fuzzy Systems at the Command Line, Build Fuzzy Systems Using Fuzzy Logic Designer, and Tuning Fuzzy Inference Systems.

You can generate code for evaluating the following types of fuzzy systems.

  • Type-1 Mamdani FIS implemented using a mamfis object

  • Type-1 Sugeno FIS implemented using a sugfis object

  • Type-2 Mamdani FIS implemented using a mamfistype2 object

  • Type-2 Sugeno FIS implemented using a sugfistype2 object

  • FIS trees implemented using fistree objects that contain any combination of mamfis, sugfis, mamfistype2, or sugfistype2 objects

In this example, you generate code for a type-2 Sugeno FIS and a FIS tree. The workflows also apply to the other types of fuzzy systems.

Load the final tuned type-2 Sugeno FIS from Predict Chaotic Time Series Using Type-2 FIS.

data1 = load("tunedfischaotictimeseriestype2.mat");
fis = data1.fisout3;

Load the final tuned FIS tree from Design Controller for Artificial Pancreas Using Fuzzy Logic.

data2 = load("fuzzyPancreasExampleData.mat");
fisTree = data2.fisTMFTuned;

Convert Fuzzy System into Homogeneous Structure

Generating code using MATLAB Coder does not support FIS objects or FIS tree objects directly. Instead, to generate code for evaluating a fuzzy system, you must convert your FIS or FIS tree into a homogeneous structure using the getFISCodeGenerationData function.

Convert the Sugeno FIS into a homogeneous structure.

fisData = getFISCodeGenerationData(fis);

Convert the FIS tree into a homogeneous structure.

fisTreeData = getFISCodeGenerationData(fisTree);

Generate Code with Embedded Fuzzy System

If you do not want to change the fuzzy system properties after compilation, you can embed the FIS or FIS tree data within the generated code. There are three methods for embedding the fuzzy system.

  • Embed FIS data from MATLAB workspace

  • Embed FIS data from MAT file

  • Embed FIS data from FIS file (*.fis).

Embed FIS from MATLAB Workspace

Create a function for evaluating a fuzzy system for input vector x. Within this function, you can specify options for the evalfis function using an evalfisOptions object.

function y = evaluatefis1(fis,x)
    %#codegen
    opt = evalfisOptions('NumSamplePoints',51);
    y = evalfis(fis,x,opt);
end

Generate code for evaluatefis1 using the type-2 Sugeno FIS stored in fisData. When generating code:

  • Specify that the fis input argument is constant, which embeds the FIS data within the generated code.

  • Specify the size of the input vector. In this case, the FIS has four input variables. Therefore, specify the input vector as a vector of zeros with four elements.

  • Specify the target for your build, such as a static library, an executable, or a MEX file. For this example, generate a MEX file.

codegen('evaluatefis1','-args',{coder.Constant(fisData),[0 0 0 0]},'-config:mex')
Code generation successful.

To validate the execution of the MEX file:

  1. Evaluate the MEX file for one or more input values. When you call the MEX file, specify the same FIS structure that you used at compile time.

  2. Evaluate the original FIS for the same input values using evaluatefis1. When evaluating the FIS using evaluatefis1, use the same homogeneous FIS structure.

  3. Compare the evaluation results.

mexOutput1 = evaluatefis1_mex(fisData,[0.3 0.1 0.8 0.2])
mexOutput1 = 
0.6913
evalfisOutput1 = evaluatefis1(fisData,[0.3 0.1 0.8 0.2])
evalfisOutput1 = 
0.6913

Embed FIS from MAT File

If your FIS or FIS tree is stored in a MAT file, you can embed the FIS data in the generated code by loading the FIS data from within the evaluation function.

Save the FIS tree data to a MAT file.

save("FISTreeData.mat","fisTreeData","-mat");

Specify a function for evaluating the FIS tree for input vector x. This function assumes that the FIS tree is stored in the MAT file as variable fisTreeData.

function y = evaluatefis2(x)
    %#codegen
    s = coder.load('FISTreeData.mat');
    opt = evalfisOptions('NumSamplePoints',51);
    y = evalfis(s.fisTreeData,x,opt);
end

Generate code for evaluatefis2.

codegen('evaluatefis2','-args',{[0 0 0]},'-config:mex')
Code generation successful.

Validate the execution of the MEX file.

mexOutput2 = evaluatefis2_mex([105 0.25 0.001])
mexOutput2 = 
0.8634
evalfisOutput2 = evaluatefis2([105 0.25 0.001])
evalfisOutput2 = 
0.8634

Embed FIS from FIS File

If your type-1 or type-2 FIS is stored in a FIS file, you can embed the FIS data in the generated code by reading the FIS data from within the evaluation function. This workflow is not supported for FIS trees.

Save the type-2 FIS to a file.

writeFIS(fis,'predictTimeSeries.fis')

Specify a function for evaluating a fuzzy system for input vector x. Within this function, read the FIS data from the file predictType2.fis. Since the stored FIS is a type-2 system, you must specify the FIS type when calling getFISCodeGenerationData. If your stored FIS is a type-1 system, you do not have to specify the FIS type.

function y = evaluatefis3(x)
    %#codegen
    fisData = getFISCodeGenerationData('predictTimeSeries.fis','FuzzySetType','type2');
    opt = evalfisOptions('NumSamplePoints',51);
    y = evalfis(fisData,x,opt);
end

Generate code for evaluatefis3.

codegen('evaluatefis3','-args',{[0 0 0 0]},'-config:mex')
Code generation successful.

Validate the execution of the MEX file.

mexOutput3 = evaluatefis3_mex([0.3 0.1 0.8 0.2])
mexOutput3 = 
0.6913
evalfisOutput3 = evaluatefis3([0.3 0.1 0.8 0.2])
evalfisOutput3 = 
0.6913

Generate Code That Loads Fuzzy System at Run Time

To change the FIS properties after compilation, you can generate code for evaluating a FIS that is read from a FIS file specified at run time. In this case, the FIS data is not embedded in the generated code. Modifying the fuzzy system properties after compilation using a FIS file is not supported for FIS trees.

Specify a function for evaluating the fuzzy system defined in a specified FIS file for input vector x. This function uses the same input data types as evaluatefis4.

function y = evaluatefis4(fisFileName,x)
    %#codegen
    fis = getFISCodeGenerationData(fisFileName,'FuzzySetType','type2');
    opt = evalfisOptions('NumSamplePoints',51);
    y = evalfis(fis,x,opt);
end

Define input data types for this function.

fileName = coder.newtype('char',[1 Inf],[false true]);
x = coder.newtype('double',[1 Inf],[false true]);

Generate code for evaluatefis4.

codegen('evaluatefis4','-args',{fileName,x},'-config:mex')
Code generation successful.

Validate the execution of the MEX file.

mexOutput4 = evaluatefis4_mex('predictTimeSeries.fis',[0.3 0.1 0.8 0.2])
mexOutput4 = 
0.6913
evalfisOutput4 = evaluatefis4('predictTimeSeries.fis',[0.3 0.1 0.8 0.2])
evalfisOutput4 = 
0.6913

Each time you run evaluatefis4, the function reloads the fuzzy system from the specified file. For computational efficiency, you can create a function that does not reload a previously loaded FIS. For example, the evaluatefis5 function loads a FIS from a FIS file only when a new file name is specified.

function y = evaluatefis5(fileName,x)
    %#codegen
    
    persistent fisName fis
    if isempty(fisName)
        [fisName,fis] = loadFIS(fileName);
    elseif ~strcmp(fisName,fileName)
        [fisName,fis] = loadFIS(fileName);
    end

    opt = evalfisOptions('NumSamplePoints',51);
    y = evalfis(fis,x,opt);
end

function [fisName,fis] = loadFIS(fileName)
    fisName = fileName;
    fis = getFISCodeGenerationData(fisName,'FuzzySetType','type2');
end

Generate code for evaluatefis5. The input data types for this function are the same as for evaluatefis2.

codegen('evaluatefis5','-args',{fileName,x},'-config:mex')
Code generation successful.

Execute the MEX file using the stored type-2 FIS.

mexOutput5 = evaluatefis5_mex('predictTimeSeries.fis',[0.3 0.1 0.8 0.2])
mexOutput5 = 
0.6913

You can use this function to evaluate another FIS with the same number of inputs. For example, save an intermediate version of the FIS from Predict Chaotic Time Series Using Type-2 FIS to predictTimeSeries2.fis, and evaluate the FIS using the MEX file for the same input values.

writeFIS(data1.fisout2,'predictTimeSeries2.fis')
mexOutput5_2 = evaluatefis5_mex('predictTimeSeries2.fis',[0.3 0.1 0.8 0.2])
mexOutput5_2 = 
0.7934

Generate Code for Single-Precision Data

The preceding examples generated code for double-precision data. To generate code for single-precision data, specify the data type of the input values as single. You can use single-precision data when evaluating FIS objects and FIS tree objects. For example, generate code for evaluatefis2 using single-precision data.

codegen('evaluatefis2','-args',{single([0 0 0])},'-config:mex')
Code generation successful.

Execute the MEX file, passing in single-precision input values.

mexOutputSingle = evaluatefis2_mex(single([105 0.25 0.001]))
mexOutputSingle = 
0.8634

See Also

| |

Related Topics