# 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
\begin{array}{ ll}0&\text{IF }\left(T_{exc-IoT}\leq T_{\text{QoS}}\right)\\ 1&\text{IF }\left(T_{exc-IoT}>T_{\text{QoS}}\right)\end{array} ,\right. \tag{6}\]

\[D_{Off-IoT}=D_{Off-E-IoT}\bigvee\ D_{Off-T-IoT}, \tag{7}\]If the decision engine decides to offload, i.e., \(D_{Off\_IoT}=1\), the computing task is offloaded to the I-MEC server. The I-MEC server deploys the same server structure developed in [36]. The decision engine of the I-MEC server receives the offloading request from the IoT device and decides either to handle the task at the I-MEC server or to offload the task to the higher offloading level, i.e., C-MEC server. The I-MEC server's decision engine calculates the execution time required to execute the requested task, \(T_{exc\_I-MEC}\), and then the total time required to handle it, \(T_{h\_I-MEC}\)[36]. \[T_{exc\_I-MEC}=\frac{N_{CPU\_CYC}}{R_{I-MEC}}\quad,\ R_{I-MEC}\in f_{I-MEC}, \tag{8}\]

\[T_{h\_I-MEC}=T_{exc\_I-MEC}+\ T_{tx}+T_{rx}, \tag{9}\]

The decision engine of the I-MEC server calculates the total energy consumed to handle the requested task at the I-MEC server, \(E_{h\_I-MEC}\). \[E_{L-exc\_I-MEC}=N_{CPU\_CYC}\delta_{I-MEC}, \tag{10}\]

\[E_{h-I-MEC}=E_{L-exc\_I-MEC}+T_{tx}P_{IoT}\eta_{IoT}, \tag{11}\]

\[E_{C-I-MEC}=E_{I-MEC}-E_{h-I-MEC}, \tag{12}\]

Then, the I-MEC server's decision engine calculates the binary energy and time decision values of offloading, \(D_{Off\_E-I-MEC}\) and \(D_{Off\_T-I-MEC}\). The energy decision is calculated by comparing the residual energy, after handling the requested task, with the threshold level of energy, \(E_{thr\_I-MEC}\), as presented in (13). However, the time decision is calculated by comparing the total time required to handle the requested task with the required QoS latency, \(T_{QoS}\)[36; 39]. \[D_{Off\_E-I-MEC}=I(E_{C-I-MEC},E_{thr\_I-MEC})=\left\{ \begin{array}{ll}1& \text{If}\ \left(E_{C-I-MEC}\leq E_{thr\_I-MEC}\right)\\ 0&\text{If}\ \left(E_{C-I-MEC}>\ E_{thr\_I-MEC}\right)\end{array} \right., \tag{13}\]

\[D_{Off\_T-I-MEC}=I\big{(}T_{h-I-MEC},T_{QoS}\big{)}=\left\{ \begin{array}{ll}0 &\text{If}\ \left(T_{h-I-MEC}\leq\ T_{QoS}\right)\\ 1&\text{If}\ \left(T_{h-I-MEC}>\ T_{QoS}\right)\end{array} \right., \tag{14}\]

\[D_{Off\_I-MEC}=D_{Off\_E-I-MEC}\ \bigvee\ D_{Off\_T-I-MEC}, \tag{15}\]

For a negative offloading decision, i.e., \(D_{off\_I-MEC}=0\), the task is handled at the I-MEC server. However, for a positive offloading decision, i.e., \(D_{off\_I-MEC}=1\), the task is offloaded to the higher level, i.e., C-MEC server [36; 40]. Once the C-MEC server receives an offloading request, the decision engine of the server performs similar calculations as the I-MEC server. At first, the total time and energy required to handle the requested task, \(T_{h\_C-MEC}\) and \(E_{h\_C-MEC}\), is calculated. \[T_{exc\_C-MEC}=\frac{N_{CPU\_CYC}}{R_{C-MEC}}\quad,\ R_{C-MEC}a\in f_{C-MEC}, \tag{16}\]

\[T_{h\_C-MEC}=T_{exc\_C-MEC}+\ T_{tx}+T_{rx}, \tag{17}\]

\[E_{L-exc\_C-MEC}=N_{CPU\_CYC}\delta_{C-MEC}, \tag{18}\]

\[E_{h\_C-MEC}=E_{L-exc\_C-MEC}+T_{tx}P_{S}\eta_{S}, \tag{19}\]

\[E_{C-C-MEC}=E_{C\_MEC}-E_{h\_C-MEC}, \tag{20}\]

The C-MEC server's decision engine makes the energy and time decisions of offloading, \(D_{Off\_E-C-MEC}\) and \(D_{Off\_T-C-MEC}\), according to (21, 22). \[D_{Off\_E-C-MEC}=I(E_{C-C-MECEC},E_{thr\_C-MEC})=\left\{ \begin{array}{ll}1& \text{If}\ \left(E_{C-C-MEC}\leq E_{thr\_C-MEC}\right)\\ 0&\text{If}\ \left(E_{C-C-MEC}>\ E_{thr\_C-MEC}\right)\end{array} \right., \tag{21}\]\[D_{Off-T-C-MEC}=I\big{(}T_{h-C-MEC},T_{QoS}\big{)}=\left\{ \begin{array}{ll}0& \text{IF }\left(T_{h-C-MEC}\leq T_{QoS}\right)\\ 1&\text{IF }\left(T_{h-C-MEC}>T_{QoS}\right)\end{array} \right., \tag{22}\]

\[D_{Off-C-MEC}=D_{Off-E-C-MEC}\bigvee\,D_{Off-T-C-MEC}, \tag{23}\]

For a negative offloading decision, the task is handled at the edge server of cellular networks, i.e., C-MEC. Otherwise, the task is offloaded to the core networks cloud.