# Energy-Efficient Resource Allocation for Downlink Non-Orthogonal Multiple Access Systems
## 3 Proposed Algorithm
### Power Allocation Problem Based on MPSO  
On the premise of obtaining the subchannel allocation, in this section, we use the MPSO algorithm to solve the optimization problem of power allocation between subchannels and user power allocation within subchannels. Since our objective function is a constrained nonlinear programming problem, we introduce a penalty function to transform the original problem into an unconstrained optimization problem. Then according to the constraint conditions of the original objective function, the penalty function can be obtained as  
\[\phi=\left(\text{max}\left(\sum_{n=1}^{N}p_{n}-P_{tot},0\right)\right)^{2}+ \left(\text{max}(R_{\text{min}}-R_{m,n},0)\right)^{2} \tag{23}\]  
Then, the fitness function of MPSO can be expressed as  
\[f=-\sum_{n=1}^{N}\left(\frac{B_{s}\log_{2}(1+\alpha_{n}p_{n}H_{1,n})+B_{s}\log _{2}\left(\frac{1+p_{n}H_{2,n}}{1+\alpha_{n}p_{n}H_{2,n}}\right)}{p_{n}+p_{c}} \right)+\phi \tag{24}\]  
In addition, we set the particle dimension to 2D. The first D-dimensional particle represents the power value allocated to the subchannel, and the second D-dimensional particle represents the power allocation factor of the secondary user in the channel. The optimal solution is determined by the iterative results of MPSO algorithm, which can be recorded as vector \(\Psi=(p_{1},p_{2},\cdots,p_{N},\alpha_{1},\alpha_{2},\cdots,\alpha_{N})\). The detailed process of power allocation strategy can be referred to in Algorithm 2.  
In MPSO, we improve the calculation method of the inertia weight and acceleration coefficient but do not increase the algorithm time. In addition, when the crossover operator is used to update the particle position, we only add a linear step of computation in the inner loop. Therefore, for the convenience of calculation, we assume that the total number of PSO is \(K\), the particle dimension is \(D\), and the number of iterations is \(T\). Through the description of the optimization process and improvement measures of particle swarm optimization algorithm, we know that the time complexity of the basic PSO algorithm is \(O(K*D*T)\), and the time complexity of the MPSO algorithm is also \(O(K*D*T)\).  
```
1:for\(i=1\) to \(K\)do
2: initialize the position \(x_{i}(t)\) and the velocity \(v_{i}(t)\) within the search range randomly;
3:endfor
4: Evaluate each particle
5: Set the best position of the current particle (pbest) to its initial position
6: Set the current best position of the entire population (gbest) to its initial position
7:while\(t\leq t_{max}\)do
8: Update \(\omega\), \(c_{1}\) and \(c_{2}\) by (19), (20), (21) respectively
9:for\(i=1\) to \(K\)do
10: Update the velocity \(v_{i}(t+1)\) by (17)
11: Generate a random number \(rand\) in [0,1]
12:if\(rand\) <\(C_{R}\)then
13: Update the position \(x_{i}(t+1)\) by (22)
14:endif
15:if\(rand\geq C_{R}\)then
16: Update the position \(x_{i}(t+1)\) by (18)
17:endif
18: Evaluation fitness of particle \(x_{i}(t+1)\)
19:if\(f(x_{i}(t+1))\) >\(f(pbest_{i})\)then
20: Set \(pbest_{i}\)=\(x_{i}(t+1)\)
21:endif
22:if\(f(pbest_{i})\)>\(f(gbest)\)then
23: Set \(gbest\)=\(pbest_{i}\)
24:endif
25:endfor
26:endwhile
```  
**Algorithm 2** Power Allocation using MPSO