2.1. Ant Colony
The ant colony optimization algorithm is a probabilistic algorithm that simulates the foraging behavior of ants in nature, with characteristics of distribution calculation, positive feedback of information, and heuristic searching [
22]. In ant colony optimization-based routing algorithm simulations, the parameters are
,
and
. The numerical value of these parameters is crucial for the performance and effectiveness of the algorithm. These parameters play different roles in ant colony algorithms and affect the decision-making process of ants when choosing paths.
Parameter
: The factor that usually represents the importance of pheromones determines the degree to which ants are influenced by pheromones when choosing a path. A pheromone is a chemical substance released by ants during their search for food, used to transmit pathway information [
23]. When the value of
is high, ants are more inclined to choose paths with higher pheromone concentration, which helps the algorithm quickly converge to the optimal solution. However, an excessively large
value may cause the algorithm to fall into local optima too early while ignoring other potentially better paths [
24]. Therefore, adjustments need to be made based on specific issues deriving from the
value to strike a balance between global search and local search.
Parameter
: The factor representing the importance of heuristic information reflects the degree to which ants are influenced by heuristic information (such as path length, congestion level, etc.) when choosing a path [
25]. Heuristic information helps ants discover better paths during the exploration process. When the value of
is large, ants are more inclined to choose paths with better heuristic information, which helps the algorithm to avoid becoming stuck in local optima during the search process. However, an excessively large
value may cause the algorithm to ignore the role of pheromones, thereby reducing the convergence speed of the algorithm. Therefore, it is also necessary to make adjustments according to specific issues deriving from the
value to achieve an efficient search process of the algorithm.
Parameter ρ: This parameter indicates the volatility coefficient of pheromones, which determines the rate of decay of pheromones along the path. The volatilization of pheromones is an important step in the ant colony algorithm, which helps to prevent the algorithm from becoming stuck in local optima and promotes ants to explore other paths. When the value of ρ is large, pheromone decay is faster, which helps ants discover new and better paths more quickly [
26]. However, an excessively large ρ value may cause pheromones to quickly disappear on the path, making it difficult for ants to find a stable and optimal path [
27]. Therefore, it is necessary to choose the appropriate one based on the characteristics and requirements of the problem. The ρ value is used to achieve the stability and efficiency of the algorithm [
28].
Parameters
,
,
The data size setting is not supported by a comprehensive mathematical model. It is necessary to comprehensively consider factors such as the characteristics of the problem, the complexity of the network environment, and the performance requirements of the algorithm [
29]. Through reasonable parameter settings and adjustments and multiple experimental optimizations, the efficient, stable, and accurate performance of ant colony optimization-based routing algorithms can be achieved [
30].
Assuming there is a base station with
and
wireless sensor nodes, these
wireless sensor nodes can choose the next base station with a certain probability, and
is the probability of the
-th sensor node from base station
to base station
at time
:
Among them,
represents the pheromone content on path
at time
;
is the visibility on path
at time
, which is the heuristic information of path
. It is generally set as
=1/
, where
is the distance between base station
and
;
is the base station that node
can access in the next step;
is the heuristic factor, representing the relative importance of pheromones
; P is the expected factor, representing the importance of visibility
. And the corresponding pheromones left by the sensor nodes on the route they pass through are as follows:
Among them, is the volatility coefficient of the pheromones, is the residual coefficient of the pheromones, is the pheromone content left by node on path (, and is the sum of the pheromones left by all ants passing through path at the end of a cycle.
Using the experimental data from the TSPLIB test library as the object, the main analysis focuses on the total number of wireless sensor nodes
, the heuristic factor
, the pheromone volatility coefficient
, and the expected factor
. After 15 tests,
Table 1 is obtained:
The range of values is [0.5, 5], the range of values is [1, 20], and the range of values is [0.3, 0.99]. The obtained optimal solution of is 5, of is 0, and of is 0.5
2.4. Increase the Coverage of WSNs
We use a newly added optimized clustering method to determine WSN clustering.
In wireless sensor network clustering, the number of clusters and initialization centers are uncertain and need to be improved.
- (1)
We use the canopy algorithm to preprocess the data:
In order to increase the anti-interference ability, the K-means clustering algorithm has been optimized and improved. The specific algorithm is as follows (as shown in
Figure 9):
Randomly delete one point from a set of points, create a canopy containing that point, and iterate over the remaining set of points.
For each point, if its distance from the first point is less than T1, then that point is added to this cluster.
In addition, if the distance is less than T2, remove this point from this set. This point, which is very close to the origin, will avoid all future calculations and no longer focus on other canopy centers. This algorithm loops until the initial set is empty, aggregating a set of canopies, each containing one or more points. Each point can be included in more than one canopy. After establishing the canopies, those containing fewer data points can be deleted, which can reduce signal interference.
- (1)
Put all data into the list and select two distances T1, T2, T1 > T2.
- (2)
While (list is not empty)
{
Randomly select a node as the center of the canopy and delete the point from the list;
Traversing the list:
For any record, calculate its distance to each canopy;
If the distance is less than T2, mark these data strongly and delete this record from the list;
If the distance is less than T1, mark these data weakly;
If the distance to any canopy center is greater than T1, then treat this record as a new canopy center and remove this element from the list;
}
It is important to note the adjustment of parameters:
When T1 is too large, it can cause many points to belong to multiple canopies, which may result in closer distances between the center points of each cluster and less obvious differences between clusters.
When T2 is too large, increasing the number of strongly labeled data points will reduce the number of clusters; if T2 is too small, it will increase the number of clusters and also increase the calculation time.
- (2)
Optimizing clustering methods to determine WSN clustering
- i.
Optimize the selection of initial cluster centers
As shown in
Figure 10, if cluster centers are too dense, most data points will form a cluster. Clustering centers that are too scattered can lead to sparse data points and unreasonable classification. Choosing the optimal cluster center is crucial. An excellent cluster center needs to meet the high density of nodes around the cluster center and the long distance between each cluster center.
- (a)
Average distance
Data point collection is as follows:
. Calculate the average distance between all data points as follows:
In the formula, represents the distance between two points.
represents the distance from node to node , represents the maximum number of wireless sensor nodes, and represents the average distance between all nodes.
- (b)
Cluster radius
When the number of cluster centers and the total cluster area are determined, the ideal radius for obtaining clusters is as follows:
- (c)
Data point density
Calculate the standard deviation of the distance between each data point and other data points as the density of that data point. The density of the data point is
, calculated as follows:
The basic idea for selecting the initial clustering center for data point density is to use the data point with the highest density as the first initial clustering center . Then, calculate the distance from the remaining data points to (Euclidean distance); if the distance is less than the average distance between each data point, then move it into the class of . Then, select the data point with the highest density from the data points outside the cluster radius of the first initial cluster center as the second initial cluster center , and continue the cycle until k initial cluster centers are selected.
The specific steps for selecting the initial cluster center are as follows:
- (1)
Initialize the parameter , cluster radius , and data point set ;
- (2)
Calculate the average distance between all non-cluster-center data points in according to Formulas (1) and (3) and the data point density in ;
- (3)
Select the data point with the highest density among all data points as the first cluster center .
- (4)
Select the data point with the highest density of data points outside the initial cluster center I and cluster radius J as the second cluster center ;
- (5)
Continuously perform steps 2, 3, and 4 until initial cluster centers are selected.
- ii.
Improve the K-means algorithm process
Combine the canopy algorithm with the K-means algorithm to improve the K-means algorithm process. Set the set of data points in the improved K-means algorithm as follows:
The Euclidean distance calculation method between two data points
and
is as follows:
In the formula, represents the distance between data points and , and represent data points with -dimensional attributes, and represents the dimension of the data points.
The objective function
adopts the sum of squared errors function as follows:
In the formula, represents the number of data points in class , and represents the -th data point in class .
The objective function E is a standard function represented by the sum of squared errors. The smaller the value of the objective function, the smaller the difference in data points within the class, and the greater the difference in data points between different classes, the better the clustering effect.
The specific steps to improve the K-means algorithm are as follows:
- (1)
Initialize the data point set and cluster radius ;
- (2)
Run the canopy algorithm to obtain the number of clusters ;
- (3)
Calculate the average distance and data point density between non-cluster-center data points;
- (4)
Select the data point with the highest density among all data points as the first initial clustering center;
- (5)
Select the data point with the highest density outside the cluster radius of the first initial cluster center as the second initial clustering center;
- (6)
To determine whether cluster centers have been selected, proceed to step 7; otherwise, jump to step 3;
- (7)
Calculate the distance (Euclidean distance) between all data points in the data point set and all initial cluster centers.
Then, based on the distance from the data points to each initial cluster center, assign the data points to the nearest cluster.
- (8)
Update all cluster centers, recalculate the mean of each cluster, and adjust the position of the point closest to the mean. The nearest point serves as the new cluster center;
- (9)
Continuously perform steps 7 and 8 until the positions of all cluster centers no longer change or reach the maximum number of iterations;
- (10)
Output the clustering results.
- iii.
Improved K-means algorithm for WSN clustering
The design of clustering routing for wireless sensor networks first requires reasonable clustering of nodes in WSNs.
Each node in a WSN can be regarded as a data point, and the number of clusters and the initial cluster head position can be improved, as mentioned earlier.
The K-means algorithm is used to calculate the clustering results, and the improved K-means algorithm can meet the clustering requirements of wireless sensor networks.
The specific design steps of the WSN clustering method based on the improved K-means algorithm are as follows:
- (1)
Input the wireless sensor network node set and clustering radius ;
- (2)
Run the canopy algorithm to obtain the number of clusters ;
- (3)
Calculate the average distance and node density between non-cluster-head nodes;
- (4)
Select the node with the highest node density as the cluster head;
- (5)
Select the node with the highest density outside the clustering radius of the cluster head as the next cluster head;
- (6)
Determine whether to select k cluster heads. If yes, proceed to step 7; otherwise, jump to step 3;
- (7)
Substitute the initial cluster head and the number of clusters into the K-means algorithm for clustering;
- (8)
Output the clustering results.
- iv.
Simulation verification
In order to test the performance of the improved K-means clustering algorithm proposed in this article, the MATLAB 2023a simulation platform was used for calculation.
We conducted simulation experiments using four typical datasets from UCI, namely Iris, Wine, Seeds, and Glass.
Using clustering accuracy as the evaluation criterion, the improved K-means algorithm and the traditional K-means algorithm were used to conduct ten experiments on four different datasets to obtain the clustering accuracy, and the accuracy of each cluster was evaluated.
The average was obtained, and the experimental results are shown in
Table 2.
From the
Table 3, it can be seen that the improved K-means algorithm has higher accuracy, which is also the result of optimizing the selection of initial cluster centers.
- (3)
Improving the Whale Algorithm to Determine WSN Cluster Heads
Due to the weak global exploration ability of the whale algorithm, it is prone to becoming stuck in local searching. Therefore, improvements should be made to the whale algorithm.
- i.
Basic Principles of the Whale Algorithm
The standard WOA simulates the unique search methods and trapping mechanisms of humpback whales, mainly including three important stages: trapping prey, bubble net predation, and searching for prey. The position of each humpback whale in the WOA represents a potential solution, and by continuously updating the whale’s position in the solution space, the global optimal solution is ultimately obtained.
- (a)
Surround and hunt prey
In Equations (9) and (10), represents the -th iteration of the -th individual, represents the position of the optimal whale in the -th iteration, is the displacement of the optimal individual in the current t-th iteration, decreases linearly from to with the number of iterations, is a random number of , and L is a random number uniformly distributed within .
- ii.
Improving the Whale Algorithm
To further improve the convergence speed and optimization accuracy of the standard whale algorithm, this paper adjusts the convergence factor and contraction probability in the whale algorithm to improve the convergence speed and optimization accuracy.
(1) Improvement in convergence factor: In the standard whale algorithm, the value of the convergence factor decreases with the number of iterations, so the algorithm has a larger search range and stronger search ability in the early stage, and gradually tends toward the optimal solution in the later stage. Due to the linear decrease in the value of , the algorithm may experience a slow convergence speed in the early stage, a small search range in the later stage, and falling into local optima, resulting in insufficient optimization accuracy.
The improvement method adjusts the convergence factor
to a nonlinear reduction formula:
In the formula, is the starting value of , is the final value of , is a constant, and is the current number of iterations.
is the maximum number of iterations.
(2) Improvement in contraction probability: in the standard whale algorithm, the value is set to 0.5, which means surrounding prey and using gas.
The probability of bubble net predation is equal throughout the entire process. Due to the constant equality of the two probabilities, the algorithm may experience a slow convergence speed.
As the number of iterations gradually increases, the individual gradually approaches the optimal solution, and the probability of surrounding the prey should gradually be greater than the probability of net predation by using bubbles.
The improvement method in this article adjusts the contraction probability
to a linear reduction formula:
In the formula, is the adjustment parameter, and is the current iteration number.
In summary, the specific implementation steps of the improved whale algorithm are as follows:
- (1)
Initialize the population and location information of whales, and set the maximum number of iterations ;
- (2)
Calculate the fitness of each whale;
- (3)
Record the optimal whale position;
- (4)
If the termination condition is met, proceed to step 8; otherwise, proceed to step 5;
- (5)
Update parameters ,,,;
- (6)
To determine if is less than , proceed to step 7. Otherwise, use bubble nets for predation. Use the mathematical models in (12) and (13) to update the position and proceed to step 2;
- (7)
Determine if A is less than 1. If so, surround the prey and use the mathematical model to update the position. Go to step (2). Otherwise, search for prey and use the mathematical models in (16) and (17) to update the position. Go to step (2);
- (8)
Output the optimal solution.
- iii.
Determine the WSN cluster heads
The improved whale algorithm will be used for clustering, assuming that each whale contains the position of m cluster heads (m is the optimal number of cluster heads). The structure of each whale is as follows:
Among them, refers to the -th cluster head vector of the -th whale in cluster . Each whale represents a clustering method, and the entire whale population represents multiple clustering methods. The improved whale algorithm takes the function as the fitness function, takes its minimum value as the optimal value, and finds the position of the whale when takes the minimum value. At this time, the position of the cluster heads in the optimal whale is the optimal clustering method.
The specific steps for WSN cluster head selection based on the improved whale algorithm are as follows:
- (1)
Initialize the whale population; the position of the
-th whale is as follows:
where m is the optimal number of cluster heads. Set the termination condition for the algorithm;
- (2)
Calculate the fitness value of each whale;
- (3)
Record the optimal individual and location;
- (4)
If the termination condition is met, proceed to step 6; otherwise, proceed to step 5;
- (5)
Update and based on the number of iterations;
- (6)
Update the whale position and proceed to step 2;
- (7)
Output the optimal solution and obtain the optimal cluster head.
Generate 200 two-dimensional data samples in Matlab2023a, randomly distributed in a square of 200 m × 200 m.
Within the scope, as shown in
Figure 11, conduct ten control experiments on ten different sets of data, and compare them with the traditional K-means performance.
Compare the clustering performance of the LEACH-C and EEUC algorithms to verify the superiority of the improved K-means algorithm in this paper.
The black “*” in the figures below represents ordinary nodes, and the red “*” represents cluster heads.
The clustering effect of the K-means algorithm in experimental scenarios shows that the improved K-means algorithm has a better clustering effect than the traditional K-means, LEACH-C, and EEUC algorithms. This conclusion also demonstrates the universality of the improved K-means algorithm in optimizing the clustering effect of WSN networks.
2.5. BP Neural Network
A BP (backpropagation) neural network is a multi-layer feedforward neural network trained using the error backpropagation algorithm, and is the most widely used neural network. As shown in
Figure 16:
Perceptron—a single node in a BP neural network, consisting of input items, weights, biases, activation functions, and outputs.
Input node:;
Weight:;
Bias: ;
Activation function:;
Output nodes: .
A BP neural network is a typical nonlinear algorithm.
A BP neural network consists of an input hidden layer (also known as an intermediate layer) and an output layer, with one or more hidden layers. Each layer can have several nodes. The connection status of nodes between layers is reflected through weights.
There is only one hidden layer in traditional shallow neural networks. There are multiple hidden layers in neural networks for deep learning, as shown in
Figure 17:
Input Layer: The input layer is the starting layer of a neural network, responsible for receiving external input data or features. The number of neurons in the input layer usually corresponds to the number of features in the input data.
Hidden Layers: Located between the input and output layers, hidden layers are a crucial part of neural networks for processing information. The hidden layer can have one or more layers, each consisting of a certain number of neurons. Neurons are connected through weights and biases, and nonlinearity is introduced through activation functions.
Output Layer: The output layer is the last layer of a neural network, responsible for generating the final output of the network. The number of neurons in the output layer usually corresponds to the number of target variables that need to be predicted. The output layer usually uses the softmax function to convert the output into a probability distribution. For regression problems, the output layer may directly output predicted values.
Activation Function: Each neuron in the hidden layer uses an activation function to process its input. Common activation functions include the sigmoid function, ReLU (Corrected Linear Unit) function, Tanh function, etc. The choice of activation function has a significant impact on the performance of the network, as they determine how neurons respond to their inputs.
Weights and Biases: Weights and biases are learnable parameters in neural networks used to control the strength of connections and bias terms between neurons. During the training process, these parameters are updated through backpropagation algorithms to minimize the prediction error of the network. The weights and biases are derived from the solution space position and fitness values of the whale algorithm.
Backpropagation Algorithm: The backpropagation algorithm is the core algorithm of a BP neural network used to calculate the prediction error of the network and update weights and bias parameters. It adjusts the parameters of the network layer by layer through the backpropagation of error signals, making the output of the network gradually approach the true value.
In the process of forward propagation, there is a step that compares whether the expected result is satisfactory or not. In this step, there is an error between the actual output result and the expected output result. In order to reduce this error, this is converted into an optimization process. For any optimization problem, there is always an objective function, which is the loss function.
The purpose of repeated training is to find the minimum value of this function in order to minimize the error between the actual output result and the expected output result. Each iteration will generate a weight update, and then, the updated weights will be propagated forward with the training samples. If the results are not satisfactory, backpropagation will be performed and the iteration will continue. This process is repeated until a satisfactory result is obtained.
As shown in
Figure 18, a BP neural network is used to calculate the global optimum, not the local optimum. Ant colony algorithm optimization is used to calculate the global optimum.
The whale algorithm (WOA) combined with a BP neural network model is used to calculate the global optimum, as shown in
Figure 19: