pyneat.reproduction module

Implement the NEAT reproduction scheme.

The reproduction scheme is specifies the behaviour for creating, mutating and in any way altering the population of genomes during evolution.

class pyneat.reproduction.Reproduction(config, reporters, stagnation)[source]

Bases: object

Implements the NEAT reproduction scheme.

TODO: Decide which attributes should be private.

reproduction_config

The configuration for reproduction hyperparameters.

Type:ReproductionConfig
reporters

The set of reporters to log events via.

Type:ReporterSet
genome_key_generator

Keeps track of the next genome key when generating offspring.

Type:generator
stagnation

Keeps track of which species have stagnated.

Type:Stagnation
ancestors

A dictionary that stores the parents of each offspring produced.

Type:dict
static compute_num_offspring(remaining_species, pop_size)[source]

Compute the number of offspring per species (proportional to fitness).

Note: The largest remainder method is used to ensure the population size is maintained (https://en.wikipedia.org/wiki/Largest_remainder_method).

TODO: Investigate a more efficient implementation of offspring allocation

Parameters:
  • remaining_species (dict) – A dictionary ({species key: species}) of the remaining species after filtering for stagnation.
  • pop_size (int) – The specified size of the population.
Returns:

A dictionary of the number of offspring allowed for each

species of the form {species key: number of offspring}.

Return type:

dict

create_new(genome_type, genome_config, num_genomes, innovation_store)[source]

Create a brand new population.

Note: This is a required interface method.

Parameters:
  • genome_type (Genome) – The type of the genome to create individuals using.
  • genome_config (GenomeConfig) – The genome configuration.
  • num_genomes (int) – The number of genomes to create (population size).
  • innovation_store (InnovationStore) – The population-wide innovation store used for tracking new structural mutations.
Returns:

A dictionary of genome key, genome pairs that make up the new

population.

Return type:

dict

generate_parent_pools(remaining_species)[source]

Culls the lowest performing members of each remaining species

Parameters:remaining_species (dict) – Species key/species pairs for the remaining species after stagnated species have been removed.
Returns:
The parent genomes for each species. A dictionary of the form
species key, genomes.
Return type:dict
classmethod parse_config(param_dict)[source]

Takes a dictionary of configuration items, returns an object that will later be passed to the write_config method.

Note: This is a required interface method.

Parameters:param_dict (dict) – A dictionary of configuration parameter values.
Returns:The reproduction configuration.
Return type:ReproductionConfig
reproduce(config, species, pop_size, generation, innovation_store, refocus)[source]

Produces the next generation of genomes.

Note: This is a required interface method.

The steps are broadly as follows:
  1. Filter stagnant species.
  2. Compute the number of offspring for each remaining species.
  3. Generate the parent pool for each remaining species (eliminate
    the lowest performing members).
  4. Generate the new population.
Parameters:
  • config (Config) – The experiment configuration.
  • species (SpeciesSet) – The current allocation of genomes to species.
  • pop_size (int) – The desired size of the population.
  • generation (int) – The number of the next generation.
  • innovation_store (InnovationStore) – The population-wide innovation store used for tracking new structural mutations.
Returns:

A dictionary of genome key, genome pairs that make up the new

population.

Return type:

dict

classmethod write_config(filename, config)[source]

Takes a file-like object and the configuration object created by parse_config. This method should write the configuration item definitions to the given file.

Note: This is a required interface method.

Parameters:
  • filename (str) – The filename of the file to write the configuration to.
  • config (ReproductionConfig) – The reproduction config to save.
class pyneat.reproduction.ReproductionConfig(params)[source]

Bases: object

Sets up and hold configuration information for the Reproduction class.

Config Parameters:
mutate_only_prob (float): The probability that a child is generated
through mutation alone. Crossover is only an option if there is more than one remaining parent in the parent pool for the species in question.
crossover_avg_prob (float): The probability that the weights of mutual
connections are averaged from both parents instead of chosen at random from one or the other.
crossover_only_prob (float): The probability that a child
generated via crossover is not also mutated.
inter_species_crossover_prob (float): The probability (given crossover)
that the child is instead generating using parents from different species. Relies on their being more than one species.
num_elites (int): The number of elites from each species to be copied to
the next generation. The size of a species must surpass the elitism_threshold for elitism to occur.
elitism_threshold (int): Elitism will only be applied for a species if
the number of remaining parents exceeds this threshold.
survival_threshold (float): The proportion of members of each species
that are added to the parent pool and are allowed to reproduce. The fittest members are kept.
save(filename)[source]

Save the reproduction configuration.

Parameters:filename (str) – The filename to write the configuration to.