# Prediction of Grain Porosity Based on WOA-BPNN and Grain Compression Experiment
## 4 Modeling Prediction of Grain Porosity
### Construction of Grain Porosity Prediction Model Based on WOA-BPNN  
The BPNN is essentially a gradient descent method, which offers significant advantages such as self-adaptation, self-learning, nonlinearity, and strong self-organization ability. However, the BPNN is highly reliant on the initialization parameters during operation. The objective function to be optimized is very complex, which leads to slow convergence and makes it easy to fall into a local optimum.  
The WOA is introduced into the BPNN algorithm to address the numerous defects encountered when the BPNN runs independently. The WOA leverages the fast convergence speed and strong global search ability to optimize the weights and biases of the BPNN. In this approach, the weights and biases are treated as the position information of humpback whales and are updated using the whales' ability to search for prey. This results in the updating of the weights and biases of the BPNN. After numerous iterations, the optimal global weights and biases are obtained, optimizing the BPNN.  
The flowchart depicting the grain porosity prediction model based on WOA-BPNN is shown in Figure 5. The specific establishment process is outlined below.  
Figure 5: Flowchart of the grain porosity prediction model based on the WOA–BPNN.  
(1) Initialization of a BPNN. Normalize the sample data, determine the topology of the BPNN based on the empirical formula, and initialize the initial weights and biases of the BPNN. The empirical formula is:  
\[H_{hidden}=\sqrt{I_{input}+O_{output}}+R \tag{16}\]  
where \(H_{hidden}\) is the number of nodes in the hidden layer, \(I_{input}\) is the number of nodes in the input layer, \(O_{output}\) is the number of nodes in the output layer, and \(R\) is an integer whose value range is [1; 10].  
(2) Initialize the WOA. In step (1), convert the initial weights and biases into position vectors of WOA. Then, initialize other fundamental parameters of the whale optimization algorithm such as setting the population size (\(N\)), the maximum number of iterations (\(T_{\text{max}}\)), the initial minimum weight (\(w_{1}\)), the initial maximum weight (\(w_{2}\)), and the convergence factor (\(a\)).  
(3) Calculate the individual fitness scores. Identify the whale with the highest current fitness value, record its position vector, and use it as the current optimal individual position \(X^{*}(t)\).  
(4) Update the position of each humpback whale in the population. Different position updating strategies are adopted according to the value of A. If \(\mid\)A\(\mid\)\(\geq\) 1, update the position according to Equation (9); if \(\mid\)A\(\mid\)\(<\) 1, update the position according to Equation (13).  
(5) The optimization algorithm terminates after reaching the maximum number of iterations or meeting the error accuracy requirement. At this point, the current optimal parameters are assigned to the BPNN.  
(6) In the BPNN training, the outputs of each layer are utilized to compute the training error and to adjust the weights and biases of each layer. This process is repeated until the maximum number of iterations is reached or the error falls below the bias.