pub trait Evaluator<C, T>: Send + Syncwhere
C: Chromosome,{
// Required method
fn eval(
&self,
ecosystem: &mut Ecosystem<C>,
problem: Arc<dyn Problem<C, T>>,
) -> Result<usize, RadiateError>;
}Expand description
A trait for evaluating fitness of individuals in the ecosystem.
The Evaluator trait defines the interface for fitness evaluation strategies. Implementors can define different approaches to computing fitness scores, such as individual evaluation, batch evaluation, or specialized evaluation strategies for specific problem domains.
The two main implementations provided are:
- FitnessEvaluator: Evaluates individuals one at a time
- BatchFitnessEvaluator: Evaluates individuals in batches Custom evaluators can be created and used, however, take special note on how the members of the ecosystem are accessed and modified, (taken out of the phenotype then restored). This is important to ensure memory safety and avoid unnecessary cloning of genotypes.
§Generic Parameters
C: The chromosome type that represents the genetic materialT: The target type that the problem operates on
Required Methods§
Sourcefn eval(
&self,
ecosystem: &mut Ecosystem<C>,
problem: Arc<dyn Problem<C, T>>,
) -> Result<usize, RadiateError>
fn eval( &self, ecosystem: &mut Ecosystem<C>, problem: Arc<dyn Problem<C, T>>, ) -> Result<usize, RadiateError>
Evaluates the fitness of unevaluated individuals in the ecosystem.
This method processes individuals that don’t have fitness scores yet, evaluates them using the provided problem, and updates the ecosystem with the computed scores.
§Arguments
ecosystem- The ecosystem containing the populationproblem- The problem instance used to evaluate fitness
§Returns
The number of individuals that were evaluated during this call
Implementors§
impl<C, T> Evaluator<C, T> for BatchFitnessEvaluatorwhere
C: Chromosome + 'static,
T: 'static,
Implementation of Evaluator for BatchFitnessEvaluator.
This implementation groups individuals into batches and evaluates them together, which can significantly improve performance for large populations or when the problem supports efficient batch evaluation.
§Algorithm
The algorithm largely follows the same steps as FitnessEvaluator:
- Collect Unevaluated Individuals: Find individuals without fitness scores
- Calculate Batch Size: Determine optimal batch size based on worker count
- Create Batches: Group individuals into batches for parallel processing
- Execute Batches: Run batch evaluations through the executor
- Update Ecosystem: Store computed scores and restore genotypes
§Batch Size Optimization
The batch size is calculated to ensure optimal work distribution:
- Small Batches: Ensure all workers have work to do
- Large Batches: Minimize overhead from job creation and distribution
- Balanced Distribution: Work is evenly distributed across available threads
§Performance Optimizations
- Efficient Batching: Batches are created with minimal memory allocation
- Parallel Execution: Multiple batches are processed simultaneously
- Memory Management: Genotypes are efficiently restored after evaluation
impl<C, T> Evaluator<C, T> for FitnessEvaluatorwhere
C: Chromosome + 'static,
T: 'static,
Implementation of Evaluator for FitnessEvaluator.
This implementation evaluates individuals one at a time, collecting unevaluated individuals and processing them through the executor system.
§Algorithm
- Collect Unevaluated Individuals: Find individuals without fitness scores
- Create Evaluation Jobs: Package genotypes and indices into jobs
- Execute Jobs: Run evaluations through the executor
- Update Ecosystem: Store computed scores and restore genotypes
§Performance Optimizations
- Efficient Job Creation: Jobs are created with minimal allocation
- Batch Execution: Multiple jobs are submitted to the executor at once
- Memory Reuse: Genotypes are restored to avoid unnecessary cloning