AI Course · Week 6

Evolutionary Algorithms

Borrowing the one idea that built every living thing, survival of the fittest, and turning it into a way to evolve solutions to hard problems.

By Arj Optimisation Est. reading time: 16 minutes

In local search, week 5, you climbed a hill one step at a time and hoped you did not get stuck. Evolutionary algorithms take a bolder idea: instead of improving one solution, keep a whole population of them, let the best ones survive and breed, add a little random mutation, and repeat. Over many generations, good solutions emerge on their own, the same way nature designs living things without any designer. This page shows you exactly how that works, and lets you watch it happen.

1

The core idea

Some problems are too big to solve by checking every option and too bumpy for hill climbing to work reliably. Designing a delivery route, tuning a set of parameters, or packing a container can have millions of possible answers with no obvious way to improve step by step. Evolutionary algorithms attack exactly this kind of problem, and they do it by copying biology.

The insight is that evolution is a search algorithm. Nature never sat down and designed the eye. Instead, many slightly different creatures existed, the ones better suited to their environment survived and had offspring, those offspring varied a little, and over enormous time this produced astonishing designs. Strip that down and you get a recipe a computer can follow.

  • Keep a population of candidate solutions, not just one.
  • Score each by a fitness measure: how good a solution is it?
  • Let fitter candidates survive and reproduce more often.
  • Combine parents (crossover) and add small random changes (mutation) to make offspring.
  • Repeat for many generations, and the population drifts toward better solutions.
One line to hold on to
A genetic algorithm is survival of the fittest, run on data instead of animals. No solution is designed directly. Good ones are discovered by breeding and selection.
2

The genetic algorithm loop

The most common evolutionary algorithm is the genetic algorithm, or GA. Everything else on this page is a detail inside this one loop, so learn the loop first and the rest slots into place.

  1. Initialise: create a random population of candidate solutions.
  2. Evaluate: score every candidate with the fitness function.
  3. Select: pick parents, favouring fitter ones.
  4. Crossover: combine pairs of parents to create offspring.
  5. Mutate: randomly tweak some offspring a little.
  6. Replace: the offspring form the next generation.
  7. Repeat from step 2 until a solution is good enough or you run out of generations.

Rather than trace this on paper, watch it. Below is a live genetic algorithm with one job: evolve a random jumble of characters into a target phrase. It starts from pure noise and uses only selection, crossover, and mutation. Press play.

Interactive Watch a population evolve

A population of 200 random strings evolves toward the target. The best candidate each generation is shown, with matched characters in green.

................
Generation
0
Best fitness
0 / 16
Population
200
Ready. Press play to evolve from random noise to the target phrase.
Notice how fitness climbs fast at first, then slows as the last few characters are the hardest to fix by chance. That slowdown is typical of real GAs.
3

Representation and chromosomes

Before a GA can breed solutions, you must decide how to write a solution down as data. This is called the representation or encoding, and getting it right is half the battle. A single candidate solution is called a chromosome, and each piece of it is a gene.

  • Binary encoding: a string of bits, like 10110010. Classic and flexible.
  • Character encoding: a string of letters, as in the demo above.
  • Permutation encoding: an ordering, like a sequence of cities for a travelling-salesman route.
  • Real-valued encoding: a list of numbers, for tuning continuous parameters.

The encoding decides what crossover and mutation even mean, so it is the first design choice you make and the one exams most often ask you to justify.

Exam technique
If asked to apply a GA to a new problem, always start by stating the representation: what is one chromosome, and what does a single gene mean? Marks are lost when students jump straight to crossover without defining what they are crossing over.
4

The fitness function

The fitness function is the heart of a GA. It takes a chromosome and returns a number saying how good that solution is. The entire algorithm is steered by this one score, so if the fitness function is wrong, the GA cheerfully evolves the wrong thing.

Fitness is problem-specific. A few examples:

ProblemA chromosome isFitness is
Evolve a target phraseA string of charactersNumber of characters that match the target
Travelling salesmanAn order of citiesThe negative of the total route distance (shorter is fitter)
TimetablingAn assignment of classes to slotsFewer clashes means higher fitness
The classic trap
A GA optimises exactly what you measure, not what you meant. If your fitness function rewards the wrong thing, you get a perfect solution to the wrong problem. Designing fitness carefully matters more than any other tuning.
5

Selection

Selection decides which candidates get to be parents. The goal is to favour fitter candidates so good genes spread, while not being so ruthless that the population loses variety and gets stuck. Three common methods:

  • Roulette-wheel (fitness-proportionate): imagine a roulette wheel where each candidate gets a slice sized by its fitness. Spin it to pick a parent. Fitter candidates have bigger slices, so they are picked more often, but weaker ones still have a chance.
  • Tournament selection: pick a few candidates at random and let the fittest of that small group become a parent. Simple, fast, and easy to tune by changing the tournament size.
  • Elitism: copy the very best one or two candidates straight into the next generation, untouched. This guarantees the best solution is never lost to unlucky breeding.
Why not just breed the single best?
Because a population of identical best-so-far solutions can never discover anything new. Selection must keep some variety alive, or evolution stops. This tension between favouring the fit and keeping diversity runs through the whole topic.
6

Crossover

Crossover, also called recombination, is how two parents produce a child. The idea is that if two decent solutions each got some things right, combining them might produce a child that inherits the best of both.

  • Single-point crossover: choose one cut point. The child takes everything before the cut from parent A and everything after from parent B.
  • Two-point crossover: two cuts, and the middle section comes from the other parent.
  • Uniform crossover: for each gene, flip a coin to decide which parent it comes from.

