| Algorithm 2: Second-level algorithm of the learning hierarchy. |
| 1. Initialization |
| 2. While not converged, do |
| 1. For each element i of the learner’s flag, do |
| 1. If flag(i) is converted from 1 to 0, do Find device u that helped device i among all devices, including neighbors’ helpers, and return the borrowed bandwidth from di to du |
| 2. Else if flag(i) is converted from 0 to 1, do |
| a. Make a feasible device set, including the nodes’ own feasible devices and neighbors’ feasible help lists. |
| b. With probability ɛ, randomly choose device k among the feasible device set, otherwise: , where j is the index of the selected. |
| c. Increase the bandwidth of di and decrease the bandwidth of dj, as much as di needs. |
| d. Calculate penalty or punish(i) or blame(i) based on the learner’s action and then calculate RnQ: |
| e. Update Q matrix: |
| 3. End if |
| 2. End for |
| 3. Make the next flag based on the learner’s device priorities. |
| 4. Repeat lines 3 to 26 for neighboring fog nodes |
| 5. Calculate SBMn using countpun considering all fragments |
| 6. Calculate the average SBM |
| 7. Increase the time step n by 1 |
| 3. End while |