Nexus Learn

Lecture 29: Genetic Algorithms

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

↓ Download slides (PowerPoint)

Recap of Lecture 28 and Homework Check

LAST LECTURE · 5 MIN

Today: back to search (L15-L16). Instead of one climber on the hill - a whole population that breeds better and better answers.

Today's Plan (50 minutes)

ROADMAP

0-5

Recap of Lecture 28 and homework check

S02

5-8

Plan and learning outcomes

S03-S04

8-15

Module 1: Evolution as search, the GA loop

S05-S07

15-27

Module 2: One generation by hand

S08-S11

27-35

Module 3: Encoding real problems (8-queens, rickshaw load)

S12-S14

35-43

Class activity: a GA generation by hand

S15

43-48

Common mistakes and recap quiz

S16-S17

48-50

Homework and next lecture

S18

Learning Outcomes (SLOs)

BY THE END OF THIS LECTURE YOU WILL BE ABLE TO

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

  • Course Learning Outcome(s) served
  • CLO3: Apply various techniques and algorithms of AI used in general problem solving, optimization problems, constraint satisfaction problems, and game programming.

Slide 5

MODULE 1

Evolution as Search

Survival of the fittest - in a computer

From One Climber to a Population

MODULE 1 · THE IDEA

Hill climbing

Annealing

Beam search

Genetic algorithm

One climber; only goes up.

One climber; sometimes goes down.

k climbers; keep the best k.

A population that breeds.

Holland, 'Adaptation in Natural and Artificial Systems' (1975); Goldberg's textbook (1989) made GAs popular.

The Genetic Algorithm Loop

MODULE 1 · ALGORITHM

PSEUDOCODE (L29/GALIB.PY)

Elitism: copy the best answer unchanged into the next generation, so it is never lost.

  • 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

Randomised: two runs can give different answers - like simulated annealing (L16).

Slide 8

MODULE 2

One Generation by Hand

Maximise f(x) = x^2, x = 0 to 31

Step 1: Fitness and the Roulette Wheel

MODULE 2 · FITNESS-PROPORTIONAL SELECTION

REAL OUTPUT: L29/GOLDBERG.PY

BOARD

  • 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
  • share = fitness / total
  • 169 / 1170 = 0.144
  • 576 / 1170 = 0.492

Picture: a lucky draw where better answers hold more tickets - the best usually wins, but not always.

Step 2: Selection - Roulette and Tournament

MODULE 2 · CHOOSING PARENTS

REAL OUTPUT: L29/GOLDBERG.PY (FOUR SPINS OF THE WHEEL)

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

Roulette wheel

Tournament (k = 3)

Chance = fitness / total. Classic; needs positive fitness; weak when fitnesses are close.

Pick 3 at random; the best one is a parent. Simple and robust - used in our 8-queens GA.

Result: the mating pool has two copies of 11000 and none of 01000 - fitter answers get more children.

Too much selection pressure = everyone copies the best too early and diversity is lost (premature convergence).

Step 3: Crossover and Mutation

MODULE 2 · MAKING CHILDREN

REAL OUTPUT: L29/GOLDBERG.PY

  • 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)

ONE-POINT CROSSOVER AT 2

  • parent A 1 1 | 0 0 0
  • parent B 1 0 | 0 1 1
  • child 1 1 1 | 0 1 1 = 27
  • child 2 1 0 | 0 0 0 = 16

Why mutation? it keeps variety: if no string has a 1 in the first bit, crossover alone can never make one.

One generation: average fitness 292.5 -> 438.5, best 576 -> 729.

Slide 12

MODULE 3

Encoding Real Problems

The hard part is the chromosome and the fitness

A GA on 8-Queens

MODULE 3 · COMPARE WITH L15-L16

Fitness: non-attacking pairs; 28 = solved.

REAL OUTPUT: L29/GA_QUEENS.PY

  • 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

Honest lesson: no method wins everywhere. Crossover helps when good PARTS of different answers can be joined.

Loading a Rickshaw for the Mandi

MODULE 3 · KNAPSACK PROBLEM

ITEMS (L29/KNAPSACK.PY)

ENCODING

  • 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
  • chromosome 1 1 1 0 0 1 1 1 1 0
  • wheat ... kinnow
  • fitness = profit if weight <= 500
  • = 0 otherwise (penalty)

REAL OUTPUT: L29/KNAPSACK.PY

  • 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.

Where GAs are used: timetables, crop plans, delivery routes, and NASA's evolved satellite antenna (2006).

Activity: A GA Generation by Hand

CLASS ACTIVITY · PAIRS · 8 MIN

Pairs, 6 min + 2 min check. f(x) = x^2 on 5-bit strings. Population: 10110, 01001, 01111, 00110.

Decode 10110 and 01001 and compute their fitness.

Mutate bit 1 of the second child. New x and fitness?

Fitness of all four, the total, and each roulette share.

8-queens: fitness of 11111111 (all queens in row 1)?

Cross 10110 and 01001 at point 3. Write both children and their x.

Why do we need mutation if crossover already makes new strings?

Common Mistakes

WATCH OUT

WRONG IDEA

CORRECT IDEA

✗ The best string is always selected.

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

✗ Crossover makes better children every time.

✓ Children can be worse - selection removes them later.

✗ Mutation should be large to explore more.

✓ Too much mutation = random search; keep it small.

✗ A GA always finds the best answer.

✓ It is a heuristic: 69/100 on 8-queens, 47/50 on the load.

✗ Overweight loads get fitness = profit.

✓ Rule-breaking answers need a penalty (here fitness 0).

Recap Quiz

CHECK YOUR UNDERSTANDING · 3 MIN

  • Q1. In a GA, a chromosome is:
  • (a) A fitness value (b) One candidate answer as a string (c) The whole population (d) A crossover point
  • Q2. Fitnesses 100, 300, 600. Roulette share of the 300 string:
  • (a) 0.10 (b) 0.60 (c) 0.33 (d) 0.30
  • Q3. Crossing 111|00 and 000|11 at point 3 gives:
  • (a) 11100 and 00011 (b) 11111 and 00000 (c) 11011 and 00100 (d) 10101 and 01010
  • Q4. Keeping the best answer unchanged in the next generation is:
  • (a) Mutation (b) Tournament (c) Elitism (d) Crossover

Homework and Next Lecture

BEFORE THE NEXT CLASS

  • Task 1: Bag limit 10 kg. Items A 4 kg Rs 40, B 3 kg Rs 30, C 2 kg Rs 25, D 5 kg Rs 45, E 1 kg Rs 10. Find weight, profit and fitness (0 if over 10 kg) of 11010, 01101 and 10101.
  • Task 2: Cross 11010 and 10101 at point 2. Find the fitness of both children. Is either better than both parents?
  • Task 3: Fitnesses 1000, 1001, 1002: find the roulette shares. Why is this a problem? Subtract 999 from each and find the shares again.

NEXT LECTURE

  • Lecture 30:
  • Natural Language Processing Basics

← All CS-511 material