~ruther/ctu-fee-eoa

b39687d45e3132a97454a0e72a080e02708bc23c — Rutherther a month ago 7adc581
feat(binary_string): add single bit perturbation, flip perturbation
1 files changed, 52 insertions(+), 0 deletions(-)

M codes/eoa_lib/src/perturbation/mod.rs
M codes/eoa_lib/src/perturbation/mod.rs => codes/eoa_lib/src/perturbation/mod.rs +52 -0
@@ 38,6 38,58 @@ where
    }
}

pub struct BinaryStringSingleBitPerturbation<D> {
    _phantom: PhantomData<D>
}

impl<D> BinaryStringSingleBitPerturbation<D> {
    pub fn new() -> Self {
        Self {
            _phantom: PhantomData
        }
    }
}

impl<D> PerturbationOperator for BinaryStringSingleBitPerturbation<D>
where
    D: Dim,
    DefaultAllocator: Allocator<D>
{
    type Chromosome = BinaryString<D>;

    fn perturb(&self, chromosome: &mut Self::Chromosome, rng: &mut dyn RngCore) {
        let bit_range = 0..chromosome.vec.len();
        let flip_bit = rng.random_range(bit_range);

        chromosome.vec[flip_bit] = 1 - chromosome.vec[flip_bit];
    }
}

pub struct BinaryStringFlipPerturbation<D> {
    _phantom: PhantomData<D>
}

impl<D> BinaryStringFlipPerturbation<D> {
    pub fn new() -> Self {
        Self {
            _phantom: PhantomData
        }
    }
}

impl<D> PerturbationOperator for BinaryStringFlipPerturbation<D>
where
    D: Dim,
    DefaultAllocator: Allocator<D>
{
    type Chromosome = BinaryString<D>;

    fn perturb(&self, chromosome: &mut Self::Chromosome, _: &mut dyn RngCore) {
        chromosome.vec
            .apply(|c| *c = 1 - *c);
    }
}

pub struct RandomDistributionPerturbation<const LEN: usize, TDistribution: Distribution<f64>> {
    distribution: TDistribution,
    parameter: f64