liveBook · Manning

Tournament selection

As explained in Classic Computer Science Problems in Swift: Essential techniques for practicing programmers

A genetic algorithm selection technique that randomly challenges several chromosomes and selects the one with the best fitness.

Tournament selection chooses parents for a genetic algorithm by randomly challenging a fixed number of chromosomes and selecting the participant with the highest fitness. The tournament size controls how many candidates compete. section 5.3

A tournament with four participants

With SelectionType.tournament(4), the algorithm samples four population members, compares their fitness values, and returns the fittest one as a parent. It repeats the process independently to choose a second parent. section 5.3

An implementation proceeds through these steps:

  • Store tournament selection as a configurable selection type, with its associated value representing the number of participants. section 5.3
  • Choose random population indexes and compare their cached fitness values. section 5.3
  • Keep the best participant and pass selected parents to reproduction, crossover, and later mutation. section 5.3
    // find k random individuals in the population and pick the best one
    private func pickTournament(numParticipants: UInt) -> ChromosomeType {
        var best: ChromosomeType = ChromosomeType.randomInstance()
        var bestFitness: Double = best.fitness
        for _ in 0..<numParticipants { // find the best participant
            let test = Int(arc4random_uniform(UInt32(population.count)))
            if fitnessCache[test] > bestFitness {
                bestFitness = fitnessCache[test]
                best = population[test]
            }
        }
        return best
    }

The implementation shows the compact tournament loop: initialize a candidate, sample population members, replace the candidate when a higher fitness appears, and return the winner. section 5.3

Tournament selection is paired with roulette-wheel selection as a configurable alternative. Roulette selection weights every chromosome by its share of total fitness; tournament selection instead makes local competitions among random candidates. section 5.3

A useful extension lets a tournament sometimes choose the second- or third-best chromosome using diminishing probabilities, preserving more variation than always selecting the winner. section 5.8

How Classic Computer Science Problems in Swift: Essential techniques for practicing programmers covers it

How deep it goes
Defines and implements random sampling, fitness comparison, winner return, and parent reuse, then proposes probabilistic variants.
Examples
Uses a four-participant tournament and a Swift pickTournament function that compares cached population fitnesses.
What it assumes
Assumes chromosomes expose fitness and mutation or crossover behavior within a genetic algorithm.
What you can do after
Apply tournament selection by configuring participant count and implementing repeated random fitness comparisons.
How it connects
Connects chromosomes, fitness, genetic algorithms, reproduction, crossover, mutation, and roulette-wheel selection.

Also explained in

Compare how 4 books explain tournament selection →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Classic Computer Science Problems in Swift: Essential techniques for practicing programmers. The section links point to the parts of the book it draws on.