# Path Optimization of Two-Posture Manipulator of Apple Packing Robots
## 3 Materials and Methods
### Comparison and substitution operation  
The comparison and substitution operation of population chromosomes is conducted after the crossover and mutation operations of every generation of population to realize the rapid convergence and stability of the genetic algorithm. The fitness value of every chromosome in the current population is calculated in accordance with the fitness function. The chromosome with the maximum fitness value in the current population is labeled as the optimal chromosome, where its fitness value is named \(f_{max}\). The optimal chromosome fitness value \(f_{max}\) in the current population is compared with the optimal chromosome fitness value \(f_{\text{oldmax}}\) in all previous populations. The optimal chromosome in the current population is updated to be the new optimal chromosome when \(f_{max}\) is larger than \(f_{\text{oldmax}}\). The optimal chromosome of all previous populations is retained as the new optimal chromosome of the current population when \(f_{oldmax}\) is larger than \(f_{max}\). Then, the new optimal chromosome of the current population is determined and the fitness value of the new optimal chromosome is \(f_{newmax}\). The new optimal chromosome of the current population is used to replace the chromosome where its fitness values are less than \(f_{lim}\) in the current population, as shown in Equation (5). \[f_{lim}=(f_{newmax}-f_{min})\cdot c+f_{min},\]where \(f_{min}\) is the fitness value of the worst chromosome in the current population, \(c\) is the substitution coefficient, and the value range is (0, 1). The population of chromosomes evolves. The process of the comparison and substitution operator is shown in Figure 10 when the population size is 5 and the substitution coefficient \(c\) is 0.4. (6) Termination condition setting  
After the crossover, mutation, comparison, and substitution operations, the new population gradually generates a convergent progeny population through repeated cycles. The algorithm terminates and outputs the optimal path result and corresponding working time when the iteration times reach the set value. Genetic algorithms consistently use empirical parameters. However, the use of empirical parameters in genetic algorithms is not necessarily reasonable for this study, so this study proposed an optimal parameter selection algorithm to select the optimal values of the four parameters. Four parameters are used in the genetic algorithm, and they are population size, crossover probability, mutation probability, and comparison substitution coefficient. We propose an optimal parameter selection algorithm to select the optimal values of the four parameters and to optimize the motion path of the manipulator. Then, the four selected optimal parameters are inputted into the genetic algorithm. The optimal path and time are obtained, as shown in Figure 7a. The main steps of the optimal parameter selection algorithm are as follows. (1) Determination of the ranges of the four parameters. The range of population size is [10; 100]. The ranges of crossover probability, mutation probability, and comparison substitution coefficient are (0, 1), and the values are rounded to two decimal places. A total of 365 groups of nonrepeating valid parameter values within their ranges are obtained. (2) Acquisition of the target path and program running times. The target path time denotes the packing time taken to fill up an apple tray corresponding to the optimal paths of the mechanic arm acquired by the genetic algorithm, and the program running time is the time taken to run the genetic algorithm. A total of 365 valid parameter values are inputted into the improved genetic algorithm, and the corresponding target path and program running times are obtained, as shown in Table 1.  
(3) Fitting between the two types of time and the corresponding four parameters. The four parameters were regarded as inputs, and the target path and program running times were regarded as outputs.