## 4 The CH-Active Algorithm  
In this section, we introduce the CH-active algorithm that is based on the selfish herd hypothesis and uses the same concept of _round_, as that used by LEACH. Each round consists of two states: the setup state and the steady state. In the setup state, we adopted the same function used by LEACH for CH selection. CHs are elected and clusters are formed in this stage. In the steady state, the WSN sends data to the BS.  
The steady-state in the CH-active algorithm is quite different from LEACH because it includes solar-powered CH relocations. After all the CHs are chosen, each CH relocates itself to the center of the masses of the cluster. The CH moves to the gravity center of the active nodes. The center of the masses is calculated each time as the center of the live (active) nodes in the formed cluster. This center is roaming slightly inside the cluster in later steps. The center of the mass of each cluster is calculated based on the following equation:  
\[\overset{\rightarrow}{r}_{cm}=\sum_{i=1}^{K}\frac{\overset{\rightarrow}{r}_{ i}m_{i}}{m_{tot}}, \tag{3}\]  
where:  
* \(K\) is the number of sensors in the cluster;
* \(r_{i}\) is the position of each sensor \(i\);
* \(m_{i}\) is the mass of each sensor \(i\), we assume that all sensor masses are equal;
* \(m_{tot}\) is the total mass of all sensors in the cluster.  
After moving to the center of the masses, the CH collects the data from the cluster members, and relocates itself back to the position it had before relocating to the center of the masses. This is done to avoid the grouping of all the nodes in one-time spot. Then, the CH aggregates data, and sends them to the sink. It is noteworthy that we considered the situation when a CH sensor does not have enough energy to position itself in the center of the mass, but it can reach a certain percentage of the distance from its initial position to the center. In our framework, this modification of the CH-active algorithm is called CH-partially-active algorithm. Algorithm 1 shows the pseudocode for the proposed algorithm(s).  
Figure 2: Radio energy dissipation model.  
```
Parameters n: number of nodes on terrain n_active: number of active nodes x_y: dimensions of the field x_sink, y_sink: sink position (x,y) node_energy: energy dedicated to the nodes initial_node_energy: initial energy per node message_length: length of the messages round_number: number of round in progress epoch_number: number of epoch in progress
1: Distribute nodes randomly on the terrain
2: Prepare the (4D) matrix for storage of all node parameters and statuses in all rounds and all epochs
3: While number of active nodes > 0
4: Start new round
5: If round_number > 20 start a new epoch
6: Filter-out the nodes with no power
7: Choose the CHs from live nodes
8: Mark used CH statuses for chosen CHs in the matrix
9: Choose the CH for each individual sensor node--create clusters
10: Calculate the center of masses of all nodes in each created cluster
11: Case 1:
12: If Method = CH-partially-active
13: Move the CHs partially toward the center of masses of still active nodes of the cluster
14: Endif
15: Case 2:
16: If Method = CH-active
17: Move the CHs to the very center of masses of still active nodes of the cluster
18: Endif
19: For \(i\) = 1 to n
20: If \(i\) is not in cluster: Send the sensed data to the sink
21: If \(i\) is in cluster: Send the sensed data to the CH
22: Move the CH back to the initial position
23: Aggregate data on the CH
24: Send their data from the CH to the sink
25: Endif
26: End while
```  
**Algorithm 1** The CH-active and the CH-partially-active algorithms