# Optimizing Rule Weights to Improve FRBS Clustering in Wireless Sensor Networks
## 2 Materials and Methods
### Proposed Optimization Algorithm
Their dimension is fixed and is linked to the number of fuzzy rules. The particles change their position according to the velocity in Equation (1), considering that the velocity of each particle (\(V_{i}\)) is determined by Equation (6). \[V_{i}= \begin{pmatrix}V_{1}^{i}\\...\\ V_{n}^{i}\end{pmatrix}  \tag{9}\]  
Throughout the PSO process, each particle changes its position in the search domain (workspace). Since each particle is composed of a set of rule weights, modifying the particle's value implies that the set of rule weights has changed. If any of the weights are outside the [0, 1] interval, the value is adjusted. The process continues until the stop condition is reached. The following algorithm (Algorithm 1) summarizes the proposed scheduling and tuning process based on PSO. The process detailed in the PSO-RWT algorithm was repeated three times to obtain the three different KBs enumerated at the beginning of this section. First, the PSO-RWT algorithm was applied for 30 different layouts taken from each of the three scenarios described in Section 2.4 (10 from each scenario), to obtain the weights that maximized the FND, which was the stop condition for this phase, obtaining \(KB_{initial}\). Then, the PSO-RWT was repeated, taking \(KB_{initial}\) as the starting KB, and optimizing the rule weights from the FND until a maximum HND was reached. The new rule weights defined \(KB_{intermediate}\) \begin{table}
\begin{tabular}{c c c c} \hline \hline \(d_{0}\) & \(d_{1}\) & \(d_{2}\) & \(maxgen\) \\ \hline \(e^{-\frac{1}{(maxgen)^{2}}}\) & 1 & 2 & 100 \\ \hline \hline \end{tabular}
\end{table} 
Table 3: PSO parameters. Finally, the PSO-RWT algorithm was executed to obtain the maximum value for LND, using \(KB_{initial}\) until the FND was reached, \(KB_{intermediate}\) from FND to HND, and from HND to LND, and PSO-RWT optimized the rule weights to obtain the new \(KB_{final}\). Therefore, the optimization process produced three different KBs, which were then used by the clustering algorithm in the three stages of the WSN lifetime. ```
1. Swarm: Num_particles,  Num_iter, Init_rate (\(r_{0}\)), Inertial_weight \(\omega\), Factors \(c_{1}\) and \(c_{2}\). 2. Random setting of swarm position. 3. Random setting of velocity. 4. Velocity constraints. 5. Initialize Gbest (\(P^{*}\))/Pbest (\(P^{\#}\)) Do Do
1. Update position. Equation (7). 2. Constraints RB-Swarm position. 0 \(\leq|P_{i}|\leq 1\)
3. Evaluate-Fitness. Evaluation system. Particles++
4. While (Num_particles) Update Gbest (\(P^{*}\)). Do
1. Update Pbest (\(P^{\#}\)). 2. Update velocity. Equation (6). 3. Velocity constraints. Particles++
5. While (Num_particles) iter++
6. While (Num_iter) Return solution: Gbest (\(P^{*}\))
```  
**Algorithm 1**PSO-RWT algorithm  
Finally, when the optimization process ended, three different KBs were obtained: each one to be used in a different stage of the WSN lifetime, as mentioned in Section 2.2. Consequently, although three KBs were obtained, this does not mean that the clustering method had three variants, because each KB was only applied in one stage during the lifetime of the WSN. In the next section, we present a comparison of the proposed CFC3 PSO clustering method with other algorithms, to contrast their behavior and performance.