Class NeatSelectorImpl<T extends Number & Comparable<T>>

java.lang.Object
net.bmahe.genetics4j.neat.selection.NeatSelectorImpl<T>
Type Parameters:
T - the fitness value type (typically Double)
All Implemented Interfaces:
Selector<T>

public class NeatSelectorImpl<T extends Number & Comparable<T>> extends Object implements Selector<T>
Concrete implementation of species-based selection for NEAT (NeuroEvolution of Augmenting Topologies) algorithm.

NeatSelectorImpl implements the core species-based selection mechanism that is fundamental to NEAT's ability to maintain population diversity and protect structural innovations. It organizes the population into species based on genetic compatibility, applies fitness sharing within species, manages species lifecycle, and allocates reproduction opportunities across species.

NEAT selection process:

  1. Species assignment: Organize population into species using compatibility predicate
  2. Species trimming: Remove lower-performing individuals within each species
  3. Species filtering: Eliminate species below minimum viable size
  4. Reproduction allocation: Determine number of offspring for each species
  5. Parent selection: Select parents within species using configured selector
  6. Species maintenance: Update species representatives for next generation

Key features:

  • Genetic compatibility: Groups individuals with similar network topologies
  • Fitness sharing: Reduces competition between similar individuals
  • Innovation protection: Allows new topologies time to optimize
  • Population diversity: Maintains multiple species exploring different solutions

Species management:

  • Species formation: Creates new species when compatibility threshold exceeded
  • Species growth: Assigns compatible individuals to existing species
  • Species extinction: Eliminates species that fall below minimum size
  • Species continuity: Maintains species representatives across generations

Common usage patterns:


 // Create NEAT selector implementation
 RandomGenerator randomGen = RandomGenerator.getDefault();
 SpeciesIdGenerator speciesIdGen = new SpeciesIdGenerator();
 
 // Configure species predicate for compatibility
 BiPredicate<Individual<Double>, Individual<Double>> speciesPredicate = 
     (i1, i2) -> NeatUtils.compatibilityDistance(
         i1.genotype(), i2.genotype(), 0, 2, 2, 1.0f
     ) < 3.0;  // Compatibility threshold
 
 // Configure NEAT selection policy
 NeatSelection<Double> neatSelection = NeatSelection.<Double>builder()
     .perSpeciesKeepRatio(0.8f)  // Keep top 80% of each species
     .minSpeciesSize(5)  // Minimum 5 individuals per species
     .speciesPredicate(speciesPredicate)
     .speciesSelection(Tournament.of(3))  // Tournament within species
     .build();
 
 // Create within-species selector
 Selector<Double> speciesSelector = new TournamentSelector<>(randomGen, 3);
 
 // Create NEAT selector
 NeatSelectorImpl<Double> selector = new NeatSelectorImpl<>(
     randomGen, neatSelection, speciesIdGen, speciesSelector
 );
 
 // Use in population selection
 Population<Double> selectedPopulation = selector.select(
     eaConfiguration, population, 100  // Select 100 individuals
 );
 

Species lifecycle management:

  • Initialization: Empty species list for first generation
  • Assignment: Individuals assigned to species based on compatibility
  • Trimming: Poor performers removed while preserving species diversity
  • Reproduction: Offspring allocated proportionally to species average fitness
  • Evolution: Species composition changes as population evolves

Fitness sharing mechanism:

  • Within-species competition: Individuals primarily compete within their species
  • Species-based allocation: Reproduction opportunities distributed across species
  • Diversity protection: Prevents single topology from dominating population
  • Innovation preservation: New structural innovations get time to optimize

Performance considerations:

  • Compatibility caching: Species assignment optimized for repeated use
  • Efficient trimming: In-place species membership updates
  • Memory management: Species structures reused across generations
  • Parallel processing: Species-based organization enables concurrent operations
See Also:
  • Field Details

  • Constructor Details

    • NeatSelectorImpl

      public NeatSelectorImpl(RandomGenerator _randomGenerator, NeatSelection<T> _neatSelection, SpeciesIdGenerator _speciesIdGenerator, Selector<T> _speciesSelector)
      Constructs a new NEAT selector implementation with the specified components.

      The selector uses the random generator for stochastic decisions, the NEAT selection policy for species management parameters, the species ID generator for creating new species, and the species selector for within-species parent selection.

      Parameters:
      _randomGenerator - random number generator for stochastic selection operations
      _neatSelection - NEAT selection policy defining species management parameters
      _speciesIdGenerator - generator for unique species identifiers
      _speciesSelector - selector for choosing parents within each species
      Throws:
      IllegalArgumentException - if any parameter is null
  • Method Details

    • trimSpecies

      protected Species<T> trimSpecies(Species<T> species, Comparator<Individual<T>> comparator, int minSpeciesSize, float perSpeciesKeepRatio)
      Trims a species by removing lower-performing individuals while preserving minimum viable size.

      This method applies fitness-based selection within a species, keeping the best performers while ensuring the species maintains sufficient size for genetic diversity. The number of individuals kept is the maximum of the minimum species size and the keep ratio applied to the current species size.

      Parameters:
      species - the species to trim
      comparator - comparator for ranking individuals by fitness
      minSpeciesSize - minimum number of individuals to keep
      perSpeciesKeepRatio - proportion of current species to preserve
      Returns:
      a new species containing only the selected individuals
      Throws:
      IllegalArgumentException - if species is null
    • eliminateLowestPerformers

      protected List<Species<T>> eliminateLowestPerformers(AbstractEAConfiguration<T> eaConfiguration, List<Species<T>> allSpecies)
      Eliminates the lowest-performing individuals from all species while maintaining species diversity.

      This method applies the species trimming process to all species in the population, removing poor performers within each species while preserving the overall species structure. Species that become empty after trimming are filtered out.

      Parameters:
      eaConfiguration - evolutionary algorithm configuration containing fitness comparator
      allSpecies - list of all species in the population
      Returns:
      list of species after trimming, with empty species removed
      Throws:
      IllegalArgumentException - if any parameter is null
    • select

      public Population<T> select(AbstractEAConfiguration<T> eaConfiguration, int numIndividuals, List<Genotype> genotypes, List<T> fitnessScore)
      Description copied from interface: Selector
      Selects a specified number of individuals from the given population based on their fitness values.

      This method implements the core selection logic, choosing individuals according to the specific selection strategy. The selection process typically involves evaluating fitness values and applying probabilistic or deterministic rules to choose parents for reproduction.

      The selection process should:

      • Consider fitness values when making selection decisions
      • Return exactly the requested number of individuals
      • Allow for the possibility of selecting the same individual multiple times
      • Maintain the integrity of the original population
      Specified by:
      select in interface Selector<T extends Number & Comparable<T>>
      Parameters:
      eaConfiguration - the evolutionary algorithm configuration containing selection parameters
      numIndividuals - the number of individuals to select from the population
      genotypes - the list of genotypes in the current population
      fitnessScore - the list of fitness values corresponding to each genotype
      Returns:
      a new population containing the selected individuals