Interactive classroom example

How does a genetic algorithm learn?

A genetic algorithm solves a problem by evolving a population of candidate solutions. In this example, every candidate is a string of letters trying to evolve into the target phrase.

1 · EvaluateMeasure how close each candidate is.
2 · SelectPrefer stronger candidates as parents.
3 · CrossoverCombine parts of two parents.
4 · MutateRandomly change a few genes.
Evolution = selection + variation

Each generation repeats the same cycle. Good traits become more common, while random mutation keeps the population exploring.

%
Generation0
Best fitness0%
Best candidate—
Unique candidates0
Mutations this gen0

Fitness over time

The population should generally trend upward, but mutation can create temporary setbacks.

Current population

Each row is an individual. Green = best candidate.

The four stages

1

Evaluate fitness

Compare each individual's genes with the target. Here, fitness is simply the percentage of matching characters.

2

Select parents

Better individuals get more chances to reproduce. This is selection pressure: useful traits become more likely to survive.

3

Crossover

Pick a crossover point and combine two parents. The child inherits some genes from each parent.

4

Mutation

Randomly alter genes with a small probability. Mutation prevents the population from becoming too predictable.