# A Hybrid Method for Mobile Agent Moving Trajectory Scheduling using ACO and PSO in WSNs
## 5 Our Presented HM-ACOPSO Algorithm
### Shortest Path Selection Based on ACO  
For some delay-tolerant applications such as environment monitoring, there is no need for multi-hop communication among cluster heads which will result in a heavy forwarding burden on CHs. In our proposed HM-ACOPSO algorithm, we use a mobile agent for data collection. During each round, the mobile agent needs to traverse all the CHs and sojourn for data gathering. The moving trajectory of the mobile agent has a significant impact on the performance of the network. A vital feature for moving trajectory planning is the length of the path. A short moving path will cost less time for the mobile agent traveling. In this section, we utilize the ACO algorithm for shortest path selection. We use an undirected complete graph \(G=<V,E>\) to represent the network and \(V\) denotes the set of cluster heads and \(E\) denotes the set of edges between any two cluster heads. The following steps are used to conduct the ACO algorithm.  
**Step 1:**\(m\) ants are randomly placed in \(n\) CHs and a matrix with \(m\times n\) dimension is used to record each ant's traveling path.  
**Step 2:** The possibility of next cluster heads that ants will choose can be calculated using Formula (8):  
Figure 5: The sensor field after clustering.  
\[p_{ij}^{k}(t)=\left\{\begin{array}{ll}\frac{\tau_{ij}^{k}(t)\cdot p_{ij}^{k}(t)} {\sum\limits_{k\in\text{next}}\tau_{ik}^{a}(t)p_{ik}^{k}(t)}&\text{if }j\in\text{next}\\ 0&\text{otherwise}\end{array}\right. \tag{8}\]  
where \(p_{ij}^{k}\) denotes the possibility that the \(k\)th ant travels from CH \(i\) to CH \(j\) and next denotes the set of CHs that ant \(k\) has not travelled. \(\tau\) is the pheromone, and \(\mu\) is the inspired factor, which is the reciprocal of the distance between CH \(i\) to CH \(j\). Each ant travels all the CHs in accordance with the above possibility. \(\alpha\) and \(\beta\) are two constant control factors.  
**Step 3:** A fitness function is utilized to evaluate the quality of the traveling path of the ants' and it can be defined as follows:  
\[\mathit{Fitness}(k)=\sum\limits_{e\in\mathit{Path}}e \tag{9}\]  
where Path denotes the set of edges the ant travels. The best travel path is recorded as the parameters for PSO.  
**Step 4**: The pheromone will volatilize after a while and when an ant travels through a path, it will emit a pheromone. Therefore, the pheromone can be updated as:  
\[\tau_{ij}(t+1)=(1-\rho)*\tau_{ij}(t)+\Delta\tau_{ij} \tag{10}\]  
\[\Delta\tau_{ij}=\sum\limits_{k=1}^{m}\Delta\tau_{ij}^{k} \tag{11}\]  
\[\Delta\tau_{ij}^{k}=\left\{\begin{array}{ll}\frac{Q}{\mathit{fitness}(k)}& \text{if }\text{ant}-\text{k pass path}_{ij}\\ 0&\text{otherwise}\end{array}\right. \tag{12}\]  
where \(\rho\) is the evaporation rate.  
**Step 5**: Repeat steps 1-4 until reaching the necessary number of iterations. Finally, we record the path with the shortest length. The sample after path selection using the ACO algorithm is shown as Figure 6.  
The pseudocode of the ACO-based shortest path selection is described in Algorithm 1.  
Figure 6: Shortest path selection using the ACO algorithm.  
```
1:Input: Set of CHs \(C=\{C_{1},C_{2}\ldots C_{n}\}\), distance between \(CH_{i}\) and \(CH_{j}C_{ij}\), pheromone \(Tabu_{ij}\), set of visited CHs \(C\_Visit\), the probability of next CH for visiting \(P\)
2:Output: Shortest Path \(L=\{A_{1},A_{2}\ldots A_{n}\}\)
3:Step 1:
4: Initialize \(m\) ants, \(C_{ij}\) and \(Tabu_{ij}\)
5:Step 2:
6:for\(i=1\) to Iteration Numberdo
7: Reinitialize \(C\_Visit\)
8: Randomly place \(m\) ants in \(n\) CHs
9:for\(j=1\) to \(m\)do
10:for\(k\)=1 to \(n\)do
11: Calculate \(P\) /* Using formula (8) */
12: Select next visit \(CH_{k}\) according \(P\)
13: Add \(CH_{k}\) into \(C\_visit\)
14:endfor
15:endfor
16: Calculate Fitness(m) /* Using formula (9) */
17: Record the best path \(L\)
18: Update \(Tabu_{ij}\) /* Using formula (10), (11) and (12)*/
19:endfor
20: Return the best path \(L\)
21:Step 3: Stop
```  
**Algorithm 1**Pseudocode of ACO-based shortest path selection