## 6 The Framework
### Detailed Algorithms  
The proposed SCwA-IFCCS-3S agent consists in dynamic and adaptive modes. The flowchart of _SCwA-IFCCS-3S_ is illustrated in Figure 10. The algorithm for establishing an optimal composite service for IoT, Fog, and Cloud environments changes over time due to context changes (processing load, change of customer's needs, low latency, insufficient energy of devices, and other factors). As a result, it is critical to adapt the actual composite service while taking into account a variety of management elements. The algorithm consists mainly of three main stages: _virtual business process generating_, _parallel multi-objective concrete composite service optimization_, and _adaptation of concrete composite service_. The stages involved in the implementation of SCwA-IFCCS-3S is given below:  
**Stage 1. Generate virtual business process:** In this stage, the research before the algorithm is used to systematically generate virtual IoT-BP according to the client's functional needs. Virtual services (e.g., tasks) are generated and optimized based on semantic relationships between VSs [28] and the reputation of each virtual service [42]. We automate this process to ensure dynamic changes in functional needs of the customer and his context. **Stage 2. Find local optimal solutions:** We focus on dynamic multi-objective service selection and composition problems, we target each service provider, and the discovery agent closes in towards it. Each service provider, whether IoT, Fog, or Cloud, is modeled by ERP-ICS, ERP-FCS, and ERP-CCS agents, respectively, that cooperate to share local services. Therefore, each service provider has a set of services \(S_{i}\) which is a subset of the set \(S\). The service provider agents create a set of NSGA-II agents to select the best Concrete Composite Service (CCS). It initializes them by a subset \(S_{i}\) and the energy cost and aggregated QoS to its composite service. Simultaneous use of penalties for energy consumption and penalty for QoS violation may hamper the reliability of service selection as well. The NSGA-II agent  
Figure 10: SCwA-IFCCS-3S functional model. finishes when finding the local optimal composite service for a set of service providers, thus _ERP-ICS_ and _ERP-FCS_ end by sending a set of optimal composite services to ERP-CCS-3S Agent. The collaborative mechanism between different agents via messages allows us to find the best optimal composite service. * **Step 1. ** Each individual (composite service) is encoded by an array of \(n\) concrete services, called chromosome. * **Step 2. Evaluation:** Each individual is evaluated using the aggregated fitness function. * **Step 3. Selection:*
* To pick the optimal individual, tournament selection is used. In the proposed approach, the relationship of non-domination has been adapted by adding a constraint violation criterion of selection to respect the customer's constraints and energy consumption. The new CSS violation degree (\(deg_{v\_CCS}\)) is evaluated to penalize IoT, Fog, and Cloud services that do not agree with both QoS and energy constraints. This degree is calculated as follows: \[\deg_{v\_CCS}=w_{\_Qos}\times\deg_{\_}{\_}{\text{W-ccS}}^{QoS}+w_{\_}{energy }\times\deg_{\_}{\text{W-ccS}}^{energy}\] (11) \[\text{With deg}_{\_}{\text{V-ccS}}^{qos}=\left\{ \begin{array}{cc}C_{\_}{ qos}-qos&\text{If}&C_{\_}{qos}>qos\\ 0&\text{Otherwise}\end{array} \right.\text{ and,}\] (12) \[\deg_{\_}{\text{V-ccS}}^{energy}=\left\{ \begin{array}{cc}C_{\_}{ energy}-energy&\text{If}&C_{\_}{energy}>energy\\ 0&\text{Otherwise}\end{array} \right..\] Here \(w_{\_}{\_}{\text{Qos}}\) and \(w_{\_}{energy}\) denote weights of the QoS and energy, respectively, which is increased when the use of IoT services is greater than the Fog and Cloud services;
* _qos_: Aggregated value of QoS. * _energy_: Aggregated value of energy. * C_\({}_{\_}{\text{Qos}}\): The QoS constraint. * C_\({}_{\_}{energy}\): The energy constraint. * For every CSS and its new dominance relation, we select the CSS based on the lowest dominance value.