Real-Time Weight Optimization of a Nonlinear Model Predictive Controller Using a Genetic Algorithm for Ship Trajectory Tracking

: This paper presents a weight optimization method for a nonlinear model predictive controller (NMPC) based on the genetic algorithm (GA) for ship trajectory tracking. The weight coefﬁcients Q and R of the objective function in NMPC are obtained via the real-time optimization of the genetic algorithm instead of the trial and error method, which improves the efﬁciency and accuracy of the controller. In addition, targeted improvements are made to the internal crossover operator, mutation operator, crossover rate, and mutation rate of the genetic algorithm. The simulation comparison of trajectory tracking between NMPC with real-time-optimized weight coefﬁcients and the one with constant coefﬁcients is performed. Finally, the simulation result shows that the controller with real-time-optimized weight coefﬁcients has a better trajectory tracking effect than that with constant weight coefﬁcients.


Introduction
With the rapid development of deep-sea applications, ship automatic control has received unprecedented attention. Trajectory tracking is one of the key technologies used in ship intelligent control. Therefore, the research and design of trajectory tracking control to ensure the accurate tracking of the desired trajectory plays an important role in the safe and effective implementation of marine engineering applications [1][2][3].
Relevant scholars have made many efforts and achievements in the field of ship trajectory tracking control. Katayama and Aoki [4] used the characteristics of the Euler approximation model of ship dynamics to design a reduced order observer and output feedback controller, which can effectively track a preset straight-line trajectory. Qu et al. [5] proposed an unmanned ship exponential tracking controller based on an observer to estimate external disturbances and system uncertainties, with the controller able to ensure exponential convergence and strong robustness. Liu et al. [6] improved the nonlinear robust control algorithm based on sliding mode control theory to simulate the maneuverability of the surface ship.
However, the above control methods cannot take the system input constraints directly into account due to the physical limits of the thrusters. Considering that the model predictive control (MPC) has a superior ability to establish constraints on optimization problems, it has been widely used in various fields, such as semiconductor, energy, and aerospace, to achieve advanced control [7][8][9][10]. In the field of marine vessel control, Oh and Sun [11] combined the LOS guidance algorithm with MPC to implement the tracking control in the presence of input constraints. Li and Sun [12] proposed a new disturbance-compensated MPC algorithm for ship heading control. Kapetanović et al. [13] proposed applied linear model predictive control (LMPC) for the trajectory tracking of underactuated ships under disturbance and input constraint saturation. Furthermore, since ship dynamics is highly nonlinear, in order to improve the control performance, it stimulates the development of nonlinear model predictive control (NMPC) [14]. Abdelaal et al. [15] proposed a combined NMPC for the position and velocity tracking of underactuated surface vessels. Yang et al. [16] designed a robust NMPC scheme based on a nonlinear disturbance observer for the trajectory tracking control of a dynamic positioning ship.
It can be seen from the above research that NMPC can perform well for trajectory tracking. However, in traditional NMPC, the most important parameters of weight coefficients in objective function are generally obtained through trial or experience, which is time-consuming and easily falls into local optimization. Therefore, many studies have been conducted for the optimal weight tuning of model predictive control. Based on the real-time weight adjustment strategy, Zhao et al. [17] designed a spacing control law for the adaptive cruise control system, which can adjust the weight coefficient according to actual output demand. In addition, the development of intelligent algorithms also provides good ideas for this. For example, the genetic algorithm (GA) is combined with NMPC for parameter optimization because of its superior characteristics of global optimization [18]. In addition, Ramasamy et al. [19] used GA to determine the MPC weights to minimize overall energy utilization, reduce tracking error, and realize optimal control of the cement kiln. Essa et al. [20] adopted the combination of the Rhododendron search algorithm and GA to adjust the weight coefficients of MPC, and gained strong stability and robustness in the electro-hydraulic servo system. Zhang and Zhuan [21] proposed an improved genetic algorithm of the variable chromosome length coevolution method to optimize the weight matrices Q and R in the electromagnetic isolation system, and received better damping performance.
Aiming to improve the optimization performance of GA, the crossover operator and mutation operator have been improved upon in many studies. For example, Tang and Tseng [22] proposed an adaptive directed mutation operator, which can enhance the global search ability and accelerate the convergence of GA by integrating local directional and adaptive random search strategy. Chuang et al. [23] proposed the use of the directionbased crossover operator and the dynamic random mutation operator. The crossover operator uses the ranking information of chromosome fitness value in the population to generate offspring chromosomes. The mutation operator generates new chromosomes through an adaptively adjusted mutation step and random disturbance vector. However, the crossover operators cannot effectively produce offspring in some specific areas where the initial population is located, which reduces the performance of the algorithm. For the mutation operators, due to their random variability, on the one hand, the optimal solution of the current population may be mutated, thus destroying the optimal solution. On the other hand, it may not make any mutation to the worst solution, which cannot improve the population diversity. In addition, the crossover rate and the mutation rate are two important control parameters in GA, and their values affect the convergence of the algorithm. The traditional determination method is to take a constant value according to the results of many experiments, which cannot ensure the algorithm to finally converge to the global optimal solution.
On the basis of the above analysis, this paper proposes a novel real-time weight optimization method of a nonlinear model predictive controller using a genetic algorithm for ship trajectory tracking, and the main contributions are as follows: (1) NMPC is applied to the ship trajectory tracking control, and the genetic algorithm is used to optimize the weight coefficients of the objective function in NMPC in real time; (2) The crossover operator, mutation operator, crossover rate, and mutation rate in the genetic algorithm are improved to enhance the performance of the genetic algorithm.
The rest of the paper is organized as follows: Section 2 introduces the structure of NMPC for ship control. Section 3 puts forward the GA-based real-time weight optimization algorithm of NMPC. In Section 4, simulations are conducted and results are discussed. The last section gives the conclusions.

