Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 36 ยท Applications and Tuning

Tuning GA Parameters

Learn to tune genetic algorithm parameters with evidence: population size, mutation and crossover rates, tournament size and elitism, using repeated runs and diversity metrics.

Every genetic algorithm comes with knobs: population size, crossover rate, mutation rate, selection pressure, elitism, number of generations. Tutorials give default values (population 100, crossover 0.8, mutation 1/length), and the defaults are a good start. But a GA that works on one problem may stall or wander on another, and then you need to tune it systematically, not by changing numbers until it looks right.

This lesson gives you a method and shows the effect of each knob on a test problem where you can see the effects clearly.

The two forces: exploration and exploitation

Almost every parameter shifts the balance between two forces.

  • Exploration searches new regions: random immigrants, high mutation, weak selection, large populations.
  • Exploitation refines what is already good: strong selection, elitism, low mutation, crossover between good parents.

Too much exploration is a random search that never settles. Too much exploitation is premature convergence: the population fills with copies of one decent solution and nothing new can happen. Tuning means finding the balance for your problem.

flowchart LR
    E["More exploration"] --- M["Higher mutation, larger population, weaker selection"]
    X["More exploitation"] --- S["Lower mutation, stronger selection, more elitism"]
    M --> B{"Balance"}
    S --> B
    B --> G["Good final fitness without early stagnation"]

Each parameter pushes the search one way or the other.

A test bed: a deceptive trap function

OneMax (count the ones) is too easy: almost any settings work. We use a trap: the bit string is split into blocks of 4. A block scores 4 if all ones, otherwise 3 - (number of ones). Climbing toward all ones inside a block lowers the score until the very last bit, so the problem punishes lazy exploitation. The optimum for 10 blocks (40 bits) is 40.

Python A reusable GA and a trap-function test bed

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Rule 1: repeat every experiment

One run of a GA is one sample from a random process. To compare two settings, run each at least 10 to 30 times with different seeds and compare the mean and the spread (min, max or standard deviation). The code above does that with 12 seeds. If two settings differ by less than their noise, they are equal for practical purposes.

Rule 2: change one thing at a time, and fix the budget

A bigger population is slower per generation. To compare fairly, fix the number of fitness evaluations (pop_size * gens), not the number of generations. A population of 200 for 30 generations spends as much as a population of 100 for 60.

Python One-at-a-time sweeps with an equal evaluation budget

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Read the three tables the way a practitioner would:

  • Population size: with this budget the largest population wins, because a trap problem needs many building blocks in the first generation. Too small and the population lacks them; on other problems, where more generations matter, a mid-range size often wins. Measure, do not assume.
  • Mutation rate: here 1 / length (0.025) is best. Four times that already hurts, and twelve times is close to random search.
  • Tournament size: bigger tournaments mean stronger selection pressure and faster, but riskier, convergence.

What the main parameters do

Parameter Typical range Too low Too high
Population size 50 to 500 (more for big problems) Premature convergence Slow progress per evaluation
Crossover rate 0.6 to 0.95 Little recombination Disrupts good solutions (rarely harmful)
Mutation rate (per gene) 0.5/L to 3/L Stagnation Random walk
Tournament size 2 to 7 Slow, weak pressure Fast loss of diversity
Elitism 1 to 5 percent Best solution can be lost Diversity drops

Monitor diversity, not just fitness

A single best-fitness curve hides trouble. Track a diversity measure: the mean pairwise Hamming distance for bit strings, or the standard deviation of each gene for real vectors. If diversity falls to near zero while fitness is still poor, you have premature convergence. Remedies: lower the selection pressure, raise mutation, increase the population, or add random immigrants (replace a few individuals with random ones every generation).

Adaptive and self-adaptive parameters

Instead of fixing a rate, you can let it change:

  • Schedules: start with a high mutation rate and decrease it over time.
  • Feedback rules: raise mutation when diversity falls below a threshold, lower it when the population is spread out.
  • Self-adaptation: put the mutation rate inside the chromosome and let evolution tune it. This is standard in evolution strategies (see the differential evolution lesson for another self-scaling idea).

Automated tuning

For serious projects you can tune a GA with another optimizer: random search, Bayesian optimization (for example Optuna) or even a second GA. The cost function is the average result over several seeds. Always keep a separate set of test problems so you do not tune to a single instance.

Common mistakes

  • Judging by a single run.
  • Comparing at equal generations instead of equal evaluations.
  • Tuning on one instance. Test on several problem instances.
  • Fiddling with rates before checking the fitness function. A broken fitness defeats every setting.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why should you repeat each GA configuration many times with different seeds?

    Differences smaller than the run-to-run noise are meaningless.
  2. 2. When comparing two population sizes, what should you hold constant?

    A larger population costs more per generation, so equal generations is unfair.
  3. 3. Fitness is poor and the population diversity has dropped to nearly zero. What is this called, and what helps?

    The population has collapsed onto one region and needs new genetic material.
  4. 4. A common starting value for the per-gene mutation rate on a bit string of length L is:

    That gives about one flipped bit per child.

Summary

  • Parameters trade exploration against exploitation; tuning finds the balance.
  • Repeat every experiment (10 to 30 seeds) and compare means and spread.
  • Compare settings at equal numbers of fitness evaluations, changing one parameter at a time.
  • Monitor population diversity to spot premature convergence early.
  • Adaptive schedules, self-adaptation and automated tuners can replace manual search.

Next: Fitness Caching and Parallel Evaluation