A Compact Snake Optimization Algorithm in the Application of WKNN Fingerprint Localization

Indoor localization has broad application prospects, but accurately obtaining the location of test points (TPs) in narrow indoor spaces is a challenge. The weighted K-nearest neighbor algorithm (WKNN) is a powerful localization algorithm that can improve the localization accuracy of TPs. In recent years, with the rapid development of metaheuristic algorithms, it has shown efficiency in solving complex optimization problems. The main research purpose of this article is to study how to use metaheuristic algorithms to improve indoor positioning accuracy and verify the effectiveness of heuristic algorithms in indoor positioning. This paper presents a new algorithm called compact snake optimization (cSO). The novel algorithm introduces a compact strategy to the snake optimization (SO) algorithm, which ensures optimal performance in situations with limited computing and memory resources. The performance of cSO is evaluated on 28 test functions of CEC2013 and compared with several intelligent computing algorithms. The results demonstrate that cSO outperforms these algorithms. Furthermore, we combine the cSO algorithm with WKNN fingerprint positioning and RSSI positioning. The simulation experiments demonstrate that the cSO algorithm can effectively reduce positioning errors.


Introduction
Metaheuristic algorithms are computational algorithms that draw inspiration from the behavioral patterns of various natural creatures. Particle swarm optimization (PSO) is a classical metaheuristic algorithm proposed by Kennedy and Eberhart based on birds' feeding behavior. It involves a global optimal position and an individual optimal position. In each iteration, these two locations are updated according to the fitness values to obtain the optimal solution to the problem. The PSO algorithm's attributes, including ease of implementation, fast optimization, and limited parameters, enable its application to numerous fields [1][2][3]. Due to the excellent performance and broad application of PSO, many research endeavors have dedicated substantial efforts to this domain. Zhan et al. proposed adaptive PSO (APSO) by using a parameter adaptation scheme and an elitist learning strategy [4]. Neri et al. proposed compact PSO (cPSO) to reduce memory usage [5]. Zheng et al. proposed compact adaptive PSO (cAPSO) by combining the compact strategy with APSO for mobile sensor localization [6]. Liang et al. proposed comprehensive learning PSO (CLPSO), which uses a novel learning strategy to enable the diversity of the swarm and discourage premature convergence [7]. Hu et al. proposed a multisurrogate assisted binary PSO, which develops a modified strategy for updating the population in binary PSO [8]. Inspired by the hunting behavior of whales, Mirjalili et al. proposed the whale optimization algorithm (WOA), which divides whaling into two phases: surround prey and bubblenet attacking method [9]. The artificial bee colony (ABC) algorithm eliminates the influence of the global best individual by allowing bees to become onlooker bees or employed bees. Employed bees explore the environment around their position, and onlooker bees select an employed bee to follow based on its fitness value, ultimately concluding the algorithm by returning the best fitness value discovered by all bees [10]. The black hole algorithm (BH) was devised by Hatamlou, drawing inspiration from the phenomenon of black holes [11]. Zheng et al. proposed an opposition-based learning black hole (OBH) algorithm to improve the accuracy of mobile wireless sensor network localization [12]. Zheng et al. proposed a new black hole algorithm by developing a compact strategy and an elitist learning strategy to improve the ability to jump out of the local optimum [13]. Zheng et al. also proposed the Levy Flight Edge Regeneration Black Hole algorithm (LEBH) to speed up convergence and enhance optimization capabilities [14]. Furthermore, Pan et al. conducted a comprehensively state-of-the-art investigation of the engineering applications employed by binary metaheuristic algorithms [15].
Wireless sensor networks (WSNs) have been widely used in various fields, such as safety monitoring of underground mines [16], home automation [17], and agriculture [18]. The localization of sensor nodes plays a significant role in such applications of WSNs. WSNs are also used for forest fire detection [19] and environmental monitoring [20]. To obtain accurate information about monitored objects, the positioning and status of sensors are critical for WSNs. The current location service field can be divided into outdoor and indoor. Most outdoor positioning is based on satellite positioning, such as global positioning systems (GPS), the principle of which is to calculate the user's position information using the relationship between the receiver and the satellite. However, building occlusion weakens satellite signals in indoor environments, leading to inaccurate positioning results [21]. Therefore, accurate indoor localization has become a research frontier in the localization field, offering great potential for research institutions. There are two main categories of WSN-based indoor localization techniques: range-based (arrival-based) and fingerprint-based [22]. In the range-based or arrival-based field, many researchers have introduced several techniques, such as angle of arrival (AOA) [23], time difference of arrival (TDOA) [24], and received signal strength indicator (RSSI) [25,26].
Traditional fingerprint-based localization usually uses RSSI to measure the distance between the transmitter and the receiver and then match the location [27]. However, the accuracy of the matching process is often impacted by various factors, including multipath effects, shadow effects, and non-line-of-sight (NLOS) propagation, which adversely affect localization precision [28]. To improve accuracy, Ma et al. proposed an RSSI ranking fingerprint localization system that considers the ranking of the access point (AP) [29]. Oh et al. proposed an adaptive K-nearest neighbor algorithm in which reference points (RPs) are involved in the positioning process only when their characteristic distances toward the corresponding test point (TP) are under a certain threshold [30].
The SO algorithm has characteristics such as fast convergence speed, strong search capability, robustness, and low algorithm complexity. Wireless sensor nodes have the characteristics of low energy consumption and low memory, requiring low algorithm complexity and a small population size. However, the population size is related to the diversity of the algorithm and therefore affects its performance. In order to reduce the population without affecting algorithm performance, we introduced a compact strategy to the SO algorithm that simulates the population with probability. In this paper, a compact strategy is applied to improve the performance and reduce the memory usage of snake optimization (SO). The compact snake optimization (cSO) algorithm is tested in 28 test functions of CEC2013 and compared with the common heuristic algorithms SO, ABC, fish migration optimization (FMO), grey wolf optimization (GWO), and differential evolution (DE). The simulation results show that cSO achieves better performance. Furthermore, we combine the cSO algorithm with WKNN fingerprint positioning and RSSI positioning.
The simulation experiments demonstrate that the cSO algorithm can effectively reduce positioning errors.
The remainder of this paper is organized as follows. Section 2 introduces the SO algorithm, RSSI localization, and the weighted k-nearest neighbor (WKNN) method used in WSN localization. Section 3 provides a detailed explanation of the compact strategy employed in cSO and the combination of cSO and WKNN fingerprint localization. The simulation results are analyzed in Section 4 to discuss the efficiency of the algorithm and its effectiveness when applied to WKNN fingerprint localization. Section 5 provides a summary of the paper.

