Class SpeciesIdGenerator

java.lang.Object
net.bmahe.genetics4j.neat.SpeciesIdGenerator

public class SpeciesIdGenerator extends Object
Generates unique identifiers for NEAT (NeuroEvolution of Augmenting Topologies) species.

SpeciesIdGenerator provides thread-safe generation of unique species identifiers used to distinguish between different species in the NEAT population. Each species receives a unique ID that remains constant throughout its lifecycle, enabling species tracking across generations and proper species management.

Key characteristics:

  • Thread safety: Concurrent ID generation for parallel species creation
  • Uniqueness guarantee: Each generated ID is unique across the generator's lifetime
  • Sequential ordering: IDs are generated in sequential order for consistent tracking
  • Configurable start: Initial ID value can be customized for different scenarios

Species lifecycle integration:

  • Species creation: New species receive unique IDs upon formation
  • Species tracking: IDs enable consistent species identification across generations
  • Population management: Species IDs facilitate population organization and statistics
  • Evolution monitoring: IDs enable tracking of species formation, growth, and extinction

Common usage patterns:


 // Default species ID generator (starts from 0)
 SpeciesIdGenerator generator = new SpeciesIdGenerator();
 
 // Custom starting ID
 SpeciesIdGenerator customGenerator = new SpeciesIdGenerator(1000);
 
 // Generate unique species IDs
 int speciesId1 = generator.computeNewId();  // Returns 0
 int speciesId2 = generator.computeNewId();  // Returns 1
 int speciesId3 = generator.computeNewId();  // Returns 2
 
 // Create species with generated IDs
 Species<Double> species1 = new Species<>(speciesId1, ancestors1);
 Species<Double> species2 = new Species<>(speciesId2, ancestors2);
 
 // Thread-safe concurrent generation
 CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(generator::computeNewId);
 CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(generator::computeNewId);
 // Both futures will receive unique IDs
 

Integration with NEAT species management:

  • Species formation: New species created during population organization get unique IDs
  • Selection handlers: Used by NeatSelectionPolicyHandler for species tracking
  • Population statistics: Enables species-based metrics and analysis
  • Evolution context: Integrated into NEAT execution contexts for species management

Thread safety considerations:

  • Atomic operations: Uses AtomicInteger for thread-safe ID generation
  • Concurrent access: Multiple threads can safely generate IDs simultaneously
  • Lock-free: No synchronization overhead for high-performance scenarios
  • Consistency: Guarantees unique IDs even under high concurrency

Performance characteristics:

  • O(1) generation: Constant time ID generation
  • Memory efficient: Minimal memory footprint with single atomic counter
  • High throughput: Suitable for high-frequency species creation
  • No contention: Lock-free implementation avoids thread contention
See Also:
  • Field Details

  • Constructor Details

    • SpeciesIdGenerator

      public SpeciesIdGenerator(int initialValue)
      Constructs a new species ID generator with the specified initial value.

      The initial value determines the first ID that will be generated. Subsequent IDs will be incremented sequentially from this starting point.

      Parameters:
      initialValue - the first ID value to generate
    • SpeciesIdGenerator

      public SpeciesIdGenerator()
      Constructs a new species ID generator with the default initial value (0).
  • Method Details

    • computeNewId

      public int computeNewId()
      Generates and returns a new unique species ID.

      This method atomically increments the internal counter and returns the previous value, ensuring that each call produces a unique identifier. The operation is thread-safe and can be called concurrently from multiple threads without risk of duplicate IDs.

      Returns:
      a unique species identifier