~ruther/ctu-fee-eoa

c0b18ff799543a76b25416a014595793b539a896 — Rutherther 25 days ago fce9bfb
feat: add possibility to map EvolutionResult evaluations
1 files changed, 37 insertions(+), 4 deletions(-)

M codes/eoa_lib/src/evolution.rs
M codes/eoa_lib/src/evolution.rs => codes/eoa_lib/src/evolution.rs +37 -4
@@ 30,6 30,42 @@ pub struct EvolutionResult<TInput, TResult> {
    pub evaluations: usize
}

impl<TInput, TResult> EvolutionResult<TInput, TResult> {
    pub fn map<TNewResult>(self, map: impl Fn(TResult) -> TNewResult) -> EvolutionResult<TInput, TNewResult> {
        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
    <TChromosome: Clone,
     TResult: Clone,


@@ 75,7 111,6 @@ pub fn evolution_algorithm
    fn apply_new_eval<TChromosome: Clone, TResult: Clone>(
        current_evaluation: &mut usize,
        better_than: &impl BetterThanOperator<TResult>,
        fitness: &impl FitnessFunction<In = TChromosome, Out = TResult>,
        current_iteration: &usize,
        stats: &mut EvolutionStats<TChromosome, TResult>,
        population: &EvaluatedPopulation<TChromosome, TResult>,


@@ 114,7 149,6 @@ pub fn evolution_algorithm
    apply_new_eval(
        &mut current_evaluation,
        better_than,
        fitness,
        &0,
        &mut stats,
        &current_population,


@@ 138,7 172,6 @@ pub fn evolution_algorithm
        apply_new_eval(
            &mut current_evaluation,
            better_than,
            fitness,
            &iteration,
            &mut stats,
            &current_population,


@@ 184,7 217,7 @@ pub mod tests {
    pub fn test_evolution_one_max() {
        const D: usize = 512;
        let optimum = BinaryString::<Const<D>>::new(vec![0; D]);
        let one_max = OneMax::<Const<D>>::new();
        let mut one_max = OneMax::<Const<D>>::new();

        let initializer = RandomInitializer::<Const<D>, BinaryString::<Const<D>>>::new_binary();
        let population_size = 10;