## 3 VirtFogSim: A View of the Simulated Formal Models
### Supported Task Allocation Strategies and Adaptive Resource Allocation
\(\triangleright\)Begin O_TAS_par function \(\triangleright\)Initialization phase
1: Randomly generate a list \(\left\{\vec{x}_{i}\in\left\{0,1,2\right\}^{V},\,i=1,\ldots,PS\right\}\) of task allocation vectors and store it into \(\left[Popmatrix\right]\);
2: Compute and store into the \(\left(V+1\right)^{\text{th}}\) column of \(\left[Popmatrix\right]\) the energy consumed by each \(\vec{x}\in\left[Popmatrix\right]\) by calling \(PS\) times the \(evaluatestaticenergy\_p\) function;
3: Sort the \(PS\) elements of \(\left[Popmatrix\right]\) for increasing values of their energies;
4: Copy the first row of the sorted \(\left[Popmatrix\right]\) into \(\vec{x}_{OTAS}\), \(\mathcal{E}_{OTAS}\), and \(\mathcal{E}_{OTAS}^{NET}\);
5: Set the number \(Q=\left\lceil CF\times PS\right\rceil\) of the elements of \(\left[Popmatrix\right]\) for crossover at each generation;
6:for\(j=1:G_{MAX}\)do\(\triangleright\)Iterative phase
7: Perform the pair-wise crossover of the first \(Q\) elements of \(\left[Popmatrix\right]\) by calling \(\left(Q/2\right)\) times the \(Crossover\) function and store the \(Q\) crossover elements in \(\left[Childlist\right]\);
8: Randomly mut \(MN\) positions of the last \(\left(PS-Q\right)\) elements of \(\left[PopMatrix\right]\) by calling \(\left(PS-Q\right)\) times the \(Mutation\) function and store the mutated elements into \(\left[Mutationlist\right]\);
9: Compute and store the consumed energy of each element of \(\left[Childlist\right]\) and \(\left[Mutationlist\right]\) by calling \(PS\) times the function \(evaluatestaticenergy\_p\);
10: Copy the first \(Q\) elements of \(\left[Popmatrix\right]\) and the overall \(\left[Childlist\right]\) and \(\left[Mutationlist\right]\) into \(\left[Candidatelist\right]\);
11: Sort the \(\left(PS+Q\right)\) elements of \(\left[Candidatelist\right]\) for increasing values of their energies;
12: Copy the first \(PS\) elements of \(\left[Candidatelist\right]\) into \(\left[Popmatrix\right]\);
13:if the total energy of the first element of \(\left[Popmatrix\right]\) is lower than \(\mathcal{E}_{OTAS}\)then
14: Copy the first row of \(\left[Popmatrix\right]\) into \(\vec{x}_{OTAS}\), \(\mathcal{E}_{OTAS}\) and \(\mathcal{E}_{OTAS}^{NET}\);
15:endif
16:endfor
17:return\(\left[\vec{x}_{OTAS},\,\mathcal{E}_{OTAS},\,\mathcal{E}_{OTAS}^{NET}\right]\). \(\triangleright\)End O_TAS_par function
```  
**Algorithm 1**\(O\_TAS\_par\) function:  
The \(RAP\_p\) function implements primal-dual adaptive iterations on the cluster of available parallel workers that supports the execution of \(VirtFogSim\). The goal is to perform the optimal resource allocation under the input task allocation vector \(\vec{x}\). Its input formal variables are the vectors \(\vec{x}\), \(\vec{z}\), and \(\vec{s}\) and the matrices \(A\), \(Da\), and \(S_{RAP}\). The corresponding output variables are the vector \(\vec{\overline{y}}\) and the scalars \(\vec{\mathcal{E}}_{TOT}\) and \(\vec{\mathcal{E}}_{NET}\). Specifically, we have that:  
1. \(\vec{x}\) is a \(\left(1\times V\right)\)-dimensional \(\left\{0,1,2\right\}\)-ternary vector. It fixes the allocation to the Fog/Mobile/Cloud nodes of the \(V\) tasks that compose the considered application DAG. Specifically, \(x\left(i\right)=0\), \(1\), and \(2\) means that the \(i^{\text{th}}\) application task is executed at the Fog clone, Mobile device, and Cloud clone, respectively;2. \(z\triangleq\left[f_{M^{\prime}}^{0},f_{F^{\prime}}^{0},f_{C}^{0},R_{U}^{0},R_{D}^{0},R_ {U}^{0},B_{U}^{0},\,\lambda^{0}\right]\) is a \((1\times 8)\) vector of real-valued non-negative scalars. It fixes the vector starting point of the primal-dual iterations to be performed. Its first seven components are measured in bit/s, while the starting Lagrange multiplier \(\lambda_{0}\) is measured in Joule. 3. \(\widetilde{\widetilde{y}}\triangleq\left[\widetilde{f}_{M^{\prime}},\widetilde {f}_{F},\widetilde{f}_{C},\widetilde{R}_{U},\widetilde{R}_{D},\,\widetilde{ B}_{U},\,\widetilde{B}_{D}\right]\) is a \((1\times 7)\) vector of real-valued non-negative scalars.