~ruther/ctu-fee-eoa

ba97623c86b473f5e5b00b0520be2e15cfc6d041 — Rutherther 2 months ago 296be2c
fix: put Rng to Box instead of generics
2 files changed, 45 insertions(+), 23 deletions(-)

M env/src/binary_string.rs
M env/src/perturbation/mod.rs
M env/src/binary_string.rs => env/src/binary_string.rs +2 -3
@@ 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<TRng>(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()

M env/src/perturbation/mod.rs => env/src/perturbation/mod.rs +43 -20
@@ 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<TRng: Rng> {
    rng: TRng,
pub struct BinaryStringBitPerturbation {
    rng: Box<dyn RngCore>,
    p: f64,
}

impl BinaryStringBitPerturbation<rand::rngs::ThreadRng> {
impl BinaryStringBitPerturbation {
    pub fn new(p: f64) -> Self {
        Self {
            rng: rand::rng(),
            rng: Box::new(rand::rng()),
            p
        }
    }
}

impl<TRng: Rng> PerturbationOperator for BinaryStringBitPerturbation<TRng> {
impl PerturbationOperator for BinaryStringBitPerturbation {
    type Chromosome = BinaryString;

    fn perturb(self: &mut Self, chromosome: &Self::Chromosome) -> Self::Chromosome {


@@ 32,30 32,53 @@ impl<TRng: Rng> PerturbationOperator for BinaryStringBitPerturbation<TRng> {
    }
}

pub struct RandomDistributionPerturbation<const LEN: usize, TRng: Rng, TDistribution: Distribution<f64>> {
pub struct RandomDistributionPerturbation<const LEN: usize, TDistribution: Distribution<f64>> {
    distribution: TDistribution,
    rng: TRng
    rng: Box<dyn RngCore>,
    parameter: f64
}

impl<const LEN: usize> RandomDistributionPerturbation<LEN, rand::rngs::ThreadRng, Normal<f64>> {
    pub fn normal(variance: f64) -> Result<Self, NormalError> {
impl<const LEN: usize> RandomDistributionPerturbation<LEN, Normal<f64>> {
    pub fn normal(std_dev: f64) -> Result<Self, NormalError> {
        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<f64, NormalError> {
        self.parameter = std_dev;
        self.distribution = Normal::new(0.0, std_dev)?;
        Ok(std_dev)
    }
}

impl<const LEN: usize> RandomDistributionPerturbation<LEN, rand::rngs::ThreadRng, Uniform<f64>> {
impl<const LEN: usize> RandomDistributionPerturbation<LEN, Uniform<f64>> {
    pub fn uniform(range: f64) -> Result<Self, uniform::Error> {
        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<f64, uniform::Error> {
        self.parameter = range;
        self.distribution = Uniform::new(-range/2.0, range/2.0)?;
        Ok(range)
    }
}

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

    fn perturb(self: &mut Self, chromosome: &Self::Chromosome) -> Self::Chromosome {


@@ 63,21 86,21 @@ impl<TRng: Rng, TDistribution: Distribution<f64>, const LEN: usize> Perturbation
    }
}

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

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

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

    fn perturb(self: &mut Self, chromosome: &Self::Chromosome) -> Self::Chromosome {