technique

Genetic algorithm

Population -> fitness -> weighted selection -> crossover+mutation -> repeat, evolving a population of candidate solutions toward a target.

Also called: GA

What it is

A genetic algorithm evaluates the fitness of each member of a population, builds a mating pool from that evaluation, and gives some individuals — not all — the chance to become parents; their genes cross, and the new population replaces the old before the loop repeats. It’s commonly shortened to GA. Nature of Code sets out to “walk through the steps of the classic GA in a more general way” before writing any implementation.

How it works

These excerpts cover four of the loop’s stages; initialization, mating-pool weighting, and mutation aren’t among them, so they’re left out rather than guessed.

Separately: “There must be a variety of traits present in the population of creatures or a means to introduce variation for evolution to take place.” No mechanism or rate for introducing variation appears here; mutation is the obvious candidate, but the word isn’t in these excerpts.

Parameters & tuning

Where it’s been used

These excerpts don’t name a work that used this loop. The neighbours below diverge from it at different points — see Variants & neighbours.

Variants & neighbours

Interactive selection replaces a GA’s fitness function with fitness assigned by human observers (viewing time, clicks, ratings), keeping the same selection/reproduction loop.

Roulette wheel selection gives each member a slice of a wheel sized by normalized fitness, then samples it so fitter individuals are more likely, not certain — a candidate for the mating-pool-weighting gap above.

Continuous (steady-state) evolutionary ecosystem replaces the generational structure entirely — fitness becomes a creature’s lifespan, birth and death happen per frame, and dying removes it from ever reproducing rather than waiting for a batch replacement.

Go deeper

Related, in brief

Covered here rather than as pages of their own — each is described by a single source, and gets promoted the moment a second one corroborates it.

Roulette wheel selection

Give each population member a slice of a wheel sized by its normalized fitness, then sample so fitter elements are more likely, but not certain, to be chosen as parents.

Connected to

Variant of: Interactive selection

Built from: Roulette wheel selection

Variants: Continuous (steady-state) evolutionary ecosystem · Interactive selection

Steps: Roulette wheel selection

Try it yourself

The book gives two separate preconditions for evolution to do anything at all: variety of traits in the population, and unequal odds of reproducing. Suppose your population has plenty of trait variety, but every individual is equally likely to become a parent. What happens over many generations, and which precondition is missing?

Nothing improves, generation after generation. The population still has 'a variety of traits' — the book's other stated precondition — but the missing piece is 'a mechanism by which some creatures have the opportunity to be parents and pass on their genetic information, while others don't.' Without that mechanism, nothing biases who passes genes on, so variety alone doesn't produce improvement; selection does.


Revision 4 · 1 source · Something wrong? Tell us.