ML 101
M13 · L02
Module 13

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.

01 / 13
ML 101
M13 · L02
The Loop

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
02 / 13
ML 101
M13 · L02
Representation

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?

03 / 13
ML 101
M13 · L02
Binary Encoding

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.

Decoding L Bits
x = x_{\min} + \frac{x_{\max} - x_{\min}}{2^{L} - 1} \sum_{i=0}^{L-1} b_i \, 2^{i}
04 / 13
ML 101
M13 · L02
Selection

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.

05 / 13
ML 101
M13 · L02
Baker, 1985

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.

Linear Rank Selection
p_i = \frac{1}{N}\left( s - 2(s-1)\,\frac{i-1}{N-1} \right)
06 / 13
ML 101
M13 · L02
Crossover

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.

Crossover Rate
0.6 to 0.9 is a common starting point; otherwise the parents pass through unchanged
07 / 13
ML 101
M13 · L02
Permutations

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.

A = 1 2 3 4 5 6 7 8  ·  B = 3 7 5 1 6 8 2 4
Keep 4 5 6 from A → child 1 8 2 4 5 6 3 7
08 / 13
ML 101
M13 · L02
Mutation

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.

Expected Flips Per Child
\mathbb{E}[\text{flips}] = L \, p_m
09 / 13
ML 101
M13 · L02
Elitism

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
10 / 13
ML 101
M13 · L02
Replacement

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.

Generational
N children
Steady-state
1 or 2
11 / 13
ML 101
Knowledge Check

Check what stuck

Four questions from this lesson. Answer to see why — the explanation appears whether you were right or wrong. Nothing is scored or saved.

Question 1 of 0
Score 0/0

12 / 13
ML 101
Summary
The Dial

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.

13 / 13