## 4 Model for Calculating the Roll Stability Boundary
### Transformer Model Construction
#### 4.1.2 Encoder  
The encoding component comprises four identical encoders arranged in a stack, with each encoder consisting of two sub-layers known as the Multi-Head Attention layer and the Feed-Forward Neural Network layer. The process of the calculation is illustrated in Figure 8. The processed data first flow into the self-attention mechanism layer, allowing the model to focus on other pertinent information during the data processing. The pertinent information includes data that are related to the currently processed elements in terms of time or space and is valuable for determining the roll stability boundary of intelligent semi-trailers. Specifically, it encompasses input features such as the road adhesion coefficient, vehicle driving speed, load weight, and the variation of these features at different times or positions. This information is of utmost importance for accurately assessing the vehicle's driving state and predicting the rollover risk. The self-attention mechanism layer dynamically assigns attention weights to input features by computing pairwise dependencies. Specifically, given the input sequence, the self-attention mechanism calculates query (\(Q\)), key (\(K\)), and value (\(V\)) matrices through linear transformations:  
\[\left\{ \begin{array}{l}Q=XW_{Q}\\ K=XW_{K}\\ V=XW_{V}\end{array} \right. \tag{20}\]  
where \(W_{Q}\), \(W_{K}\), and \(W_{V}\) are learnable weight matrices. The attention scores are then computed as scaled dot-products between \(Q\) and \(K\):  
\[Attention(Q,K,V)=softmax\left(\frac{QK^{T}}{\sqrt{d_{K}}}\right)V \tag{21}\]  
where \(d_{K}\) represents the dimension of \(K\), and the calculation of \(QK^{T}\) measures the correlation between each element in the query matrix \(Q\) and the key matrix \(K\). Specifically, \(Q\) is multiplied by \(K^{T}\) in matrix operation, and each element in the resulting matrix represents  
Figure 8: Computational procedure of self-attention. the similarity between the query vector and the corresponding key vector. \(\sqrt{d_{k}}\) is used to scale the result. This is because when \(d_{k}\) is large, the value of \(QK^{T}\) may become very large, causing the gradient of the softmax function to become extremely small, resulting in the problem of gradient vanishing. By dividing by \(\sqrt{d_{k}}\), this situation can be effectively avoided, making the model training more stable. The softmax function then transforms the scaled result into a probability distribution. These probability values are the attention scores, which represent the degree of attention the model pays to each other element when processing the current element. The higher the attention score, the stronger the model believes the correlation between the element and the currently processed element is, and the greater the weight assigned to this element in subsequent calculations. In semi-trailer path planning, this mechanism prioritizes interactions among dynamic parameters, such as road adhesion coefficient, vehicle velocity, and load mass. For instance, when predicting roll stability boundaries, higher attention weights may be assigned to velocity-load correlations under low-friction conditions, ensuring adaptive path adjustments. By connecting different \(head_{i}\), the multi-head attention can be obtained, and the calculation process is illustrated in Figure 9. The formula for the calculation is presented as follows:  
\[MultiHead(Q,K,V)=Concat(head_{1},head_{2},\ldots head_{n})W^{O} \tag{22}\]  
\[head_{i}=Attention(QW_{i}^{Q},KW_{i}^{K},VW_{i}^{V}) \tag{23}\]  
where \(head_{i}\) is the attention output of a single head, the \(Concat\) function represents concatenating all \(head_{i}\), and \(W^{O}\) is the learnable weight matrix of the linear transformation. After being processed by the multi-head attention layer, the input data are fed into the feedforward neural network. This network can make further non-linear changes to the processed input data, aiding the model in understanding the relationships among the input data across various dimensions.