# Dynamic Compensation of a Fluxgate Magnetometer Based on a Hybrid Optimizing Algorithm
## 2 Materials and Methods
### GA-SA Hybrid Optimization Algorithm
Determine whether convergence criteria are met:  
Determine if the optimal fitness \(f_{opt}\) is less than the threshold \(th\) or the number of iterations reaches the maximum number \(tmax\). If '\(Yes\)', output the result. If '\(No\)', keep the algorithm proceeding. 4. Perform replication, crossover, and variation operations on individuals. Generate the next generation population. (1) Replication: The purpose of replication is to retain excellent individuals in the evolutionary process of the population so that the population converges in the direction of the optimal solution. The gambling wheel selection method not only ensures that excellent individuals have a greater probability of being selected, but also allows individuals with poor fitness to have a chance of being selected so as to maintain the diversity of the population. The specific implementation is as follows:  
First, the probability function \(pi\) of each \(Ai\) is calculated according to the fitness function. \[p_{i}=\frac{\sum\limits_{i=1}^{n}\left(f_{i}-\overline{f}\right)}{(n-1)\sum \limits_{i=1}^{n}f_{i}} \tag{7}\]  
In this way, each individual \(Ai\) forms a unit interval of length 1 according to the probability function \(pi\), and the interval length represented by each individual \(Ai\) is equal to its probability function \(pi\), generating random numbers \(r\) between [0, 1]. According to the interval where the random number is located, the individuals represented by the interval are selected to be copied and retained by the next generation. If the random number \(r\) is satisfied with \(p_{0}+p_{1}+\cdots+p_{i-1}<r<p_{0}+p_{1}+\cdots+p_{i-1}+p_{i}(p_{0}=0)\), select individual \(Ai\) to copy into the next generation population. (2) Crossover: The purpose of crossover is to pass the good genes in the population to the next generation and generate a new generation of individuals through information exchange to maintain the diversity of the population. In order to avoid excessive damage to excellent individuals caused by the crossing process, the single-point crossing method with the weakest strength is selected as the crossing method, which is implemented as follows:  
The individual is encoded in binary to generate a binary string. In a crossover operation, a number pair \((i,j)\), \(i,j\)\(\in\)[1, n] in the range of the population size is randomly generated. That is, the binary strings of individuals \(Ai\) and \(Aj\) are selected for crossover operation;then, an integer \(m\) within the length range of the binary string is randomly generated, and the \(m\)th point in the binary string is selected as the intersection point. Finally, a random number \(r^{\prime}\)\(\in\) [0, 1] is generated for comparison with the crossover probability \(pc\). If the random number \(r^{\prime}\) is less than the crossover probability \(pc\), the crossover will be carried out, and the coding bits after the crossover point of the two individuals will be exchanged to generate the next generation of individuals. If the random number \(r^{\prime}\) is greater than the crossover probability \(pc\), no crossover is performed. For example, when \(m=4\), the change in binary strings \(Ci\) and \(Cj\) of individuals \(Ai\) and \(Aj\) is shown in Equation (8). \[\left\{\begin{array}{l}\left\{ \begin{array}{l}C_{i}^{k}=01100001\\ C_{j}^{k}=1001101010\end{array} \right.\right.\rightarrow\left\{ \begin{array}[ ]{l}\left\{\begin{array}{l}C_{i}^{k+1}=0110101010\\ C_{j}^{k+1}=1001|010001\end{array}\right.(r^{\prime}<pc)\\ C_{i}^{k}=0110010001\end{array} \right.\right.\rightarrow\left\{ \begin{array}[ ]{l}\left\{\begin{array}{l}C_{i}^{k+1}=0110101001\\ C_{j}^{k+1}=1001|101001\end{array}\right.(r^{\prime}\geq pc)\end{array} \right. \tag{8}\]  
where \(C_{i}^{k}\) and \(C_{j}^{k}\) represent the binary strings of the \(k\)th generation of individuals \(Ai\) and \(Aj\). (3) Variation: The purpose of variation is to maintain the diversity of the solution set, and the process is basically the same as crossover.