Ship Mathematical Model
For surface ships, the heave, pitch, and roll motions are neglected, and only the horizontal motions of surge, sway, and yaw need to be considered. According to Fossen [24,25], the inertial earth-fixed frame and the body-fixed frame attached to the moving vessel are constructed to build the nonlinear mathematical model of a surface ship as: where η = [x, y, ψ] T represents the ship's position and heading vector in the inertial earthfixed frame, ν = [u, v, r] T is the velocity vector in the body-fixed frame, J(ψ) is the transfer matrix, M is the mass matrix, including the added mass, and D is the damping matrix. These matrices are given as: In addition, τ = [τ u , τ v , τ r ] T is the control force vector, composed of surge force τ u , sway force τ v , and yaw moment τ r , which are limited by the nonlinear saturation characteristic of the propeller: where τ min = [τ umin , τ vmin , τ rmin ] T , τ max = [τ umax , τ vmax , τ rmax ] T are the minimal and maximal force vectors, respectively, that can be provided by the thrusters. Furthermore, d = [d 1 , d 2 , d 3 ] T refers to the unknown time-varying disturbance vector composed of wind, wave, and current environmental interference and model uncertainties.
Generally, the surface ship model can be expressed by the state-space equation as: where x = [x, y, ψ, u, v, r] T is the state vector, u = τ is the control input vector, and z is the output vector. In combination with Equation (1), A(t), B(t), C(t), Γ(t) can be expressed as: ,

