# Multi-Agent Dynamic Fog Service Placement Approach
## 3 Service Placement Orchestrator Implementation
### Service Placement Decision-Making Method  
The method that was used for a service placement in this research was proposed in [55], and it is made of two stages. The first stage uses IMOPSO and the second one uses AHP. IMOPSO is a slightly adapted version of MOPSO, which was originally introduced by [PERSON] et. all in [56]. AHP was first introduced [PERSON] in [57]. The purpose of this two-stage method is to distribute \(n\) services among \(k\) fog nodes. All the QoS features of the \(i\)-th placement for \(X_{i}\) are defined by objective functions \(f_{j}(x)\), \(j=1,2,\ldots,m\). An optimization process seeks to find the best service placement \(X_{opt}\) while minimizing its objective functions \(\mathbf{f}_{\mathrm{j}}\):  
\[\mathrm{X_{opt}}=\underset{\mathrm{i}}{\mathrm{argmin}}\mathrm{F}(\mathrm{X_{ i}}). \tag{1}\]  
Figure 7: Proposed distributed orchestrator architecture.  
As a part of fog computing paradigm, fog nodes can have different technical capabilities, network throughput, and security units. Such qualities to consider may include CPU, RAM, power usage, network range, communication security protocols and authentication etc. Optimization based on one feature may come at the expense of neglecting other ones. Therefore, there is no universal solution to a multi-objective task. This leads to obtaining a set of non-dominated solutions based on fog node constraints. Please see Figure 8 below for more details.  
The IMOPSO method is used to obtain Pareto potential solutions. Since these placements, which are added to a repository, are non-dominated, it means that they are better by some criteria scores. AHP is used for the final best placement based on prioritized criteria. Alternatives are compared with each other using a judgement matrix. More details about an implementation of the IMOPSO algorithm and AHP process are provided in the publication [55]. A short summary is given below.  
The IMOPSO algorithm is applied in the following way as it is in Figure 9:  
1. A swarm is generated based on predefined test parameters such as a number of objective functions, particles, epochs, inertia weight, cognitive coefficient, social coefficient, number or services, and a range of particles. Particle positions with the swarm are randomly assigned. Initial values such as a global best score and position as well as velocities are given;
2. Each particle position gets evaluated for its new individual score based on its objective function res = (x - y)2;
3. Velocity is updated using the formula oldVelocity[i] = inertia + (pBest[i] - pos[i]) \(\times\) cognitiveComponent \(\times\) r1 + (gBest[i] - pos[i]) \(\times\) socialComponent \(\times\) r2;
4. Individual particle positions are updated by adding its velocity to its position;
5. Searching for individual best scores and individual best particle positions within the swarm;
6. Searching for the global best score and global best position;
7. If a particle dominates or if it neither dominates nor is dominated, it is added to a repository as an individual best result or global best result, respectively;
8. Repeat until the required number of cycles is completed.  
Figure 8: Optimal service placement method flow chart.  
The AHP algorithm is applied in the following way as it is in Figure 10:  
1. Repository particle positions in prepo are pairwise compared. This is completed with each criterion (objective function) fs[i];
2. Comparison results are stored in the array allComp[i];
3. The priority vector eigenvectorC is to be calculated. It is a matrix normalized Eigen vector. Each column of the priority matrix Comp is summed up. Each element of the column is divided by its sum to obtain a normalized relative weight in the double array matrixnormC;
4. The transposed matrix eigenFinal is used to find the best particle position. The best service distribution is the alternative that has the highest level of priority.  
Figure 9: IMOPSO algorithm.