# Design of a NARX-ANN-Based SP Controller for Control of an Irrigation Main Canal Pool
## 2 ANN-Based System Identification of an Irrigation Main Canal Pool
The mathematical expressions of the NARX-ANN- and TD-NARX-ANN-based models are, respectively, [43]:  
\[y_{m1}(t)=f_{0}\left(\sum_{h=1}^{n_{h}}w_{0,h}f_{h}\left(\sum_{i=1}^{n_{u}}w_{h, i}u(t-i)+\sum_{j=1}^{n_{y}}w_{h,j}y_{m1}(t-j)+b_{h}\right)+b_{0}\right), \tag{3}\]  
\[y_{m2}(t)=f_{0}\left(\sum_{h=1}^{n_{h}}w_{0,h}f_{h}\left(\sum_{i=1}^{n_{u}}w_{ h,i}u(t-d-i)+\sum_{j=1}^{n_{y}}w_{h,j}y_{m2}(t-j)+b_{h}\right)+b_{0}\right), \tag{4}\]  
where \(w_{h,i}\) are the synaptic weights of each \(h=1,\ldots,3\) neuron of the hidden layer from the \(i=1,2,\ldots,n_{u}=13\) inputs \(u(t)\), \(w_{h,j}\) are the synaptic weights of each \(h\) neuron of the hidden layer from the \(j=1,2,\ldots,n_{y}=13\) outputs \((y(t))\), \(w_{0,h}\) are the synaptic weights of the output layer neuron from each \(h\) neurons of the hidden layer, \(b_{h}\) are the bias weights of each \(h\) neuron of the hidden layer, \(b_{0}\) is the bias weight of the output layer neuron, \(f_{h}\) is the hyperbolic tangent activation function of each \(h\) neuron of the hidden layer, which is defined by the expression:  
\[f_{h}(x)=\frac{e^{x}-e^{-x}}{e^{x}+e^{-x}} \tag{5}\]  
\(n_{h}\) is the neuron number of the hidden layer, and \(f_{0}\) is a piecewise-linear activation function of the output layer, which is formed by three linear pieces joined to form a non-linear function (symmetric saturating linear) defined as:  
\[f_{0}(x)=\left\{ \begin{array}{l}-1,x<-1,\\ x,-1<x<1,\\ 1,x>1.\end{array} \right. \tag{6}\]  
The optimal number of neurons in the hidden layer was attained by using the Akaike Information Criterion (AIC). The lowest value of AIC \(=\)\(-\)6.29 was obtained for \(h=\)3. Figure 4: Architecture of the TD-NARX-ANN-based model designed. The input-memory and output-memory orders (\(n_{u}=n_{y}=\)13) were obtained from the time-series modeling using the Box-Jenkins approach. The data set \(Z^{N}\) that describes the irrigation canal pool studied in its entire range of operation was attained by exciting the upstream equivalent undershoot gate, i.e., the process input \(u_{1}(t)\), with a pseudo random binary sequence (PRBS), and measuring the downstream end water level \(y_{1}(t)\):  
\[Z^{N}=\{[u_{1}(t),y_{1}(t)]|t=1,\ldots,N\} \tag{7}\]  
Figure 5 illustrates the dataset \(Z^{N}\) obtained, which was split into three parts, 50% for the training set, 25% for the validation set, and 25% for the testing set. Training is an iterative process that consists of determining the synaptic weights \(w_{h}\) and bias \(b_{h}\) of each of the \(h\) neurons of the ANNs designed, so that their output signals converge towards the desired states [72]. The training algorithm should adjust the ANN parameters to minimize the following performance index (mean square error) [71]:  
\[F(x_{k})=\frac{1}{N}\sum_{i=1}^{N}e_{i}(x_{k})^{2}=\frac{1}{N}\sum_{i=1}^{N} \left(y_{i}-y_{mi}\right)^{2} \tag{8}\]  
Figure 5: The obtained data set \(Z^{N}\) of the irrigation canal pool studied. where \(x_{k}\) is a vector of current parameters (weights and biases) of the ANN, \(e_{i}(x_{k})\) is the training error, \(y_{i}\) is the observed (measured) data of the downstream end water level, \(y_{mi}\) is the ANN output (estimation of the downstream end water level developed by the ANN), and \(N\) is the total number of samples used to train the ANN. For the training of the two designed NARX-ANNs, a modified Levenberg-Marquardt optimization algorithm was used, which is a non-linear optimization algorithm that combines the Gauss-Newton method and the Steepest-Descend Gradient method in a single expression and finds the minimum of the function (either linear or nonlinear) over a space of parameters and optimizes the solution [74; 75]. This algorithm is characterized by its high convergence speed and reliability, although the complexity of its calculations is greater than in other algorithms [43].