## 2 Optimization of Spindle Built-In Dynamic Balance Mass Compensation Strategy
### Optimization Model  
The optimization of the mass compensation strategy optimizes the moving strategy of the two counterweights, builds a mathematical model of the compensation strategy of the counterweights, and then uses the genetic algorithm to optimize the phase of the counterweights in MATLAB. A schematic flow chart is shown in Figure 3.  
It can be seen from the principle of the built-in dynamic balancing device that the resultant force of the two counterweights balances the unbalanced force of the spindle when the spindle is balanced. According to the balance principle, the residual unbalanced force of the balanced spindle is taken as the optimization target, and the moving phase of the two counterweights is taken as the variable when establishing the mathematical model. When balancing the spindle, the vector expression of the residual unbalance force of the spindle is as follows:  
\[\overset{\rightarrow}{P}=\overset{\rightarrow}{F}+\overset{\rightarrow}{W} \tag{2}\]  
where \(\overset{\rightarrow}{P}\) is the residual force after balancing the spindle, \(\overset{\rightarrow}{F}\) is the resultant of the centrifugal forces of the two counterweights, \(\overset{\rightarrow}{W}\) is the inertia force of the unbalanced mass, and \(P\) is the value of the residual stress. As can be seen from Formula (2), if \(P\) is small enough or close to 0, the balance is more effective. Thus, \(P\) is considered as the optimization objective. Figure 2: Mechanical model of double counterweight balance. When applying the genetic algorithm to simulate the movement strategy of two counterweights, it is necessary to establish a mathematical model for the angles of the two counterweights that are in polar coordinates. The angles \(\varphi_{A}\) and \(\varphi_{B}\) of the movement of the two counterweights are set as two variables, and the residual unbalance force of the spindle is the optimization target. The mathematical model is as follows:  
\[\begin{array}{l}find\quad\varphi_{A},\varphi_{B}\\ \min\!P(\varphi_{A},\varphi_{B})=\sqrt{ \begin{array}{l}\left(2F\cos\!\left( \frac{\pi(\varphi_{B}-\varphi_{A})}{360}-\frac{\theta_{B}-\theta_{A}}{2} \right)\right)^{2}+W^{2}+2F\cos\!\left(\frac{\pi(\varphi_{B}-\varphi_{A})}{360} -\frac{\theta_{B}-\theta_{A}}{2}\right)\\ W\cos\!\left(\frac{\pi(\varphi_{B}-\varphi_{A})}{360}-\frac{\theta_{B}-\theta _{A}}{2}-\eta\right)\\ 0\leq\varphi_{B}\!\!<\!\!2\pi\end{array} } \tag{3}\]  
where \(P(\varphi_{A},\varphi_{B})\) is the residual stress, \(F\) is the resultant of the centrifugal force, \(W\) is the inertia force of the unbalanced mass, \(\varphi_{A}\) is the movement angle value of counterweight A in the mechanical balance head in polar coordinates, \(\varphi_{B}\) is the movement angle value of counterweight B in the mechanical balance head in polar coordinates, \(\theta_{A}\) represents the initial phase of counterweight A in the mechanical balance head, \(\theta_{B}\) represents the initial phase of the counterweight B in the mechanical balance head, and \(\eta\) is the phase of the inherent unbalance of the spindle. An N-group (\(\varphi_{A}\), \(\varphi_{B}\)) is randomly generated by computer as the initial solution of the first iteration. One can then calculate the \(P(\varphi_{A},\varphi_{B})_{j,m}\) that corresponds to the initial solution of group \(j\) in the \(m\)-th iteration and select and eliminate according to the size of \(P(\varphi_{A},\varphi_{B})_{j,m}\), where \(j=1,2,3,\ldots,N\), \(m=1\)  
Figure 3: Flowchart. 2, 3,..., \(H\), and \(H\) is the preset number of iterations. The probability \(p_{j,m}\) that the initial solution of the \(j\)-th group can be retained is:  
\[p_{j,m}=\frac{f(\varphi_{A},\varphi_{B})_{j,m}}{\sum\limits_{i=1}^{N}f(\varphi _{A},\varphi_{B})_{i,m}} \tag{4}\]  
where \(P(\varphi_{A},\varphi_{B})_{i,m}\) is the \(P(\varphi_{A},\varphi_{B})\) corresponding to the initial solution of group \(i\) in the \(m\)-th iteration, where \(i=1,2,3,...,N\).