liveBook · Manning

Mutation

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

Mutation is a change to an existing value, object, data structure, state, or representation.

In a genetic algorithm, mutation makes a small, fairly random change to a chromosome. Alongside crossover, it creates variation among candidate solutions. In biology, environmental pressures can favor individuals whose mutations improve survival; the pressure does not cause the mutation itself. section 5.1 section 5.3

Mutation in a generic genetic algorithm:

  • Gives each individual a chance of mutation in each generation; the chance is configurable. section 5.3
  • Leaves the specific change to the chromosome’s mutation implementation. section 5.3

Changing a letter chromosome

For SEND+MORE=MONEY, mutation either swaps two randomly chosen gene positions or puts a random letter in a randomly chosen position. section 5.5

public protocol Chromosome {
    var fitness: Double { get } // how well does this individual solve
     the problem?
    init(from: Self) // must be able to copy itself
    static func randomInstance() -> Self
    func crossover(other: Self)
     -> (child1: Self, child2: Self) // combine with other to form children
    func mutate() // make a small change somewhere
    func prettyPrint()
}

The Chromosome protocol makes mutation an operation each chromosome type must implement. section 5.3

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

How deep it goes
Defines mutation and applies it probabilistically per individual, while leaving the specific mutation strategy to each chromosome.
Examples
Shows bacterial antibiotic resistance and mutations that swap genes or insert a random letter in SEND+MORE=MONEY.
What it assumes
Treats chromosomes as individuals composed of genes and introduces genetic algorithms as simulations of natural selection.
What you can do after
Apply mutation through a chromosome’s mutate() operation, with a configurable chance for each individual per generation.
How it connects
Connects chromosome mutation to genetic algorithms, natural selection, fitness, and crossover.

Also explained in

Compare how 12 books explain mutation →

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.