Snake Optimizer
The SO algorithm is a new intelligent optimization algorithm proposed by Hashim et al. that turns the feeding, fighting, and mating behavior of snakes into a mathematical mode [31][32][33][34]. Feeding is divided into two phases: the exploration phase and the exploitation phase. The survival strategies of snakes are complex and intriguing. Unlike other metaheuristic algorithms, snake optimization divides the population into males and females. SO is initialized using randomly generated populations. In addition, as snakes are cold-blooded animals, the temperature has a decisive influence on their feeding and copulatory behavior.
The temperature (Temp) is calculated using the following equation.
where t refers to the current iteration, and T is the maximum number of iterations. The quantity of food (Q) can be defined by Equation (2).
Q < 0.25 indicates inadequate food in the environment, meaning that snakes are in the exploration phase and search randomly for food.
The exploration phase is expressed as follows: where X t+1 i,m and X t+1 i, f refer to the positions of the i-th male and female, respectively, and X t random,m and X t random, f refer to the positions of individuals randomly selected from the male or female population. C 2 is set to 0.05, rand is a random value ranging from 0 to 1, and t is the current iteration of the algorithm. A m and A f indicate the ability of males and females to find food, which can be represented by Equations (5) and (6), respectively.
where f rand,m and f rand, f refer to the fitness of X rand,m and X rand, f , respectively, and f i,m and f i, f are the fitness values of the i-th individual in the male and female groups, respectively. In the exploitation phase, there is enough food available in the environment. If the temperature > 0.6, snakes continue to search for food. Male and female positions can be updated according to Equation (7).
where X i,j is the new position of an individual in both the male and female populations. If temperature < 0.6, snakes enter a fight-or-mating state.
Fight Mode: where FM and FF represent males' and females' fighting abilities, respectively, and X best,m and X best, f are the positions of the best male and female individuals, respectively. f best, f and f best,m indicate the fitness of X best, f and X best,m , respectively. Mating Mode: where MM and MF represent males' and females' mating abilities, respectively.

