Main Content

Tune Fuzzy Inference System Using Fuzzy Logic Designer

Since R2023a

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.

  1. Learn the rule base while keeping the input and output MF parameters constant.

  2. 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:

  1. Number of cylinders

  2. Displacement

  3. Horsepower

  4. Weight

  5. Acceleration

  6. 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 26=64 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:

For this example, open Fuzzy Logic Designer and import the mpgInitialFIS system from the MATLAB® workspace.

fuzzyLogicDesigner(mpgInitialFIS)

Default app view showing a plot the fuzzy system structure in the center document. To the left of the plot are the Design Browser and System Browser panes. To the right of the plot is the Property Editor pane.

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.

Tuning tab toolstrip highlighting the Input Data and Output Data drop-down lists in the Source section.

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 shows Max 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.

Tuning options dialog box configured for particle swarm optimization using the previously specified settings.

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.

Tunable Parameters pane where the Tune checkbox is cleared for all parameters in the Input and Output tables. Since there are no rules, the Rule table is empty.

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.

Tune tab showing tuning results. The document on the left is a plot showing that the training error decreases from around 6.5 to around 4.5 over 20 iterations. The document in the middle shows more tuning details for each iteration, including function counts, best cost, and mean cost. On the right is a tuning summary.

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.

Design Browser table containing two entries, the original FIS in the first row and the tuned FIS in the second row. In the Design column, the tuned FIS name is now "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.

Design tab toolstrip highlighting the Input Data and Output Data drop-down lists in the Simulation section.

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.

Design Browser table where, in the Compare column, the checkbox for the original FIS is cleared and the checkbox for the tuned FIS is selected.

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.

System validation document where the top plot shows the output of the fis_learned system along with the reference output. The bottom plot shows the output error. The legend of the bottom plot shows the RMSE error for the fuzzy system.

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.

Tuning options dialog box configured for pattern search optimization using the previously specified settings.

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.

Tunable Parameters pane where the Tune checkbox is selected for all parameters in the Input, Output, and Rule tables.

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.

Tune tab showing tuning results. The plot on the left shows that the training error decreases from around 4.1 to around 3.3 over 61 iterations. On the right is a tuning summary indicating that 655 parameters are being tuned.

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.

Design Browser table with the new tuned system in the third row. In the Design column, the tuned FIS name is now "fis_tuned".

Open the System Validation document. The plots update to show the validation results for fis_tuned.

System validation document where the top plot shows the output of the fis_learned and fis_tuned systems along with the reference output. The bottom plot shows the output error for both systems.

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.

Toolstrip showing the Save drop-down expanded and the cursor over the fis_tuned system.

In the Save Fuzzy Inference System window, specify a file name and click Save.

See Also

Apps

Related Topics

External Websites