Tune Fuzzy Inference System Using Fuzzy Logic Designer
This example shows how to interactively tune membership function (MF) and rule parameters of a Mamdani fuzzy inference system (FIS) using Fuzzy Logic Designer. This example uses particle swarm and pattern search optimization, which require Global Optimization Toolbox software.
For this example, you tune a FIS using a two-step process.
Learn the rule base while keeping the input and output MF parameters constant.
Tune the parameters of the input and output MFs and rules.
The first step is less computationally expensive due to the small number of rule parameters, and it quickly converges to a fuzzy rule base during training. In the second step, using the rule base from the first step as an initial condition provides fast convergence of the parameter tuning process.
For an example that programmatically tunes a FIS using the same data, see Tune Fuzzy Inference System at the Command Line.
For an example that interactively tunes a FIS tree using the same data, see Tune FIS Tree Using Fuzzy Logic Designer.
Load Example Data
This example trains a FIS using automobile fuel consumption data. The goal is for the FIS to predict fuel consumption in miles per gallon (MPG) using several automobile profile attributes. The training data is available in the University of California at Irvine Machine Learning Repository and contains data collected from automobiles of various makes and models.
This example uses the following six input data attributes to predict the output data attribute MPG with a FIS:
Number of cylinders
Displacement
Horsepower
Weight
Acceleration
Model year
Load the data. Each row of the data set obtained from the repository represents a different automobile profile.
[data,name] = loadGasData;
data
contains seven columns, where the first six columns contain the input attribute values. The final column contains the predicted MPG output. Split data
into input and output data sets, X
and Y
, respectively.
X = data(:,1:6); Y = data(:,7);
Partition the input and output data sets into training data (odd-indexed samples) and validation data (even-indexed samples).
trnX = X(1:2:end,:); % Training input data set trnY = Y(1:2:end,:); % Training output data set vldX = X(2:2:end,:); % Validation input data set vldY = Y(2:2:end,:); % Validation output data set
Initial FIS Structure
To train a FIS, you must first create an initial FIS structure. For this example, load the initial FIS structure.
load mpgInitialFIS
This FIS has six inputs, one for each automobile profile attribute, and one output, the predicted fuel consumption.
Each input has two membership functions (MFs), which results in input MF combinations. Therefore, the FIS uses a maximum of 64 rules corresponding to the input MF combinations.
To improve data generalization beyond the training data, the FIS has 64 MFs for the output variable. Doing so allows the FIS to use a different output MF for each rule.
The input and output variables use default triangular MFs, which are uniformly distributed over the variable ranges.
For more information on creating this FIS structure, see Tune Fuzzy Inference System at the Command Line.
Define Initial FIS Structure
The first step of the tuning process is to define the initial structure of your FIS. To define this structure, specify:
Input and output variables with defined ranges
Initial membership functions for each variable
Initial rule base (optional)
To create your FIS in the app, you can:
Create a FIS based on your training data. For more information, see Get Started Using Fuzzy Logic Designer.
Manually build a FIS. For an example, see Build Fuzzy Systems Using Fuzzy Logic Designer.
Create a FIS at the command line and import It into the app.
For this example, open Fuzzy Logic Designer and import the
mpgInitialFIS
system from the MATLAB® workspace.
fuzzyLogicDesigner(mpgInitialFIS)
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.
Learn Rules
To learn the rules for your FIS, first specify the tuning options. Click Tuning Options.
In the Tuning Options dialog box, configure the following tuning options:
In the Optimization Type section, select Learning.
In the Method drop-down list, select
Particle swarm
. Particle swarm optimization is a global optimization method. Such methods perform better in large parameter tuning ranges as compared to local optimization methods.Set the maximum number of optimization iterations to
20
.Clear the Use default method options parameter
Under Method Options: Particle swarm, in the leftmost drop-down list, select
Run time limits
. By default, the next drop-down list showsMax Iterations
.In the text box, enter
20
.
Set the maximum number of rules to generate during learning. Clear the auto parameter and set the Max number of rules option to
64
.For reproducible results, set the Random number seed parameter to the
Initialize Mersenne Twister generator
option.Keep the remaining training options at their default values.
Click OK.
To only learn rules without modifying the MF parameters, you must 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.
To train the FIS, on the Tuning tab, click Tune. For this example, learning rules takes several minutes.
The Tune tab shows the training progress.
The Convergence Plot document plots the optimization cost (training error) after each epoch for both the training and validation data.
The Convergence Results document shows the algorithm-specific tuning progress.
The following figure shows the completed training process. The training error decreases throughout the tuning process. The tuning stops after the maximum number of iterations is reached.
The final root mean-squared error (RMSE) cost value for the tuned FIS is 4.452 MPG.
To accept the training results, click Accept.
The app adds the tuned FIS mpgInitialFIS_tuned
to the
Design Browser and sets this FIS as the active
design.
Select the mpgInitialFIS_tuned
row in the Design
Browser. Click the FIS name and rename the FIS to
fis_learned
.
To validate the performance of the tuned FIS, compare its performance to the validation data.
To select validation data, on the Design tab:
In the Input Data drop-down list, under Workspace Data Sets, select vldX.
In the Output Data drop-down list, under Workspace Data Sets, select vldY.
To ensure that the first untuned FIS is not included in any system validation, in the Design Browser, clear the corresponding entry in the Compare column.
On the Design tab, in the Simulation gallery, click System Validation.
The System Validation document shows the input values, reference output values, and FIS output values.
To view just the reference and FIS output values, click Unselect All for the Reference Inputs table.
To view the error between the reference and FIS output values, select Prediction errors.
The FIS output tracks the reference output well.
The bottom plot shows the output error. The legend for this plot displays an RMSE of 4.315 MPG for the validation data, which is comparable to the RMSE for the training data.
Tune MF and Rule Parameters
To further improve the FIS performance, you can tune the MF and rule parameters of
fis_learned
. To do so, first specify the tuning
options.
On the Tuning tab, click Tuning Options.
In the Tuning Options dialog box, configure the following tuning options:
In the Optimization Type section, select Tuning.
In the Method drop-down list, select
Pattern search
, which is a local optimization method that converges quickly for the parameter tuning.Set the maximum number of optimization iterations to
60
.Under Method Options: Pattern search, in the leftmost drop-down list, select
Run time limits
.In the next drop-down list, select
Max Iterations
.In the text box, enter
60
.
To improve the pattern search results, use a complete poll.
Under Method Options: Pattern search, click +. The app adds a new option row.
In this row, in the leftmost drop-down list, select
Poll settings
In the next drop-down list, select
Do a complete poll
.Select the checkbox.
Keep the remaining training options at their previous values.
Click OK.
To tune the MF and rule parameters, you must enable the corresponding tunable parameter settings.
In the System Browser, select
fis_learned
.In the Tunable Parameters pane, in the Tune column of each table, insure that all inputs, outputs, and rules are selected for tuning.
To train the FIS, on the Tuning tab, click Tune. For this example, tuning parameters can take about 30 minutes.
The following figure shows the completed training process. The training error decreases throughout the tuning process. The tuning stops after the maximum number of iterations is reached.
The final root mean-squared error (RMSE) cost value for the tuned FIS is 3.337 MPG.
To accept the training results, click Accept.
The app adds the tuned FIS fis_learned_tuned
to the
Design Browser and sets this FIS as the active
design.
Rename this FIS to fis_tuned
.
Open the System Validation document. The plots update to show
the validation results for fis_tuned
.
The results are similar to the results for fis_learned
. In the
Prediction Errors plot, the RMSE for
fis_tuned
is 3.556 MPG. Tuning the MF and rule parameters has
improved the performance of the tuned FIS.
Save Tuned FIS
To save your FIS for further analysis and development, you can either export it to the MATLAB workspace or save it to a FIS file. For this example, save the FIS to a file.
On the Design tab, under Save, select
fis_tuned
.
In the Save Fuzzy Inference System window, specify a file name and click Save.