## 3 Construction of Carbon Steel Corrosion Prediction Model
### Verification of Prediction Model
#### 3.2.3 Optimized BP Neural Network Prediction Model  
Figures 10 and 12 show that the error of the two machine learning prediction models, the BP neural network model and the random forest regression model, is minor when predicting the corrosion of carbon steel. Nevertheless, the training results of random forest regression predictions are remarkably consistent among the five training runs, whereas those of the BP neural network prediction model exhibit significant variability. This discrepancy is also evident in the error curve charts. The error trend curve of the random forest regression prediction model follows a consistent pattern, whereas the error curve of the BP neural network prediction model appears relatively chaotic. This discrepancy indicates that the random forest regression prediction model demonstrates greater stability, albeit with a marginally higher overall error, whereas the BP neural network model exhibits a lower overall error despite requiring a longer training period. Moreover, the BP neural network prediction model achieves superior accuracy, albeit at the expense of a lengthy optimization process during training. According to the fundamental principles of the BP neural network algorithm, its prediction accuracy hinges significantly on the appropriate tuning of weights and thresholds between layers. The suitability of these values directly impacts training efficiency and the precision of prediction calculations. Therefore, in this paper, we used the enhanced particle swarm optimization algorithm to fine-tune these parameters. According to the principle of the particle swarm optimization algorithm [30,31], the initial position, velocity, particle range, and expected fitness value for single-factor optimization can be iteratively calculated to obtain the optimal value. In this part, the optimization of the two factors, weight and threshold, may be difficult due to the different value ranges of the two factors [32]. Local optimality or long iteration times may occur, as shown in Figure 13a. In light of these challenges, the particle swarm optimization algorithm  
Figure 12: Corrosion prediction by random forest regression model. was refined first. It assigns specific value ranges to the weight and threshold, allowing them to search for optimization within their respective local domains. The mean square error serves as the fitness value, and the combined values of the two factors, which comprise the smallest fitness value, represent the optimal solution, as shown in Figure 12(b). In addition, when the inertia weight factor is introduced into the particle velocity updating formula, the larger the value is, the better the global search effect will be. Otherwise, it will be conducive to local searches. To balance the two situations, the algorithm has a strong global search ability in the early stages of operation, conducts accurate searches after locking the approximate optimal solution range, and introduces dynamic inertia weight factors [33; 34]. Its calculation formula is as follows:  
\[w=w_{s}-(w_{s}-w_{e})(\frac{i}{m})^{2} \tag{8}\]  
where \(w_{s}\) is the initial inertia weight, \(w_{e}\) is the inertia weight when iteration reaches the maximum, \(i\) is the current iterations, and \(m\) is the total number of iterations. Generally speaking, when the initial and final inertia weights are 0.9 and 0.4, respectively, the PSO performance is the best. When the progress of iteration shows a parabolic decline, the optimization ability gradually changes from global to local. The dynamic inertia weight factor is introduced into the velocity update formula of the particle swarm optimization algorithm, as shown in Equation (9). \[v_{i}=w\times v_{i}+c_{1}\times rand()\times(pbest_{i}-x_{i})+c_{2}\times rand( )\times(gbest_{i}-x_{i}) \tag{9}\]  
where \(w\) is the inertia factor; \(v_{i}\) is the particle velocity; \(i\) = 1,2 \(\ldots\) \(N\), \(N\) is the total number of particles; \(c_{1}\) and \(c_{2}\) are learning factors that are usually set to 2; and \(rand()\) is a random number between 1 and 2.