RSSI Localization
RSSI is a widely used parameter for indoor localization due to its ability to provide information about the strength of the signal between the transmitter and the receiver [35]. It relies on the signal power measurement from an AP to a device. As radio waves attenuate following the inverse-square law, the distance between the AP and the client device can be estimated by analyzing the relationship between the transmitted and received signal strengths. As the number of available APs increases, it is possible to collect more information.
In recent years, many improved methods based on RSSI localization have been proposed. Jondhale et al. proposed a range-free algorithm based on RSSI measurements, namely support vector regression [36]. The support-vector-regression-based localization scheme estimates target locations directly through field measurements, bypassing the need for distance computations. Shin et al. proposed a crossing assistance system based on RSSI measurement and Bluetooth for outdoor and indoor location tracking [37]. Herein, we use the KNN method and support vector machine to overcome the problems of the system to enable accurate outdoor positioning.
For RSSI distance measurement, the receiver determines the distance from the transmitter by measuring the energy of the radio frequency signal. Using the distance information, the location of TP can be obtained by the methods of trilateration, least squares, etc. When a wireless signal is transmitted in the atmosphere, the signal strength decreases with distance due to various factors. The mathematical relationship between signal strength change and propagation distance is expressed as follows: where RSSI indicates the signal strength at a distance of d from the sending node; A is the signal strength at a distance of d 0 from the sending node, which is generally derived from experience or hardware specification definitions; d is the distance between nodes; n is the signal attenuation index; d 0 is the reference distance, usually sets to 1 m; and N 0 is a Gaussian random noise variable. We use RSSI i to represent the RSSI between TP and the i-th AP. The distance (d i ) between TP and the i-th AP is shown in Equation (17).

Fingerprint Localization
Fingerprint localization is an important indoor localization technology, with many scholars conducting research in this area. Hoang et al. proposed recurrent neural networks for WiFi fingerprinting indoor localization [38]. Ahmed Shokry et al. proposed a quantum fingerprint-based localization algorithm to enable large-scale location tracking systems [39]. Fingerprint localization involves two stages: offline training and online positioning. Before these two stages, depending on the size of the target area, the distribution density of RPs is selected appropriately. The location of each RP is determined based on density and the size of the target area. In the offline training stage, the receiver collects the RSSI from different APs at each arranged RP. Then, the collected data are stored in a database to construct and calibrate the radio map. During the online positioning stage, we use the nearest neighbor (NN), k-nearest neighbor (KNN), and WKNN methods to locate the TP. The introduction of WKNN is as follows. The collected RSSI between the i-th RP and M APs can be represented as The characteristic distance between the TP and the i-th RP is as follows: where RSSI tj represents the RSSI between TP and the j-th AP, and RSSI rij represents the RSSI between the i-th RP and the j-th AP. The calculation result corresponds to the Manhattan distance (MD) and Euclidean distance (ED) when r = 1 and r = 2, respectively. Distances from all RPs to TP are calculated and ranked, and the closest K RPs are selected. We use p 1 = (x 1 , y 1 ), p 2 = (x 2 , y 2 ),. . . , p k = (x k , y k ) to represent the coordinates of RPs. The estimated location of TP (x t , y t ) is calculated by the following equation.
where ω i represents the weight of p i , as shown in Equation (21).
where D i represents the distance between the TP and the i-th RP.

Application of cSO for Partition WKNN Fingerprint Localization
This section mainly introduces the compact strategy and its application to the snake optimizer algorithm. Then, we apply the cSO algorithm to indoor positioning algorithms to verify its performance and the improvement of the cSO algorithm in terms of positioning accuracy.

