## Tune Fuzzy Inference System Using Fuzzy Logic Designer

This example shows how to tune membership function (MF) and rule parameters of a Mamdani fuzzy inference system (FIS) at the MATLAB® command line. 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 tunes a FIS at the command line, see Tune Fuzzy Inference System at the Command Line.

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 dataset obtained from the repository represents a different automobile profile.

`[data,name] = loadGasData;`

`data` contains 7 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 ${2}^{6}=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. When define this structure, specify:

• Input and output variables with defined ranges.

• Initial membership functions for each variable.

• (optional) Initial rule base

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)`

### 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 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 ```Initialize Mersenne Twister generator``` option.

• Keep the remaining training options at their default values.

Click .

To only learn rules without modifying the MF parameters, you must disable the input and output tunable parameter settings.

• In the System Browser, select `fis`.

• In the Tunable Parameters section, click 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 ANFIS system properties, the training error and minimum root mean-squared error results for the training and validation data sets.

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 `fis_tuned` to the Design Browser and sets this FIS as the active design.

Select the `fis_tuned` row in the Design Browser. Click the FIS name and rename the FIS `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.

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

In Fuzzy Logic Designer, on the Tune 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`. Particle swarm optimization is a local optimization method which provides 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```. By default, the next drop-down list shows `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 .

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 section, click for both the input and output tables.

• In the rule table, all the rule parameters are already selected.

To train the FIS, on the Tuning tab, click Tune. For this example, tuning parameters takes approximately 5 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.

### Export 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 .