Class FitnessEvaluatorVirtualThread<T extends Comparable<T>>

java.lang.Object
net.bmahe.genetics4j.core.evaluation.FitnessEvaluatorVirtualThread<T>
Type Parameters:
T - the type of fitness values produced, must be comparable for selection operations
All Implemented Interfaces:
FitnessEvaluator<T>

public class FitnessEvaluatorVirtualThread<T extends Comparable<T>> extends Object implements FitnessEvaluator<T>
Virtual thread-based fitness evaluator that creates one virtual thread per individual evaluation.

This evaluator leverages Java 21+ virtual threads to provide massive parallelism for fitness evaluation without the overhead of traditional platform threads. Each genotype gets its own virtual thread, making this approach particularly suitable for:

  • I/O-bound fitness functions: Database queries, web service calls, file operations
  • Large populations: Thousands of individuals without thread pool limitations
  • Complex simulations: Long-running evaluations that benefit from massive parallelism
  • External service integration: Fitness evaluations requiring network calls

Unlike traditional thread pool-based evaluators, this implementation:

  • Creates one virtual thread per genotype (no partitioning)
  • Leverages virtual thread's lightweight nature for maximum concurrency
  • Automatically manages virtual thread lifecycle
  • Provides optimal resource utilization for I/O-intensive workloads

Performance characteristics:

  • Memory overhead: ~200 bytes per virtual thread vs ~2MB per platform thread
  • Creation cost: Nearly zero compared to platform threads
  • Blocking behavior: Virtual threads don't block carrier threads during I/O
  • Scalability: Can handle millions of concurrent virtual threads

This evaluator automatically creates and manages a virtual thread executor, eliminating the need for explicit thread pool configuration and management.

See Also:
  • Field Details

  • Constructor Details

  • Method Details

    • preEvaluation

      public void preEvaluation()
      Description copied from interface: FitnessEvaluator
      Called before fitness evaluation begins for a generation.

      This lifecycle method allows implementations to perform setup operations such as initializing resources, establishing connections, or preparing computational environments before the evaluation process starts.

      Common setup activities include:

      • Starting thread pools or worker processes
      • Establishing database or network connections
      • Loading configuration or reference data
      • Initializing caches or temporary storage

      The default implementation does nothing, making this method optional for implementations that don't require setup.

      Specified by:
      preEvaluation in interface FitnessEvaluator<T extends Comparable<T>>
    • postEvaluation

      public void postEvaluation()
      Description copied from interface: FitnessEvaluator
      Called after fitness evaluation completes for a generation.

      This lifecycle method allows implementations to perform cleanup operations such as releasing resources, closing connections, or persisting results after the evaluation process completes.

      Common cleanup activities include:

      • Shutting down thread pools or worker processes
      • Closing database or network connections
      • Flushing caches or saving state
      • Logging performance metrics or statistics

      The default implementation does nothing, making this method optional for implementations that don't require cleanup.

      Specified by:
      postEvaluation in interface FitnessEvaluator<T extends Comparable<T>>
    • evaluate

      public List<T> evaluate(long generation, List<Genotype> population)
      Description copied from interface: FitnessEvaluator
      Evaluates the fitness of all genotypes in the given population.

      This is the core method that computes fitness values for an entire population of genotypes. The implementation strategy (synchronous, parallel, asynchronous) is determined by the specific evaluator implementation.

      Requirements:

      • Return fitness values in the same order as input genotypes
      • Return exactly one fitness value per input genotype
      • Ensure fitness values are comparable for selection operations
      • Handle empty populations gracefully (return empty list)

      The generation parameter can be used for:

      • Adaptive fitness functions that change over time
      • Logging and monitoring evaluation progress
      • Implementing generation-specific evaluation strategies
      • Debugging and performance analysis
      Specified by:
      evaluate in interface FitnessEvaluator<T extends Comparable<T>>
      Parameters:
      generation - the current generation number (0-based)
      population - the population of genotypes to evaluate
      Returns:
      a list of fitness values corresponding to each genotype