liveBook · Manning

Roulette-wheel selection

As explained in Grokking AI Algorithms, Second Edition

Roulette-wheel selection is a probabilistic evolutionary selection technique that assigns larger selection probabilities to higher-fitness candidates while retaining nonzero probabilities for lower-fitness candidates.

Roulette-wheel selection chooses candidates probabilistically: stronger candidates receive larger slices, while weaker candidates retain a smaller chance of selection. Imagine a casino wheel divided into slices, with each candidate occupying a slice sized according to its fitness. section 4.10 section 4.8

The technique is useful when an evolutionary algorithm should favor high-performing candidates without eliminating diversity entirely. A lower-fitness candidate may still carry a valuable gene, so occasional selection can support better future solutions. section 4.10 section 4.8

One spin of the wheel

Suppose a population has four candidates with fitness scores 1, 2, 3, and 4. Their total fitness is 10, so their selection probabilities are 0.1, 0.2, 0.3, and 0.4. The wheel divides the interval from 0 to 1 into corresponding ranges. A random spin landing in the largest range selects the candidate with fitness 4; a landing in the smallest range can still select the candidate with fitness 1. Repeat the spin until enough parents have been chosen. The same candidate may be selected more than once. section 4.10 section 4.8

A straightforward implementation follows these steps:

  • Add the fitness scores to obtain the population total. section 4.10
  • Divide each candidate’s fitness by that total to obtain its slice size. section 4.10
  • Store each candidate’s cumulative start and end boundaries on the wheel. section 4.10
  • Generate a random number between 0 and 1 and return the candidate whose slice contains it. section 4.10
def set_probabilities(population):
    population_sum =
    ➥sum(individual[INDIVIDUAL_FITNESS_INDEX] #A
    ➥for individual in population)
    for individual in population:
        individual[INDIVIDUAL_PROBABILITY_INDEX] =
        ➥individual[INDIVIDUAL_FITNESS_INDEX]
        ➥/ population_sum #B
 
 
def roulette_wheel_selection(population,
➥number_of_selections):
    set_probabilities(population)
    slices = []
    total = 0
    for r in range(0, len(population)):
        individual = population[r]
        slices.append([r, total, total
        ➥+ individual[INDIVIDUAL_PROBABILITY_INDEX]]) #C
        total += individual[INDIVIDUAL_PROBABILITY_INDEX]
    chosen_ones = []
    for r in range(number_of_selections):
        spin = random.random()
        result = [s[0] for s in slices if s[1]
        ➥< spin <= s[2]] #D
        chosen_ones.append(population[result[0]]) #E
    return chosen_ones

Python pseudocode shows probability assignment, cumulative slice construction, repeated spins, and selected-candidate lookup. section 4.10

The same mechanism can select the next attraction for an ant: available attractions receive probabilities, cumulative slices are built, and one random spin chooses the next visit. section 6.7

How Grokking AI Algorithms, Second Edition covers it

How deep it goes
Explains the wheel analogy, fitness normalization, cumulative intervals, random spins, repeated selection, and a Python implementation.
Examples
Uses casino-wheel slices, fitness-based parent selection, Python listings, and probabilistic attraction visits by ants.
What it assumes
Assumes familiarity with candidates and fitness, while introducing probabilistic selection through an intuitive roulette analogy.
What you can do after
Build it from scratch by converting fitness values into cumulative probability slices and sampling candidates with random numbers.
How it connects
Connects fitness, genetic algorithms, population diversity, convergence, parent selection, reproduction, and ant path selection.

Also explained in

Compare how 4 books explain roulette-wheel selection →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking AI Algorithms, Second Edition. The section links point to the parts of the book it draws on.