# An Unequal Clustering and Multi-Hop Routing Protocol Based on Fuzzy Logic and Q-Learning in WSNs
## 4 Proposed Method
### Fuzzy Logic-Based Clustering  
In each clustering round, each sensor node generates a random number between 0 and 1. If the generated random number is less than the threshold \(T\), the node is selected as a tentative CH. As depicted in Figure 5, once the potential CHs are determined, they use a predefined FIS to evaluate the probability of becoming the final CH and calculate their competition radius. The input variables in FQ-UCR include the residual energy, distance to the BS, number of neighbors, and node centrality. **Residual Energy (RE):**:  
Figure 4: Block diagram of FIS. Figure 5: FIS developed for selecting CHs in FQ-UCR. This factor is crucial as sensor nodes with higher remaining energy have a greater likelihood of being chosen as CHs. The remaining energy of a node is calculated using Formula (6). \[RE=\frac{E_{ini}-E_{con}(i)}{E_{ini}} \tag{4}\]  
\[E_{con}(i)= \begin{cases}E_{non-CH},\text{ if node i is the normal node}\\ E_{CH},\text{ if node i is the CH}\end{cases}  \tag{5}\]  
In this context, \(E_{ini}\) indicates the initial energy assigned to a node, whereas \(E_{con}(i)\) represents the amount of energy utilized by the node \(i\). The communication cost of a CH is denoted as \(E_{CH}\) and is determined using the following equation:  
\[E_{CH}=k\times l\times E_{elec}+k\times l\times\varepsilon_{fs}\times d_{toBS}^ {2}+E_{DA} \tag{6}\]  
\[E_{DA}{=}k\times l\times E_{pDb} \tag{7}\]  
where \(d_{toBS}\) is the distance from the CH to the next hop or BS, \(E_{DA}\) is the energy for data aggregation, \(l\) is the number of bits, \(k\) is the cluster size, and \(E_{pDb}\) is the energy for 1-bit fusion. The energy consumed by a cluster member node, denoted as \(E_{non-CH}\), denotes the energy consumed by a cluster member node and is calculated by  
\[E_{non-CH}=l\times\left(E_{elec}+\varepsilon_{fs}d_{toCH}^{2}\right) \tag{8}\]  
In this context, \(d_{toCH}\) represents the distance between a cluster member and its corresponding CH. Figure 6 presents the fuzzy set for the residual energy input, categorized into Low, Medium, and High levels. The Low and High categories are defined by trapezoidal membership functions, whereas the Medium category is characterized by a Gaussian membership function. Figure 6: Membership function for residual energy. **Distance to BS (DisToBs):**  
This factor is vital because a shorter distance between the CH and BS leads to reduced energy consumption during communication. The distance to the BS is calculated using Formula (8). \[DisToBs=\sqrt{(x_{i}-X_{BS})^{2}+(y_{i}-Y_{BS})^{2}} \tag{9}\]  
where \((x_{i},y_{i})\) and \((X_{BS},Y_{BS})\) denote the position of the CH and the BS. Figure 7 shows the fuzzy set for the distance to the BS, which is classified into three ranges: Near, Medium, and Far. The Near and Far ranges are represented using trapezoidal membership functions, whereas a Gaussian membership function is used for the Medium range. **Number of neighbors (NN):** This factor represents the count of neighboring nodes that have energy levels above the average. A higher number of such neighbors improves data transmission efficiency and reduces the data forwarding load on each neighbor node of the cluster head, thus promoting overall system energy optimization. The calculation of the NN is outlined in Equations (9) and (10). \[NN=\frac{Num\big{(}E_{res}(Neighbor(i))>E_{avg}\big{)}}{totalAlive} \tag{10}\]  
where \(Num(Neighbor(i))\) denotes the number of neighbor nodes of the node \(i\), and \(totalAlive\) represents the total number of active nodes in the network. Figure 8 illustrates the fuzzy set for the input variable of the number of neighbors, with three categories: Few, Moderate, and Many. Trapezoidal membership functions define Few and Many, while the Moderate category is represented by a Gaussian membership function. **Node Centrality (NC):**  
This factor reflects a node's central position among its neighbors. Centrality evaluates a node's position within its cluster, reflecting how evenly it is spaced relative to other cluster members.