Discrete-Time NMPC Scheme
In general, the NMPC scheme is designed based on the discrete-time model of the system. Therefore, referring to Zhu et al. [26] and Veerasamy et al. [27], the ship model (5) can be discretized utilizing a sampling time of T s as: where k is the current time step with regard to the current time t, T are corresponding vectors of the continuous-time model (5), and the discrete-time matrices A(k), B(k), as well as Γ(k), can be determined at each time step as: For trajectory tracking, the control objective is to operate the ship by thrusters to track the predefined reference trajectory under external disturbances and input saturation constraints. That is, for a given reference trajectory z r = [x r , y r , ψ r ] T , we need to determine the control input to make lim x→∞ z(t) − z r (t) = 0. Thus, the objective function of NMPC can be constructed as: where U(k) = [u 0 (k) T , . . . , u N c −1 (k) T ] T is the control input sequence in the control horizon N c , and Z(k) = [z 0 (k) T , . . . , z N p (k) T ] T is the predictive output sequence in the predictive horizon N p . Since N c is less than N p , it is assumed that, when N c ≥ n ≥ N p , u n (k) = u n−1 (k). In addition, Q = diag(q 1 , q 2 , q 3 ) and R = diag(r 1 , r 2 , r 3 ) are weight matrices that are positive and definite matrices, which proportionally allocate the state vector and the input vector in the objective function (9). Note that the system is nonlinear; thus, the system matrices are time-varying during the predictive horizon. However, since the navigation state of the ship changes slowly, it can be assumed that A(k), B(k), and Γ(k) remain unchanged during the prediction horizon. Therefore, the predictive outputs can be calculated like a linear model, and the nonlinear optimization problem will be transformed into a relatively simple quadratic optimization problem. Thus, the optimal control sequence U T is obtained by optimizing the objective function (9), and the first value u * 0 (k) is substituted into the process.
The estimated value of disturbance is introduced into the online optimization calculation of NMPC, so as to eliminate the influence of disturbance and realize robust control. Based on Yang et al. [16], the calculation method of interference estimation value is as follows: whered(t) is the estimate of d(t) , p(t) is an auxiliary state vector of the disturbance observer, and L 0 is a positive definite symmetric matrix. In addition, the predictive output sequence Z(k) can be obtained from Equations (7) and (8) as: where O d (k) = Γ(k)d(k), and: . . .

Genetic Algorithm-Based Weight Tuning of NMPC
The weight coefficients in objective function (9) are very important for the NMPC online optimization calculation. If not selected properly, it will lead to a non-optimal solution, or even non-convergence. However, the weight matrices Q and R are usually determined by trial or experience, which is usually time-consuming and cannot be adjusted in real time with the change of control state to maintain the optimal solution [28]. Therefore, it is necessary to adjust the weight matrices Q and R online in real time to improve the control accuracy of NMPC. In this way, the adjusted parameters will have the direct physical significance of making the optimization process faster and the results more accurate [29]. Considering that the genetic algorithm (GA) has the characteristics of strong adaptability and automatic search [30], it is selected to optimize the weight matrices Q and R.

General Principle of GA
GA is a method of searching an optimal solution by simulating the process of natural evolution. It transforms the problem-solving process into a process that is similar to the crossover and mutation of chromosome genes in biological evolution. The main components of GA are fitness function, selection operator, crossover operator, and mutation operator, etc. Fitness function is an index that is used to judge the quality of individuals in the group. It is evaluated according to the objective function of the problem. The function of the selection operator is to screen out individuals with excellent genes for follow-up. The crossover operator refers to the operation of replacing and reorganizing some structures of two parent individuals to generate new individuals. The content of the mutation operator is to change the gene value on some loci of the individual string in the population.
Generally, when using GA to solve an optimization problem, a population composed of multiple individuals should be constructed first. Each chromosome of individuals in the population represents a feasible solution to the problem. Additionally, each solution is substituted into the fitness function to evaluate the fitness of the individual. The solutions with low fitness are gradually eliminated, and those with high fitness are added. Then, the next generation is generated through replication, crossover, mutation, and other operations. In this way, individuals with high fitness will be obtained after n generations of evolution; thus, a relatively accurate solution to the problem can be obtained.
Herein, to obtain online the optimal weight factors of the weight matrices Q and R in the NMPC objective function, the weight factors q 1 , q 2 , q 3 , r 1 , r 2 , r 3 can be continuously optimized in GA. To improve the solution efficiency, a novel GA algorithm is proposed, considering the actual needs of ship track control, and utilizing the improved crossover operator, mutation operator, crossover rate, and mutation rate.

