~ruther/ctu-fee-eoa

f304d07126d7123c7af3fff8a4bbc3109978ed21 — Rutherther a month ago e3d7f21
refactor(lib): implement generic evolutionary strategy using .wrapped_mut

Thanks to wrapped_mut now the one_to_five strategy can be implemented
without relying on BoundedPerturbation etc.
1 files changed, 18 insertions(+), 17 deletions(-)

M codes/eoa_lib/src/evolutionary_strategy.rs
M codes/eoa_lib/src/evolutionary_strategy.rs => codes/eoa_lib/src/evolutionary_strategy.rs +18 -17
@@ 3,7 3,7 @@ use std::{convert::Infallible, error::Error};
use nalgebra::SVector;
use rand_distr::{Normal, NormalError};

use crate::{local_search::LocalSearchStats, perturbation::{BoundedPerturbation, PerturbationOperator, RandomDistributionPerturbation}};
use crate::{local_search::LocalSearchStats, perturbation::{apply_to_perturbations, BoundedPerturbation, PerturbationOperator, RandomDistributionPerturbation}};

pub trait EvolutionaryStrategy<TOut, TPerturbation: PerturbationOperator> {
    type Err: Error + 'static;


@@ 33,30 33,31 @@ fn normal_one_to_five<const LEN: usize>(perturbation: &mut RandomDistributionPer
}

pub struct OneToFiveStrategy;
impl<const LEN: usize, TOut> EvolutionaryStrategy<TOut, RandomDistributionPerturbation<LEN, Normal<f64>>> for OneToFiveStrategy {
impl<const LEN: usize,
     TPerturbation: PerturbationOperator<Chromosome = SVector<f64, LEN>>,
     TOut> EvolutionaryStrategy<TOut, TPerturbation> for OneToFiveStrategy {
    type Err = NormalError;

    fn step(&mut self,
            perturbation: &mut RandomDistributionPerturbation<LEN, Normal<f64>>,
            perturbation: &mut TPerturbation,
            better: bool,
            _: &LocalSearchStats<SVector::<f64, LEN>, TOut>
    ) -> Result<(), Self::Err> {
        normal_one_to_five(perturbation, better)
    }
}
        let mut found = false;
        let mut result = Ok(());
        apply_to_perturbations::<_, RandomDistributionPerturbation<LEN, Normal<f64>>>(
            perturbation,
            &mut |perturbation| {
                found = true;
                result = normal_one_to_five(perturbation, better);
            }
        );

// TODO: I don't really like this to be honest. This would basically have to take care of any perturbation wrapper
// that there is. But that just seems wrong.
impl<const LEN: usize, TOut> EvolutionaryStrategy<TOut, BoundedPerturbation<LEN, RandomDistributionPerturbation<LEN, Normal<f64>>>> for OneToFiveStrategy {
    type Err = NormalError;
        if !found {
            panic!("There is no random distribution perturbation!");
        }

    fn step(&mut self,
            perturbation: &mut BoundedPerturbation<LEN, RandomDistributionPerturbation<LEN, Normal<f64>>>,
            better: bool,
            _: &LocalSearchStats<<BoundedPerturbation<LEN, RandomDistributionPerturbation<LEN, Normal<f64>>> as PerturbationOperator>::Chromosome, TOut>
    ) -> Result<(), Self::Err> {
        println!("{}", perturbation.inner().std_dev());
        normal_one_to_five(perturbation.inner_mut(), better)
        result
    }
}