use std::convert::Infallible; use nalgebra::SVector; use rand_distr::{Normal, NormalError}; use crate::{local_search::LocalSearchCandidate, perturbation::{BoundedPerturbation, PerturbationOperator, RandomDistributionPerturbation}}; pub trait EvolutionaryStrategy { type Err; fn step(&mut self, perturbation: &mut TPerturbation, better: bool, stats: &Vec> ) -> Result<(), Self::Err>; } fn normal_one_to_five(perturbation: &mut RandomDistributionPerturbation>, better: bool) -> Result<(), NormalError> { let exp: f64 = if better { 1.0 } else { 0.0 } - 0.2; let sigma = perturbation.std_dev(); let new_sigma = sigma * exp.exp().powf(1.0 / LEN as f64); perturbation.set_std_dev(new_sigma)?; Ok(()) } pub struct OneToFiveStrategy; impl EvolutionaryStrategy>> for OneToFiveStrategy { type Err = NormalError; fn step(&mut self, perturbation: &mut RandomDistributionPerturbation>, better: bool, _: &Vec, TOut>> ) -> Result<(), Self::Err> { normal_one_to_five(perturbation, better) } } impl EvolutionaryStrategy>>> for OneToFiveStrategy { type Err = NormalError; fn step(&mut self, perturbation: &mut BoundedPerturbation>>, better: bool, _: &Vec>> as PerturbationOperator>::Chromosome, TOut>> ) -> Result<(), Self::Err> { normal_one_to_five(perturbation.inner_mut(), better) } } pub struct IdentityStrategy; impl EvolutionaryStrategy for IdentityStrategy { type Err = Infallible; fn step(&mut self, _: &mut TPerturbation, _: bool, _: &Vec> ) -> Result<(), Self::Err> { Ok(()) } }