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

PropertyTypeDescription
genomesArrayCollection of all genomes in the current generation
speciesArrayCollection of species containing genomes grouped by similarity
populationIdString/NumberUnique identifier for the population
generationNumberCurrent generation number
configObjectConfiguration parameters for the population

Constructor

javascript
new Population(config)

Creates a new population with genomes initialized according to the provided configuration.

ParameterTypeDescription
configObjectConfiguration object containing parameters for the population and NEAT algorithm

Methods

evolve

javascript
population.evolve()void

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:

  1. Assigns genomes to species based on genetic similarity
  2. Tracks innovation numbers for new genes
  3. Preserves elite genomes from each species
  4. Handles stagnation by removing underperforming species
  5. Removes worst-performing genomes from each species
  6. Calculates how many offspring each species should produce
  7. Generates offspring through crossover and mutation
  8. Adds elite genomes back into the new generation
  9. Replaces the current generation with the new one
  10. Increments the generation counter
javascript
// Create and evolve a population for 100 generations
const 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

javascript
population.evaluatePopulation()void

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

javascript
population.speciate()void

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

javascript
population.getBestGenome()Genome

Returns the genome with the highest fitness value in the current population.

ReturnsTypeDescription
bestGenomeGenomeThe genome with the highest fitness in the population
javascript
// Get the best genome from the population
const bestGenome = population.getBestGenome();
console.log(`Best fitness: ${bestGenome.fitness}`);
// Propagate the genome
const output = bestGenome.propagate([1, 0]);