# A Bilevel Optimization Approach for Tuning a Neuro-Fuzzy Controller
## 4 Bilevel Optimization for Tuning a NFC
### First Level  
In the first-level optimization problem, the weights are excluded from the decision variables, resulting in a solution vector and a noise vector structured as in (24) and (25), respectively.  
\[\vec{x}=[m,\sigma_{1},\sigma_{2},\eta_{\phi},\eta_{\sigma},\eta_{r}] \tag{24}\]  
\[\vec{w}=[w_{1},w_{2},...,w_{n=m^{2}},\phi_{1},\phi_{2},...,\phi_{m}] \tag{25}\]  
This separation ensures that the size of the solution vector (\(\vec{x}\)) is fixed. However, the vector needs to be complemented with weight values. To achieve this, each solution vector is combined with 10 randomly generated sets of weights of size \(m^{2}\), and the combination that produces the lowest objective function is selected as the fitness for the solution vector. In the next optimization level, the elements in the noise vector are tuned, as they were undetermined in the lower level. An additional algorithm could be used at the first level to tune them, but this would increase computational costs. On the other hand, due to the learning capability of the NFC, different random sets have the potential to be good solutions, although not necessarily the optimum. An example of the same parameter set but with different noise vectors for a randomly selected system is shown in Figure 3.  
Furthermore, an elitist selection mechanism has been incorporated to mitigate the effect of the noise, in that for new executions, the noise vector \(\vec{w}\) that generates the lowest objective function is preserved. It is important to remember that this subset will be refined in the second-level problem.  
The previous procedure addresses the challenge that not all variables can be simultaneously handled in this optimization process. Therefore, in the first-level optimization, the aim is to find the solution that best works with the randomly generated weight vectors. In other words, the goal is to identify the solution with the best performance when combined with the random weights. Similar concepts have been explored in the context of cooperative co-evolution [32; 33; 34].  
Another important consideration is that the number of membership functions (\(m\)) is limited to integer values, whereas the remaining variables are real. Therefore, a mixed-variable optimization problem is carried out at the first-level optimization. Several authors have emphasized the performance issues of evolutionary algorithms in such scenarios, where the loss of population diversity leads to fast stagnation at integer values [19; 20; 21]. To prevent this premature convergence, the repulsion strategy proposed by [PERSON] et al. [18] is employed in the low-level problem. In mixed-variable problems, the integer variables define several discontinuous feasible parts where the integer constraints are fulfilled. The repulsion strategy assumes that the population is trapped in one of these discontinuous feasible parts when there is no fitness improvement for a predefined number of consecutive generations. Under this condition, the population is reset, and the explored discontinuous feasible part is penalized to avoid attracting the population again.  
The general process is shown in Figure 4, and the operations for the mutation, cross, and selection are described by (26), (27), and (28), respectively.  
\[Mutantx^{j}=x^{r1}+F(x^{r2}-x^{r3}) \tag{26}\]  
Figure 3: Example with two dynamics of the reactor temperature using the same NFC architecture but with different values of \(\vec{w}\).  
\[Newx_{i} =\left\{\begin{array}{ll}Mutantx_{i},&rand(0,1)\leq CR\\ Fatherx_{i},&rand(0,1)>CR.\end{array}\right. \tag{27}\] \[x^{g+1} =\left\{\begin{array}{ll}Newx_{i},&f(Newx_{i})\leq f(x^{g})\\ x^{g},&other.\end{array}\right. \tag{28}\]