Fitness Function
Fitness function determines the direction of GA evolution, and is set according to practical problems. In NMPC, the best choice of Q and R is required to minimize the objective function (9) for the best trajectory tracking effect. Therefore, the Q and R optimized by the fitness function in this novel GA needs to meet the requirements of minimizing the objective function, and the fitness function is defined as follows:

Improvement of Crossover Operator
In order to ensure that the genes of better individuals can be inherited to the offspring, the tournament selection strategy is adopted for the genetic operator. The selected individuals will generate a new individual through the crossover operator, and the standard crossover algorithm can be expressed as follows: where Q 1 is the offspring chromosome; T 1 and T 2 are parent chromosomes. β is a random number between [0,1]. However, there is a certain chance that the tournament strategy will repeatedly choose parent chromosomes, which indirectly leads to insufficient diversity. In order to solve this problem, by introducing T 3 , the improved crossover strategy is as follows: Comparing Equations (14) and (15), the former is conducive to individual stability and less individual change range, due to less utilization of parent information. In Equation (15), each individual uses more information from the surrounding individuals, so that it is easy to quickly search a certain area in the early stage of the algorithm. Therefore, if the current number of iterations is less than T × Gen max , the crossover algorithm of Equation (15) is adopted. Otherwise, Formula (14) is adopted. The specific T and Gen max will be determined in subsequent experiments.

Adoption of Mutation Operator
Aiming to solve the problems of mutation operator, an improvement is made on the basis of the method proposed by Tsoulos et al. [31]. Firstly, chromosomes in the population are sorted according to their fitness values from high to low, and then the mutation scale is determined according to the mutation rate P m . In order to purposefully mutate the chromosome with low fitness rather than the optimal one, the mutation operator equation calculated using the PSO optimization method is as follows: where l 1 and l 2 are two positive constants, m 1 and m 2 are uniform random numbers between [0,1], and c b represents the chromosome with largest fitness currently found. The improvement can ensure that only the worst solutions will be mutated. This will improve the gene, increase the diversity of the population, and prevent the algorithm from falling into a local optimal solution.

Improvement of Crossover and Mutation Rate
According to the Darwinian evolution principle, in the early stage of genetic algorithm evolution, the crossover operator can enhance the global search ability of the algorithm, which can avoid falling into the local optimal solution. In the later stage of evolution, when getting closer to optimal solution, the crossover operator will affect the convergence speed. At this time, the mutation operator plays a more important role. Herein, the crossover rate P c and the mutation rate P m can be defined as follows: where P 1 is the initial crossover rate and P 2 is the initial mutation rate. Gen max is the maximum number of iterations and gen is the current number of iterations. With the progress of the search process, the crossover rate is getting smaller and smaller, while the mutation rate is getting larger and larger.

Optimization Procedure of GA-NMPC
The genetic algorithm-based nonlinear model predictive control (GA-NMPC) for vessel trajectory tracking is shown in Figure 1. The weight matrices Q and R of NMPC are optimized in real time using the genetic algorithm, and the implementation steps are concluded as follows. (1) Initialize the weight matrices Q and R, and obtain U(k) and Z(k) through Equations (9) and (10), respectively. (2) Define parent population size as N pop , and q 1 , q 2 , q 3 , r 1 , r 2 , r 3 are the genes carried by each individual's chromosome. (3) Encode chromosomes and calculate the fitness values through Equation (13), and sort the chromosomes from high to low according to fitness values. (4) Determine the number of chromosomes that need to be crossed according to crossover rate P c . Select a corresponding number of chromosomes with high fitness values through the tournament strategy, and then cross these chromosomes according to Equations (14) and (15) to obtain new chromosomes. Finally, the newly generated chromosomes are used to replace the chromosomes with the lowest fitness values in the parent population. (5) After crossover, the chromosome will be mutated according to the mutation rate P m .
The individuals with low fitness values in the parent population are mutated based on Equation (16). Thus far, a new parent population has emerged. (6) If the number of iterations does not reach the maximum number of iterations Gen max , continue the cycle. Otherwise, the individual with largest fitness value is introduced into objective function (9) as weight matrices Q and R. The optimal solution sequence U * (k) = [u * 0 (k) T , . . . , u * N c −1 (k) T ] T is obtained by solving the objective function (9), and then the first element u * 0 (k) is input into the closed-loop system to complete the ship trajectory tracking.

