# Enabling Heterogeneous IoT Networks over 5G Networks with Ultra-Dense Deployment--Using MEC/SDN
## 4 Energy-Aware and Latency-Aware Offloading Scheme
### Energy-Aware and Latency-Aware Offloading Algorithm for IoT Networks
The decision engine of the end device calculates the required time and energy for handling the task locally, \(T_{exc-LL}\) and \(E_{L-exc-LL}\), based on the currently available resources. \[T_{exc-LL}=\frac{N_{CPU-CYC}}{R_{LL}}\quad,\ R_{LL}\in f_{LL}, \tag{24}\]  
\[E_{L-exc-LL}=N_{CPU-CYC}\delta_{LL}, \tag{25}\]  
\[E_{C-LL}=E_{LL}-E_{L-exc-LL}, \tag{26}\]  
\[D_{Off-E-LL}=I(E_{C-LL},E_{thr-LL})=\left\{ \begin{array}{ll}1&\text{IF }\left(E_{C-LL}\ \leq E_{thr-LL}\right)\\ 0&\text{IF }\left(E_{C-LL}\ >\ E_{thr-LL}\right)\end{array} \right., \tag{27}\]  
\[D_{Off-T-LL}=I\big{(}T_{exc-LL},T_{\text{QoS}}\big{)}=\left\{ \begin{array}[] {ll}0&\text{IF }\left(T_{exc-LL}\leq T_{\text{QoS}}\right)\\ 1&\text{IF }\left(T_{exc-LL}>\ T_{\text{QoS}}\right)\end{array} \right., \tag{28}\]  
\[D_{Off-LL}=D_{Off-E-LL}\bigvee\ D_{Off-T-LL}, \tag{29}\]  
For a positive offloading decision, the LL device offloads the computing task to the first offloading level, i.e., GW device. The GW device receives the offloading request and processes it. The decision engine of the GW device decides either to handle the offloaded task at the GW device or to offload it to the higher level, i.e., the HL device. GW performs similar steps as LL device to make the decision according to (30-37)  
\[T_{exc-GW}=\frac{N_{CPU-CYC}}{R_{GW}}\quad,\ R_{GW}\in f_{GW}, \tag{30}\]  
\[T_{h-GW}=T_{exc-GW}+\ T_{tx}+T_{rx}, \tag{31}\]  
\[E_{L-exc-GW}=N_{CPU-CYC}\delta_{GW}, \tag{32}\]  
\[E_{h-GW}=E_{L-exc-GW}+T_{tx}P_{LL}\eta_{D2D}, \tag{33}\]  
Figure 6: Heterogenous task handling and offloading over a D2D network. \[E_{C\text{-}GW}=E_{C\text{-}GW}-E_{\text{-}GW}, \tag{34}\]  
\[D_{Off-E-GW}=I(E_{C\text{-}GW},E_{thr-GW})=\left\{ \begin{array}{ll}1&\text{IF }\ (E_{C-GW}\leq E_{thr-GW})\\ 0&\text{IF }\ (E_{C-GW}>\ E_{thr-GW})\end{array} \right., \tag{35}\]  
\[D_{Off-T-LL}=I\big{(}T_{exc-GW},T_{\text{QoS}}\big{)}=\left\{ \begin{array}{ ll}0&\text{IF }\ (T_{exc-GW}\leq\ T_{\text{QoS}})\\ 1&\text{IF }\ (T_{exc-GW}>\ T_{\text{QoS}})\end{array} \right., \tag{36}\]  
\[D_{Off-GW}=D_{Off-E-GW}\bigvee\ D_{Off-T-GW}, \tag{37}\]  
If the GW device fails to handle the requested task, it offloads the task to the nearby HL device. HL device receives the offloading request and processes it to either handle the requested task or offload it to the C-MEC server. The decision engine performs similar steps as LL and GW devices to make the energy and time decisions of offloading. \[T_{exc\text{-}HL}=\frac{N_{CPU\text{-}CYC}}{R_{HL}}\quad,\ R_{HL}\in f_{HL}, \tag{38}\]  
\[T_{h\text{-}HL}=T_{exc\text{-}HL}+\ T_{tx}+T_{rx}, \tag{39}\]  
\[E_{L\text{-}exc\text{-}HL}=N_{CPU\text{-}CYC}\delta_{HL}, \tag{40}\]  
\[E_{h\text{-}HL}=E_{L\text{-}exc\text{-}HL}+T_{tx}P_{GW}\eta_{\text{}D2D}, \tag{41}\]  
\[E_{C\text{-}HL}=E_{C\text{-}HL}-E_{h\text{-}HL}, \tag{42}\]  
\[D_{Off-E-HL}=I(E_{C\text{-}HL},E_{thr-HL})=\left\{ \begin{array}{ll}1&\text{ IF }\ (E_{C-HL}\leq E_{thr-HL})\\ 0&\text{IF }\ (E_{C-HL}>\ E_{thr-HL})\end{array} \right., \tag{43}\]  
\[D_{Off-T-HL}=I\big{(}T_{exc-HL},T_{\text{QoS}}\big{)}=\left\{ \begin{array}{ ll}0&\text{IF }\ \big{(}T_{exc-HL}\leq\ T_{\text{QoS}}\big{)}\\ 1&\text{IF }\ (T_{exc-HL}>\ T_{\text{QoS}})\end{array} \right., \tag{44}\]  
\[D_{Off-HL}=D_{Off-E-HL}\bigvee\ D_{Off-T-HL}, \tag{45}\]  
For a positive offloading decision of the HL device, i.e., \(D_{Off\text{-}HL}\) = 1, the task is offloaded to the C-MEC server at the edge of the RAN.