Nexus Learn

Lecture 29: Genetic Algorithms

Lecture Notes CS-511 Artificial Intelligence 7 min read Updated 8 Nov 2026

↓ Download notes (Word)

On this page
  1. Learning Outcomes
  2. 1. Evolution as Search
  3. 2. One Generation by Hand: Maximise f(x) = x^2
  4. 3. Encoding Real Problems
  5. 4. Common Mistakes
  6. Word Bank
  7. Self-Check Questions

Learning Outcomes

SLO

After this lecture you will be able to...

CLO

Bloom

SLO 29.1

Explain how a genetic algorithm searches with a population, using the terms chromosome, fitness, selection, crossover and mutation

CLO3

C2 Understand

SLO 29.2

Carry out one generation of a genetic algorithm by hand: fitness, roulette selection, one-point crossover and mutation

CLO3

C3 Apply

SLO 29.3

Encode a problem (8-queens, a knapsack load) as chromosomes with a fitness function, and compare a GA with hill climbing and simulated annealing

CLO3

C3 Apply

§1.1 From one climber to a population

Slide L29-S06

Hill climbing (L15) keeps one state and only moves up; simulated annealing (L16) keeps one state but sometimes moves down; local beam search keeps k states. A genetic algorithm (GA) keeps a whole population of answers and makes new answers by combining two parents - so good parts of different answers can be joined. The idea copies natural evolution (Darwin, 1859) and was turned into an algorithm by John Holland (1975). Picture: a farmer who keeps seed from the best cotton plants every season.

Word

Meaning in a GA

Example (x^2 problem)

Chromosome

One candidate answer written as a string

01101 (x = 13)

Gene

One position (bit) in the string

the 1st bit

Fitness

How good the answer is (bigger = better)

f(13) = 169

Population

All current answers

4 strings

Generation

One round of selection, crossover and mutation

round 1

§1.2 The GA loop

Slide L29-S07

population = random strings

repeat for G generations:

fitness of each string

new = [best] # elitism

while new is not full:

a, b = select(), select()

a, b = crossover(a, b)

new += [mutate(a), mutate(b)]

population = new

Elitism copies the best answer unchanged into the next generation so it is never lost. A GA is randomised: two runs can give different answers, just like simulated annealing.

2. One Generation by Hand: Maximise f(x) = x^2

§2.1 Fitness and the roulette wheel

Slide L29-S09

x from 0 to 31 is written as a 5-bit binary string (01101 = 8 + 4 + 1 = 13). Fitness = x^2. In roulette-wheel (fitness-proportional) selection each string gets a slice of the wheel equal to its fitness divided by the total fitness (Goldberg, 1989):

No String x f(x)=x^2 Share of wheel Cumulative

1 01101 13 169 0.144 0.144

2 11000 24 576 0.492 0.637

3 01000 8 64 0.055 0.691

4 10011 19 361 0.309 1.000

Total 1170, average 292.5, best 576

§2.2 Selection: roulette and tournament

Slide L29-S10

Spin: take a random number r between 0 and 1 and choose the first string whose cumulative share is at least r. Four spins:

Spins [0.1, 0.3, 0.55, 0.8] -> mating pool ['01101', '11000', '11000', '10011']

Method

How

Notes

Roulette wheel

Chance = fitness / total

Needs positive fitness; weak when fitnesses are close

Tournament (k = 3)

Pick 3 at random; the best one is a parent

Simple, robust; used in our 8-queens GA

Too much selection pressure makes everyone copy the best answer too early, and the variety (diversity) of the population is lost - this is called premature convergence.

§2.3 Crossover and mutation

Slide L29-S11

One-point crossover cuts both parents at the same point and swaps the tails. Mutation flips a gene with a small probability. Real output for the mating pool above:

Cross 0110|1 with 1100|0 at point 4 -> 01100 (12), 11001 (25)

Cross 11|000 with 10|011 at point 2 -> 11011 (27), 10000 (16)

New generation ['01100', '11001', '11011', '10000'] x = [12, 25, 27, 16]

New total 1754, average 438.5, best 729 (was average 292.5, best 576)

In one generation the average fitness rose from 292.5 to 438.5 and the best from 576 to 729 (x = 27). Mutation is needed because crossover only reshuffles bits that already exist: if no string has a 1 in the first bit, crossover alone can never make one.

