From ba97623c86b473f5e5b00b0520be2e15cfc6d041 Mon Sep 17 00:00:00 2001 From: Rutherther Date: Fri, 10 Oct 2025 15:17:24 +0200 Subject: [PATCH] fix: put Rng to Box instead of generics --- env/src/binary_string.rs | 5 ++- env/src/perturbation/mod.rs | 63 +++++++++++++++++++++++++------------ 2 files changed, 45 insertions(+), 23 deletions(-) diff --git a/env/src/binary_string.rs b/env/src/binary_string.rs index 100ec320f28641926d5225154dd636e94b4e32d3..6425b8594396565bda1f51949cbd904c3700a7cb 100644 --- a/env/src/binary_string.rs +++ b/env/src/binary_string.rs @@ -1,5 +1,5 @@ use std::str::FromStr; -use rand::Rng; +use rand::{Rng, RngCore}; #[derive(Debug, Clone, PartialEq)] pub struct Bounds { @@ -46,8 +46,7 @@ impl BinaryString { &self.vec } - pub fn perturb(self: &Self, rng: &mut TRng, p: f64) -> Self - where TRng : Rng + pub fn perturb(self: &Self, rng: &mut dyn RngCore, p: f64) -> Self { BinaryString::new( self.into_iter() diff --git a/env/src/perturbation/mod.rs b/env/src/perturbation/mod.rs index 42b3b89a4411d27fb79b8fad9dd071df9d216152..d60530d8cb9b0810d6af655d74f67ea929cf2e40 100644 --- a/env/src/perturbation/mod.rs +++ b/env/src/perturbation/mod.rs @@ -1,5 +1,5 @@ use nalgebra::SVector; -use rand::{distr::Distribution, Rng}; +use rand::{distr::Distribution, Rng, RngCore}; use rand_distr::{uniform, Normal, NormalError, Uniform}; use crate::binary_string::BinaryString; @@ -10,21 +10,21 @@ pub trait PerturbationOperator { fn perturb(self: &mut Self, chromosome: &Self::Chromosome) -> Self::Chromosome; } -pub struct BinaryStringBitPerturbation { - rng: TRng, +pub struct BinaryStringBitPerturbation { + rng: Box, p: f64, } -impl BinaryStringBitPerturbation { +impl BinaryStringBitPerturbation { pub fn new(p: f64) -> Self { Self { - rng: rand::rng(), + rng: Box::new(rand::rng()), p } } } -impl PerturbationOperator for BinaryStringBitPerturbation { +impl PerturbationOperator for BinaryStringBitPerturbation { type Chromosome = BinaryString; fn perturb(self: &mut Self, chromosome: &Self::Chromosome) -> Self::Chromosome { @@ -32,30 +32,53 @@ impl PerturbationOperator for BinaryStringBitPerturbation { } } -pub struct RandomDistributionPerturbation> { +pub struct RandomDistributionPerturbation> { distribution: TDistribution, - rng: TRng + rng: Box, + parameter: f64 } -impl RandomDistributionPerturbation> { - pub fn normal(variance: f64) -> Result { +impl RandomDistributionPerturbation> { + pub fn normal(std_dev: f64) -> Result { Ok(Self { - distribution: Normal::new(0.0, variance)?, - rng: rand::rng() + distribution: Normal::new(0.0, std_dev)?, + rng: Box::new(rand::rng()), + parameter: std_dev }) } + + pub fn std_dev(&self) -> f64 { + self.parameter + } + + pub fn set_std_dev(&mut self, std_dev: f64) -> Result { + self.parameter = std_dev; + self.distribution = Normal::new(0.0, std_dev)?; + Ok(std_dev) + } } -impl RandomDistributionPerturbation> { +impl RandomDistributionPerturbation> { pub fn uniform(range: f64) -> Result { Ok(Self { distribution: Uniform::new(-range/2.0, range/2.0)?, - rng: rand::rng() + rng: Box::new(rand::rng()), + parameter: range, }) } + + pub fn range(&self) -> f64 { + self.parameter + } + + pub fn set_range(&mut self, range: f64) -> Result { + self.parameter = range; + self.distribution = Uniform::new(-range/2.0, range/2.0)?; + Ok(range) + } } -impl, const LEN: usize> PerturbationOperator for RandomDistributionPerturbation { +impl, const LEN: usize> PerturbationOperator for RandomDistributionPerturbation { type Chromosome = SVector; fn perturb(self: &mut Self, chromosome: &Self::Chromosome) -> Self::Chromosome { @@ -63,21 +86,21 @@ impl, const LEN: usize> Perturbation } } -pub struct PatternPerturbation { +pub struct PatternPerturbation { d: f64, - rng: TRng + rng: Box } -impl PatternPerturbation { +impl PatternPerturbation { pub fn new(d: f64) -> Self { Self { d, - rng: rand::rng() + rng: Box::new(rand::rng()) } } } -impl PerturbationOperator for PatternPerturbation { +impl PerturbationOperator for PatternPerturbation { type Chromosome = SVector::; fn perturb(self: &mut Self, chromosome: &Self::Chromosome) -> Self::Chromosome {