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
new Population(config)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
population.evolve() → voidAdvances 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
// Create and evolve a population for 100 generationsconst population = new Population(config);
for (let i = 0; i < 100; i++) { // Evaluate all genomes in the population population.evaluatePopulation();
// Display information about the current generation const bestGenome = population.getBestGenome(); console.log(`Generation ${i}: Best fitness = ${bestGenome.fitness}`);
// Evolve to the next generation population.evolve();}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
population.evaluatePopulation() → voidEvaluates 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
population.speciate() → voidDivides 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
population.getBestGenome() → GenomeReturns 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 |
// Get the best genome from the populationconst bestGenome = population.getBestGenome();console.log(`Best fitness: ${bestGenome.fitness}`);
// Propagate the genomeconst output = bestGenome.propagate([1, 0]);