Compact Strategy
The main objective of the compact strategy is to minimize memory usage while maintaining or even enhancing the performance of the original algorithm. Naturally, reducing memory usage leads to improved running speed. In order to reduce memory usage and improve running speed when running the SO algorithm, we introduce the compact strategy into the SO algorithm. Populationless is the most prominent feature of compact algorithms. A virtual population is used instead of the actual population. The virtual population is a probability model that represents the overall movement state of the population. The perturbation vector is defined as PV t = (µ t , σ t ), where t is the current iteration. µ and σ are the mean and standard deviation of the probability distribution function (PDF), respectively. The PDF and cumulative distribution function (CDF) are computed by Equations (22) and (23), respectively.
The iterative update of the perturbation vector is based on comparison to identify the winner and loser. The following equations are used to update µ and σ.
where N p is the number of virtual populations, and t represents the current iteration. The pseudocode of the cSO is shown in Algorithm 1. X t mc and X t f c represent the t-th male and female individual generated from PV, respectively. X t newm and X t new f are the t-th male and female individual generated by the updating formula, respectively. Initialize X m , X f randomly 2: while (t ≤ T) do X t mc = generateFrom (PV) Calculate the fitness value of X mc X t f c = generateFrom (PV) Calculate the fitness value of X mc Define Temp using Equation (1) Define Q using Equation (2) 3: Execution Exploration Phase using Equations (3) and (4) 4: else if (Q > 0.6) then Execution Exploration Phase using Equation (7) 5: else 6: if (rand > 0.6) then Perform fight mode using Equations (8) and (9) 7: else Perform mating mode using Equations (12) and (13) 8:

end if
[winner, loser] = compare (fitness(X t mc ), fitness(X t newm )) X t m = winner Update the PV disturbance vector by Equations (24) and (25) [winner, loser] = compare (fitness(X t f c ),fitness(X t new f )) X t f = winner Update the PV disturbance vector by Equations (24) and (25) 10: end while

Partition Method
In order to reduce positioning errors, a partition positioning method is proposed in this paper. Dividing indoor space into corresponding areas according to different rooms. Place RPs at an appropriate density based on area. Unlike ordinary fingerprint localization, partitioned fingerprint localization first determines the partition based on the closest RP to TP, and then selects the K RPs closest to TP from the selected partition for calculation. The partitioning method can eliminate the interference of RPs outside the region and limit the RPs used to calculate the final coordinates to the partition where TP is located, thereby effectively reducing the error of fingerprint localization.

A Combination of the cSO Algorithm with RSSI Localization and Partition WKNN Fingerprint Localization
In the first phase of localization, this paper combines cSO with RSSI localization and uses cSO to discover the coordinate of TP and reduce the localization error. To improve positioning accuracy, we use the following fitness function.
where d RA represents the distance between the calculated result and APs, and d TA is the distance between TP and APs. In order to measure the accuracy of localization results, we use Equation (27) to calculate the error.
where (x, y) represents the coordinates of computed results, and (x t , y t ) represents the actual coordinates of TP.
In the second phase of localization, another TP coordinate is obtained via partition WKNN fingerprint localization.
In the final phase of positioning, the coordinates obtained from the first two stages are weighted and summed to obtain the final coordinates; the formula is shown in Equation (28).
where C 1 and C 2 are both set to 0.5. The coordinates (x r , y r ) and (x p , y p ) represent the results computed by cSO-based RSSI positioning and partition WKNN fingerprint localization, respectively.

Results and Discussion
In this section, we deploy simulation experiments of the algorithm and the new positioning method. The simulation results of the cSO are tested under 28 test functions of CEC2013 [40]. These functions are divided into three main types: unimodal functions, basic multimodal functions, and composition functions. They can evaluate the performance differences between the new algorithm and other algorithms. In addition, we deploy simulation experiments for WSN localization. The simulation outcomes of cSO-based localization indicate that the cSO algorithm can significantly decreases the positioning error.

