TL;DR
A genetic algorithm searches a large space of candidate solutions by keeping a population of them, scoring each one with a fitness function, and producing the next population from the better ones. New candidates come from combining two parents (crossover) and from small random changes (mutation). It is useful when the best answer is hard to find by direct calculation and no gradient tells you which way to move.
Builds on: Global Optimization · Leads to: Symbolic Regression · Related: Evolution Strategies
Where it's used
Engineering design. An evolutionary algorithm was used to shape an antenna for NASA's ST5 spacecraft, which flew in 2006. The search tried many antenna shapes, kept the ones that transmitted best, and bred variations of them, reaching designs that human engineers had not drawn.
Finding formulas from data. Symbolic regression searches for a mathematical formula that fits a table of measurements. The space of formulas is too large to list, so the search breeds expression trees and keeps the ones that fit the data best.
Scheduling and routing. Assigning shifts to staff, or ordering the stops of a delivery route, has a huge number of possible plans. A genetic algorithm can keep a population of plans, score each by cost and rule violations, and improve them without checking every plan. Simulated annealing is another search method used for the same kind of problem.
How it works
Take a tiny problem: find a string of 4 bits with as many 1s as possible. The fitness of a string is the number of 1s in it, so the best string is 1111 with fitness 4.
Start with a population of four strings. Each generation runs the same loop: score, select, cross, mutate, replace.
- Score the population. The strings are 1010, 0111, 0001, 1100. Their fitnesses are 2, 3, 1, 2. Check: 1010 has two 1s, 0111 has three, and so on.
- Select parents. Choose parents with random draws, with probability proportional to fitness, so better strings are picked more often. Pick 0111 (fitness 3) and 1010 (fitness 2). Check: 0111 is more likely to be picked than 1010, but both can be picked.
- Cross the parents. Choose a cut point after bit 2 and swap the tails. The parent 01|11 and the parent 10|10 give the children 01|10 and 10|11. Check: each child has the same length as its parents, and every bit comes from one of them.
- Mutate a child. Flip each bit with a small probability (each flip is a yes-or-no draw), here one bit only. Flip the first bit of 0110 to get 1110. Check: 1110 has three 1s, one more than 0110 had.
- Replace the population. Keep the best four strings from the old and new candidates: 0111, 1011, 1110, 1010, with fitnesses 3, 3, 3, 2. Check: the average fitness rose from 2.0 to 2.75.
Repeat the loop, and the population tends to move toward 1111. The search has no guarantee of reaching the best answer. It improves on average, and it can stall.
The math
Step 2 in symbols: selection
Fitness-proportional selection picks candidate with probability
The chosen candidate is a Random Variable whose distribution is given by these . Over draws, the expected number of times is picked is .
For the example, the fitnesses are 2, 3, 1, 2, which sum to 8. So for 0111 is , and for 1010 is . Check: the four probabilities sum to 1.
Step 3 in symbols: one-point crossover
For strings of length and a cut point with , the children are
where and are the parents. With and , the parents 0111 and 1010 give and , as in step 3.
Step 4 in symbols: mutation
Each bit is flipped independently with probability , a Bernoulli draw per bit. With and , the chance that a child is left unchanged is . Check: the other 0.68 of children change at least one bit.
Step 5 in symbols: replacement
Common choices keep the best candidates from the union of old and new ones (elitism), or replace all of the old population. The choice controls how fast the population converges, and a fast-converging population explores less.
Worked examples
- The population is 11, 00, 10. Their fitnesses (number of 1s) are 2, 0, 1.
- The total fitness is .
- The probability of picking 11 is .
- The probability of picking 00 is , so 00 is never chosen as a parent.
- The probability of picking 10 is .
- Check: .
Interpretation: a candidate with zero fitness is never bred, so it is removed from the population.
- The parents are 1100 and 0011. Choose the cut point after bit 2.
- Child 1: take the first two bits of 1100 and the last two of 0011, giving 11|11 = 1111.
- Child 2: take the first two bits of 0011 and the last two of 1100, giving 00|00 = 0000.
- Fitnesses: 1111 has 4 ones and 0000 has none. So child 1 is the best possible string.
- Mutate child 2 by flipping bit 2 from 0 to 1, giving 0100.
- Check: 0100 has one 1, one more than 0000 had.
Interpretation: crossover can combine a good prefix from one parent with a good suffix from the other. Here the shortcut it found was the best possible string in one step, which is luck and not a rule.
- A model needs one setting . Its quality is scored by , where a higher score is better and the best setting is .
- The population is 0, 2, 5, 7. Their fitnesses are , , , .
- The two best are 2 (fitness ) and 5 (fitness ). Select them as parents.
- Crossover by averaging the two parents (a real-number form of crossover) gives .
- The child has fitness .
- This is better than either parent, and it is close to the best setting 3.
- Try it: take parents 2 and 7 and average them. What is the child's fitness? Answer: the child is , and its fitness is .
Common mistakes and tradeoffs
It is tempting to think a genetic algorithm always finds the best answer. In fact it has no guarantee. It can converge to a good but not best solution, or stall when the population loses variety. Running it several times with different random seeds is standard practice.
It is tempting to think more mutation always helps exploration. In fact a very high mutation rate turns the search into random guessing, and a very low one lets the population stop changing. Mutation rates are tuned for each problem.
It is tempting to think a fitness function that scores well must be good. In fact the search finds whatever the fitness function rewards, so a poorly designed score can produce a solution that exploits a loophole. The fitness function is the most important design choice.
It is tempting to think a genetic algorithm beats gradient descent in general. In fact when the function is smooth and its gradient is available, gradient descent is usually much faster. Genetic algorithms earn their cost when the search space is discrete, the objective is not differentiable, or only scores can be measured.
| Method | Needs gradients | Good for |
|---|---|---|
| Gradient descent | yes | smooth objectives, large models |
| Genetic algorithm | no | discrete choices, black-box scores |
| Evolution strategies | no | continuous parameters, parallel scoring |
Use a genetic algorithm when the candidates can be scored cheaply and in parallel, and when the space is too large to list. In reinforcement learning, evolution-style search is an alternative to policy-gradient methods when rewards are noisy or sparse. Do not use one when a single exact method works, or when each evaluation is so expensive that only a handful can be afforded.
History
Early work on evolutionary computing by Ingo Rechenberg and Hans-Paul Schwefel in Germany in the 1960s led to evolution strategies. In the United States, John Holland at the University of Michigan developed the genetic algorithm, and his 1975 book Adaptation in Natural and Artificial Systems set out the method. The approach spread in the 1980s as computers became cheap enough to run populations of candidates. David Goldberg's 1989 book made it accessible to engineers, and it became a standard tool for search and design.
Recap
- A genetic algorithm keeps a population of candidate solutions and scores each one with a fitness function.
- Better candidates are chosen as parents more often, following .
- Crossover combines two parents, and mutation makes small random changes, so the population explores new candidates.
- The loop of score, select, cross, mutate and replace can improve the population on average, but it has no guarantee of the best answer.
- It is the search method inside Symbolic Regression, and it is related to Evolution Strategies, which tune continuous parameters.
Read next: Symbolic Regression uses a genetic algorithm to search for formulas, and Evolution Strategies uses the same idea with continuous parameters.