## 3 The PSOTSC Algorithm
### Overall Framework  
Trajectory segmentation and compression need to deal with the contradiction or balance between segmentation accuracy and compression rate. Retaining more original trajectory points can bring better similarity, but at the expense of compression rate. On the contrary, excessive pursuit of data compression rate will deteriorate the quality of approximate trajectory, which loses the significance of similar compression. How to optimize the whole effect of trajectory segmentation and compression? The PSOTSC algorithm is developed, which takes the selection of trajectory segmentation feature points as the starting point, and gives full play to the global optimization advantages of PSO. A better target trajectory segmentation and compression solution can be found. Compared with the results obtained by previous algorithms, the new solution can achieve better data compression rate on the same degree of trajectory similarity, i.e., fewer original trajectory points are retained. From another point of view, it can make the compressed trajectory more similar to the original trajectory under the same data compression rate level. The framework of PSOTSC is shown in Algorithm 1.  
```
Input: A set of trajectories \(STR=\{TR_{1},TR_{2},\ldots,TR_{N}\}\) Output: A set of compressed trajectories \(STR^{\prime}=\{TR^{\prime}_{1},TR^{\prime}_{2},\ldots,TR^{\prime}_{N}\}\) A compression ratio of \(\delta_{C}\) and a segmentation accuracy of \(\delta_{P}\)
01: for each \(TR\in STR\) do
02: Number of segments \(N_{S}=1\);
03: while true do
04: Search optimal segmentation solution \(SegSol=PSOSegSearch(N_{S})\);
05: Calculate the compression ratio \(\delta_{CTR}\) and segmentation accuracy \(\delta_{PTR}\) of \(TR\);
06: Satisfaction judgment \(SJ=TSCsuccess(\delta_{PTR},\delta_{CTR})\);
07: if \(SJ=1\)
08: break;
09: Increase number of segments \(N_{S}=N_{S}+1\);
10: end while
11: end for
12: Generate a set of compressed trajectories \(STR^{\prime}\)
13: Calculate \(\delta_{C}\) and \(\delta_{P}\) of \(STR^{\prime}\)
```  
**Algorithm 1** PSOTSC  
First, the input and output of the PSOTSC algorithm should be clarified. In general, the input of the algorithm is the original trajectory data set, which contains many trajectories with large amount of data. The output of the algorithm is the compressed trajectory data set and the evaluation values of data compression rate and segmentation accuracy. The compressed data set contains approximately processed trajectories, which are one-to-one corresponding to the trajectories in the original data set. The key links in the internal processing of the algorithm also have parameters that need to be set by the user, such as the scale of particle swarm, the number of search iterations, etc., which will be described in the subsequent parts of this section. Second, the basic method of trajectory segmentation optimization based on PSO is established (line 4), which can complete the search of the optimal segmentation solution under a fixed number of segments. After that, the fixed number of segments will start from 1 and increase from small to large to search for the corresponding optimal segmentation solution (line 2-10). By calculatingand evaluating the compression ratio and segmentation accuracy, a solution meeting the satisfaction requirements will be found (line 5-8). Finally, each trajectory in the original data set is optimized by the above method (line 1-11), the compressed trajectory data set can be generated (line 12), and the compression ratio and segmentation accuracy are given (line 13).