From c0b18ff799543a76b25416a014595793b539a896 Mon Sep 17 00:00:00 2001 From: Rutherther Date: Sun, 16 Nov 2025 19:50:02 +0100 Subject: [PATCH] feat: add possibility to map EvolutionResult evaluations --- codes/eoa_lib/src/evolution.rs | 41 ++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/codes/eoa_lib/src/evolution.rs b/codes/eoa_lib/src/evolution.rs index 0126ff5902b0523cdd35e4310137f27116338797..a9e2c5ee06d98b859a85a399cffc1db99e88fd98 100644 --- a/codes/eoa_lib/src/evolution.rs +++ b/codes/eoa_lib/src/evolution.rs @@ -30,6 +30,42 @@ pub struct EvolutionResult { pub evaluations: usize } +impl EvolutionResult { + pub fn map(self, map: impl Fn(TResult) -> TNewResult) -> EvolutionResult { + EvolutionResult { + population: EvaluatedPopulation::from_vec( + self.population.deconstruct() + .into_iter() + .map(|chromosome| EvaluatedChromosome { + chromosome: chromosome.chromosome, + evaluation: map(chromosome.evaluation) + }) + .collect() + ), + stats: EvolutionStats { + best_candidates: self.stats.best_candidates + .into_iter() + .map(|candidate| + EvolutionCandidate { + evaluated_chromosome: EvaluatedChromosome { + chromosome: candidate.evaluated_chromosome.chromosome, + evaluation: map(candidate.evaluated_chromosome.evaluation) + }, + evaluation: candidate.evaluation, + iteration: candidate.iteration + }) + .collect() + }, + best_candidate: EvaluatedChromosome { + chromosome: self.best_candidate.chromosome, + evaluation: map(self.best_candidate.evaluation) + }, + evaluations: self.evaluations, + iterations: self.iterations, + } + } +} + pub fn evolution_algorithm ( current_evaluation: &mut usize, better_than: &impl BetterThanOperator, - fitness: &impl FitnessFunction, current_iteration: &usize, stats: &mut EvolutionStats, population: &EvaluatedPopulation, @@ -114,7 +149,6 @@ pub fn evolution_algorithm apply_new_eval( &mut current_evaluation, better_than, - fitness, &0, &mut stats, ¤t_population, @@ -138,7 +172,6 @@ pub fn evolution_algorithm apply_new_eval( &mut current_evaluation, better_than, - fitness, &iteration, &mut stats, ¤t_population, @@ -184,7 +217,7 @@ pub mod tests { pub fn test_evolution_one_max() { const D: usize = 512; let optimum = BinaryString::>::new(vec![0; D]); - let one_max = OneMax::>::new(); + let mut one_max = OneMax::>::new(); let initializer = RandomInitializer::, BinaryString::>>::new_binary(); let population_size = 10;