# An Unequal Clustering and Multi-Hop Routing Protocol Based on Fuzzy Logic and Q-Learning in WSNs
## 4 Proposed Method
### Fuzzy Logic-Based Clustering
A CH at the edge uses more energy due to its greater distance from other nodes. Equation (12) outlines the calculation of a node's centrality. Figure 7: Membership function for distance to base. \[NC= \begin{cases}1,Num(Neighbor(i))=0\\ \frac{\sqrt{\frac{N_{i=1}^{Num(Neighbor(i))}}{{\sum\
olimits_{i=1}^{Num(Neighbor(i) )}}}{{\sum\
olimits_{i=1}^{Num(Neighbor(i))}}}}}{{\frac{Num(Neighbor(i))}}{{ \sum\
olimits_{i=1}^{Num(Neighbor(i))}}}},Num(Neighbor(i))>0\end{cases}  \tag{11}\]  
where \(dis^{2}(i,j)\) denotes the distance between the nodes \(i\) and \(j\) and the _Dimension_ value is \(A\) in the \(A\times A\) field area. Figure 9 illustrates the fuzzy set for the input variable of a node's centrality, consisting of three linguistic categories: Close, Adequate, and Far. Close and Far are represented by trapezoidal membership functions, while Adequate is modeled with a Gaussian membership function. Figure 8: Membership function for number of neighbors. Figure 9: Membership function for node centrality. FQ-UCR has two output variables: the CH probability and competition radius. **CH probability:** this probability is calculated to select the optimal CH among sensor nodes, with nodes having higher probabilities being chosen as CHs. Figure 10 depicts the fuzzy output variable for probability, which includes nine linguistic terms: Very Low, Low, Rather Low, Medium-Low, Medium, Medium-High, Rather High, High, and Very High. Triangular membership functions are used for the majority of these terms. **Competition radius:** this refers to the broadcast range and competition area of a tentative CH, with a larger radius raising the likelihood of selection. Figure 11 presents the fuzzy set for the competition radius output variable, which includes nine linguistic variables: Very Short, Short, Rather Short, Medium-Short, Medium, Medium-Long, Rather Long, Long, and Very Long, with most represented by triangular membership functions. Each sensor node derives the input variables for the fuzzy system using Equations (3)-(11) and translates them into linguistic variables through fuzzification. As outlined in Table 1, these variables are processed using a Mamdani-based fuzzy \"if-then\" rule set, comprising \(3\times 3\times 3\times 3=81\) rules in total. To derive precise results, the defuzzification process employs the Center of Area (COA) technique. Each node computes its CH probability and competition radius and then broadcasts the probability. The node exhibiting the greatest probability is selected as the CH. Once the CH is elected, it broadcasts \"\(FINAL\_CH\_MSG\)\" to inform ordinary nodes of its status. Ordinary nodes respond with \"\(JOIN\_MSG\)\" to establish their connection to the CH. Algorithm 1 outlines the detailed selection process. ```
1:\(CH\_List\leftarrow\{\varnothing\}\)
2:\(S\_FQUCR(i).Type\leftarrow\) Normal
3:\(S\_FQUCR(i).TentativeCH\leftarrow\) FALSE
4:for\(i=1\) to \(TN\)do
5: /*\(TN\) is the total number of all nodes in the network*/
6:\(S\_FQUCR(i).\mu\gets rand(0,1)\)
7:if\(S\_FQUCR(i).\mu<T\)then
8: /*\(T\leftarrow\) probability to become tentative CH*/
9:\(S\_FQUCR(i).TentativeCH\leftarrow\) TRUE
10: Calculate \(S\_FQUCR(i).RE\) according to Equations (3)-(7) /*\(S\_FQUCR(i).RE\) is the Residual Energy of node \(i\)*/
11: Calculate \(S\_FQUCR(i).DisToBs\) according to Equation (8) /*\(S\_FQUCR(i).DisToBs\) is the Distance to BS of node \(i\)*/
12: Calculate \(S\_FQUCR(i).NN\) according to Equations (9)-(10) /*\(S\_FQUCR(i).NN\) is the Neighbors of node \(i\)*/
13: Calculate \(S\_FQUCR(i).NC\) according to Equation (11) /*\(S\_FQUCR(i).NC\) is the Centrality of node \(i\)*/
14:\(Fitness\leftarrow\) EvaluateFIS(\(S\_FQUCR(i).RE\_S\_FQUCR(i).DisToBs\),
15:\(S\_FQUCR(i).NN\),
16:\(S\_FQUCR(i).NC\)
17:\(S\_FQUCR(i).Probability\gets Fitness(1)\)
18:\(S\_FQUCR(i).Radius\gets Fitness(2)\)
19: Broadcast \(CH\_Message(i,S\_FQUCR(i).Probability,S\_FQUCR(i).Radius)\)