## 2 Intelligent Inversion Method of the In Situ Stress Field
### Optimization Solution Based on PSO-BP
#### 2.2.2 PSO Optimized BP Network Algorithm  
The fundamental principle of in situ stress inversion involves the establishment of a nonlinear relationship between the stress state and the model boundary conditions in order to identify the optimal boundary conditions that correspond with the measurement data. The BP neural network is widely used in underground engineering due to its capacity to deal with nonlinear problems [25,26]. However, the BP algorithm has some unavoidable limitations in addressing complex nonlinear problems. For example, many parameters should be initialized in the BP algorithm, such as weights, thresholds, error tolerance, etc. Some of these parameters that are determined empirically and roughly are sometimes not appropriate for a specific problem, and this limitation makes the BP algorithm quite unstable. Thus, in this paper, the PSO has been used to optimize the BP initial parameters to improve the stability and accuracy of the final result. The specific implementation steps of the PSO-BP intelligent optimization algorithm are as follows [21,27]:  
(1) Determine the structure of the BP neural network.  
(2) Encode the BP connection weights and thresholds; the particle dimension \(D\) is the sum of all weights in neural networks and is expressed as:  
\[D=S_{1}\times R+S_{1}\times S_{2}+S_{1}+S_{2} \tag{2}\]  
where, \(R\), \(S_{1}\), and \(S_{2}\) are the numbers of neurons in the input, hidden, and output layers, respectively.  
(3) Initialize the basic parameters of the particle swarm.  
(4) Define the fitness function: the mean square error of BP is used as the function of fitness value, and the function is expressed as:  
\[\centering Fitness=\frac{1}{N}{\sum}_{i=1}^{N}{\sum}_{=0}^{S_{2}}{(d_{io}(x_{i})- y_{io})}^{2}\@add@centering \tag{3}\]  
where \(N\) represents the total number of input samples, \(d_{io}(x_{i})\) is the \(o\)-th output result for the \(i\)-th group of input samples, and \(y_{io}\) is the corresponding actual value.  
(5) Based on the input-output sample pair, each particle's fitness value is calculated using the forward algorithm and the fitness function of the BP network.  
(6) For each particle, if \(Fitness[i]\) is less than the \(P_{best}\), let \(P_{best}\) equal \(Fitness[i]\); if \(Fitness[i]\) is less than the \(G_{best}\), let \(G_{best}\) equal \(Fitness[i]\);  
(7) Update the velocity and position of the particles by Equations (4) and (5) and ensure that the particle velocity does not exceed the velocity limit (\(v_{\max}\)).  
\[\centering v_{i\bar{y}}(t+1)=v_{i\bar{y}}(t)+c_{1}r_{1}(t)\big{[}p_{i\bar{y}} (t)-x_{i\bar{y}}(t)\big{]}+c_{2}r_{2}(t)\big{[}p_{g\bar{y}}(t)-x_{i\bar{y}}(t) \big{]}\@add@centering \tag{4}\]  
\[\centering x_{i\bar{y}}(t+1)=x_{i\bar{y}}(t)+v_{i\bar{y}}(t+1)\@add@centering \tag{5}\]  
where \(x_{i\bar{y}}\) and \(v_{i\bar{y}}\) are the position and velocity of particles \(i\) in the \(j\)-th dimensional component, respectively; \(P_{j\bar{y}}\) and \(P_{g\bar{y}}\) are the individual extremum and global extremum of  
\begin{table  
particles \(i\) in the \(j\)-th dimensional component, respectively; \(c_{1}\) and \(c_{1}\) are acceleration constants, which normally take the same value between 1.5 and 2.5; \(r_{1}\) and \(r_{2}\) are random numbers ranging from 0 to 1.  
(8) Calculate the error of the PSO-BP algorithm as:  
\[E=\frac{1}{t}{\sum_{i=1}^{t}{J(G_{best}}^{(i)})} \tag{6}\]  
where \({J(G_{best}}^{(i)})\) is the fitness of the global optimal value in the \(i\)-th iteration and \(t\) is the current iteration time.  
(9) If the error (\(E\)) is within the preset tolerance or the number of iterations reaches its maximum, the final solution is the vector of the global optimal position (\(G_{best}\)) in the last iteration. Otherwise, return to step 5.