Genetic operators

Starndard Population Initializers

These are two population initializers that work across all representations:



All individuals are created with full trees (maximum depth in all branches).

How to inject pre-existing programs into the initial population.

The InjectInitialPopulationWrapper class allows you to pass a list of programs to include in the initial population. The remainder of the initial population will be selected via a backup initializer.

Selection operators

Tournament Selection

class, with_replacement=False)

TournamentSelection represents a tournament selection algorithm, where tournament_size individuals are selected at random, and only the best passes to the next generation.

  • tournament_size (int) – number of individuals from the population that will be randomly selected

  • with_replacement (bool) – whether the selected individuals can appear again in another tournament (default: False)

Lexicase Selection

This function generates a selection operator that uses Lexicase Selection [1]


Implements Lexicase Selection (


epsilon (bool) – if True, espilon-lexicase is performed. We use the method given by equation 5 in

Elitism and Novelty



Selects the best individuals from the population.



Creates new individuals for the population.

Mutation and Crossover



Applies a mutation to individuals with a given probability.


probability (float)

Note that the operator parameter allows different representations to introduce their own custom mutators.



Changes the population by crossing individuals two-by-two together, according to a given probability.


probability (float)

Note that the operator parameter allows different representations to introduce their own custom crossover operators.


Combinators allow you to design your own evolutionary algorithms. When they are created, they are agnostic to the population size, so they can be used inside other combinator steps (like SequenceStep or ParallelStep).

In the following example, we are defining a Genetic Programming step that can be used in the Genetic Programming algorithm.

default_generic_programming_step = ParallelStep([
    ], weights=[.05, .05, .90])

In that case, and according to the ParallelStep, a new generation with population size 1000 will have:

  1. the best 50 individuals of the previous generation (5% of elistim);

  2. 50 new individuals (5% of novelty); and

  3. 900 individuals that are create as follows (90% according to the Sequence Step weight):

    • First, 900 individuals will be selected using a tournament each. For each tournament, 5 individuals are chosen at random from the original population, and the best will pass to the next step.

    • The 900 individuals selected via tournament have a 1% probability of being crossover with another individual. Otherwise they will remain to the next step.

    • The 900 individuals that went through crossover or not are not mutated with 90% of probability.


Applies multiple steps in order, passing the output population of one step to the input population of the next step.


steps (

class, weights=None)

Creates a new population, using different steps for different slices of the target population. The input population for each parallel step is always the complete input population. The output/target population is the one that is split across all of the slices. The size of each slice is given by the proportion of the weight of that particular weight, compared to the sum of all weights.

Consider the example:

another_step = ParallelStep([AStep(), BStep()], weights=[2,3])

In this example, the first 2/5 of the next population will be generated using AStep(), and the next 3/5 will be generated using BStep.

class, weights=None)

A version of ParallelStep, where each parallel step receives a portion of the input population equal to the target population of each slice.