# Prediction Model for Compaction Quality of Earth-Rock Dams Based on IFA-RF Model
## 2 Materials and Methods
### Improving Firefly Optimization Algorithm
#### 2.5.2 Improved Firefly Optimized Random Forest Algorithm  
The core concept of the IFA optimized RF algorithm is to utilize the RF hyperparameters as the initial positions of the fireflies. The fitness of each firefly is computed, and through the movement and updating of the fireflies, the one with the lowest fitness is selected to determine the optimal RF hyperparameters. The pseudocode of the IFA-RF algorithm is presented in Algorithm 1. The procedure is as follows:  
Step 1: Initialization of the FA: Set the Parameters: Initialize the FA parameters, including population size, maximum iterations, initial brightness, attraction coefficient, and step size factor. Generate the initial population: Randomly generate a set of firefly individuals, each representing a solution (i.e., a combination of the RF's _Ntree_ and _Mtry_ parameters). Step 2: Introduction of dynamic Inertia Weight and Adaptive Factors: Introduce a dynamic inertia weight to regulate the search range of solutions during firefly movement. As iterations proceed, the inertia weight gradually decreases, improving the local search capability in the later stages of convergence. Dynamically adjust the movement step size and attraction coefficient based on the relative brightness and fitness differences between fireflies. The attraction between superior individuals is increased to accelerate convergence, while the attraction between inferior individuals is decreased to avoid local optima. Step 3: Introduction of DES: Apply the differential evolution strategy to each firefly. Select three individuals from the current population, compute the difference vector, and generate a new candidate solution. Compare the fitness of the candidate solution with that of the current solution, selecting the superior one to advance to the next generation. Step 4: Firefly Movement and Updates: Based on the objective function (the error between predicted and actual compaction quality), compute the brightness of each firefly. Calculate the movement vector of each firefly based on brightness differences and the introduced dynamic inertia weight and adaptive factor, then update the individual positions. Update the positions and brightness values for each firefly. Step 5: Adaptive Optimization of RF Hyperparameters: Upon convergence or reaching the maximum iteration, extract the _Ntree_ and _Mtry_ parameter combination corresponding to the optimal firefly. Use this optimal parameter combination to train the RF and apply it to compaction quality prediction. Step 6: Model Evaluation and Validation: Evaluate the RF model optimized by the IFA by calculating the prediction accuracy, error rates, and other performance indicators. Apply the model to real-world data to validate its generalization capability and robustness. ```
1:Rolling parameters, material characteristic parameters, meteorological factors
2:Initialize IFA-RF parameters, such as the number of fireflies, maximum iteration count (max_gen), _Mtry_, _Ntree_, lower bounds for parameters, upper bounds for parameters, and dim. 3:Initialize the firefly population and fitness values. 4:Compute the initial fitness of each firefly's position in the random forest model and identify the initial optimal solution. 5:For gen = 1:max_gen % Loop through the number of iterations
6:Dynamically adjust the differential evolution parameter (F) using a sine-function-based adaptive factor. 7:For i = 1:n % Iterate over each firefly
8:For j = 1:n % Compare each firefly
9:If Check each firefly's fitness and compute the distance between two fireflies, then update the position using the dynamically adjusted inertia weight. 10:Verify if the search position exceeds the search space. If so, reposition the firefly within the bounds; otherwise, leave the position unchanged. 11:End if
12:End For
13:Randomly select two fireflies and perform differential mutation to generate a new solution, ensuring that the mutated solution stays within the search space.