Try single-point crossover yourself. Drag the slider to move the cut point and watch how the child is assembled from the two parents.

Interactive Crossover and mutation

Parent A is blue, parent B is amber. The child takes genes before the cut from A and after the cut from B.

Parent A
Parent B
4
Child
A mutated gene turns green. Mutation is rare but vital: it is the only way to introduce a gene that neither parent had.
7

Mutation

Mutation randomly changes a gene with some small probability, for example flipping a bit or replacing a character. It sounds like it would just add noise, and if you turn it up too high it does exactly that. But at a low rate it does something crossover alone cannot.

Crossover can only shuffle genes that already exist in the population. If every candidate happens to have a 0 in one position, no amount of crossover will ever produce a 1 there. Mutation is the only operator that can introduce genuinely new genetic material, which is what lets a GA escape a rut.

Mutation rateEffect
Too low (near zero)Little new variety, the population can stagnate and get stuck
Well chosen (small)Enough fresh variation to keep exploring, without destroying good solutions
Too highGood solutions are constantly broken, the search becomes little better than random
Crossover vs mutation, in one sentence
Crossover exploits combinations of what the population already knows, mutation explores brand new possibilities. A good GA needs both.
8

Convergence and diversity

Run a GA long enough and the population starts to look similar: it has converged. Convergence is what you want if it settles on a great solution, but there is a failure mode with a name examiners love.

Premature convergence happens when the population becomes too similar too early, before finding a truly good solution. One decent-but-not-great candidate takes over, everything starts to look like it, diversity collapses, and the GA gets stuck, much like local search trapped on a small hill. The cures are the tools you have already met:

  • Enough mutation to keep introducing new genes.
  • Selection that is not too greedy, so weaker candidates still occasionally breed.
  • A large enough population to hold variety.
  • Elitism to protect the best, balanced against diversity so it does not dominate.
The balancing act
Every knob in a GA trades exploration against exploitation. Too greedy and it converges prematurely, too random and it never converges at all. Tuning a GA is really tuning that balance.
9

Uses and comparisons

Genetic algorithms shine on large, bumpy problems where you can score a solution but cannot easily compute the best one directly: route and schedule optimisation, engineering design, tuning parameters, game strategies, and antenna or circuit layout. They do not need the problem to be smooth or differentiable, which is their big advantage.

How they relate to what you already know

MethodKeepsExplores byMain weakness
Local search (week 5)One solutionSmall moves to neighboursGets stuck in local optima
Genetic algorithm (this week)A populationCrossover and mutationSlow, many parameters to tune
Swarm intelligence (week 7)A population (a swarm)Sharing information between agentsAlso parameter-sensitive

Local search, genetic algorithms, and swarm intelligence are all members of the same family: optimisation by trial, feedback, and improvement. The GA's distinctive trick is keeping a whole population and breeding it. Next week, week 7, swarm intelligence keeps a population too, but coordinates it in a completely different way.

10

Key points and practice

  • A GA keeps a population of candidate solutions, not one.
  • The loop is: evaluate, select, crossover, mutate, replace, repeat.
  • The representation defines a chromosome and its genes, and everything else depends on it.
  • The fitness function steers the whole search, so design it with care.
  • Selection favours the fit, crossover combines them, mutation adds novelty.
  • Watch out for premature convergence: keep enough diversity alive.
Worked example: one generation by hand

Target 1111. Fitness is the number of 1s. Start with a tiny population of four:

Candidate Fitness 1001 2 1100 2 0110 2 1011 3 <- fittest Select the two fittest as parents: 1011 and 1100 Single-point crossover after gene 2: 10|11 and 11|00 Child A = 10 + 00 = 1000 Child B = 11 + 11 = 1111 <- target reached Mutation may flip a random bit, adding variety
Child B is 1111, a perfect solution, found by breeding
4 marks
Q1. Describe the steps of a genetic algorithm.
Model answer

Initialise a random population of candidate solutions. Evaluate each with a fitness function. Select parents, favouring fitter candidates. Apply crossover to combine parents into offspring. Apply mutation to add small random changes. Replace the old generation with the offspring, and repeat until a solution is good enough or a generation limit is reached.

3 marks
Q2. Explain the difference between crossover and mutation, and why both are needed.
Model answer

Crossover combines genes from two parents, so it recombines existing genetic material. Mutation randomly changes a gene, introducing new material that may not exist anywhere in the population. Both are needed because crossover alone can only shuffle what is already present, while mutation alone lacks the power to combine good building blocks. Together they balance exploiting known good genes and exploring new ones.

4 marks
Q3. What is premature convergence and how can it be reduced?
Model answer

Premature convergence is when the population becomes too similar too early and settles on a suboptimal solution, losing the diversity needed to improve. It can be reduced by increasing the mutation rate, using less greedy selection so weaker candidates still breed, using a larger population, and limiting how strongly elitism dominates, all of which preserve diversity.

Same family, different trick
Genetic algorithms breed a population. Next, swarm intelligence keeps a population too, but coordinates it by having simple agents share information, like ants and flocks of birds.
Swarm intelligence

Stop wrestling with confusion.

Join thousands of students mastering Computer Science without the academic jargon.

From syntax to systems. We break down the hardest ideas in computer science so you can actually build things.

© 2026 Painless Programming. Built for students.
Scroll to Top