## 3 PSO-Based Optimization of RIS Phase Shifts
### PSO Main Loop  
At the beginning of the main loop of the PSO algorithm, the swarm status is updated by the initial settings, denoted as  
\[\theta_{1}[m+1] =\theta_{1}[m]+v_{1} \tag{16}\] \[\theta_{2}[m+1] =\theta_{2}[m]+v_{2}.\]After updating the swarm status at the beginning of each loop, the swarm velocity in each loop can be denoted using random parameter \(s=\text{rand}(\cdot)\) between 0 and 1, and constants \(c_{1}\) and \(c_{2}\)  
\[\begin{split} v_{1}[m+1]&=v_{1}[m]+c_{1}s(\theta_{P_{ 1}}[m]-\theta_{1}[m])+\\ c_{2}s(\theta_{G_{1}}[m]-\theta_{1}[m])\\ \end{split} \tag{17}\]  
\[\begin{split} v_{2}[m+1]&=v_{2}[m]+c_{1}s(\theta_{ P_{2}}[m]-\theta_{2}[m])+\\ c_{2}s(\theta_{G_{2}}[m]-\theta_{2}[m]).\end{split} \tag{18}\]  
After the swarm position and velocity are all updated, the current pointing error loss can be gained for further comparison. Furthermore, the cross boundary treatment [45] is included as a limitation for the searching swarm could not move outside the search zone, denoted as  
\[\theta_{1}[m]=\left\{\begin{aligned} \min(\theta_{max},2\theta_{min}- \theta_{1}[m]),&\quad\theta_{1}[m]<\theta_{min}\\ \max(\theta_{min},2\theta_{max}-\theta_{1}[m]),&\quad \theta_{1}[m]>\theta_{max},\end{aligned}\right. \tag{19}\]  
\[\theta_{2}[m]=\left\{\begin{aligned} \min(\theta_{max},2\theta_{min}- \theta_{2}[m]),&\quad\theta_{2}[m]<\theta_{min}\\ \max(\theta_{min},2\theta_{max}-\theta_{2}[m]),&\quad \theta_{2}[m]>\theta_{max},\end{aligned}\right. \tag{20}\]  
where the search boundary is \(\Theta[n](\theta_{1}[n],\theta_{2}[n])\in(-\pi,\pi)\), and normally the search zone is set to include all the phase shifts, which make the beam footprint overlap with the PD. After confirming that the swarm will not be outside the search boundary, the local best position of the swarm should be updated. The update method uses a comparison of the target function output between the current swarm and the local best swarm position, then updates the local best pointing error loss if it's less than the current output. Similar to updating the global best, by comparing the global best and local best pointing error loss output, the most optimized phase shifts and their corresponding pointing error loss can be found. The whole algorithm is written as pseudocode in Algorithm 1.  
```
1:input the parameter set of the PSO \(P,M,c_{1},c_{2},v_{1},v_{2}\)
2:derive the rotation quaternion and its rotation matrix.
3:extract the leading angles of phase shifts.
4:input the initial local and global best positions \(\theta_{P_{1}},\theta_{P_{2}},\theta_{G_{1}},\theta_{G_{2}}\).
5:for\(m=1,2,\dots,M\)
6:for\(p=1,2,\dots,P\)
7:update the swarm status and swarm velocity as (16) and (17).
8:output the current pointing error loss \(h_{p}[m]\)
9:implement the cross boundary treatment as (18) and (19).
10:compare the current swarm output and the local best swarm output.
11:if\(h_{p}(\theta_{1}[m],\theta_{2}[m])>h_{p}(\theta_{P_{1}}[m],\theta_{P_{2}}[m])\)
12:update the current angles and output as personal best angles and output
13:endif
14:compare the personal best swarm output and the global best swarm output.
15:if\(h_{p}(\theta_{P_{1}}[m],\theta_{P_{1}}[m])>h_{p}(\theta_{G_{1}}[m],\theta_{G_{2}}[m])\)
16:update the personal angles and output as global best angles and output
17:endif
18:endfor
19:endfor
20:output the global best outcome as the maximum pointing error.
```  
**Algorithm 1****PSO**