Skip to main content

GA: A Full Worked Example

Source: Unit 5 §6

FactsThe problem
  • Minimise f(x)=xsin(10πx)+1.0f(x) = x \sin(10\pi x) + 1.0.
  • Subject to 1x2-1 \le x \le 2.
  • Crossover rate 0.5, mutation rate 0.1, population 10 chromosomes.
  • One generation, worked end to end.

Step 1: initialisation and evaluation

Ten values of xx are drawn, and f(x)f(x) is computed for each.

x = −0.4691, 0.1572, −0.5914, 0.6454, 0.7796,
1.0580, 0.0991, 0.2772, 0.5605, 1.6327
 
min f(x) = 0.4697 at x = 0.5605 ← the best of this generation

Step 2: selection, with normalisation

Sort f(x)f(x) ascending, then normalise the costs between 0.1 and 0.9:

f(x)=(maxrangeminrange)(f(x)minf(x))maxf(x)minf(x)+minrangef'(x) = \frac{(\text{max}_{range} - \text{min}_{range})\,(f(x) - \min f(x))}{\max f(x) - \min f(x)} + \text{min}_{range}

Here minrange=0.1\text{min}_{range} = 0.1, maxrange=0.9\text{max}_{range} = 0.9, minf=0.4697\min f = 0.4697 and maxf=2.3962\max f = 2.3962.

StepsTurning costs into selection probabilities
  1. Normalise every cost into [0.1, 0.9] with the formula above.
  2. Divide each normalised cost by the sum of all of them to get a probability.
  3. Flip the probabilities, so the lowest-cost chromosome ends up with the highest selection probability.
  4. Spin the roulette wheel 10 times to fill the mating pool. A random draw of 0.6777 falls in the interval [0.6508, 0.7458], which selects the 5th chromosome, x=0.0991x = 0.0991.
GotchaThe slides' "5th chromosome" is 5th after sorting

x=0.0991x = 0.0991 is the 7th entry in the initial list above, but the 5th once step 1 sorts the population by cost: the ascending order runs 0.5605 (f=0.4697f = 0.4697), 0.7796, −0.5914, 0.1572, then 0.0991 at f=1.0028f = 1.0028. The ordinal refers to the sorted population. Say which ordering you are counting in and the discrepancy disappears.

GotchaThis is a minimisation problem, so raw cost is not fitness

Feeding cost straight into a roulette wheel would give the worst chromosomes the biggest arcs. Step 3 is not decoration; it is what converts a cost into a fitness. Any minimisation GA needs an equivalent flip.

Step 3: directional crossover

A crossover rate of 0.5 over 10 chromosomes means 5 crossovers; a mutation rate of 0.1 means 1 mutation.

C1=P1+R(P2P1)C2=P2+R(P1P2)C_1 = P_1 + R(P_2 - P_1) \qquad C_2 = P_2 + R(P_1 - P_2)
StepsOne crossover, computed
  1. Take P1=0.0991P_1 = 0.0991, P2=0.5605P_2 = 0.5605, and a random R=0.3451R = 0.3451.
  2. C1=0.0991+0.3451(0.56050.0991)=0.0991+0.1592=0.2583C_1 = 0.0991 + 0.3451(0.5605 - 0.0991) = 0.0991 + 0.1592 = \mathbf{0.2583}.
  3. C2=0.5605+0.3451(0.09910.5605)=0.56050.1592=0.4013C_2 = 0.5605 + 0.3451(0.0991 - 0.5605) = 0.5605 - 0.1592 = \mathbf{0.4013}.
-1012P10.0991C10.2583C20.4013P20.5605R = 0.3451the search domain is [−1, 2]; offspring outside it are rejected
Directional crossover on real-valued genes: both children land on the line between the parents, at mirrored fractions R of the gap.
GotchaCheck the boundary before replacing a parent

Every new offspring must lie within [1,2][-1, 2]. If it does not, the parent is not replaced. Skipping this check is how a real-coded GA quietly starts evaluating points outside its own domain.

Step 4: mutation

Randomly select an offspring - say the third - and replace it with a new random number drawn from [1,2][-1, 2].

Step 5: termination

Stop after a set number of generations, or on another criterion. Otherwise loop back to the evaluation in step 1 and run the next generation.