# Power Control and Channel Allocation Algorithm for Energy Harvesting D2D Communications
## 3 Optimal Joint Power Control and Channel Allocation Algorithm
### Optimal Joint Power Control and Channel Allocation Algorithm
The Lagrange function of (17) with respect to Equation (19) is formulated as  
\[L\left(P_{j,i}^{d},t_{j,i}\right)=\sum\limits_{j=1}^{K}\left(\prod\limits_{k \
eq j}t_{k,j}T\log\left(1+\frac{P_{j,i}^{d}h_{j}}{\Psi_{1}P_{j,i}^{d}+\sum \limits_{k\
eq j}\Psi_{3}P_{k,i}^{d}+\Psi_{2}}\right)\right)-q_{m,i}^{*}\prod \limits_{j=1}^{K}t_{j,i}-\lambda\left[\frac{1}{t_{j,i}}P_{j,i}^{d}-\left(1- \frac{1}{t_{j,i}}\right)\Psi_{4}\right] \tag{21}\]  
where \(\lambda\) is the Lagrange multiplier. The D2D users that share the same channel are modeled as non-cooperative game, thus, for D2D user \(j\), the interference of other D2D users can be treated as noise in the solving process. We can get the following formulas through seeking the second partial derivative of Equation (21) with \(P_{j,i}^{d}\) and \(t_{j,i}\)  
\[P_{i}^{*}=\frac{rth_{\text{d}}\bigg{(}P_{j,i}^{d*}I_{jB}+\sum\limits_{k\
eq j} P_{k,j}^{d*}h_{kB}+N_{0}\bigg{)}}{h_{iB}} \tag{13}\]0  
\[P_{i}^{*}=\frac{rth_{\text{d}}\bigg{(}P_{j,i}^{d*}I_{jB}+\sum\limits_{k\
eq j} P_{k,j}^{d*}h_{kB}+N_{0}\bigg{)}}{h_{iB}} \tag{13}\]1  
\[P_{i}^{*}=\frac{rth_{\text{d}}\bigg{(}P_{j,i}^{d*}I_{jB}+\sum\limits_{k\
eq j} P_{k,j}^{d*}h_{kB}+N_{0}\bigg{)}}{h_{iB}} \tag{13}\]2  
When \(\lambda=0\), Equation (26) has no solution because of \(\sum\limits_{k\
eq j}\Psi_{3}P_{k,i}^{d}+\Psi_{2}\
eq 0\); when \(\lambda\
eq 0\), we can obtain the optimal value of \(P_{j,i}^{d}\) and \(t_{j,i}\) according to the equation set consists of Equations (24)-(26):  
\[P_{i}^{*}=\frac{rth_{\text{d}}\bigg{(}P_{j,i}^{d*}I_{jB}+\sum\limits_{k\
eq j} P_{k,j}^{d*}h_{kB}+N_{0}\bigg{)}}{h_{iB}} \tag{13}\]3  
\[P_{i}^{*}=\frac{rth_{\text{d}}\bigg{(}P_{j,i}^{d*}I_{jB}+\sum\limits_{k\
eq j} P_{k,j}^{d*}h_{kB}+N_{0}\bigg{)}}{h_{iB}} \tag{13}\]4  
where \(\Delta=h_{j}^{2}\Bigg{(}\sum\limits_{k\
eq j}\Psi_{3}P_{k,i}^{d}+\Psi_{2} \Bigg{)}^{2}+\frac{4\Psi_{4}h_{j}T\big{(}\Psi_{1}{}^{2}+\Psi_{1}h_{j}\big{)} \Big{(}\sum\limits_{k\
eq j}\Psi_{3}P_{k,i}^{d}+\Psi_{2}\Bigg{)}}{\ln 2\cdot \sigma_{m,j}^{d}}>0\). When \(K\) D2D users reuse the same channel, the transmission power of D2D user \(j\) varies while other \(K-1\) D2D users' transmission power change, meanwhile, the transmission power of D2D user \(j\) also affects the transmission power of other \(K-1\) D2D users. Therefore, the transmission power of D2D users is dynamically adjusted by using the joint iterative optimization method until the optimal values are obtained. After obtaining the optimal transmission power and energy harvesting time of \(K\) D2D users in the cluster when each D2D cluster reuses the \(M\) channels of cellular users respectively, the KM algorithm is used to achieve the optimal matching between D2D clusters and cellular users' channels [18]. The optimization problem to maximize the total capacity of D2D users by allocating the cellular users' channels for D2D clusters can be transformed into the optimal matching problem of a weighted bipartite graph, and the KM algorithm can transform the problem of the maximum weight matching into the problem of completeness matching by giving a label to each vertex. As shown in Figure 2, D2D clusters set and cellular users set respectively represents two mutually disjointed vertex sets in the bipartite graph, are two mutually disjointed vertex sets in the dichotomy graph respectively. When the D2D cluster \(m\) reuses the channel of cellular user \(i\) it will create a connecting line between the vertex \(m\) and the vertex \(i\), and the weight value on the connecting line is \(C_{D2D,i}^{m}\), which indicates the capacity of the D2D cluster \(m\) when it reuses the channel of cellular user \(i\). According to the above analysis, the optimal joint power control and channel allocation algorithm is shown in Algorithm 1. _Complexity Analysis:_ The proposed optimal joint power control and channel allocation algorithm consists of three steps. In first step, clustering \(N\) D2D users into \(M\) clusters in \(N-M\) iterations, the complexity is \(O(N-M)\).