Simulation Results on CEC2013
In this section, cSO is compared with SO, GWO, FMO, DE, and ABC to evaluate the its performance. Additionally, the performance of each algorithm is compared with that of the cSO algorithm using the Wilcoxon signed rank test with a significance level of α = 0.05. Table 1 Table 1 presents a performance comparison of cSO and common heuristic algorithms. The symbol ">" indicates that cSO outperforms the other heuristic algorithm, the "=" symbol indicates that the performance of cSO is comparable to that of the other heuristic algorithm, and the "<" symbol indicates that the performance of cSO is inferior to that of the other heuristic algorithm. The final row of Table 1 summarizes the comparison results across all test functions. Table 1 shows that the test performance of cSO is better than that of SO in 17 functions and the same as that of SO in 1 function. The test performance of cSO is better than that of ABC in all functions. The test performance of cSO is better than that of FMO in 25 functions and worse than that of FMO in 3 functions. The test performance of cSO is better than that of GWO in 21 functions, the same as that of GWO in 1 function, and worse than that of DE in 6 functions. Compared with DE, the test performance of cSO is better than that of DE in 16 functions, the same as that of DE in 2 functions, and worse than that of DE in 10 functions. Therefore, the combination of the SO algorithm with the compact strategy results in significantly improved performance compared to common heuristic algorithms.
To further illustrate the algorithm's effectiveness, we employ convergence curves for evaluation. However, as the convergence of some algorithms is quite similar, the performance differences are not obvious in the convergence curves. Therefore, we choose several representative scattered curves for display. Figure 1 indicates the convergence process of the algorithm on several test functions. The X-axis represents the number of iterations, while the Y-axis represents the fitness values of the different algorithms. A lower fitness value indicates a better performance on the respective test function. Figure 1 shows that the proposed cSO algorithm outperforms other heuristic algorithms on test functions f 4, f 5, f 7, f 8, f 13, f 16, f 17, f 18, f 20, and f 26. In the unimodal functions of CEC2013, cSO achieves the optimal value on f 4 and f 5. In the f 4 function, the cSO algorithm outperforms the other five algorithms at the beginning of the iteration and maintains a more stable downward trend as the iteration progresses. In the f 5 function, except for the poor performance of the ABC algorithm, cSO, SO, FMO, DE, and GWO can all find a good value at the beginning of the iteration and gradually decrease with the iteration. Although Figure 1b shows that cSO is equal to SO, FMO, and DE in the final results, Table 1 shows that cSO still outperforms them in terms of optimal values. In the basic multimodal functions of CEC2013, cSO achieves the lowest results on f 8, f 13, f 16, f 17, f 18, and f 20. On the f 8 and 16 functions, the other five algorithms have similar convergence curves and results, and cSO is significantly better than them throughout the entire iteration cycle. On the f 13 and 18 functions, ABC and FMO fall into local optima after a brief search at the beginning of the iteration. SO, GWO, and DE continue to search for optima with each iteration, ultimately achieving better results than the first two algorithms. The cSO achieves a good result in the early stages of the algorithm and gradually decreases with each iteration, ultimately obtaining the best value among all algorithms. In the first 300 iterations of the f 17 function, ABC, FMO, and SO have similar convergence curves. After 300 iterations, SO produces better results and significantly reduces the function value, while ABC and FMO fall into local optima. The function value of GWO decreases with iterations, showing a smooth convergence curve. The initial value of DE is not good, but a good result is quickly achieved at the beginning of the iteration, and the function value decreases significantly and continues to decrease as the iteration progresses. The cSO achieves a good result at the beginning and continues to decline for the first 200 iterations, then tends to stagnate. At the beginning of function f 20, all algorithms have an almost identical function value. The function value of cSO rapidly decreases, and the convergence curve becomes smoother as the iteration progresses. SO, DE, and GWO have similar convergence curves in the first 200 iterations. Afterwards, the function values of GWO slowly decrease, while DE has a lower convergence curve. The function value of SO rapidly decreases during the 200th to 500th iterations, then tends to stagnate. In the composition functions of CEC2013, cSO achieves the optimal value on f 26. The cSO quickly finds the result closest to the optimal value among all algorithms in the first 100 iterations, then converges. The convergence curve of GWO is gentle, and the final result is the worst. FMO exhibits a steeper convergence curve than GWO and obtains a slightly better value than GWO. SO and ABC continuously achieve better results in the first 400 iterations, with the convergence curve rapidly decreasing, then tending to flatten out. The DE makes significant progress in the first 200 iterations, followed by a slow decline in the curve.

