Build Fuzzy Systems Using Fuzzy Logic Designer
This example shows how to interactively create a type-1 Mamdani fuzzy inference system (FIS) using the Fuzzy Logic Designer app.
Before R2022b: Interactively create a type-1 FIS using the previous version of the Fuzzy Logic Designer app as shown in this example (R2022a).
For this example, you create a FIS to solve the tipping problem defined in Fuzzy vs. Nonfuzzy Logic. The tipping behavior is defined using the following three rules.
If the service is poor or the food is rancid, then the tip is cheap.
If the service is good, then the tip is average.
If the service is excellent or the food is delicious, then the tip is generous.
While this example creates a type-1 Mamdani FIS, the general methods used apply to creating type-2 and Sugeno systems as well. For more information on the different types of fuzzy systems, see Mamdani and Sugeno Fuzzy Inference Systems and Type-2 Fuzzy Inference Systems.
You can also interactively create a FIS tree using Fuzzy Logic Designer. For an example, see Build FIS Tree Using Fuzzy Logic Designer.
For more information on building a FIS at the command line, see Build Fuzzy Systems at the Command Line.
For this example, you build a tipper FIS from scratch. Alternatively, you can load the
system from the tipper.fis
file. To do so, use the following
command.
fuzzyLogicDesigner("tipper.fis")
Create FIS Structure
To build the FIS yourself, first open the app. On the Apps tab, under Control System Design and Analysis, click the app icon.
The app opens a Getting Started dialog box. For more information on the options for creating your initial FIS structure, see Get Started Using Fuzzy Logic Designer.
For this example, since you are creating a type-1 Mamdani system with the two inputs and one output, you can use a built in template as a starting point. To do so:
Since you define the rules for your FIS manually, clear the Generate rules automatically check box.
Under Template Fuzzy Inference Systems, select Mamdani Type-1.
The app creates the template FIS and loads it in the app.
In the Property Editor pane, you can specify the name of your FIS along with its inference methods. For this example, use the default inference methods. For more information on the available inference methods, see Fuzzy Inference Process and Foundations of Fuzzy Logic.
In the Name field, enter the name tipper
.
Define Input Variables
For this example, the template FIS already has two input variables. In the System Browser pane, click Inputs. The app displays the properties of the input variables in the Property Editor.
In the Property Editor, define the following properties for each input variable.
Name — Input variable name. For this example, name the first input
service
and the second inputfood
.Range — Input variable range. For this example, both inputs are rated on a scale from 0 through 10. Therefore, specify each variable range as
[0 10]
You can add more input variables if they are required for your application. To do so, on the Design tab, in the Add Components gallery, click Input.
Define Output Variables
For this example, the template FIS already has one output variable. In the System Browser pane, click Outputs. The app displays the properties of the output variable in the Property Editor.
In the Property Editor, define the following properties for the output variable.
Name — Output variable name. For this example, name the output variable
tip
.Range — Output variable range. For this example, specify the output variable range as
[0 30]
You can add output variables if they are required for your application. To do so, in the Add Components gallery, click Output.
Define Membership Functions
To add or remove membership functions (MFs) for a given variable, select the variable in the System Browser or click the variable in the Fuzzy Inference System document. For example, select the food input variable. In the Property Editor, the app shows the input variable and membership function properties.
By default, the template adds three membership functions for each variable. For this example, the service input variable and the tip output variable both require three membership functions.
However, the food variable requires two membership functions. To remove one of the default membership functions, in the System Browser, under food, right-click the membership function and select Delete.
You can add membership functions to a given variable if they are required for your application. To do so, in the Add Components gallery, click MF.
To edit membership function properties, in the System Browser, select the membership function or the corresponding variable. Then, in the Property Editor, specify the following membership function properties.
Name — Membership function name
Type — Membership function type
Parameters — Membership function parameters
To view a plot of the membership functions for a given variable, select the variable in the System Browser and open the Membership Function Editor. The following figure shows the membership functions for the service input variable.
Configure the membership functions for the service input using the properties shown in the figure.
Similarly, configure the properties for the food input variable.
Finally, define the membership functions for the tip output.
For more information on defining membership functions, see Define Membership Functions Using Fuzzy Logic Designer.
Define Rule Base
Once you define the variables and membership functions for your FIS, you can define the if-then rule base for the system.
For this example, use the following rules.
If the service is poor or the food is rancid, then the tip is cheap.
If the service is good, then the tip is average.
If the service is excellent or the food is delicious, then the tip is generous.
Open the Rule Editor document.
To add a rule, click . The app adds a rule to the rule table and shows the rule properties in the Property Editor.
Modify the rule properties for the first rule: If the service is poor or the food is rancid, then the tip is cheap.
Set the Connection parameter to Or.
For the service input, select
poor
as the linguistic term.For the food input, select
rancid
as the linguistic term.For the tip output, select
cheap
as the linguistic term.
Similarly, add the next rule: If the service is good, then the tip is average. For this rule:
For the service input, select
good
as the linguistic term.For the tip output, select
average
as the linguistic term.Since this rule has only the service input in the antecedent, ignore the food input by setting its linguistic term to
none
.
Finally, add the third rule: If the service is excellent or the food is delicious, then the tip is generous. For this rule:
Set the Connection parameter to Or.
For the service input, select
excellent
as the linguistic term.For the food input, select
delicious
as the linguistic term.For the tip output, select
generous
as the linguistic term.
View the final rule base in the Rule Editor.
For more information on specifying rules for your FIS, see Define Fuzzy Rules Using Fuzzy Logic Designer.
Analyze Design
Once you define your rule base, you can analyze your design using the following documents.
Rule Inference — Specify input values and view the inference diagram and resulting output value.
Control Surface — Plot the output values for all combinations of two input variables.
System Validation — Compare the outputs from each FIS design with the corresponding output value from the testing data. (since R2023a)
Error Distribution — For a given FIS design and testing data, view the output error for different combinations of inputs. (since R2023a)
For more information on FIS analysis methods, see Analyze Fuzzy System Using Fuzzy Logic Designer.
For this example, analyze the FIS using the Rule Inference and Control Surface documents. To open either document, on the Design tab, click the corresponding icon in the Simulation gallery.
Rule Inference
In the Rule Inference document, you specify values for your input variables, inspect the inference process, and view the resulting output value.
For example, consider the case where the service was quite poor (1/10) but the food was very good (9/10). Specify these input values using the Input values parameter. Alternatively, you can drag the input bars in the inference diagram.
The low service rating causes the first rule to fire with a high firing strength and the high food rating causes the third rule to fire with a high firing strength. When the resulting output membership functions are aggregated and defuzzified, the final tip value is 15.2%.
For more information on fuzzy inference, see Fuzzy Inference Process.
Control Surface
The Control Surface document shows the FIS output value for all possible combinations of two input variables. For this example, the plot shows the tip value for all service and food rating combinations. Generally, the tip is low (5%) when the food and service inputs are both near 0 and high (25%) when the food and service inputs are both near 10.
Store and Modify Designs
You can store multiple FIS designs in the app, which allows you to explore possible design options. All stored designs must have the same number of inputs and the same number of outputs.
A common design option to explore is converting a Mamdani system into a Sugeno system. In general, Sugeno systems are more computationally efficient, which is an important consideration when deploying a FIS.
To convert the FIS to a Sugeno system, on the Design tab, select Mamdani to Sugeno.
In the Design Browser pane, the app creates a Sugeno version of the
FIS called tipper_1
.
To make this design active, select the design in the table and click Set Active Design.
You can also manually store a copy of your current FIS in the Design Browser. To do so, on the Design tab, select Store Current Design.
You can then analyze the alternative design in the same manner as the first design.
The plot for the Sugeno system in the Control Surface document looks similar to the plot for the Mamdani system.
Using the Rule Inference document, you can see that, for the same service and food ratings, the Sugeno system generates a 16.1% tip, which is similar to the 15.2% tip for the Mamdani system.
Export FIS
Once you design your FIS using Fuzzy Logic Designer, you can export the active and stored designs to the MATLAB® workspace for further simulations and deployment.
To export a FIS design to the MATLAB workspace, in Fuzzy Logic Designer, select Export > Export Fuzzy Inference System to Workspace.
The Export Fuzzy Inference System to Workspace dialog box lists the active FIS design along with any stored designs.
In the Export column, select one or more systems that you want to export.
In the Export As column, specify names for the workspace variables.
Click Export.
The app saves the FIS to the MATLAB workspace.
For more information on exporting and saving your FIS, see Export FIS and Simulation Data from Fuzzy Logic Designer.