## 3 The PSOTSC Algorithm
### Particle Swarm Segmentation Optimization
```
Input: Trajectory points ordered by time \(TR=\{P_{1},P_{2},\ldots,P_{len}\}\)  A set of parameters of PSO \(\mathit{Para\_PSO}=\{N_{p},N_{l},C_{1},C_{2},W_{\max},W_{\min},V_{\max},V_{\min}\}\)  A threshold of trajectory profile fitting \(TPF_{\min}\)  A number of compressed trajectory segments \(N_{S}\)  Output: Compressed trajectory points ordered by time \(TR^{\prime}=\{P_{u_{0}},P_{u_{1}},P_{u_{2}},\ldots,P_{u_{i-1}},P_{u_{i}}\}\)  01: Initial particle swarm and velocity \(p=\{p_{1},p_{2},\ldots,p_{N_{p}}\}\), \(v\_p=\{vp_{1},vp_{2},\ldots,vp_{N_{p}}\}\)  02: while \(n\leq N_{l}\) and \(gTPF_{best}<TPF_{\min}\) do  03: for each \(p\in p\)s do  04: Calculate fitness \(fp=\mathit{TraProfileFitting}(TR,p)\);  05: end for  06: Find Current best particle \(\mathit{pbest}\in p\)s and best fitness \(pTPF_{best}\) with maximum \(fp\);  07: Update global best particle \(\mathit{gbest}\) and best fitness \(gTPF_{best}\);  08: for each \(p\in p\)s do  09: Calculate velocity \(vp\) using \(\mathit{gbest}\);  10: Update particle based on \(NARJ\) strategy \(pe=NARJ\mathit{ParaUpdate}(p)\);  11: end for  12: end while  13: Construct compressed trajectory \(TR^{\prime}\);
```  
**Algorithm 2**_PSOSegSearch_  
The input of the algorithm is the original trajectory \(TR=\{P_{1},P_{2},\ldots,P_{len}\}\). The basic parameters of PSO include the number of particles \(N_{P}\), the maximum number of iterations \(N_{l}\), the learning factor \(C_{1},C_{2}\), the maximum and minimum inertia weight \(W_{\max},W_{\min}\) and the maximum and minimum velocity \(V_{\max},V_{\min}\).The threshold of trajectory profile fitting degree is \(TPF_{\min}\), and \(N_{S}\) is the number of trajectory segments. First, the algorithm needs to complete the initialization of particle swarm (line 1), and the premise is to define the particle format. For trajectory \(TR\), the compressed trajectory is expressed as \(TR^{\prime}=\{P_{1},P_{u_{1}},P_{u_{2}},\ldots,P_{u_{k}},P_{len}\}\), where \(P_{u_{1}},P_{u_{2}},\ldots,P_{u_{k}}\) is the segment feature point. Then, the trajectory \(TR\) with \(k\) segment feature points can be mapped into a sequence of 0 and 1, and the position of 1 in the sequence is \(P_{1},P_{u_{1}},P_{u_{2}},\ldots,P_{u_{k}},P_{len}\). Therefore, we define the particle format as sequence \(p=\{1,\Phi,1\}\) and the length is \(len\). \(\Phi\) is a sequence of 0 and 1 with the length of \(len-2\), and the number of 1 in the sequence is \(k\). Random particle means randomizing the position of 1 or the number of 1, representing a random segmentation solution. If the number of trajectory segments is fixed, only the position of 1 needs to be randomized. In order to simplify the calculation, the particle can also be defined as \(p=\Phi\), which is transformed into \(p=\{1,\Phi,1\}\) when the algorithm finally outputs the compressed trajectory. The algorithm will keep on searching for optimization, as long as the number of iterations \(n\) is no more than \(N_{l}\) and the trajectory profile fitting degree of the global optimal segmentation solution is less than \(TPF_{\min}\) (line 2). In each iteration, the fitness \(fp\) is calculated for all particles (line 3-5), i.e., the fitting degree of the compressed trajectory represented by the particles and the original trajectory. The fitness function is defined as \(\mathit{TraProfileFitting}\). The original trajectory TR and particle p are the inputs of this function. The calculation of the function has been described in detail in Section 2.2. The particle \(\mathit{gbest}\) with the highest fitness is selected to update the global optimal particle \(\mathit{gbest}\) and optimal fitness \(\mathit{gTPF_{best}}\) (line 6-7). Then, the speed of all particles is modified according to the global optimal particle \(\mathit{gbest}\) (line 9), and the particle state is updated based on the particle update strategy of NARJ, which will be discussed later. Finally, the algorithm ends the iteration and outputs the compressed trajectory \(TR^{\prime}\).