From abae71139437208822f07bb957d3832f4b5a41e3 Mon Sep 17 00:00:00 2001 From: Rutherther Date: Mon, 6 Oct 2025 20:31:15 +0200 Subject: [PATCH] feat: add PatternPerturbation Move by +/- d in random single coordinate. --- env/src/perturbation/mod.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/env/src/perturbation/mod.rs b/env/src/perturbation/mod.rs index 12cbfe4ebd83ce687efbfa95dd404a492970136a..42b3b89a4411d27fb79b8fad9dd071df9d216152 100644 --- a/env/src/perturbation/mod.rs +++ b/env/src/perturbation/mod.rs @@ -63,6 +63,42 @@ impl, const LEN: usize> Perturbation } } +pub struct PatternPerturbation { + d: f64, + rng: TRng +} + +impl PatternPerturbation { + pub fn new(d: f64) -> Self { + Self { + d, + rng: rand::rng() + } + } +} + +impl PerturbationOperator for PatternPerturbation { + type Chromosome = SVector::; + + 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,