## 3 Methodology
### TRFM Trajectory Prediction Model
#### 3.3.5 Loss Function
(**c**) Trajectories after segmentation. (**d**) Trajectories after uniform time interval resampling. To measure the distance error between predicted and actual trajectories, \(ADE\) and FDE are used to evaluate the average distance error and the endpoint distance error, respectively. \(ADE\) represents the global average distance error between predicted and actual trajectories, with a smaller value indicating closer alignment between predicted and actual trajectories. \(FDE\) represents the average error between the final predicted point and the final actual point of the trajectories, with a smaller value indicating a closer alignment of the predicted endpoint with the actual endpoint. Figure 9 presents experimental results for different models using \(ADE\) and \(FDE\) values for a prediction sequence length of 30, which corresponds to a predicted trajectory duration of 15 min. From the bar chart, it is evident that the ENC-DEC-structured TRFM model has the lowest \(ADE\) and \(FDE\) values, indicating that the TRFM model's predicted trajectories closely match the actual trajectories. Figure 8: Training loss curves for different models. Figure 9: Bar charts of \(ADE\) and \(FDE\) for different models. In real trajectory prediction scenarios, we compared the trajectory prediction method based on the TRFM model proposed in this paper with other trajectory prediction models and created comparison plots of predicted and actual trajectories. Figure 10 shows the comparison of predicted and actual trajectories for different models. It is clear from the figure that the ENC-DEC-structured TRFM model provides the best fit to the actual predicted trajectories, with the smallest deviations between predicted and actual trajectory points. This demonstrates that the trajectory prediction model based on the TRFM model proposed in this paper has superior predictive performance. To further illustrate the predictive accuracy of the TRFM trajectory prediction model proposed in this paper, Table 4 presents the evaluation of average errors in longitude, latitude, and distance for different prediction sequence lengths with a time interval of 30 s. Here, 'N' represents the prediction sequence length, 'LON' represents longitude error, 'LAT' represents latitude error, and 'FDE' represents distance error. The experimental results show that the ENC-DEC-structured TRFM model has the lowest errors across different prediction sequence lengths. For a 15 min trajectory prediction, the ENC-DEC-structured TRFM model achieves an endpoint error of 0.423 km, which is a 23.51% improvement compared to using only the DEC-structured TRFM and a 47.05% improvement compared to other state-of-the-art trajectory prediction models. These results indicate that the TRFM ship trajectory prediction method proposed in this paper performs exceptionally well. Figure 10: Comparison of predicted trajectories and actual trajectories for different models: (**a**) LSTM, (**b**) ATT-LSTM, (**c**) CNN-LSTM, (**d**) Bi-LSTM, (**e**) TRFM(DEC), (**f**) TRFM(ENC-DEC). ## 5 Conclusions

This research advances ship trajectory prediction by leveraging the comprehensive navigational data available from AIS and applying deep learning techniques. A novel preprocessing method for AIS data is introduced, aimed at reducing noise from equipment or network transmission, thus optimizing the data for a further analysis. This study centers on the implementation of the Transformer model (TRFM), which incorporates an encoder-decoder architecture and an adaptive multi-head attention mechanism. This design is proficient in extracting significant trajectory features, enhancing model inference speed, and addressing the limitations of traditional recurrent neural networks, specifically their computational inefficiency and inadequate global feature extraction capabilities. Consequently, this approach significantly improves the accuracy of trajectory predictions.