# Advancing Multi-UAV Inspection Dispatch Based on Bilevel Optimization and GA-NSGA-II
## 3 Solution Algorithm
### Nested Bilevel Optimization Algorithm
#### 3.2.1 Task Assignment Based on the NSGA-II Algorithm  
In the outer-layer optimization, the NSGA-II algorithm is adopted to encode and assign the unit tasks. The steps are as follows:  
Step 1: Population coding: The length of the chromosome is equal to the number of unit tasks, with the first half used to mark the sequence numbers of the tasks and the second half representing the UAV's serial number that directly constitutes the chromosome. To ensure the effectiveness and rationality of task assignment, we introduce three constraints: Constraint (9) ensures that each UAV is assigned at least one task; Constraint (10) ensures that each task is executed by only one UAV; Constraint (11) maintains the flow balance throughout the entire task assignment process, ensuring that the amount of tasks assigned to each UAV matches its execution capability. As illustrated in Figure 4, in the case of 12 unit tasks being carried out by 3 UAVs, each UAV will perform tasks, and each unit task can only be assigned to one UAV. Specifically, UAV number 1 is responsible for unit tasks numbered 3, 4, and 11; UAV number 2 is responsible for unit tasks numbered 1, 2, 5, 7, and 10; UAV number 3 is responsible for unit tasks numbered 6, 8, 9, and 12. Step 2: Population initialization: Firstly, a numerical sequence is generated based on the number of unit tasks to form the first layer of the chromosome encoding. Subsequently, a UAV is randomly assigned to each unit task to form the second layer of the chromosome encoding. This process ensures that each UAV is assigned at least once. The process is repeated until the population size reaches the preset value. Step 3: Pre-processing optimization: In order to meet the requirements of the location of the hives, this paper discusses the optimization strategies of the initial and landing hives. In addition, considering that the power consumption caused by the takeoff and landing process affects task execution, this paper analyzes how to reasonably divide the task under the comprehensive consideration of the takeoff and landing loss, thereby ensuring that the UAV can efficiently complete all inspection tasks before the power is exhausted. The above strategies can be found in Section 3.2.2. Step 4: ENS_SS is used for non-dominated sorting. The ENS_SS function was used to perform non-dominated ranking of population results. The function classifies the individuals in the population to determine the level and critical level of each individual. The ENS_SS function ranks the individuals of the population through a non-dominant  
Figure 4: Outer chromosome encoding. relationship, where the lowest layer is marked as 1, indicating that these individuals are not dominated by any other individuals in the population. Step 5: Calculate the crowding distance: The individual crowding distance for individuals within each class is calculated. Crowding distance is an index to measure the distribution density of individuals in the solution space, which plays an important role in maintaining the diversity of solution sets. Step 6: Select operation: The selection operator of binary tournament method is used to select the individuals with better performances according to their non-dominant ranking and crowding degree distance, and the selected individuals will be included in the next generation population. Step 7: Crossover and variation: An analog binary crossover operator is used to perform the crossover operation. The binary crossover operator simulates the single-point crossover process of binary coding to realize the gene recombination of individuals in a population, which is suitable for real coding individuals. The basic idea is to cross-operate between parent individuals in order to produce new offspring individuals while maintaining the diversity of the population. In addition, polynomial variation is used for variation operation. Polynomial variation increases the diversity of the population by introducing random perturbations to individual genes, thus improving the global search ability of the algorithm.