A genetic algorithm (GA) is a population-based search and optimization method that imitates natural selection: it maintains a set of candidate solutions encoded as chromosomes, scores them with a fitness function, and creates new generations by selection, crossover and mutation. It is the best-known family within evolutionary computation, introduced in its classic form by John Holland in the 1970s.
Genetic algorithms need no gradient and make few assumptions about the problem, so they suit discrete, noisy, multimodal or black-box objectives where calculus-based methods fail. The price is many fitness evaluations and no guarantee of finding the global optimum.
How a genetic algorithm searches
A genetic algorithm runs a generational loop. It starts with a random or seeded population, evaluates every individual, and then repeatedly builds the next generation from the fitter members of the current one until a stopping criterion is met, such as a generation limit, a target fitness or a period without improvement.
- Initialize a population of encoded candidate solutions.
- Evaluate each individual with the fitness function.
- Select parents, favoring higher fitness.
- Recombine parents with crossover to produce offspring.
- Mutate offspring with a small probability per gene.
- Replace the old population, often keeping the best individuals through elitism.
The search works because selection concentrates effort on promising regions of the fitness landscape, crossover combines partial solutions found by different individuals, and mutation restores lost diversity so that the population can still explore. The schema theorem and the building block hypothesis offer a classic, though debated, explanation of why recombination helps.
Encodings and genetic operators
The representation of a solution largely determines how well a genetic algorithm performs, because each operator must produce valid and meaningful offspring. A poor encoding makes crossover destructive, while a good one lets useful traits pass from parents to children.
| Encoding | Typical problem | Suitable crossover | Suitable mutation |
|---|---|---|---|
| Binary string | Feature selection, knapsack | One-point or uniform | Bit flip |
| Real-valued vector | Continuous parameter tuning | Blend or simulated binary | Gaussian perturbation |
| Permutation | Scheduling, traveling salesman | Order or partially mapped | Swap or inversion |
| Integer vector | Configuration and allocation | Uniform | Random reset or creep |
| Tree | Program or formula evolution | Subtree exchange | Subtree replacement |
Tree encodings define genetic programming, which is usually treated as a separate method. For continuous problems, specialized evolutionary algorithms such as CMA-ES or differential evolution frequently outperform a plain real-coded GA.
Key parameters and their effects
A genetic algorithm has several interacting control parameters, and their balance decides whether the run explores broadly or converges quickly. The main tension is exploration versus exploitation: strong selection speeds progress but risks premature convergence on a local optimum.
| Parameter | Common range | Effect when increased |
|---|---|---|
| Population size | 50 to 1,000 individuals | More diversity, more evaluations per generation |
| Crossover rate | 0.6 to 0.95 | Faster mixing of building blocks |
| Mutation rate | About 1/L per gene (L = chromosome length) | More exploration, less inheritance |
| Tournament size | 2 to 7 | Higher selection pressure |
| Elite count | 1 to 5 percent of population | Best solutions preserved, diversity reduced |
| Generations | Tens to thousands | Better results at higher compute cost |
These ranges are rules of thumb, not laws: the right settings depend on problem size, encoding and evaluation cost. Practitioners often tune parameters empirically or use self-adaptive schemes in which mutation rates evolve with the solutions.
Selection pressure deserves particular care. Fitness proportionate selection becomes weak when fitness values are close together and overly strong when one individual dominates, so many implementations prefer tournament or rank-based selection, whose pressure stays stable regardless of the scale of the fitness values. The steady-state variant replaces only one or two individuals per step instead of the whole generation, which can speed early progress but also accelerates loss of diversity.
Genetic algorithms compared with related methods
Genetic algorithms differ from other evolutionary and swarm methods mainly in their emphasis on crossover and in how solutions are represented. The comparison below helps choose a method for a given problem.
| Method | Main variation mechanism | Best suited to |
|---|---|---|
| Genetic algorithm | Crossover plus mutation | Discrete and combinatorial problems |
| Evolution strategies | Self-adapted Gaussian mutation | Continuous optimization |
| Differential evolution | Difference vectors between individuals | Continuous, non-separable functions |
| Particle swarm optimization | Velocity updates toward good positions | Continuous, moderately sized problems |
| Simulated annealing | Single solution with probabilistic moves | Problems with cheap local moves |
| Gradient descent | Derivative of the objective | Smooth, differentiable objectives |
When gradients are available and the objective is smooth, gradient-based methods are far more efficient. Genetic algorithms earn their place when the search space is discrete, the objective is discontinuous, or the evaluation is a simulation that offers no derivatives.
Practical limitations and common mistakes
The main practical cost is evaluation budget. If one fitness evaluation takes a minute of simulation, a run of 200 individuals over 500 generations requires 100,000 minutes, so engineers parallelize evaluations, use surrogate models or reduce the population.
- Premature convergence: the population loses diversity early; remedies include larger populations, niching and island models.
- Poorly designed fitness function: the algorithm optimizes exactly what is measured and may exploit loopholes in the score.
- Constraint handling ignored: invalid offspring need repair operators or penalty terms.
- Single-run conclusions: results are stochastic, so several independent runs with different seeds are required.
Genetic algorithms also offer no optimality certificate. For problems with exact solvers, such as moderate linear or integer programs, a dedicated solver usually gives better and provable answers.
A non-technical view
A genetic algorithm solves problems the way breeders improve crops. It starts with many rough guesses. Each guess gets a score. The better guesses are allowed to have children. Children mix parts of two parents and get a few random changes.
After many rounds, the guesses improve. Nobody needs to know the perfect answer in advance. You only need a fair way to tell good guesses from bad ones. The method is patient rather than clever, and it can take many hours of computer time.
A concrete example
A university registrar must place 600 exams into 20 time slots and 40 rooms while avoiding student conflicts and room overcrowding. Exact methods struggle with the many soft preferences, so the team uses a genetic algorithm with a permutation-based encoding of exam order and a fitness function that penalizes clashes and back-to-back exams.
The algorithm runs with a population of 300, tournament selection of size 3, order crossover and swap mutation, and keeps the best two schedules each generation. After about 1,500 generations and 20 minutes on a workstation, it finds a timetable with zero hard conflicts and roughly 40 percent fewer back-to-back exams than the manually built schedule of the previous year.
Because results vary between runs, the team repeats the optimization with ten different random seeds and keeps the best timetable. The spread between the best and worst runs is small, which gives the registrar confidence that the result is robust rather than a lucky outcome, and the same setup is reused each semester with updated enrollment data.