# 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 Cellular Network  
Once a C-MEC server receives an offloading request from any connected devices, it makes certain processes based on Algorithm 3 to decide whether to handle or offload the received computing task. The C-MEC server has the same structure proposed in [36]. At first, the profiler extracts the task data from the received request. These data include the QoS latency, \(T_{\text{QoS}}\), the task size in bits, \(Z\), and the communication latency, \(T_{comm}\), defined by the connected base station. The profiler passes these data to the C-MEC controller, which also receives the information about the current energy level and the available resources of the C-MEC from the resource scheduler. The controller takes its decision of handling or offloading the requested task based on the received data. The C-MEC controller calculates the total energy consumption for handling the task, \(E_{h\text{-}C\text{-}MEC}\), and then calculates the energy level of the C-MEC, \(E_{C\text{-}C\text{-}MEC}\), if the task is handled. If the remaining energy, \(E_{C\text{-}C\text{-}MEC}\), after handling the task, is higher than the predefined threshold level of energy of C-MEC, \(E_{thr\text{-}C\text{-}MEC}\), the controller then checks the latency to make the time decision of offloading. However, if the remaining energy is less than the pre-announced threshold level, the controller decides to offload the computing task to the core network cloud. \[T_{exc\text{-}C\text{-}MEC}=\frac{N_{CPU\text{-}CYC}}{R_{C\text{-}MEC}}\quad, \ R_{C\text{-}MEC}\in f_{C\text{-}MEC}, \tag{46}\]  
\[T_{h\text{-}C\text{-}MEC}=T_{exc\text{-}C\text{-}MEC}+\ T_{tx}+T_{rx}, \tag{47}\]  
\[E_{L\text{-}exc\text{-}C\text{-}MEC}=N_{CPU\text{-}CYC}\delta_{C\text{-}MEC}, \tag{48}\]  
\[E_{h\text{-}C\text{-}MEC}=E_{L\text{-}exc\text{-}C\text{-}MEC}+T_{tx}P_{HL} \eta_{C}, \tag{49}\]\[E_{\text{C-C-MEC}}=E_{\text{C-C-MEC}}-E_{\text{H-C-MEC}}, \tag{50}\]  
\[D_{\text{Off-E-C-MEC}}=I(E_{\text{C-C-MEC}},E_{\text{Hr-C-MEC}})=\left\{ \begin{array} []{ll}1&\text{IF}\ \left(E_{\text{C-C-MEC}}\leq E_{\text{Hr-C-MEC}}\right)\\ 0&\text{IF}\ \left(E_{\text{C-C-MEC}}>E_{\text{Hr-C-MEC}}\right)\end{array}  \right., \tag{51}\]  
\[D_{\text{Off-T-C-MEC}}=I\big{(}T_{\text{exc-C-MEC}},T_{\text{QoS}}\big{)}= \left\{ \begin{array}{ll}0&\text{IF}\ \left(T_{\text{exc-C-MEC}}\leq T_{\text{QoS}}\right)\\ 1&\text{IF}\ \left(T_{\text{exc-C-MEC}}>T_{\text{QoS}}\right)\end{array}  \right., \tag{52}\]  
\[D_{\text{Off-C-MEC}}=D_{\text{Off-E-C-MEC}}\bigvee\ D_{\text{Off-T-C-MEC}}, \tag{53}\]  
```
1:Initialize \(E_{\text{Hr-C-MEC}}\)
2:Calculate \(T_{\text{exc-C-MEC}}\), \(T_{\text{h-C-MEC}}\)
3:If (\(T_{\text{h-C-MEC}}\leq T_{\text{QoS}}\))
4: \begin{tabular}{l l}
5: & \(D_{\text{Off-T-C-MEC}}=0\) \\
6: & & Calculate \(E_{\text{I-exc-C-MEC}}\), \(E_{\text{h-C-MEC}}\), \(E_{\text{C-C-MEC}}\) \\
7: & & If (\(E_{\text{C-IC-MEC}}>E_{\text{Hr-C-MEC}}\)) \\
8: & & else & \(D_{\text{Off-E-C-MEC}}=0\) \\
9: & & \(D_{\text{Off-E-C-MEC}}=1\) \\
10: & & end if \\
11: & else & \\
12: & & \(D_{\text{Off-T-C-MEC}}=1\) \\
13: & end if & \\
14: & \(D_{\text{Off-C-MEC}}=D_{\text{Off-T-C-MEC}}\)\({}^{*}D_{\text{Off-E-C-MEC}}\) \\
15: & & If (\(D_{\text{Off-C-MEC}}==0\)) \\
16: & & C-MEC server accepts offloading request, and handles the computing task. \\
17: & else & \\
18: & Offload Task to CN \\
19: & end if & \\ \end{tabular} 

**Algorithm 3** Latency-aware and energy-aware offloading algorithm for mobile edge computing (MEC)-based cellular networks. ## 5 Performance Evaluation

In this part, the proposed system and the developed algorithms are evaluated over reliable environments. Various scenarios are considered to indicate the performance. The section is divided into two subsections; simulation setup and simulation results. In the first subsection, the main steps of the simulation setup process are introduced, and the simulation parameters are defined. The considered simulation scenarios are well defined.