A Practical Guide to Population Parameter Optimization
Genetic Algorithms (GA) are powerful optimization techniques inspired by natural evolution. One of the most critical challenges in GA implementation is balancing convergence (finding optimal solutions quickly) and diversity (maintaining varied solutions to avoid local optima). In this blog post, we’ll explore this balance through a concrete example: optimizing the Rastrigin function, a challenging multi-modal benchmark problem.
The Challenge: Rastrigin Function Optimization
The Rastrigin function is a classic test case for optimization algorithms:
$$f(\mathbf{x}) = 10n + \sum_{i=1}^{n} [x_i^2 - 10\cos(2\pi x_i)]$$
where $n$ is the dimensionality and $x_i \in [-5.12, 5.12]$. This function has numerous local minima, making it perfect for testing how well our GA maintains diversity while converging to the global minimum at $\mathbf{x} = (0, 0, …, 0)$ where $f(\mathbf{x}) = 0$.
Key Parameters Affecting Balance
We’ll examine three critical parameters:
- Population Size: Larger populations maintain more diversity but require more computation
- Mutation Rate: Higher rates preserve diversity but may slow convergence
- Selection Pressure: Tournament size affects how aggressively we favor better solutions
Complete Implementation
1 | import numpy as np |
Code Explanation
Let me walk you through the key components of this implementation:
1. Rastrigin Function (Lines 11-17)
The objective function we’re minimizing:
$$f(\mathbf{x}) = 10n + \sum_{i=1}^{n} [x_i^2 - 10\cos(2\pi x_i)]$$
This function is challenging because it has many local minima (ripples), making it perfect for testing GA’s ability to maintain diversity.
2. GeneticAlgorithm Class (Lines 20-172)
Initialization (Lines 21-38): Sets up the population randomly within bounds and initializes tracking arrays for fitness, diversity, and convergence metrics.
Diversity Calculation (Lines 44-57): Computes average pairwise Euclidean distance between all individuals:
$$D = \frac{1}{n(n-1)} \sum_{i=1}^{n-1} \sum_{j=i+1}^{n} ||\mathbf{x}_i - \mathbf{x}_j||$$
Higher diversity means the population is more spread out, which helps avoid premature convergence.
Tournament Selection (Lines 59-68): Randomly selects tournament_size individuals and returns the best one. Larger tournament sizes increase selection pressure (favor better solutions more strongly).
Simulated Binary Crossover (SBX) (Lines 70-111): A sophisticated crossover operator that mimics the behavior of single-point crossover in binary-coded GAs but works directly on real values. The distribution index $\eta = 20$ controls how close offspring are to parents.
Polynomial Mutation (Lines 113-142): Applies mutation with probability mutation_rate. Uses polynomial distribution to perturb values, with distribution index $\eta_m = 20$ controlling the mutation spread.
Evolution Loop (Lines 144-172): The main algorithm:
- Evaluates fitness for all individuals
- Tracks best fitness, average fitness, and diversity
- Applies elitism (keeps the best individual)
- Creates offspring through selection, crossover, and mutation
- Replaces the old population
3. Experimental Setup (Lines 175-236)
We test five configurations:
- Small Pop + Low Mutation: Fast but may get stuck in local optima
- Small Pop + High Mutation: Maintains diversity but may converge slowly
- Large Pop + Low Mutation: Good exploration but computationally expensive
- Large Pop + High Mutation: Maximum diversity but may not converge well
- Balanced (Recommended): Optimal trade-off between all factors
4. Visualization (Lines 239-367)
Creates six comprehensive plots:
- Convergence Speed: Shows how quickly each configuration finds good solutions
- Diversity Maintenance: Shows how well each maintains population spread
- Average Fitness: Indicates overall population quality
- Final Performance: Bar chart comparing final results
- 3D Surface with Solutions: Shows where each configuration found its best solution on the Rastrigin landscape
- Convergence-Diversity Trade-off: Scatter plot showing the relationship between diversity and fitness in the final generations
5. Performance Optimization
The code is already optimized with:
- Vectorized NumPy operations instead of loops where possible
- Efficient diversity calculation (only upper triangle of distance matrix)
- Pre-allocated arrays for history tracking
- Elitism to preserve best solutions
Mathematical Foundation
The balance between convergence and diversity can be expressed as:
Exploration vs Exploitation Trade-off:
$$\text{Performance} = \alpha \cdot \text{Convergence Speed} + (1-\alpha) \cdot \text{Diversity Maintenance}$$
Where:
- High mutation rate → more exploration (diversity)
- Large tournament size → more exploitation (convergence)
- Large population → better exploration capability
Expected Results Analysis
When you run this code, you should observe:
- Small populations with low mutation converge quickly but may get trapped in local minima
- High mutation rates maintain diversity longer, potentially finding better solutions but taking more generations
- Balanced parameters typically achieve the best final fitness by avoiding both premature convergence and excessive randomness
- The 3D visualizations will show how different configurations explore different regions of the search space
Execution Results
====================================================================== GENETIC ALGORITHM PARAMETER OPTIMIZATION EXPERIMENT ====================================================================== Objective: Minimize Rastrigin function in 2D Search space: [-5.12, 5.12]^2 Global optimum: f(0, 0) = 0 Generations: 100 ====================================================================== [Small Pop + Low Mutation] Population: 30, Mutation Rate: 0.01, Tournament Size: 2 Best fitness: 1.049211 Best solution: (-0.9957, 0.0165) Time: 0.78s [Small Pop + High Mutation] Population: 30, Mutation Rate: 0.2, Tournament Size: 2 Best fitness: 0.000000 Best solution: (-0.0000, -0.0000) Time: 0.84s [Large Pop + Low Mutation] Population: 100, Mutation Rate: 0.01, Tournament Size: 2 Best fitness: 0.996236 Best solution: (-0.0017, 0.9969) Time: 3.76s [Large Pop + High Mutation] Population: 100, Mutation Rate: 0.2, Tournament Size: 2 Best fitness: 0.000000 Best solution: (-0.0000, -0.0000) Time: 2.83s [Balanced (Recommended)] Population: 60, Mutation Rate: 0.1, Tournament Size: 3 Best fitness: 0.000002 Best solution: (0.0000, 0.0001) Time: 0.96s ====================================================================== [Generating visualizations...]


[Analysis Complete] Key Insights: 1. Population size affects exploration capability 2. Mutation rate controls diversity maintenance 3. Tournament size determines selection pressure 4. Balance is crucial for optimal performance
This implementation demonstrates that successful GA optimization requires careful tuning of population parameters to balance the competing goals of quick convergence and thorough exploration of the search space.














