~ruther/ctu-fee-eoa

abae71139437208822f07bb957d3832f4b5a41e3 — Rutherther 2 months ago c5320f4
feat: add PatternPerturbation

Move by +/- d in random single coordinate.
1 files changed, 36 insertions(+), 0 deletions(-)

M env/src/perturbation/mod.rs
M env/src/perturbation/mod.rs => env/src/perturbation/mod.rs +36 -0
@@ 63,6 63,42 @@ impl<TRng: Rng, TDistribution: Distribution<f64>, const LEN: usize> Perturbation
    }
}

pub struct PatternPerturbation<const LEN: usize, TRng: Rng> {
    d: f64,
    rng: TRng
}

impl<const LEN: usize> PatternPerturbation<LEN, rand::rngs::ThreadRng> {
    pub fn new(d: f64) -> Self {
        Self {
            d,
            rng: rand::rng()
        }
    }
}

impl<const LEN: usize, TRng: Rng> PerturbationOperator for PatternPerturbation<LEN, TRng> {
    type Chromosome = SVector::<f64, LEN>;

    fn perturb(self: &mut Self, chromosome: &Self::Chromosome) -> Self::Chromosome {
        let mut chromosome = chromosome.clone();

        // 1. Choose dimension
        let idx = self.rng.random_range(0..LEN);
        // 2. Direction
        let d = if self.rng.random_bool(0.5) {
            self.d
        } else {
            -self.d
        };

        // Apply
        chromosome[idx] += d;

        chromosome
    }
}

pub enum BoundedPerturbationStrategy {
    /// Trims the value to get a value within bounds
    Trim,