Genetic algorithms and particle swarm optimization
How genetic algorithms and particle swarm optimization search for good designs and parameters by evolving populations of candidate solutions, without needing gradients.
What it is
Genetic algorithms (GA) and particle swarm optimization (PSO) are population-based, metaheuristic methods for finding good solutions to optimization problems. They need only the ability to evaluate how good a candidate is, not its gradient, so they suit problems that are nonlinear, non-differentiable, noisy, discrete or full of local optima: engineering design, scheduling, parameter calibration, structure prediction, feature selection and hyperparameter tuning. A genetic algorithm (John Holland, 1975) imitates natural evolution: a population of candidate solutions undergoes selection, recombination and mutation over many generations. Particle swarm optimization (Kennedy and Eberhart, 1995) imitates the social behavior of flocks of birds: candidate solutions (“particles”) fly through the search space, pulled toward their own best position and the best position found by the swarm. Related methods are differential evolution, CMA-ES and simulated annealing.
It answers questions such as: Which combination of design variables gives the lightest structure that meets all constraints? Which parameters make a simulation match measurements? What is the best schedule or layout among astronomically many possibilities? What trade-offs exist between conflicting objectives, such as cost and performance? They provide good, though not provably optimal, answers at the cost of many function evaluations.
Fundamentals
Genetic algorithm components
Each candidate is a chromosome, a string of genes encoding the design variables (binary bits, integers, real numbers, permutations for ordering problems). The algorithm is: (1) initialize a random population (commonly 50 to 200 individuals); (2) evaluate the fitness of each, the objective value or a transformation of it; (3) select parents, favoring fitter ones, by tournament selection (pick k random individuals, keep the best), roulette-wheel (probability proportional to fitness) or rank-based selection; (4) apply crossover to combine two parents (single-point, two-point or uniform for strings; simulated binary crossover or blend crossover for real numbers) with high probability (0.6 to 0.9); (5) apply mutation, small random changes with low probability per gene (for example 1/L for chromosome length L), which preserve diversity and explore new regions; (6) form the next generation, commonly with elitism (the best few survive unchanged); and repeat for tens to hundreds of generations. Constraints are handled by penalty terms, repair operators or feasibility rules.
Particle swarm optimization
Each particle i has a position xi (a candidate solution) and velocity vi in the search space. In each iteration the velocity and position update as vi ← w vi + c1 r1 (pi − xi) + c2 r2 (g − xi), and xi ← xi + vi. Here pi is the best position particle i has visited (cognitive component), g is the best position found by the swarm or by its neighborhood (social component), r1 and r2 are random numbers in [0, 1], w is the inertia weight (typically decreasing from about 0.9 to 0.4 to shift from exploration to exploitation), and c1, c2 are acceleration coefficients (around 1.5 to 2). A swarm of 20 to 50 particles often suffices. PSO is simple, has few parameters, handles continuous variables naturally, and is easy to parallelize.
Multi-objective optimization and other evolutionary methods
Many design problems have several conflicting goals. The multi-objective GA NSGA-II (Deb et al., 2002) ranks the population by non-dominated sorting (a solution is dominated if another is at least as good in all objectives and better in one) and keeps diversity using crowding distance, producing an approximation to the Pareto front, from which the decision maker picks a trade-off. Differential evolution (DE) creates new candidates by adding a scaled difference of population members, for example v = xa + F (xb − xc) with F around 0.5, then crossover, and is very effective for continuous problems; CMA-ES adapts a multivariate normal search distribution and excels on difficult continuous functions in up to a few hundred dimensions; simulated annealing accepts worse moves with a probability that decreases over time; memetic algorithms combine global search with local refinement.
Practical issues, tuning and limits
There is no guarantee of finding the global optimum, and no single algorithm is best on all problems (the no-free-lunch theorems). Typical issues: premature convergence (the population loses diversity and stalls at a local optimum), remedies being larger populations, higher mutation, niching, restarts, or local-best topologies; slow convergence compared with gradient methods when gradients are available, so use gradient-based optimizers for smooth problems; and a large evaluation budget, often thousands to millions of function calls, which is costly if each is a simulation, so surrogate models (see Bayesian optimization and Gaussian processes) or parallel evaluation are used. Because the algorithms are stochastic, run each problem many times with different seeds (for example 20 to 30) and report the mean, spread and best result, and check convergence plots. Encode variables sensibly, normalize ranges, and define stopping rules: maximum generations, stagnation of the best value, or a target fitness.
Software, typical applications and limits
Limits: many evaluations, no optimality proof, sensitive to settings and to constraint handling, and weaker than specialized methods on convex or smooth problems.
Basic operation
- Define the problem. State variables, bounds, objectives and constraints, and make the objective function run reliably.
- Choose the algorithm. Use GA for mixed or discrete variables, PSO or DE for continuous ones, NSGA-II for multiple objectives.
- Set parameters. Pick population size, rates or coefficients, and stopping rules.
- Initialize. Create a diverse random starting population within bounds.
- Iterate. Evaluate, select or update, recombine or move, and apply elitism.
- Monitor. Track best and average fitness and population diversity.
- Repeat with several seeds. Compare runs and check consistency.
- Collect the output. You obtain the best solution or a Pareto front and convergence histories; parameter tuning and constraint handling are covered in the video.
Good practice
- Run many seeds. Report the spread, not one lucky run.
- Test on benchmarks. Verify the implementation on known functions.
- Use gradients if you have them. They are far more efficient on smooth problems.
- Preserve diversity. Watch for premature convergence.
- Handle constraints deliberately. Check that final solutions are feasible.
- Verify the final design. Re-evaluate the optimum with a high-fidelity model, and for safety-critical designs, such as structures or medical devices, confirm with independent analysis and testing rather than trusting a heuristic search result.
Key terms
- Chromosome — the encoded representation of a candidate solution.
- Fitness — the measure of how good a candidate solution is.
- Crossover and mutation — recombining parents and randomly altering genes to create new candidates.
- Particle swarm — a group of candidates moving toward personal and global best positions.
- Inertia weight — the factor controlling how much of the previous velocity a particle keeps.
- Pareto front — the set of non-dominated trade-off solutions.
- Premature convergence — the population settling on a poor local optimum too early.
Video coming soon
About five minutes on Genetic algorithms and particle swarm optimization: how it works in detail, how the data is acquired, and how it is analyzed.
More in 4.3 AI and Machine Learning
- Regularized linear models
- Decision trees and random forests
- Gradient boosting (XGBoost)
- Support vector machines
- k-NN
- k-means
- Gaussian processes
- Neural networks
- CNNs
- RNN/LSTM
- Transformers
- Autoencoders
- GANs
- Diffusion models
- Graph neural networks
- Reinforcement learning
- Transfer learning
- Federated learning
- Explainable AI (SHAP, LIME)
- Physics-informed neural networks
- Bayesian optimization
- Active learning
- Symbolic regression
- Genetic algorithms and particle swarm optimization
- Dimensionality reduction (t-SNE, UMAP)
- NLP and large language models
- Retrieval-augmented generation
- Computer vision (detection, segmentation)
- Anomaly detection
- Machine-learned interatomic potentials
- AutoML
