## 5 Optimal Offline Power Allocation for Time-Varying Channel  
Now we consider the time-varying channel scenario, and the instantaneous transmission rate is still (4), so the optimization problem P3 is  
\[\max_{\{s_{i}r_{i}l_{i}\}}\frac{1}{N}\sum_{i=1}^{N}l_{i}R(\frac{E_{i}-s_{i}+r_{i }-al_{i}}{l_{i}},h_{i}) \tag{33}\]  
\[\text{s.t.}\sum_{j=1}^{i}\left(\alpha s_{j}-r_{j}\right)\geq 0,\;\;i=1,\ldots,N \tag{34}\]  
\[E_{i}-s_{i}+r_{i}-al_{i}\geq 0,\;i=1,\ldots,N \tag{35}\]  
\[s_{i}\geq 0,r_{i}\geq 0,0\leq l_{i}\leq 1,\;\;i=1,\ldots,N \tag{36}\]  
P3 is still a convex optimization problem, and so we can also use the Lagrange multiplier method and the KKT conditions:  
\[\frac{\partial L}{\partial s_{i}}=-\frac{h_{i}}{1+h_{i}{p_{i}}^{*}}+\alpha \sum_{j=i}^{N}\lambda_{j}-\mu_{i}+\
u_{i}=0,\;i=1,\ldots,N \tag{37}\]  
\[\frac{\partial L}{\partial r_{i}}=\frac{h_{i}}{1+h_{i}{p_{i}}^{*}}-\sum_{j=i} ^{N}\lambda_{j}+\mu_{i}+_{i}=0,\;i=1,\ldots,N \tag{38}\]  
\[\frac{1}{2}\log(1+h_{i}{p_{i}}^{*})=\frac{h_{i}({p_{i}}^{*}+a)}{2(1+h_{i}{p_{ i}}^{*})}+a\mu_{i}-\theta_{i}+\sigma_{i},\;\;i=1,\ldots,N \tag{39}\]  
Other KKT conditions are the same as in (10), (11) and (12).  
From (37) and (38), we can see that  
\[{p_{i}}^{*}=\left[V-\frac{1}{h_{i}}\right]^{+} \tag{40}\]  
where \(V\) is the water level and can be written as  
\[V=\frac{1}{\alpha\sum_{j=i}^{N}\lambda_{j}+\
u_{i}} \tag{41}\]  
For (41), we still use the definitions in [13]:  
\[V_{si}=\frac{1}{\alpha\sum_{j=i}^{N}\lambda_{j}},\;i=1,\ldots,N \tag{42}\]  
\[V_{ri}=\frac{1}{\sum_{j=i}^{N}\lambda_{j}},\;i=1,\ldots,N \tag{43}\]  
In (42) and (43), \(V_{si}\) and \(V_{ri}\) are two water-level thresholds and the function of them is similar with those of \(p_{si}\) and \(p_{ri}\). Obviously,  
\[V_{ri}=\alpha V_{si} \tag{44}\]  
Like the time-invariant channel, the optimal solution should satisfy  
\[{p_{i}}^{*}=\min(V_{si}-1/h_{i},\max(V_{ri}-1/h_{i_{r}}\left[E_{i}/{l_{i}}^{*} -a\right]^{+})) \tag{45}\]  
\[{s_{i}}^{*}=\left[E_{i}/{l_{i}}^{*}-a-{p_{i}}^{*}\right]^{+},{r_{i}}^{*}=\left[ {p_{i}}^{*}-\left(E_{i}/{l_{i}}^{*}-a\right)\right]^{+} \tag{46}\]Unlike the time-invariant channel, the channel may be in a very bad situation in some time slots where \(\frac{1}{h_{i}}>V_{si}\). If this happens, the power allocation should be  
\[{p_{i}}^{*}=0,{s_{i}}^{*}=E_{i},{r_{i}}^{*}=0 \tag{47}\]  
The analysis of (39) is similar to the previous one, but the difference is that the value of \(p_{o}\) changes with \(h_{i}\), and so \({p_{i}}^{*}\) and \(l_{i}^{*}\) should satisfy  
\[{p_{i}}^{*}=\max(p_{o}(h_{i}),E_{i}-{s_{i}}^{*}+{r_{i}}^{*}-a) \tag{48}\]  
\[{l_{i}}^{*}=\frac{E_{i}-{s_{i}}^{*}+{r_{i}}^{*}}{{p_{i}}^{*}+a} \tag{49}\]  
In addition, the policy in the last time slot should be  
\[{p_{N}}^{*}=\max(p_{o}(h_{N}),E_{N}+B_{N-1}-a) \tag{50}\]  
\[{l_{N}}^{*}=\frac{E_{N}+B_{N-1}}{{p_{N}}^{*}+a} \tag{51}\]  
According to the above analyses, the optimal power allocation algorithm also has a three-threshold structure and depends on the values of \(V_{si}\), \(V_{ri}\), \(h_{i}\) and \(p_{o}(h_{i})\). Algorithm 2 is as follows:  
```
1) Calculate \(p_{o}(h_{i}),i=1,\ldots,N\), initialize: \(p_{i}=0,r_{i}=0,s_{i}=0,l_{i}=1\), \(B_{i}=0,i=1,\ldots,N\), \(ii=1\), \(k=1\);
2) for \(i=ii\):1:\(N\)do  Linear search the largest \(V_{s,k}\) and \(V_{r,k}\) which can make \(p_{i},s_{i},r_{i},i=ii,\ldots,N\) feasible in (45), (46) or in (47) and the condition \(B_{i}\geq 0,i=ii,\ldots,N\) should be satisfied;  for \(j=ii:N\) if \(B_{j}\leq\epsilon\) (\(\epsilon\) is an arbitrarily small value) or \(j\geq N-1\) for \(i=ii:j\) \(p_{i},s_{i},r_{i}\) are decided by \(V_{s,k}\) and \(V_{r,k}\); \(B_{i}=B_{i-1}+\alpha s_{i}-r_{i}\); \(V_{si}=V_{s,k}\); \(V_{ri}=V_{r,k}\);  end for \(ii=j+1\); \(k=k+1\);  break;  end if  end for
```  
**Algorithm 2** Optimal solution for the time-varying channel