## 3 PSO-Based Resource Allocation  
PSO algorithms are used to simulate the bird swarm foraging situation, which involves the search for food space with particles. Each particle simulates a bird in addition to its own normal movement, which also follows its own best moving experience. The personal best experience is denoted by \(\boldsymbol{s}^{\textit{bst}}\). After this, the global experience is referred from the group's best moving experience as the global best, which is denoted by \(\boldsymbol{s}^{\textit{bst}}\). According to the above-mentioned types of data with an iterative evolution, the final convergence obtains the optimal solution. In this study, the number of particles is \(M\) and the value of the particle's target function points to the efficiency of the iteration. The particle with the highest target function value is the best solution. A simplified formula for particle swarm optimization, which is called SPSO, is proposed to improve the performance of the resource allocation for D2D communication systems. The SPSO algorithm performs the optimization of resource block allocation, while all the relay stations cover the UEs of the allocated RB as the particle \(\boldsymbol{s}_{i}\). The particle is uniformly distributed in the solution space. A total of 13 RBs and 3 relay stations are available. The moving velocity of the first particle in the (\(g+1\))-generation is expressed by:  
\[\boldsymbol{v}_{i}^{g+1}\ =\ c_{1}\times rand()\times(\boldsymbol{s}^{\textit{ bst}}-\boldsymbol{s}_{i}^{g})+c_{2}\times rand()\times(\boldsymbol{s}^{\textit{ bst}}-\boldsymbol{s}_{i}^{g}), \tag{12}\]  
where \(\boldsymbol{s}_{i}^{g}\) is the position vector of the \(i\)th particle in \(g\)-generation; \(\boldsymbol{s}_{i}^{g}\ =\ \left[\boldsymbol{s}_{i_{1}}^{g},\boldsymbol{s}_{i_{2}}^{g}, \boldsymbol{s}_{i_{3}}^{g}\right]\); and \(\boldsymbol{s}_{i_{l}}^{g}\) is the position of the \(i\)th particle of the \(l\)th relay station. The local optimal positions of all particles are denoted by \(\boldsymbol{s}^{\textit{bst}}\ =\ \left[\boldsymbol{s}^{\textit{bst}}, \boldsymbol{s}^{\textit{bst}}{}_{2},\boldsymbol{s}^{\textit{bst}}{}^{\textit{ bst}}\right]\), where \(\boldsymbol{s}^{\textit{bst}}{}_{l}\) is the local optimal position of the \(l\)th relay station. \(\boldsymbol{s}^{\textit{bst}}\) is the global optimal positions of all particles in the nearest generation. Both \(c_{1}\) and \(c_{2}\) are the acceleration coefficients, which is called the individual factor and social factor, respectively. Generally, the factors are set by \(c_{1}\ =\ 2\) and \(c_{2}\ =\ 2\). The \(rand()\) is the random function that is uniformly distributed in the range of [0,1]. Moreover, the position updating at (\(g+1\))-generation can be expressed by:  
\[\boldsymbol{s}_{i}^{g+1}\ =\ \boldsymbol{s}_{i}^{g}+\boldsymbol{v}_{i}^{g+1}. \tag{13}\]  
In SPSO, all the \(K\) UEs distributed in the coverage area of relay station are allocated RBs and composited as a particle \(\boldsymbol{s}_{i}\) with an array of \(1\times K\). The \(M\) particles are uniformly distributed in the solution space, while a total of 13 RBs are available to be allocated to the UEs in three relay stations. In SPSO, the object function is defined by:  
\[f_{\mathbb{C}}\ =\ \sum\limits_{u_{l}\ =\ 1}^{K}y_{u_{l}}, \tag{14}\]  
where \(\sum\limits_{u_{l}\ =\ 1}^{K}y_{u_{l}}\) is the system capacity and \(y_{u_{l}}\) is defined by:  
\[y_{u_{l}}\ =\ \left\{ \begin{array}{cc}1,&\sum\limits_{n\ =\ 1}^{N}x_{u_{l}}^{(n)}R_{u_{l}}^{(n)}\geq R_{th}\\ 0,&\text{otherwise}\end{array} \right.. \tag{15}\]  
The procedure of SPSO algorithms applied in this study can be described by the following example with \(K\) = 18 and \(M\) = 3:  
1. Initialization (\(g\) = 1): Generate the positions of \(M\) particles with \(\boldsymbol{s}_{i}^{1}\), \(i\) = 1,..., \(M\) and velocity; and \(\boldsymbol{v}_{i}^{1}\), \(i\) = 1,..., \(M\). One example of the position of a particle \(\boldsymbol{s}_{i}^{1}\) can expressed by \(\boldsymbol{s}_{1}^{1}\) = [1 1 3 4 5 6 ] 2 8 3 11 11 13 ] 1 6 7 8 4 9]. 2.