stk.EvolutionaryAlgorithm

class stk.EvolutionaryAlgorithm(initial_population, fitness_calculator, mutator, crosser, generation_selector, mutation_selector, crossover_selector, fitness_normalizer=<stk._internal.ea.fitness_normalizers.null.NullFitnessNormalizer object>, key_maker=Inchi(), num_processes=None)[source]

Bases: Generic[T]

An abstract base class for evolutionary algorithms.

Notes

You might notice that the public methods of this abstract base class are implemented. This is purely for convenience, so that there is a default evolutionary algorithm implementation that users can use. However, feel free to override the default implementation when implementing subclasses.

If you do want to use the default implementation, here is a summary of the roles of the different components:

https://i.imgur.com/hGXboaU.png

Examples

Subclass Implementation

The source code of this class can work as a good example. There is only one method that a subclass of EvolutionaryAlgorithm needs to implement, get_generations(), which yields Generation instances. These correspond to the generations of your evolutionary algorithm implementation.

Usage

There are a couple of tutorials on how to use the EvolutionaryAlgorithm, which can be found in the sidebar.

Parameters:
  • initial_population (list[T]) – The initial population the EA should use.

  • fitness_calculator (FitnessCalculator[T]) – Calculates fitness values.

  • mutator (MoleculeMutator[T]) – Carries out mutation operations.

  • crosser (MoleculeCrosser[T]) – Carries out crossover operations.

  • generation_selector (Selector[T]) – Selects the next generation.

  • mutation_selector (Selector[T]) – Selects molecules for mutation.

  • crossover_selector (Selector[T]) – Selects molecules for crossover.

  • fitness_normalizer (FitnessNormalizer[T]) – Normalizes fitness values.

  • key_maker (MoleculeKeyMaker) – Used to detect duplicate molecules in the EA. If two molecules in a generation return the same key, one of them is removed.

  • num_processes (int | None) – The number of parallel processes the EA should create. If None, all available cores will be used.

Methods

get_generations

Yield the generations of the evolutionary algorithm.

get_generations(num_generations)[source]

Yield the generations of the evolutionary algorithm.

Parameters:

num_generations (int) – The number of generations which should be yielded. Note that the initial population counts as a generation.

Yields:

A generation.

Return type:

Iterator[Generation[T]]