@@ 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,