The Genetic Algorithm Loop
Four decisions become six concrete parameters. Encoding, selection, crossover, mutation, elitism, replacement — and the one dial that all of them move.
Five Steps, Repeated
- Initialise a random population
- Evaluate every candidate — this is the whole cost
- Select parents, cross them, mutate the children
- Replace to form the next population
- Report the best candidate ever seen
Four Ways to Write a Candidate
- Binary string — switches, or a decoded number
- Real vector — one float per parameter
- Permutation — an order, every element once
- Tree — an expression or a program
The test: does its natural crossover produce valid candidates?
Bits Into a Bounded Number
The sum reads L bits as an integer; the fraction rescales it onto your interval. The catch is resolution — L bits can express that many values and no more.
Three Ways to Pick Parents
- Tournament — k at random, best wins; k is the dial
- Roulette — a slice per fitness; scale-sensitive
- Rank — sort, then ignore the raw values
Tournament needs only comparisons: no sums, no sorting.
Pressure as a Single Number
Rank 1 is the best candidate. At s = 1 everyone gets 1/N and there is no pressure at all; at s = 2 the best gets twice the average share and the worst gets none.
Cut, Splice, or Coin Flip
One-point and two-point crossover keep neighbouring genes together, so where you wrote a parameter on the string starts to matter. Uniform crossover has no positional bias — and protects no block either.
When Cutting Breaks It
Splice two routes and you get a tour that visits site 3 twice and never visits site 7. Order crossover copies a segment from A, then fills the gaps in B’s order.
The Only Source of New Material
Crossover reshuffles what the population already holds. Bit flip, Gaussian step, or swap — a rate of one over the string length makes the expected change one bit per child.
Losing the Best Is a Bug
- Selection is random: the best may not be picked
- Variation is destructive: if picked, it is changed
- So population best fitness can go down
- Fix: copy the top e through unchanged
- And keep a best-ever copy outside the population
All at Once, or One at a Time
Generational replacement swaps the whole population — simple, trivially parallel, and it needs elitism most. Steady-state inserts one child at a time, so a good gene is reused immediately.
Six Settings, One Trade-Off
Explore with a bigger N, a smaller k, more mutation, fewer elites. Exploit with the reverse. Plot diversity beside fitness, and remember N times generations is the bill.