Population
The Population class is responsible for managing a collection of genomes and evolving them through generations using the NEAT algorithm. It handles speciation, fitness evaluation, selection, and reproduction to improve solutions over time.
Properties
Property | Type | Description |
---|---|---|
genomes | Array | Collection of all genomes in the current generation |
species | Array | Collection of species containing genomes grouped by similarity |
populationID | String/Number | Unique identifier for the population |
generation | Number | Current generation number |
config | Object | Configuration parameters for the population |
Constructor
Creates a new population with genomes initialized according to the provided configuration.
Parameter | Type | Description |
---|---|---|
config | Object | Configuration object containing parameters for the population and NEAT algorithm |
Methods
evolve
Advances the population to the next generation by performing the complete evolutionary process. This includes speciation, stagnation handling, selection, reproduction, and elite preservation.
The evolve method performs the following steps:
- Assigns genomes to species based on genetic similarity
- Tracks innovation numbers for new genes
- Preserves elite genomes from each species
- Handles stagnation by removing underperforming species
- Removes worst-performing genomes from each species
- Calculates how many offspring each species should produce
- Generates offspring through crossover and mutation
- Adds elite genomes back into the new generation
- Replaces the current generation with the new one
- Increments the generation counter
Note
The evolve() method does not evaluate the fitness of genomes. You must call evaluatePopulation() or set the fitness manually before evolve() to ensure fitness values are calculated for the current generation.
evaluatePopulation
Evaluates the fitness of all genomes in the population using the fitness function provided in the configuration. This method must be called before evolve() to ensure proper selection and reproduction based on fitness values.
The fitness function should be defined in the configuration object and will be applied to each genome in the population. If a fitness function is not provided, you will need to manually assign fitness values to each genome.
speciate
Divides the population into species based on the genetic similarity between genomes. Genomes are grouped into species when their compatibility distance is below the compatibility threshold defined in the configuration.
getBestGenome
Returns the genome with the highest fitness value in the current population.
Returns | Type | Description |
---|---|---|
bestGenome | Genome | The genome with the highest fitness in the population |