~ruther/ctu-fee-eoa

ref: fa9a5aff839b19a79677e6fdc8f605456c389f08 ctu-fee-eoa/codes/eoa_lib/src/evolutionary_strategy.rs -rw-r--r-- 2.7 KiB
fa9a5aff — Rutherther feat: add plotting of TSP a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use std::{convert::Infallible, error::Error};

use nalgebra::SVector;
use rand_distr::{Normal, NormalError};

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

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

    fn step(&mut self,
            perturbation: &mut TPerturbation,
            better: bool,
            stats: &LocalSearchStats<TPerturbation::Chromosome, TOut>
    ) -> Result<(), Self::Err>;
}

fn normal_one_to_five<const LEN: usize>(perturbation: &mut RandomDistributionPerturbation<LEN, Normal<f64>>, 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);

    // Hopefully prevent cases when the sigma goes too low
    let new_sigma = if new_sigma < 0.000000001 {
        0.000000001
    } else {
        new_sigma
    };

    perturbation.set_std_dev(new_sigma)?;
    Ok(())
}

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

    fn step(&mut self,
            perturbation: &mut RandomDistributionPerturbation<LEN, Normal<f64>>,
            better: bool,
            _: &LocalSearchStats<SVector::<f64, LEN>, TOut>
    ) -> Result<(), Self::Err> {
        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;

    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)
    }
}

pub struct IdentityStrategy;
impl<TOut, TPerturbation: PerturbationOperator> EvolutionaryStrategy<TOut, TPerturbation> for IdentityStrategy {
    type Err = Infallible;

    fn step(&mut self,
            _: &mut TPerturbation,
            _: bool,
            _: &LocalSearchStats<TPerturbation::Chromosome, TOut>
    ) -> Result<(), Self::Err> {
        Ok(())
    }
}