## 3 Methodology
### TRFM Trajectory Prediction Model
#### 3.3.5 Loss Function  
In this paper, \(RMSE\) (Root Mean Square Error) is used as the loss function in the prediction model. \(RMSE\) measures the similarity between predicted points and target points. The loss function is defined as follows:  
\[MAE=\frac{1}{N}{\sum_{i=1}^{N}}\Big{|}Y^{j}-\hat{Y^{j}}\Big{|} \tag{30}\]  
Since the model's predictions are relative values, the output y is scaled up by a factor of \(10^{4}\) to aid convergence. ``` Input:\(Traj\): Represents input trajectory data. \(l_{min}\): Represents the min length of trajectory. \(T_{interp}\): Represents the interpolation time interval. \(T_{sump}\): Represents the resample time interval. Output: Predicted trajectory : \(Prediction\_Traj\). Initialization:\(Nav\_Traj\): Used to store the trajectory of ship navigation. Function:\(Repair(\cdot)\): Used to store ship trajectories during the processing. \(Resample(\cdot)\): Used to store ship trajectories during the processing. Process: // Step 1 Trajectory Data Preprocessing \(1:Tmp\_Traj\leftarrow\) Create an empty set \(2:\textbf{For}\ P_{0i}\) in \(Traj\)do \(3:\) Add \(Po_{i}\) to \(Tmp\_Traj\) \(4:\) If current \(Po_{i}\) is stop point then \(5:\) If the length of \(Tmp\_Traj\geq l_{min}\)do \(6:\) Add \(Tmp\_Traj\) to \(Nav\_Traj\) \(7:\) Clear the \(Tmp\_Traj\) \(8:\textbf{End}\textbf{For}\) \(9:Input\_Traj\leftarrow\) Select the last trajectory from \(Nav\_Traj\) \(10:\textbf{For}\ P_{0i}\) in \(Input\_Traj\)do \(11:\) If \(Po_{i}\) is removal points do \(12:\) remove \(Po_{i}\) from \(Input\_Traj\) \(13:\textbf{End}\textbf{For}\) \(14:Input\_Traj\gets Repair\Big{(}Input\_Traj,\ T_{interp}\Big{)}\) \(15:Input\_Traj\gets Resample\Big{(}Input\_Traj,\ T_{sample}\Big{)}\) \(16:\textbf{For}\ P_{0i}\) in \(Input\_Traj\)do \(17:\)\(\Delta loni,\ \Delta lat_{i}\leftarrow\) Calculate the longitude and latitude differences between \(Po_{i}\) and \(Po_{i-1}\) \(18:\)\(\leavevmode\
obreak\ \leavevmode



\(ADE\) is calculated as follows:

\[ADE=\frac{1}{MN}\sum_{j=1}^{M}\sum_{i=1}^{N}\left|Po_{i}^{j}-\hat{P}o_{i}^{j}\right| \tag{33}\]

\(FDE\) is calculated as follows:

\[FDE=\frac{1}{M}\sum_{j=1}^{M}\left|Po_{last}^{j}-\hat{P}o_{last}^{j}\right| \tag{34}\]

In the calculation formulas for \(ADE\) and \(FDE\), \(M\) represents the total number of predictions, and \(N\) is the output step. ## 4 Experiments and Results

In this section, the performance of the proposed ship trajectory prediction method is validated through experiments on real AIS trajectory data. Several different evaluation metrics are applied to assess the model performance, and comparisons are made with other state-of-the-art research methods. ### Trajectory Process

Details of the data preprocessing methods are explained in Section 3.2 Data Processing. The data primarily consist of one day's AIS data from the Yangtze River Basin. This waterway experiences high ship traffic, complex river bends, and diverse ship types, making it suitable for validating ship trajectory prediction models. The collected data are stored in a database, and AIS data information is extracted, including MMSI, SOG, COG, LON, LAT, NS, and timestamps, which serve as the initial input information for data preprocessing. During data preprocessing, trajectories are segmented based on different MMSI and navigation statuses. Trajectory sequences shorter than 60 are excluded to ensure an adequate sequence length for the model input. Segmented trajectories undergo removal of outliers and redundant points, filling missing data points, and finally, uniform time interval resampling, with a set interval of 30 s. The data processing results for each step are shown in Figure 6 below. Figure 6: The multi-head attention layer. After segmenting the trajectories, a complete navigation trajectory is obtained. After removing outliers and redundant points, the trajectory is compressed, retaining trajectory points containing changes in motion states. Missing data points are interpolated to enhance trajectory resolution.