liveBook · Manning

Crossover

As explained in Grokking AI Algorithms, Second Edition

A crossover method is a recombination technique that creates offspring by combining portions of parent chromosomes.

Crossover creates offspring by combining genes from two parent chromosomes. Think of each chromosome as a row of candidate-solution parts: an offspring takes some parts from one parent and the rest from the other. In a genetic algorithm, this happens after parent selection. Mutation may then make small random changes to the offspring. section 4.11 section 5.1

Cut and swap once

Suppose parent A is [1, 1, 0, 0] and parent B is [0, 0, 1, 1]. Cut both after the second gene. Joining A’s first part to B’s second gives [1, 1, 1, 1]; joining B’s first part to A’s second gives [0, 0, 0, 0]. This is single-point crossover: the same cut produces two opposite mixes. section 4.12

def one_point_crossover(parent_a, parent_b, xover_point):
    children = [parent_a[:xover_point] #A
            ➥+ parent_b[xover_point:], #B
            parent_b[:xover_point] #C
            ➥+ parent_a[xover_point:]] #D
    return children

The Python example implements that cut with slices: each child joins a prefix from one parent to a suffix from the other. section 4.12

Different crossover methods choose inherited parts differently:

  • Two-point crossover makes two cuts and takes the middle part from the other parent, like splicing arrays. section 4.13
  • Uniform crossover uses a mask to choose a parent for each gene; an inverse mask can produce a second child. section 4.14
  • Arithmetic crossover combines parents through an arithmetic operation. It is primarily suited to continuous-valued genes, such as weights or coordinates. section 5.8

Choose the method to suit the chromosome’s encoding and check that offspring remain valid solutions. The choice can encourage or discourage diversity. Arithmetic crossover needs particular care: averaging parents can move an offspring away from a precise, high-performing solution. section 4.20 section 5.8

How Grokking AI Algorithms, Second Edition covers it

How deep it goes
Works through single-point slicing and describes two-point, uniform, and arithmetic alternatives without formal analysis.
Examples
Uses Python array slicing for single-point crossover and a gene-selection mask for uniform crossover.
What it assumes
Defines crossover through parents, offspring, genes, and chromosomes; uses basic Python slicing for the implementation.
What you can do after
Build it from scratch with array slices, then choose an encoding-compatible method that yields valid offspring.
How it connects
Connects crossover to chromosome encoding, parent selection, mutation, and population diversity.

Also explained in

Compare how 7 books explain crossover →

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.