# Routing Protocol for Heterogeneous Wireless Sensor Networks Based on a Modified Grey Wolf Optimizer
## 4 Proposed Algorithm
### Proposed Algorithm
#### 4.3.2 Modified Grey Wolf Optimizer  
The MGWO is used to select the CHs. In the original GWO [50], the prey's position was calculated based on the average weight of the \(\alpha\), \(\beta\), and \(\delta\) wolves. Considering the difference between the residual energy of the node and the distance between the node and the BS, the fitness value of the node is taken as the initial weight in the GWO, which is calculated by Equation (12). Consequently, based on the GWO and formula (12), the initial position of prey is computed as  
\[\overset{-0}{X}_{p}^{0}=\omega_{l\alpha}\overset{-0}{X}_{\alpha}^{0}+\omega_{ l\beta}\overset{-0}{X}_{\beta}+\omega_{l\beta}\overset{-0}{X}_{\delta}^{0}, \tag{13}\]  
\[\omega_{l\alpha}=F_{\alpha}/\left(F_{\alpha}+F_{\beta}+F_{\delta}\right) \tag{14}\]  
\[\omega_{l\beta}=F_{\beta}/\left(F_{\alpha}+F_{\beta}+F_{\delta}\right)\,\]  
where \(\omega_{l\alpha}\), \(\omega_{l\beta}\), and \(\omega_{l\beta}\) represent the initial weights of the \(\alpha\) wolf, the \(\beta\) wolf, and the \(\delta\) wolf, respectively. \(F_{\alpha}\), \(F_{\beta}\), and \(F_{\delta}\) are the first, the second, and the third best fitness values, respectively, which are calculated through Formula (12). The \(\alpha\), \(\beta\), and \(\delta\) wolves are the respective nodes corresponding to the three best fitness values.  
When the GWO is implemented in the proposed protocol, the fitness value of the node is changed after completing one data transmission, so the weight of the GWO will not change. In order to further improve the global search capability of the GWO, the weights are dynamically updated by vectors \(D\) and \(A\). \(D\) is the distance between the wolf and its prey, and \(A\) is the coefficient vector. \(D\) and \(A\) are calculated according to Equations (2) and (3). At the \(\left(t+1\right)^{th}\) iteration, the location of the prey and the weight updating formula are expressed as  
\[\overset{+1}{X}_{p}^{0}=\omega_{\alpha}^{t+1}\overset{-1}{X}_{\alpha}^{t+1}+ \omega_{\beta}^{t+1}\overset{-1}{X}_{\beta}^{t+1}+\omega_{\delta}^{t+1} \overset{-1}{X}_{\delta}^{t+1}, \tag{15}\]  
\[\omega_{\alpha}^{t+1}=(\overset{-1}{D}_{\alpha}\times\overset{-1}{A}_{\alpha }^{t+1})/(\overset{-1}{D}_{\alpha}\times\overset{-1}{A}_{\alpha}^{t+1}+ \overset{-1}{D}_{\betadistance between the node and the prey as parameters of the fitness function to select the CH. The node with the smaller fitness value is more likely to become the CH. The fitness function is as follows:  
\[F_{2}=\left\{\begin{array}{cc}&a_{2}\times\frac{E_{MAX}-E_{r}}{E_{MAX}-E_{MIN}}+ \left(1-a_{2}\right)\times\left(\frac{d_{MAXP}-d_{top}}{d_{MAXP}-d_{MINP}} \right),\ E_{r}>0\ \text{and the node is the normal node}\\ &\left(1-a_{2}\right)\times\frac{E_{MAX}-E_{r}}{E_{MAX}-E_{MIN}}+a_{2}\times \left(\frac{d_{MAXP}-d_{top}}{d_{MAXP}-d_{MINP}}\right),\ E_{r}>0\ \text{and the node is the advanced node}\\ &0\,\ E_{r}\leq 0\end{array}\right., \tag{17}\]  
where \(a_{2}\) is the weight; \(E_{r}\) is the residual energy of the node; \(E_{MAX}\) and \(E_{MIN}\) are the maximum and the minimum residual energy in the cluster, respectively; \(d_{top}\) is the distance from the node to the prey; and \(d_{MAXP}\) and \(d_{MINP}\) are the maximum and minimum distance, respectively, between a sensor node and the prey in the cluster.