# Clustered Distributed Learning Exploiting Node Centrality and Residual Energy (CINE) in WSNs
## 3 CINE Solution
### Protocol Details  
In this section, we briefly illustrate how the protocol works.  
Algorithm 2 represents the pseudocode for the protocol run by the generic CH.  
```
1:/*Protocol initialization */
2:Initialize \(\mathbf{w}_{CH}\)=train_model(\(\mathbb{X}_{CH}\), RND)
3:Calculate \(\mathcal{G}\) as in Equation (2)
4:Broadcast(\(\mathbf{w}_{CH}\)) to \(CM\)
5:Broadcast(\(\mathcal{G}\)) in \(CH\) network.
6:/*Regular operations */
7:whileTRUE do
8:Wait for Event
9:ifEvent.Type==Broadcast then
10:/*Cluster-Head CH will send its model parameters \(\mathbf{w}_{CH}\) */
11:Broadcast(\(\mathbf{w}_{CH}\)) in \(CH\) network.
12:Calculate \(\mathcal{G}\) as in Equation (2)
13:Broadcast(\(\mathcal{G}\)) in \(CH\) network.
14:Broadcast(\(\mathbf{w}_{CH}\)) in its cluster
15:endif
16:ifEvent.Type==Receive Model Parameters \(\mathbf{w}_{K}\)then
17:/*Node CH is receiving the model parameters \(\mathbf{w}_{CH}\) from neighbor Cluster-head \(K\) */
18:Calculate \(\mathcal{G}\) as in Equation (2)
19:\(\mathbf{w}_{CH}=\alpha\cdot\mathbf{w}_{K}+(1-\alpha)\cdot\mathbf{w}_{CH}\)
20:\(\mathbf{w}_{CH}=\text{train\_model}(\mathbb{X}_{CH},\mathbf{w}_{CH})\)
21:Broadcast(\(\mathbf{w}_{CH}\)) to \(CM\)
22:Broadcast(\(\mathcal{G}\)) in \(CH\) network.
23:endif
24:ifEvent.Type==Receive Data Chunk \(\mathbb{D}\mathbb{A}_{j}\)then
25:\(\mathbf{w}_{CH}=\text{train\_model}(\mathbb{D}\mathbb{A}_{j},\mathbf{w}_{K})\)
26:Broadcast(\(\mathbf{w}_{CH}\)) to \(j\)
27:endif
28:endwhile
```  
**Algorithm 2** CINE Protocol-Cluster Head Functioning  
At the startup, the model is trained by the CH using the local data \(X_{CH}\) and starting from random initial conditions, RND. Let \(L_{CH}\) be the resulting loss. Such value will be used, along with the residual energy and the information regarding available computing capabilities, to calculate the penalty parameter \(\mathcal{G}\) as follows:  
\[\mathcal{G}=L_{CH}/[C_{CH}\cdot P_{CH}\cdot rE_{CH}] \tag{2}\]  
where \(L_{CH}\) is the loss of the CH, \(C_{CH}\) is its degree centrality, \(rE_{CH}\) is its residual energy, and \(P_{CH}\) is its computing capability.  
To conclude the initialization phase, the cluster head broadcasts the model parameters of the obtained model \(\mathbf{w}_{CH}\) in the cluster. Moreover, it broadcasts the value of its penalty \(\mathcal{G}\) in the CH-network. This last operation is needed to allow for identification among the CHs of the node that exhibits the lowest penalty \(\mathcal{G}\), which will then send its model parameters to the neighboring CHs in the CH-network.  
After the initialization phase, regular operations are executed that are the consequence of three types of events:  
1. **Broadcast**: The cluster head exhibiting the lowest penalty broadcasts its model parameters to both its cluster members and its neighboring CHs.
2. **Receive Model Parameters**: upon receiving the model parameters \(\mathbf{w}_{k}\) by another cluster head (i.e., the one exhibiting the lowest penalty), the CH retrains the model.
3. **Receive Data Chunk**: The CH that receives a chunk of data from a CM includes it in its local data set and retrains the model. In this way, the new model is representative of a more comprehensive data set.  
For what concerns the operations performed by cluster members, we observe that they are straightforward consequences of what we have described so far. Upon receiving the model parameters sent by its cluster head, a generic cluster member estimates the loss obtained by using such a model. If the difference between the loss just calculated and the loss declared by the CH is larger than a given threshold \(\sigma_{TH}\), it sends a chunk of its data to the CH for retraining.  
To better clarify the CINE protocol, we have illustrated the functioning of the cluster head in a flow chart as shown in Figure 2. After the protocol initialization in the cluster head, the cluster head waits for the events and executes the task according to the event type shown in Figure 2.