Skip to content
Free public beta until December 4, 2026: every course is open, no card needed. After that, the first chapter of each course stays free and full access is a one-time $39 ($19 for up to 100 founding members chosen for helpful feedback). Your progress is kept. How the beta works

Recipe

Run one genetic algorithm generation

Use when: you have a population of candidate points, each with a known f-value, and are minimizing f

Goal: the next generation's population, with its best-found value no worse than this generation's

Taught in Genetic algorithms

  1. 1

    Compute \text{fitness}_i = \dfrac{1}{1+f(x_i)} for each individual.

    Example

    Population w=1,3,6,9 minimizing f(w)=(w-5)^2: f=16,4,1,16, so fitness =\frac1{17},\frac15,\frac12,\frac1{17} \approx 0.0588,\,0.2,\,0.5,\,0.0588.

    Why: a smaller f must become a LARGER fitness, since selection below favors big fitness values.

  2. 2

    p_i = \text{fitness}_i / \sum_j \text{fitness}_j; lay the p_i out as consecutive slices of [0,1].

    Example

    Sum =0.8176; probabilities \approx 0.072,\,0.245,\,0.611,\,0.072; cumulative slices [0,0.072),\,[0.072,0.317),\,[0.317,0.928),\,[0.928,1).

    Why: the fittest individual (here, w=6, closest to the minimizer) gets the widest slice, so it is drawn as a parent far more often, without ever ruling the others out entirely.

  3. 3

    Draw a random u\in[0,1) for each parent and take whichever individual's slice contains it.

    Example

    u_1=0.10 lands in w=3's slice; u_2=0.75 lands in w=6's slice. Parents: 3 and 6.

    Why: this is exactly what makes selection biased toward fitness instead of uniform -- a wider slice is more likely to contain a random draw.

  4. 4

    Blend \text{child}=a\,p_1+(1-a)p_2 for a random a\in[0,1]; with small probability, add a mutation.

    Example

    a=0.4: child =0.4(3)+0.6(6)=4.8. A mutation draw adds +0.2: final child =5.0.

    Why: crossover recombines two parents that were both already decent; mutation keeps injecting variation selection alone could never introduce.

  5. 5

    Replace the individual with the worst fitness with the new child; leave every other individual, including the current best, unchanged.

    Example

    Both w=1 and w=9 tie for worst (f=16); replacing w=1: new population 5.0,\,3,\,6,\,9, and f(5.0)=0, the true minimum.

    Why: elitism guarantees the best value found so far can only improve or stay the same from one generation to the next, never regress.

Common mistakes

  • Using f itself as fitness when MINIMIZING -- that rewards the WORST individuals with the biggest slices, exactly backwards.
  • Forgetting mutation entirely -- a population without it can converge onto one region and lose the diversity needed to explore elsewhere.
  • Replacing the best individual instead of the worst, which throws elitism's whole guarantee away.