From b39687d45e3132a97454a0e72a080e02708bc23c Mon Sep 17 00:00:00 2001 From: Rutherther Date: Tue, 28 Oct 2025 10:53:53 +0100 Subject: [PATCH] feat(binary_string): add single bit perturbation, flip perturbation --- codes/eoa_lib/src/perturbation/mod.rs | 52 +++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/codes/eoa_lib/src/perturbation/mod.rs b/codes/eoa_lib/src/perturbation/mod.rs index 492a3ec272ed93ab3fea929ae78b74f3ce74a36f..19f53273c7020293c9e6d8f756f48114ee4b756d 100644 --- a/codes/eoa_lib/src/perturbation/mod.rs +++ b/codes/eoa_lib/src/perturbation/mod.rs @@ -38,6 +38,58 @@ where } } +pub struct BinaryStringSingleBitPerturbation { + _phantom: PhantomData +} + +impl BinaryStringSingleBitPerturbation { + pub fn new() -> Self { + Self { + _phantom: PhantomData + } + } +} + +impl PerturbationOperator for BinaryStringSingleBitPerturbation +where + D: Dim, + DefaultAllocator: Allocator +{ + type Chromosome = BinaryString; + + 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 { + _phantom: PhantomData +} + +impl BinaryStringFlipPerturbation { + pub fn new() -> Self { + Self { + _phantom: PhantomData + } + } +} + +impl PerturbationOperator for BinaryStringFlipPerturbation +where + D: Dim, + DefaultAllocator: Allocator +{ + type Chromosome = BinaryString; + + fn perturb(&self, chromosome: &mut Self::Chromosome, _: &mut dyn RngCore) { + chromosome.vec + .apply(|c| *c = 1 - *c); + } +} + pub struct RandomDistributionPerturbation> { distribution: TDistribution, parameter: f64