From fcdc8b366adfe108bd901dae12a5276be3e8f0a4 Mon Sep 17 00:00:00 2001 From: Rutherther Date: Sun, 5 Oct 2025 20:11:38 +0200 Subject: [PATCH] feat: add RandomDistributionPerturbation For vectors of reals, perturb by a random distribution with expectation in 0. --- env/Cargo.lock | 18 ++++++++++++++++++ env/Cargo.toml | 1 + env/src/perturbation/mod.rs | 26 +++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/env/Cargo.lock b/env/Cargo.lock index d3a0305ee88a5b66174126493a548d3a1ceddf5a..124fe99fc62cfc5d1c3e0ad6514de6917d1a22d7 100644 --- a/env/Cargo.lock +++ b/env/Cargo.lock @@ -35,6 +35,7 @@ version = "0.1.0" dependencies = [ "nalgebra", "rand", + "rand_distr", ] [[package]] @@ -55,6 +56,12 @@ version = "0.2.176" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "58f929b4d672ea937a23a1ab494143d968337a5f47e56d0815df1e0890ddf174" +[[package]] +name = "libm" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9fbbcab51052fe104eb5e5d351cf728d30a5be1fe14d9be8a3b097481fb97de" + [[package]] name = "matrixmultiply" version = "0.3.10" @@ -138,6 +145,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" dependencies = [ "autocfg", + "libm", ] [[package]] @@ -208,6 +216,16 @@ dependencies = [ "getrandom", ] +[[package]] +name = "rand_distr" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a8615d50dcf34fa31f7ab52692afec947c4dd0ab803cc87cb3b0b4570ff7463" +dependencies = [ + "num-traits", + "rand", +] + [[package]] name = "rawpointer" version = "0.2.1" diff --git a/env/Cargo.toml b/env/Cargo.toml index 5bcb1a8e759d622a83fe024cd3e970f45c403cbf..18c352cdb80e8e50d69de3ca91252c6f3c2e0d7e 100644 --- a/env/Cargo.toml +++ b/env/Cargo.toml @@ -6,3 +6,4 @@ edition = "2024" [dependencies] nalgebra = "0.33.2" rand = "0.9.2" +rand_distr = "0.5.1" diff --git a/env/src/perturbation/mod.rs b/env/src/perturbation/mod.rs index d42907b872230b4cd55921b5163bf21d864bd02a..25e202cda3db32995312908f2638289ca49bf54a 100644 --- a/env/src/perturbation/mod.rs +++ b/env/src/perturbation/mod.rs @@ -1,4 +1,6 @@ -use rand::Rng; +use nalgebra::SVector; +use rand::{distr::Distribution, Rng}; +use rand_distr::{Normal, NormalError}; use crate::binary_string::BinaryString; @@ -30,6 +32,28 @@ impl PerturbationOperator for BinaryStringBitPerturbation { } } +pub struct RandomDistributionPerturbation> { + distribution: TDistribution, + rng: TRng +} + +impl RandomDistributionPerturbation> { + pub fn normal(variance: f64) -> Result { + Ok(Self { + distribution: Normal::new(0.0, variance)?, + rng: rand::rng() + }) + } +} + +impl, const LEN: usize> PerturbationOperator for RandomDistributionPerturbation { + type Chromosome = SVector; + + fn perturb(self: &mut Self, chromosome: &Self::Chromosome) -> Self::Chromosome { + chromosome + Self::Chromosome::zeros().map(|_| self.distribution.sample(&mut self.rng)) + } +} + #[cfg(test)] pub mod tests { use crate::binary_string::BinaryString;