Class FitnessEvaluatorVirtualThread<T extends Comparable<T>>
- Type Parameters:
T
- the type of fitness values produced, must be comparable for selection operations
- All Implemented Interfaces:
FitnessEvaluator<T>
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 Summary
FieldsModifier and TypeFieldDescriptionprivate final EAConfiguration
<T> private final EAExecutionContext
<T> static final org.apache.logging.log4j.Logger
private final ExecutorService
-
Constructor Summary
ConstructorsConstructorDescriptionFitnessEvaluatorVirtualThread
(EAExecutionContext<T> _eaExecutionContext, EAConfiguration<T> _eaConfiguration) -
Method Summary
Modifier and TypeMethodDescriptionEvaluates the fitness of all genotypes in the given population.void
Called after fitness evaluation completes for a generation.void
Called before fitness evaluation begins for a generation.
-
Field Details
-
logger
public static final org.apache.logging.log4j.Logger logger -
eaExecutionContext
-
eaConfiguration
-
virtualThreadExecutor
-
-
Constructor Details
-
FitnessEvaluatorVirtualThread
public FitnessEvaluatorVirtualThread(EAExecutionContext<T> _eaExecutionContext, EAConfiguration<T> _eaConfiguration)
-
-
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 interfaceFitnessEvaluator<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 interfaceFitnessEvaluator<T extends Comparable<T>>
-
evaluate
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 interfaceFitnessEvaluator<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
-