Class Species<T extends Comparable<T>>

java.lang.Object
net.bmahe.genetics4j.neat.Species<T>
Type Parameters:
T - the fitness value type (typically Double)

public class Species<T extends Comparable<T>> extends Object
Represents a species in the NEAT (NeuroEvolution of Augmenting Topologies) algorithm.

A Species groups together genetically similar individuals in the population, enabling fitness sharing and diversity preservation in NEAT evolution. Species are formed based on genetic compatibility distance, allowing individuals with similar network topologies to compete within their own niche rather than with the entire population.

Key characteristics:

  • Genetic similarity: Members share similar network topologies and connection patterns
  • Fitness sharing: Members compete primarily within their species for reproductive opportunities
  • Diversity preservation: Protects innovative topologies from being eliminated by established forms
  • Dynamic membership: Species composition changes as individuals evolve and compatibility shifts

NEAT speciation process:

  1. Compatibility measurement: Calculate genetic distance between individuals
  2. Species assignment: Assign individuals to species based on distance thresholds
  3. Representative selection: Choose species representatives for compatibility testing
  4. Fitness sharing: Adjust individual fitness based on species membership size
  5. Reproduction allocation: Allocate offspring based on species average fitness

Species lifecycle management:

  • Formation: New species created when individuals exceed compatibility threshold
  • Growth: Species gain members as similar individuals are assigned
  • Stagnation: Species may stagnate if they fail to improve over generations
  • Extinction: Species die out when they have no members or persistently poor performance

Common usage patterns:


 // Create new species with founding ancestors
 List<Individual<Double>> founders = List.of(individual1, individual2);
 Species<Double> species = new Species<>(42, founders);
 
 // Add members during population assignment
 species.addMember(similarIndividual1);
 species.addMember(similarIndividual2);
 species.addAllMembers(batchOfSimilarIndividuals);
 
 // Access species information
 int speciesId = species.getId();
 int memberCount = species.getNumMembers();
 List<Individual<Double>> allMembers = species.getMembers();
 
 // Species-based fitness sharing
 for (Individual<Double> member : species.getMembers()) {
     double sharedFitness = member.fitness() / species.getNumMembers();
     // Use shared fitness for selection
 }
 

Ancestor tracking:

  • Species representatives: Ancestors serve as compatibility test references
  • Historical continuity: Maintains connection to previous generations
  • Stability: Prevents species boundaries from shifting too rapidly
  • Representative selection: Best performers may become ancestors for next generation

Fitness sharing mechanism:

  • Within-species competition: Members primarily compete with each other
  • Diversity protection: Prevents single topology from dominating population
  • Innovation preservation: Allows new topologies time to optimize
  • Niche exploitation: Different species can specialize for different aspects of the problem

Integration with NEAT selection:

  • Speciation: Used by NeatSelectionPolicyHandler for population organization
  • Compatibility testing: Ancestors used as reference points for species assignment
  • Reproduction allocation: Species size influences offspring distribution
  • Population dynamics: Species creation, growth, and extinction drive population diversity
See Also:
  • Field Details

  • Constructor Details

    • Species

      public Species(int _id, List<Individual<T>> _ancestors)
      Constructs a new species with the specified ID and founding ancestors.

      The ancestors serve as reference points for compatibility testing and represent the genetic heritage of the species. New individuals are tested against these ancestors to determine species membership.

      Parameters:
      _id - unique identifier for this species
      _ancestors - founding individuals that define the species genetic signature
      Throws:
      IllegalArgumentException - if ancestors is null
  • Method Details

    • addAncestor

      public void addAncestor(Individual<T> individual)
      Adds an individual as an ancestor of this species.

      Ancestors serve as reference points for compatibility testing in subsequent generations. Typically, the best performers from a species may be promoted to ancestors to maintain species continuity.

      Parameters:
      individual - the individual to add as an ancestor
      Throws:
      IllegalArgumentException - if individual is null
    • addMember

      public void addMember(Individual<T> individual)
      Adds an individual as a member of this species.

      Members are the current generation individuals that have been assigned to this species based on genetic compatibility. They participate in fitness sharing and species-based selection.

      Parameters:
      individual - the individual to add as a member
      Throws:
      IllegalArgumentException - if individual is null
    • addAllMembers

      public void addAllMembers(Collection<Individual<T>> individuals)
      Adds multiple individuals as members of this species.

      This is a convenience method for bulk assignment of compatible individuals to the species. All individuals in the collection will participate in fitness sharing within this species.

      Parameters:
      individuals - collection of individuals to add as members
      Throws:
      IllegalArgumentException - if individuals is null
    • getNumAncestors

      public int getNumAncestors()
      Returns the number of ancestors in this species.

      Ancestors serve as reference points for compatibility testing and represent the species genetic heritage from previous generations.

      Returns:
      the number of ancestors
    • getNumMembers

      public int getNumMembers()
      Returns the number of current members in this species.

      The member count is used for fitness sharing calculations and reproduction allocation. Larger species will have their members' fitness values adjusted downward to prevent single species from dominating the population.

      Returns:
      the number of current members
    • getId

      public int getId()
      Returns the unique identifier for this species.

      Species IDs are typically assigned by a SpeciesIdGenerator and remain constant throughout the species lifecycle.

      Returns:
      the species unique identifier
    • getAncestors

      public List<Individual<T>> getAncestors()
      Returns the list of ancestors for this species.

      Ancestors are reference individuals used for compatibility testing when assigning new individuals to species. The returned list is mutable and modifications will affect the species behavior.

      Returns:
      mutable list of ancestor individuals
    • getMembers

      public List<Individual<T>> getMembers()
      Returns the list of current members in this species.

      Members are the current generation individuals that participate in fitness sharing and species-based selection. The returned list is mutable and modifications will affect species membership.

      Returns:
      mutable list of member individuals
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object