# 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 heuristic dynamic clustering method of [16] is used to divide the \(N\) D2D users into \(M\) clusters with the goal of minimizing the co-channel interference of D2D users in each cluster, and \(\text{DC}=\{1,2,\ldots,m,\ldots M\}\) represents the set of D2D clusters. Thus, \(M\) D2D clusters reuse \(M\) channels of cellular users, each D2D cluster reuses one channel, and each cellular user's channel is reused by one D2D cluster. In optimization problem P1, the channel allocation problem of D2D users can be transformed into the channel allocation problem of D2D clusters, and \(y_{mi}\) is used to denote the channel reuse indicator of D2D cluster, when D2D cluster \(m\) reuses the channel of cellular user \(i\), \(y_{mi}=1\), otherwise, \(y_{mi}=0\). Because different D2D clusters reuse channels of different cellular users, inter-layer interference only exists among D2D users in the same cluster. Therefore, the maximum capacity of a single D2D cluster can be solved first, and then the maximum total capacity of D2D users can be obtained by summing the maximum capacity of all D2D clusters. Assuming that D2D cluster \(m\) reuses the channel of cellular user \(i\), that is \(y_{mi}=1\), and the number of D2D users in D2D cluster \(m\) is \(K\). The capacity of D2D cluster \(m\) when it reuses the channel of cellular user \(i\) can be calculated by:  
\[C_{D2D,i}^{m}=\sum\limits_{k=1}^{K}C_{j,i}^{d} \tag{12}\]  
Analyzing Equation (12) we can find that the capacity of D2D cluster is monotone decreasing with regard to \(P_{i}^{c}\), the capacity of D2D cluster can be optimal when the minimum value of \(P_{i}^{c}\) is taken on the premise of satisfying the cellular user's QoS. The lower bound of \(P_{i}^{c}\) can be obtained from Equation (7) as:  
\[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}\]  
The transmission power of D2D user is related to the energy harvested by D2D transmitter at the energy harvesting time. We define a new variable as:  
\[t_{j,i}=\frac{1}{1-\theta_{j,i}} \tag{14}\]  
Then the capacity expression of the D2D user is transformed into: \(C_{j,i}^{d}=T\log\bigg{(}1+SINR_{j,i}^{d}\bigg{)}/t_{j,i}\), the numerator and denominator are all functions of energy harvesting time. Suppose the maximum capacity of D2D cluster \(m\) is \(q_{m,i}^{*}\) and denoted as:  
\[q_{m,i}^{*}=\max\limits_{\{p_{j}^{d},t_{j,i}\}}\frac{\sum\limits_{j=1}^{K} \left(\prod\limits_{k\
eq j}t_{k,j}T\log\bigg{(}1+SINR_{j,i}^{d}\bigg{)} \right)}{\prod\limits_{j=1}^{K}t_{j,i}} \tag{15}\]According to [17], the maximum capacity of D2D cluster \(m\) can be obtained if and only if,  
\[\max\limits_{[P_{j,i}^{d},j;i]}\left\{\sum\limits_{j=1}^{K}\left(\prod\limits_{k \
eq j}t_{k,i}T\log\left(1+SINR_{j,i}^{d}\right)\right)-q_{m,i}^{*}\prod\limits_ {j=1}^{K}t_{j,i}\right\}=0 \tag{16}\]  
Substituting Equations (13) and (14) into Equation (16), the optimization problem (P2) can be written as:  
\[\max\limits_{[P_{j,i}^{d},j;i]}\left\{\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}\right\} \tag{17}\]  
Subject to:  
\[t_{j,i}\geq 1 \tag{18}\]  
\[\frac{1}{t_{j,i}}P_{j,i}^{d}\leq\left(1-\frac{1}{t_{j,i}}\right)\Psi_{4} \tag{19}\]  
\[P_{i}^{c}\leq P_{\max}^{c},\ P_{j,i}^{d}\leq P_{\max}^{d} \tag{20}\]  
where \(\Psi_{1}=h_{ij}rh_{c}h_{jB}/h_{iB}\), \(\Psi_{2}=h_{ij}rh_{c}h_{bB}/h_{iB}+h_{bj}\), \(\Psi_{3}=h_{ij}rh_{c}N_{0}h_{iB}+N_{0}\), \(\Psi_{4}=\xi_{j}P_{BS}h_{Bj}\).