Setting

Typical value

Population size

20 - 200

Crossover probability

0.6 - 0.9

Mutation

small (about one gene per child)

Elitism

keep the best 1 - 2

3. Encoding Real Problems

§3.1 A GA on 8-queens

Slide L29-S13

Chromosome = 8 digits, the row of the queen in each column (as in L15). Fitness = number of non-attacking pairs; there are 8 x 7 / 2 = 28 pairs, so 28 means solved. Mutation moves one queen to a random row.

GA: population 100, tournament of 3, one-point crossover 0.9, mutation 0.3, keep the best 1

GA solved 69 / 100 runs within 300 generations; average 35 generations (about 3509 boards checked)

Example solution: 63147025 fitness 28, found in generation 14

Method (100 runs)

Solved

Hill climbing (L15)

18

Genetic algorithm

69

Simulated annealing (L16)

96

The GA is much better than plain hill climbing, but on 8-queens simulated annealing is better: crossover often breaks good boards. No method wins everywhere. GAs shine when an answer is made of parts that can be good on their own.

§3.2 Loading a rickshaw for the mandi (knapsack problem)

Slide L29-S14

A loader rickshaw can carry 500 kg. Chromosome = 10 bits (1 = take the item). Fitness = total profit, or 0 if the load is over 500 kg - a penalty for breaking the rule.

Item kg Rs

Wheat bag 100 4200

Cotton bale 170 9500

Rice bag 50 3300

Potato sack 120 3000

Onion sack 80 2800

Mango crate 40 3400

Sugar bag 50 2900

Gur (jaggery) 60 4100

Kinnow crate 30 1500

Fodder bundle 150 2200

Exact (all 1024 loads): 1110011110 500 kg, Rs 28900

GA (population 40, 60 generations), 50 runs:

best load found 47 times, average Rs 28806, worst Rs 27300

100 items: 2^100 loads - far too many to check;

the GA still checks only about 40 x 60 = 2400 loads.

The best load (wheat, cotton, rice, mango, sugar, gur, kinnow - 500 kg, Rs 28,900) was found in 47 of 50 runs. With 100 items there would be 2^100 loads - impossible to check - but a GA still checks only a few thousand. Real uses: university timetables, crop plans, delivery routes, and NASA's evolved antenna for the ST5 satellites (2006).

4. Common Mistakes

Wrong idea

Correct idea

The best string is always selected.

Selection is random; fitter strings only have a higher chance.

Crossover always makes better children.

Children can be worse; selection removes them later.

Large mutation explores more, so it is better.

Too much mutation turns the GA into random search.

A GA always finds the best answer.

It is a heuristic: 69/100 on 8-queens.

Rule-breaking answers can keep their fitness.

Give them a penalty (here fitness 0).

Word Bank

Word

Simple meaning

Example

Genetic algorithm

Search that breeds a population of answers

Holland 1975

Chromosome

One answer written as a string

01101

Fitness

Score of an answer (bigger = better)

x^2 = 169

Selection

Choosing parents, fitter ones more often

roulette, tournament

Crossover

Swapping the tails of two parents

11|000 + 10|011

Mutation

Small random change to a gene

10000 -> 10001

Elitism

Keeping the best answer unchanged

best 1

Penalty

Lower fitness for breaking a rule

overweight load -> 0

Premature convergence

Everyone becomes the same too early

lost diversity

Self-Check Questions

  1. How is a GA different from hill climbing and beam search? (see §1.1)
  2. Draw the GA loop and explain elitism. (see §1.2)
  3. Fitnesses 50, 150, 300: find the roulette shares. (see §2.1)
  4. How does tournament selection work? (see §2.2)
  5. Cross 10101 and 01110 at point 2. (see §2.3)
  6. Why is mutation needed? (see §2.3)
  7. Give the chromosome and fitness for 8-queens. (see §3.1)
  8. Why does an overweight load get fitness 0? (see §3.2)

Reading: Goldberg, Genetic Algorithms in Search, Optimization and Machine Learning, Ch 1-3; Russell & Norvig Ch 4.1.4 (evolutionary algorithms); Lectures 15-16 (local search).

← All CS-511 material