← 🤖 Algorithms

🧬 Genetic Algo

Mode
Target string
Generation 0
Best fitness
Avg fitness
Diversity
Best string
String mode: watch characters evolve letter-by-letter · Landscape: population climbs toward the global peak

About the Genetic Algorithm Visualiser

This simulation shows a genetic algorithm, an optimisation method inspired by Darwinian evolution. A population of candidate solutions is scored by a fitness function, and the fittest are preferentially bred to produce the next generation. It runs two modes: a classic Weasel string-matching task, and continuous optimisation of the 2D Rastrigin function. Each generation applies tournament selection, single-point crossover and random mutation, with elitism preserving the two best individuals unchanged.

The controls let you pick the mode and target string, set the population size (20 to 200 individuals), adjust the mutation rate (1 to 30 per cent) and change the simulation speed. The live panel reports generation count, best and average fitness, diversity and the current best string. Genetic algorithms power real engineering tasks such as antenna design, scheduling and aerodynamic shape optimisation, where the search space is too large for exhaustive methods.

Frequently Asked Questions

What is a genetic algorithm?

A genetic algorithm is a search and optimisation technique that imitates natural selection. It maintains a population of candidate solutions, scores each one with a fitness function, and repeatedly breeds the best performers using crossover and mutation. Over many generations the population tends to converge towards high-fitness solutions.

What does the String Evolution (Weasel) mode do?

It evolves a random string of letters towards a target phrase, by default METHINKS IT IS LIKE A WEASEL. Fitness is simply the fraction of character positions that match the target. This recreates Richard Dawkins' famous Weasel program, which illustrates how cumulative selection reaches a target far faster than blind random shuffling.

What is the Rastrigin function used in Landscape mode?

The Rastrigin function is a standard optimisation test case defined over a 2D grid from minus five to five on each axis. It has a single global optimum at the origin surrounded by many deceptive local optima arranged in a regular lattice. Its bumpy surface makes it a tough benchmark for showing how a population avoids getting trapped.

How does tournament selection work here?

To choose a parent, the algorithm picks five individuals at random from the population and keeps the one with the highest fitness. Repeating this process favours fitter individuals while still giving weaker ones an occasional chance, which helps maintain diversity. Selection pressure rises with the tournament size, fixed at five in this simulation.

What do crossover and mutation actually do?

Crossover combines two parents into a child. In string mode it uses a single random cut point, taking the start from one parent and the rest from the other. Mutation then randomly replaces characters with probability equal to the mutation rate. In landscape mode the child is a weighted blend of the parents' coordinates plus a small random jitter scaled by the mutation rate.

What does the mutation rate control?

The mutation rate, adjustable from 1 to 30 per cent, sets how often each gene is randomly altered when offspring are created. Low rates make the population converge quickly but risk stalling in a local optimum. High rates inject more diversity and help escape traps, but too much mutation turns the search into an inefficient random walk.

Why does the simulation keep the best individuals unchanged?

This is called elitism. The two highest-fitness individuals are copied directly into the next generation without crossover or mutation. Elitism guarantees that the best solution found so far is never lost, so the best-fitness curve never decreases. The remaining slots are filled by selection, crossover and mutation.

What does the diversity figure mean?

In string mode, diversity is measured as the average number of distinct characters appearing at each position across the whole population. High diversity early on means the population is still exploring widely; as it converges on the target, diversity falls towards one, indicating most individuals now share the same letters.

Is this a physically accurate model of biological evolution?

It is a faithful model of the core mechanism, selection acting on heritable variation, but it is deliberately simplified. Real evolution has no fixed target, no global fitness function and far richer genetics. The Weasel task in particular is a teaching illustration of cumulative selection, not a claim about how organisms evolve.

Where are genetic algorithms used in the real world?

They are applied wherever the search space is huge and gradients are unavailable or unreliable, including aircraft and antenna design, factory and timetable scheduling, circuit layout, machine-learning hyperparameter tuning and financial strategy optimisation. NASA has famously used evolved designs for spacecraft antennas with unconventional but highly effective shapes.

🧬 Genetic Algorithm — Evolution

Watch evolution in action! A population evolves through selection, crossover and mutation. Two modes: evolve a target string (like Dawkins' Weasel program) or optimise the devilishly hard Rastrigin function.

🔬 What It Demonstrates

Tournament selection picks the fittest parents. Crossover recombines their genes. Random mutations add diversity. Over generations, the population converges on the optimum.

🎮 How to Use

Adjust mutation rate, population size and crossover probability. Watch the fitness chart climb. In Rastrigin mode, see how the population escapes local optima.

💡 Did You Know?

Genetic algorithms mimic billions of years of evolution in seconds. They're used to optimise jet engine fan blades, antenna shapes for NASA spacecraft, and even trading strategies.