Simulation Evaluation and Discussion
In this section, the simulation evaluation of the trajectory tracking performance using the proposed GA-NMPC is implemented. The CSII ship, which is a replica of the supply ship with a ratio of 1:70, is chosen as the simulation object. Its a priori identified dynamic parameters are shown in Table 1.

Main Parameters Setting
The maximal and minimal thrust limit in (4) is set as τ max = [2.0N, 2.0N, 1.5N·m] T , τ min = [-2.0N, -2.0N, -1.5N·m] T , respectively. The sample time step is set as T s = 0.1. In GA-NMPC, the population size N pop = 30, and the initial crossover rate and mutation rate are P c = 0.9 , P m = 0.2, respectively. The parameters defined in Section 3.2.2 are set as T = 0.6, Gen max = 100.
In addition, two different reference trajectories are used to verify the performance, and they are set as follows: (1) Circle trajectory, with the expression described as: (2) Sine curve trajectory, with the expression described as: The desired heading ψ r of the two trajectories are both taken as the direction of the tangent vector along the path.

Results Analysis
The simulation results of the three cases based on the two trajectories are shown in  Figures 2a and 3 that the trajectory tracking results of the three cases are ideal. However, by observing the local enlarged picture, we can see that the error of the result obtained by the controller using constant weight matrices is slightly larger than that of GA-NMPC. In addition, the heading angle of NMPC with constant matrices Q and R cannot return to zero in time after completing a circle, so there will be obvious fluctuations when starting a new voyage. This problem can also be reflected in Figure 2b that, at the end of a tracking cycle, the ship velocities will fluctuate significantly. However, the adoption of real-time-optimized Q and R solves this problem well. Figures 4 and 5 show the tracking results of the sine curve trajectory. In Figures 4a and 5, the three cases have a good tracking effect on the trajectory. However, in contrast, the control result of NMPC with constant Q and R is slightly unstable compared with that of real-time-optimized Q and R. Additionally, when the element values in matrix Q are small, as can be seen from Figure 4b, the velocity ν fluctuates greatly. Therefore, by receiving ship feedback to optimize the weight matrices in real time, the tracking error can be reduced and obtain a better control effect.
Figures 2c,d and 4c,d respectively show the matrices Q and R optimized in real time by GA-NMPC during ship tracking of the two trajectories. It can be seen that, at every step, the genetic algorithm adjusts elements in the Q and R according to the tracking error to improve population fitness, which can achieve the best control effect. Although using constant weight matrices can also obtain a relatively ideal result, it needs many attempts and adjustments, which is time-consuming and laborious. In addition, the final control effect may not be the best, due to the invariance of weights.

Conclusions
In this paper, a real-time weight tuning method based on the genetic algorithm (GA) is proposed and applied in a ship nonlinear model predictive controller for ship trajectory tracking. The weight matrices Q and R to be optimized in the NMPC objective function are taken as individuals in GA. The individuals with the highest fitness are obtained as weight factors in the matrices of Q and R; thus, the NMPC will solve the objective function online and obtain the optimal control action. In addition, the crossover operator, mutation operator, crossover rate, and mutation rate of the genetic algorithm are improved to enhance the diversity of the population and the efficiency of searching for the optimal solution. This optimization method adjusts the weight proportion of the input value in the objective function in real time according to the actual navigation and makes up for the defects of the traditional trial and error method, as well as the empirical method. The controller optimized by this method is simulated and compared with the traditional NMPC. The results show that the proposed GA-NMPC method makes it easy to implement NMPC and improves the control efficiency of ship trajectory control.