liveBook · Manning

Crossover

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

A genetic algorithm technique that combines portions of parent chromosomes to produce offspring chromosomes.

Crossover combines genetic material from two parent chromosomes to produce offspring. A genetic algorithm applies it probabilistically during each generation, alongside selection and mutation. section 5.1 section 5.3

A typical crossover implementation proceeds as follows:

  • Choose two parent chromosomes of the same type. section 5.3
  • Select a random crossing point in each chromosome’s gene sequence. section 5.5
  • Exchange the sequence pieces to form two children: each receives one prefix and the other parent’s suffix. section 5.5
  • Insert the children into the replacement population when the configured crossover chance allows it. section 5.3

SEND+MORE=MONEY

With a crossing point at position 4, the first child takes the first four genes from one parent and the final six from the other; the second child reverses those segments. The resulting children combine letter arrangements that can later be evaluated and mutated. 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 Swift protocol makes crossover an explicit responsibility of every chromosome, returning two children from two same-type parents. section 5.3

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

How deep it goes
Explains random split-and-swap implementation and interface placement, but defers broader crossover variants and formal analysis.
Examples
Uses a random gene-array split in SEND+MORE=MONEY to demonstrate two swapped-segment offspring.
What it assumes
Assumes chromosomes, genes, populations, fitness, selection, and mutation; defines crossover within that genetic-algorithm vocabulary.
What you can do after
Build it from scratch as a Swift Chromosome method that returns two children from paired parents.
How it connects
Connects crossover with chromosomes, genes, fitness functions, selection, mutation, and genetic algorithms.

Also explained in

Compare how 7 books explain crossover →

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.