@@ 240,6 240,44 @@ where
}
}
+pub struct MovePerturbation<D> {
+ _phantom: PhantomData<D>
+}
+
+impl<D> MovePerturbation<D> {
+ pub fn new() -> Self {
+ Self { _phantom: PhantomData }
+ }
+}
+
+impl<D> PerturbationOperator for MovePerturbation<D>
+where
+ D: Dim,
+ DefaultAllocator: Allocator<D, D>,
+ DefaultAllocator: Allocator<D>,
+{
+ type Chromosome = NodePermutation<D>;
+
+ fn perturb(&self, chromosome: &mut Self::Chromosome, rng: &mut dyn RngCore) {
+ let from = rng.random_range(0..chromosome.permutation.len());
+ let to = rng.random_range(0..chromosome.permutation.len());
+
+ let element = chromosome.permutation[from];
+
+ if from < to {
+ for i in from..to {
+ chromosome.permutation[i] = chromosome.permutation[i + 1];
+ }
+ } else {
+ for i in (to+1..=from).rev() {
+ chromosome.permutation[i] = chromosome.permutation[i - 1];
+ }
+ }
+
+ chromosome.permutation[to] = element;
+ }
+}
+
pub struct SwapPerturbation<D> {
_phantom: PhantomData<D>
}