# Two-Stage Optimal Task Scheduling for Smart Home Environment Using Fog Computing Infrastructures
## 3 Proposed Model
### Problem Formulation  
The mathematical formulation of the task scheduling model is presented in this section. The proposed architecture contains n independent tasks \(T=T1,T2,\ldots,Tn\), as depicted in Figure 2.  
\[EC=E\times(T_{c}-T_{r})+HP_{r} \tag{1}\]  
Here, \(T\) denotes the number of tasks, and \(T_{s}\) and TE denote the start and end of the execution. There are two states for the fog nodes: idle and busy. Whenever the fog node is not processing any task, it is said to be in idle mode, and when it is executing any task, it is said to be in busy mode. The fog device's energy consumption is calculated in iFogSim using Equation (3). The energy consumption of the fog nodes (\(EC\)) is calculated using the current energy consumption E, the current time \(T_{c}\), the updated time during the last utilization \(T_{r}\), and the host power utilization \(HP_{r}\). HPR is the host power in the last utilization. Energy may be calculated using the total of all the hosts' power during the set length of time for any of the fog devices. The smart home case study deals with a large number of sensor devices; therefore, network congestion caused by these devices is very common, which can degrade the overall performance of the system. Thus, by allocating the loads to several fog nodes, we minimized the network congestion in this system. The network usage is calculated using Equation (4), where \(NU\) is the network utilization, \(n\) is the total number of tasks, \(L_{i}\) is the latency, and \(NS\) is the network size of the ith task. Furthermore, the fitness function is calculated to check whether the model needs to be trained again or should enter the optimization phase. The fitness function is provided in Equation (5), and it depends on the latency and network usage of the network. High values of the latency and network usage reduce the fitness function, and correspondingly, we have to satisfy the fitness function. The grater the value of the fitness function, the better solution is. \[NU=\sum_{i=1}^{n}L_{i}\times NS_{i} \tag{2}\]  
\[FitnessFunction=\frac{1}{latency+NU} \tag{3}\]  
Figure 3 provides the entire data flow of the machine-learning-based optimal model for task allocation. The set of tasks is generated by various IoT sensors used in the smart home environment. Furthermore, Table 2 gives the proposed ML model for optimal task allocation in smart homes and provides the number of tasks that we considered for training the ML model, where the naive Bayes multi-classifier was used for the training and testing for the allocation of the tasks on the fog nodes. Before applying the naive Bayes model, tasks were ranked according to the ranking table given in Table 3. According to Table 3, a task is ranked first if it does not tolerate any delay with respect to the services and it occupies less memory space and consumes less computing resource. As naive Bayes gives the best results for classification, here, while training the machine learning model, we used the constraints mentioned in the ranking table to correctly classify the task with different ranks, and similarly, we again used naive Bayes for the allocation of the VMs. Being a probabilistic classifier, it predicts the allocation on the basis of the probability given in Equation (6). \[P(VM_{i}|tk=compatible=\frac{p(tk=compatible|VM_{i})(VM_{i})}{p(tk= compatibility)}) \tag{4}\]  
For all VMs, \(P(tk=compatible)\) is constant and obtained according to Equation (7). If (resource capacity of VMs > resource capacity of task):  
\(P(tk=compatible)=1\). Else:  
\[P(tk=compatible=\frac{Res.cap.ofallVM-Res.cap.oftask}{Res.cap.oftask}) \tag{5}\]The model is trained until it satisfies the fitness function given in Equation (5), and once it satisfies the fitness function, the optimal task allocation is performed using the hybrid heuristic algorithm [16].