**5. FLC optimization example**

In order to present a practical application of FLC optimization via metaheuristic algorithm, this section presents an example of using a metaheuristic (PSO) for positional optimization of MFs in an FLC.

An air flow system is presented as a transfer function and an FLC is connected to it, which is parameterized manually and also by the PSO. Since the PSO algorithm is relatively simple and easy to apply, consider it for use in this application example.

The FLC is of the TSK type (control output is a specific value). In addition, the error and its derivative are considered as input to the system. The reason for this is that in addition to the error, it is also necessary to understand the speed and direction of the error variation.

In order to facilitate application, an example in MATLAB is used. **Figure 4** shows the FLC scheme associated with a metaheuristic.

From **Figure 4**, it can be seen that the FLC has the error and the error derivative as inputs. The error signal is used to calculate the ITAE and IAE indices. The FLC model has two outputs, one for the control signal and one for the rules. The control signal output passes through an integrating signal to compose the control signal. Moreover, the other output of the FLC is being used for the optimization of this controller. In this example five MFs are used (NB, NS, Z0, PS, PB) for the input error *e* (triangular), input error derived *d/dt* (triangular) and for the controller *output*. The optimization focuses on determining the position of the MFs (section 3.2.1) for the inputs and output.

First, the optimization focuses on the triangular functions, choosing one of the three positions that make up the triangle to carry out the modification. This is done by adding a variable *X*<sup>1</sup> and *X*<sup>2</sup> to this position while the others remain with a fixed value. Second, we seek to find the value of the output using the optimization variables *X*<sup>3</sup> and *X*4. In addition, the rules have already been pre-established and are not targets for optimization. The source code for the definition of the FLC, including the MFs and the respective rules are presented in **Figure 5**.

Regarding the fitness function to be used in this example, the system uses the ITAE and IAE error indexes to formulate this function (**Figure 6**).

$$J = \mathbf{0.5} \ast ITAE + \mathbf{0.5} \ast IAE \tag{5}$$

For each new population resulting from the PSO swarm, the variables from the algorithm are stored and inserted in the FLC and, from that, the result of the fitness function (*J*) is estimated. This is done until reaching the stopping criterion. In this example, add the value of *J* <10<sup>2</sup> as the main stop criterion for the algorithm. The code that presents the implementation of the objective function is shown below.

**Figure 4.** *FLC scheme for optimization by PSO in MATLAB.*

### *Fuzzy Systems - Theory and Applications*

```
Figure 5.
FLC code in MATLAB.
```
#### **Figure 6.** *Fitness function code in MATLAB.*

For the application of the PSO, it is first necessary to initialize the algorithm parameters. In this case, initially the value of the particle positions in the swarm is randomly defined. The position of all particles is usually started with some speed, in this example all speeds are started at zero. Finally, it is defined as a minimum value of the best initial particle in 1000. The particles in the swarm must reach and exceed at least this value of the "best initial particle". **Figure 7** shows the code used.

The operation of the PSO is simple, the population is evaluated and its position is updated based on its position and previous speeds. The swarm positions are then entered in the *X* variables, which in turn are the inputs for the "*OptFuzzy*" optimization function. The *X* values are actually the values that will adjust the positions of the FLC's input MFs. If the value obtained in the optimization is less than the current value of the objective function, the best individual particle values and the best global value are increased. This is done until reaching the stopping criterion. **Figure 8** shows the PSO operation code in this FLC optimization example.

In order to test the PSO in the optimization of the FLC MFs of the present example, we try to test the algorithm with 10, 20, 50, 100, 200 and 500 particles in the swarm. **Figure 9** shows the behavior of the objective function "*OptFuzzy*" over 50 iterations.

*Fuzzy Logic Control with PSO Tuning DOI: http://dx.doi.org/10.5772/intechopen.96297*

In general, the greater the amount of swarms in the PSO, the faster the minimization of the objective function will occur, since this way there will be a greater exploration of the search space of the algorithm.

```
Figure 7.
PSO parameters.
```
**Figure 9.** *Fitness function optimization.*

In addition to optimizing the FLC's input (*e* and *de/dt*) and output via PSO, manual tuning is also applied to these MFs. The manual tuning basically consists of determining "*manually*" the positional values of the triangular functions of the MFs

**Figure 10.** *MFs manual tuning vs. MFs PSO tuning.*

**Figure 11.** *FLC manual tuning vs. FLC PSO tuning.*

*Fuzzy Logic Control with PSO Tuning DOI: http://dx.doi.org/10.5772/intechopen.96297*


**Table 1.**

*Comparison between the different tunings of the FLC.*

and the value of the MFs at the output. This choice is empirical and is based on the operator's knowledge and experience regarding the control system. The result of the MFs tuning process in different ways is shown in **Figure 10**.

From the MFs resulting from the tuning processes discussed above (**Figure 4**), the FLC controller is simulated in both tuning situations (Manual and PSO) by applying different steps to the control system input. **Figure 11** shows the result of the behavior of the FLC resulting from the different synotnias mentioned above.

From **Figure 11** it is possible to notice that the FLC obtained from the PSO presented a much more satisfactory performance in terms of dynamic behavior. **Table 1** presents detailed values of the performance indices for both processes.

### **6. Conclusions**

The FLC is an important controller in the area of intelligent systems of control engineering, however its design aspects represent a challenge to the specialist, as its operational requirements require a characteristic knowledge based on linguistic resources.

An alternative to the development of the FLC is the use of optimization resources to aid the development of this type of controller. Metaheuristics can clearly contribute to this issue, as they represent an effective way of exploring solutions to complex problems. The use of this feature involves the definition of an optimization problem, that is, the definition of a fitness function and which variables are associated in the process.

An FLC optimized by a metaheuristic algorithm represents a viable alternative for several applications, as the solutions can be adapted to the optimization criteria of these algorithms, thus allowing the specialist to develop an FLC to meet their specific performance needs.
