## 4 Model for Calculating the Roll Stability Boundary
### Transformer Model Construction
#### 4.1.1 Input Data Processing  
We obtained the input dataset through MATLAB/TruckSim co-simulation. Each piece of data contain five features: roadway adhesion coefficient, velocity of the self-vehicle, load, \(\omega_{1}\), and \(\omega_{2}\), with \(LTR_{max}\) as the output. We import 1472 such data points into MATLAB to obtain a 1472 \(\times\) 6 matrix D. The matrix is processed as follows:  
1. Data partitioning: D is divided into four matrices, which are the training set feature matrix, training set output matrix, test set feature matrix, and test set output matrix. And the ratio of training data to test data is 7:3.
2. Data normalization: To speed up training and improve training stability, we normalized the input data and processed the input data matrix by mapping the row maximum and minimum values to [0, 1]: \[x_{norm}=\frac{x-x_{\min}}{x_{\max}-x_{\min}}\] (18) where \(x_{norm}\) is the normalized data, \(x\) is the original data, \(x_{max}\) indicates the maximum value within the dataset, and \(x_{min}\) indicates the minimum value within the dataset.
3. Position encoding: The Transformer model utilizes a self-attention mechanism. Unlike some traditional neural network architectures, the self-attention mechanism in the Transformer does not inherently capture the order of elements in the input data. As a result, the elements within the data lack an implicit order for the model to distinguish them. Therefore, position coding is necessary. Position coding assigns unique position information to each element in the input data sequence. This enables the Transformer to differentiate between different elements, which is crucial for accurately processing sequential data. The formula for calculating position coding is as follows: \[\left\{\begin{array}{l}PE_{(pos,2i)}=\sin\left(\frac{pos}{10000^{\frac{2}{2}} }\right)\\ PE_{(pos,2i+1)}=\cos\left(\frac{pos}{10000^{\frac{2}{2}}}\right)\end{array}\right.\] (19) where \(PE_{(pos,2i)}\) is the position encoding, \(pos\) is the position, \(i\) is the index of the dimension, and \(d_{model}\) is the dimension of the input and output vectors. The use of sine and cosine functions in position encoding offers several advantages. First, these functions can represent the position information continuously and periodically.  
Figure 7: Structure of Transformer.  
The sine and cosine waves have unique periodicity properties, which can capture the relative positions of elements in the sequence effectively. For example, as the position increases, the values of sine and cosine functions change periodically, and this periodic change can be used to distinguish different positions. Secondly, the combination of sine and cosine functions allows the model to learn both the absolute and relative positions of elements. The sine and cosine of the same position value provide two different dimensions of information, which enriches the position-related features available for the model. This is crucial for the Transformer to understand the order of input data, especially in tasks like path planning for intelligent semi-trailers, where the order of data related to different driving conditions and vehicle states is significant. Thirdly, the use of these functions in position encoding is computationally efficient. They can be easily integrated into the neural network architecture without introducing excessive computational complexity, which is beneficial for the overall performance of the Transformer model during training and prediction.