# A Bilevel Optimization Approach for Tuning a Neuro-Fuzzy Controller
## 2 Neuro-Fuzzy Controller  
The neuro-fuzzy controller, considered as a case study in this work, is an improved version of the definition presented in [22]. It represents an important alternative to on-off systems and PID controllers that are used in industrial processes such as biodiesel production but often perform poorly due to their inadequate handling of disturbances over time [13]. There are two inputs in this NFC, \(E\) and \(\Delta E\), corresponding to the error and the increment of the error over time, respectively. These signals are calculated using (2) and (3), where \(SP(k)\) and \(PV(k)\) denote the set point and the value of the process variable in the instant \(k\), \(E(k-1)\) represents a delay in the time of \(E(k)\), and \(Ts\) is the sampling time. \[E(k)=SP(k)-PV(k)\]\[\Delta E(k)=\frac{E(k)-E(k-1)}{Ts} \tag{3}\]  
Sampling a signal in process control is a common practice in the industry. Although there is no analytical method to determine the exact value of the sampling rate, it is possible to establish a limit to ensure no loss of information when discretizing a continuous signal from the real world. The Shannon-Nyquist theorem [23] establishes that the sample rate, \(f_{s}\), must be higher than twice the highest frequency component of interest in the signal, known as the Nyquist frequency, \(f_{N}\), as expressed in (4):  
\[f_{s}>2*f_{N} \tag{4}\]  
Figure 1 shows the five layers of the network architecture in the NFC. The first one provides the input vector \(X(k)\) for the fuzzification, considering \(E(k)\) and \(\Delta E(k)\). The second layer is used to make the inferences using a fuzzy rule base, whereas the third to fifth layers are used in the defuzzification process to formulate the output \(u(k)\). The first layer maps the real values of the input vector \(X(k)\) to the linguistics applied to make the fuzzification, according to the Takagi-Sugeno method [24]. The structure of this step is defined by (5):  
\[\mu_{A_{j,k}}=\Gamma_{j}(\Lambda_{j}(k),X_{i}(k)) \tag{5}\]  
where \(\mu_{A_{j,k}}\) denotes the number of membership functions selected for the vector \(X(k)\), which is normally set by the designer through a trial and error process. The last value of the index, \(j\), corresponds to the number of membership functions. The terms \(\Gamma_{j}(\cdot)\) and \(\Lambda_{j}(k)\) describe the function selected to make the fuzzification and its parameters, respectively. As an example, for the Gaussian bell described by (6), the \(\Lambda_{j}(k)\) set contains the parameters \(\phi_{j,k}\) and \(\sigma_{j,k}\), representing the position and spread of the Gaussian function [13]. \[\mu_{A_{j,k}}(X_{i}(k))=e^{0.5\left(\frac{X_{i}(k)\phi_{j,k}}{\sigma_{j,k}} \right)^{2}} \tag{6}\]  
The second layer makes inferences based on the _if-then_ statements to generate fuzzy sets using the vector \(X(k)\) for subsequent membership. In this way, the functions are interconnected, enabling the classification and determination of possible scenarios in the behavior of the system. Subsequently, they propose an output value according to each of  
Figure 1: Neuro-fuzzy model controller scheme [13]. the cases established by the set of fuzzy rules denoted by (7), where the index \(p=2,3\) corresponds to the second or third layer, respectively [13]. \[O^{p}=w(k)=\mu_{A_{j,k}}(X_{i}(k))^{T}*\mu_{A_{j,k}}(X_{i}(k)) \tag{7}\]  
The output of the third layer should be normalized according to (8), where the index \(l=1,2,...,R\) is defined by the number of fuzzy rules \(n\) since \(R=n\times n\)[13]. \[\overline{w}(k)=\frac{w(k)}{\sum_{l=1}^{R}w_{l}(k)} \tag{8}\]  
The output of the fourth layer (\(O^{4}\)) is composed of the products of the normalized firing strength and the parameter \(r_{l}=\gamma_{j}(\lambda_{j}(k))\).