@@ 62,7 62,7 @@ impl<TChromosome: 'static> PerturbationOperator for IdentityPerturbation<TChromo
}
pub struct BinaryStringBitPerturbation<D> {
- p: f64,
+ pub p: f64,
_phantom: PhantomData<D>
}
@@ 139,6 139,36 @@ where
}
}
+pub struct BinaryStringFlipNPerturbation<D> {
+ n: usize,
+ _phantom: PhantomData<D>,
+}
+
+impl<D> BinaryStringFlipNPerturbation<D> {
+ pub fn new(n: usize) -> Self {
+ Self {
+ n,
+ _phantom: PhantomData
+ }
+ }
+}
+
+impl<D> PerturbationOperator for BinaryStringFlipNPerturbation<D>
+where
+ D: Dim,
+ DefaultAllocator: Allocator<D>
+{
+ type Chromosome = BinaryString<D>;
+
+ 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<const LEN: usize, TDistribution: Distribution<f64>> {
distribution: TDistribution,
parameter: f64