Main Content

Generate Code for Fuzzy System Using MATLAB Coder

您可以使用MATLAB®CODER™生成用于评估模糊推理系统的代码。有关生成代码的更多信息,请参阅代码生成(MATLAB Coder)

要生成用于评估模糊系统的代码,您必须首先创建模糊推理系统(FIS)。有关更多信息,请参阅在命令行中构建模糊系统and使用模糊逻辑设计师构建模糊系统

尽管此示例为1型Mamdani模糊推理系统生成代码,但该工作流也适用于Sugeno和Type-2模糊系统。

使用MATLAB编码器生成代码不支持模糊FIS对象(金宝appmamfis,,,,sugfis,,,,mamfistype2,,,,sugfistype2)。To generate code for evaluating fuzzy systems, you must convert your fuzzy inference system objects into homogeneous structures using thegetFISCodeGenerationData功能。

将FIS数据嵌入生成代码中

You can embed the data for your fuzzy inference system within the generated code. Use this option if you do not want to change the FIS data after compilation.

首先,创建模糊系统,或从一个模糊系统中加载模糊系统。FISfile. For this example, load the fuzzy system fromtipper.fis

fisobject = readfis("tipper.fis");

To use this FIS for code generation, convert it to a homogeneous structure.

fis = getFisteNerationData(fisObject);

By default,getFISCodeGenerationData假设FIS对象是类型1系统。要为2型系统生成代码,必须使用getFISCodeGenerationData(fisObject,"type2")

创建一个用于评估模糊系统的函数FISfor a given input vectorX。Within this function, you can specify options for the评估函数使用评估Options

functiony = estaruefis1(fis,x)%#codegenopt = evalfisoptions('NumSamplePoints',51);y = evalfis(fis,x,opt);结尾

生成代码评估FIS1,,,,specifying that theFIS输入参数是恒定的。您可以为构建指定不同的目标,例如静态库,可执行文件或MEX文件。对于此示例,生成一个MEX文件。

Codegen(Codegen)('evaluefis1',,,,'-args',{coder.constant(fis),[0 0]},'-config:mex'
代码生成成功。

To verify 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. 使用相同的输入值评估原始FI评估。评估使用时评估,,,,use the same homogeneous FIS structure.

  3. Compare the evaluation results.

mexOutput1 = evaluatefis1_mex(fis,[7 9])
mexoutput1 = 21.0327
opt = evalfisoptions('NumSamplePoints',51);evalfisoutput= evalfis(fis,[7 9],opt)
evalfisoutput= 21.0327

The MEX file output matches the评估output.

Alternatively, you can embed the FIS data in the generated code by reading the FIS data from a file at code generation time. Specify a function for evaluating a fuzzy system for given input vector x. Within this function, read the FIS data from the filetipper.fis

functiony = evaluatefis2(x)%#codegenFIS= getFISCodeGenerationData('Tipper.fis'); opt = evalfisOptions('NumSamplePoints',51);y = evalfis(fis,x,opt);结尾

生成代码评估FIS2

Codegen(Codegen)(“ esturefis2',,,,'-args',{[0 0]},'-config:mex'
代码生成成功。

使用相同的输入值验证MEX文件的执行X。在这种情况下,您不必指定编译时使用的原始FIS结构。

mexOutput2 = evaluatefis2_mex([7 9])
mexoutput2 = 21.0327
evalfisoutput
evalfisoutput= 21.0327

生成用于在运行时加载FIS数据的代码

您可以生成用于评估FIS的代码。FIS在运行时指定的文件。在这种情况下,FIS数据未嵌入生成的代码中。指定用于评估指定文件中定义的模糊系统的函数文件名for a given input vectorX

functiony = estaruefis3(文件名,x)%#codegenfis = getFiscodeGenerationData(filename);opt = evalfisoptions('NumSamplePoints',51);y = evalfis(fis,x,opt);结尾

为此功能定义输入数据类型。

filename = coder.newtype('char',[1 inf],[false true]);x = coder.newtype('双倍的',[1 inf],[false true]);

生成代码评估FIS3

Codegen(Codegen)(“评估FIS3”,,,,'-args',,,,{fileName,x},'-config:mex'
代码生成成功。

使用相同的输入值验证MEX文件的执行X。在这种情况下,您指定了。FISfile.

mexoutput3 = estaruefis3_mex('Tipper.fis',[7 9])
mexoutput3 = 21.0327
evalfisoutput
evalfisoutput= 21.0327

Each time you run评估FIS3,它从文件中重新加载模糊系统。对于计算效率,您可以创建一个仅在指定新文件名时加载FIS的函数。

functiony = evaluatefis4(fileName,x)%#codegen执着的fisname fis如果Isempty(fisname)[fisname,fis] = loadfis(filename);Elseif~strcmp(fisName,fileName) [fisName,fis] = loadFIS(fileName);结尾opt = evalfisoptions('NumSamplePoints',51);y = evalfis(fis,x,opt);结尾function[fisName, fis] = loadFIS fisName = fileNa(文件名)me; fis = getFISCodeGenerationData(fisName);结尾

Generate code评估FIS4。此功能的输入数据类型与评估FIS3

Codegen(Codegen)(“评估FIS4”,,,,'-args',,,,{fileName,x},'-config:mex'
代码生成成功。

使用相同的输入值文件名来验证MEX文件的执行。

mexOutput4 = evaluatefis4_mex('Tipper.fis',[7 9])
mexoutput4 = 21.0327
evalfisoutput
evalfisoutput= 21.0327

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单身的。For example, generate code for评估FIS2using single-precision data.

Codegen(Codegen)(“ esturefis2',,,,'-args',{单([0 0])},'-config:mex'
代码生成成功。

验证MEX文件执行,以单精度输入值传递。

mexOutputsingle = evaluefuatefis2_mex(单个([7 9]))
mexoutputsingle =单身的21.0327
evalfisoutput
evalfisoutput= 21.0327

也可以看看

|

Related Topics