Simulation Results of the New Localization Method
To verify the effectiveness of the cSO algorithm in indoor positioning, we compare the new positioning method with RSSI localization and cSO-based RSSI localization by simulation.
In the simulation experiment, a two-dimensional positioning area with a length of 300 m and a width of 300 m is set. Four APs are set at the four vertices of the experimental area, with coordinates of (0, 0), (300, 0), (300, 300), and (0, 300). The simulation experiments are carried out using MATLAB 2022a. In order to avoid obtaining the same distance from AP at two centrally symmetric RPs in the positioning area, the transmission powers of the four APs are set to different values in the simulation experiment.
With the aim of verifying the performance of the cSO algorithm proposed in this paper, the performances of RSSI localization based on SO, ABC, FMO, GWO, and DE are compared. Six sets of coordinates of TP are randomly generated, and we use six algorithms to estimate the position of TP and calculate the error.
Then, three groups of controlled experiments are designed according to the number of partitions, RP density, and noise. Each experiment contains six groups of data. In these three simulation experiments, RPs are evenly deployed in the positioning area, and TPs are randomly placed within the experimental field.

RSSI Localization Experiment
Simulation experiments are conducted to further validate the reliability of the proposed approach. We randomly place six TPs in the experimental area, as shown in Figure 2. Then, RSSI localization combined with each of the six algorithms is used for calculation, the results of which are shown in Table 2. Based on the experimental data, we can conclude that cSO is superior to other algorithms.  In the partition experiment, we divide the experimental area into four, five, six, seven, eight, and nine small areas. The different partitions are shown in Figure 3. Table 3 shows the effect of different numbers of partitions on localization error. Table 3 shows that even partitions have better errors than odd partitions in partition WKNN fingerprint localization because even partitions divide the simulation area more evenly. In addition, for a fixed simulation area, too many partitions cause the error to increase. Four-partition WKNN localization is more accurate than RSSI positioning in all partitions. Benefiting from the precise partitioning of WKNN fingerprint localization, the accuracy of the new method is superior to that of cSO-based RSSI localization in all experiments.

Density Experiment
For the purpose of investigating the impact of node density on experimental data, each test has a different density, with RP spacing is set to one, two, three, four, five, and six. Table 4 shows that the best density is 3 m between RPs. Excessive density leads to increased computational complexity and incorrect distance calculation. Low density results in too few RPs, making it difficult to accurately calculate the distance between RPs and TPs. With the intention of verifying the effect of noise on positioning accuracy, six groups with different noise levels (three, four, five, six, seven, and eight) are set up in this experiment. Table 5 shows that as the noise increases, the errors of all localization methods also increase. Noise directly affects the positioning errors of localization methods.

Conclusions
In this paper, we propose cSO as an improvement of WSN localization, which employs a compact strategy. By conducting experiments on 28 classical test functions, we comprehensively evaluated the proposed algorithm's performance. The simulation results demonstrate that the novel algorithm achieves competitive performance and good stability. Furthermore, we applied the cSO algorithm to localization and compared its performance with that of WKNN fingerprint localization, RSSI localization, and cSO-based RSSI localization. The results show that the cSO algorithm and fitness function proposed in this article can effectively reduce positioning errors.
In the future, we plan to propose new strategies and explore the potential benefits of combining new algorithms with the WSN localization method. These efforts have the potential to yield excellent results.
Author Contributions: Conceptualization, W.Z. and S.P.; methodology, S.P. and N.L.; software, L.X.; investigation, S.P. and Q.C.; writing-original draft preparation, W.Z. and S.P. All authors have read and agreed to the published version of the manuscript.