From ec7345a58c11d82038bf20c41484c27f07f3e4dc Mon Sep 17 00:00:00 2001 From: Rutherther Date: Sat, 1 Nov 2025 10:23:03 +0100 Subject: [PATCH] feat(lib): add n consecutive bit flip perturbation for binary string --- codes/eoa_lib/src/perturbation/mod.rs | 32 ++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/codes/eoa_lib/src/perturbation/mod.rs b/codes/eoa_lib/src/perturbation/mod.rs index 513432684c8b5d672f494050991b27c474ac8490..d8d9dd9429293f6aeca137f64db96d01133019b0 100644 --- a/codes/eoa_lib/src/perturbation/mod.rs +++ b/codes/eoa_lib/src/perturbation/mod.rs @@ -62,7 +62,7 @@ impl PerturbationOperator for IdentityPerturbation { - p: f64, + pub p: f64, _phantom: PhantomData } @@ -139,6 +139,36 @@ where } } +pub struct BinaryStringFlipNPerturbation { + n: usize, + _phantom: PhantomData, +} + +impl BinaryStringFlipNPerturbation { + pub fn new(n: usize) -> Self { + Self { + n, + _phantom: PhantomData + } + } +} + +impl PerturbationOperator for BinaryStringFlipNPerturbation +where + D: Dim, + DefaultAllocator: Allocator +{ + type Chromosome = BinaryString; + + fn perturb(&self, chromosome: &mut Self::Chromosome, rng: &mut dyn RngCore) { + let index = rng.random_range(0..chromosome.vec.len()); + + for i in index..(index + self.n).min(chromosome.vec.len()) { + chromosome.vec[i] = 1 - chromosome.vec[i]; + } + } +} + pub struct RandomDistributionPerturbation> { distribution: TDistribution, parameter: f64