@@ 229,6 229,12 @@ pub struct SwapPerturbation<D> {
_phantom: PhantomData<D>
}
+impl<D> SwapPerturbation<D> {
+ pub fn new() -> Self {
+ Self { _phantom: PhantomData }
+ }
+}
+
impl<D> PerturbationOperator for SwapPerturbation<D>
where
D: Dim,
@@ 238,15 244,8 @@ where
type Chromosome = NodePermutation<D>;
fn perturb(&self, chromosome: &mut Self::Chromosome, rng: &mut dyn RngCore) {
- let first = rng.random_range(0..=chromosome.permutation.len());
- let second = rng.random_range(0..=chromosome.permutation.len());
-
- (
- chromosome.permutation[first],
- chromosome.permutation[second]
- ) = (
- chromosome.permutation[second],
- chromosome.permutation[first]
- );
+ let first = rng.random_range(0..chromosome.permutation.len());
+ let second = rng.random_range(0..chromosome.permutation.len());
+ chromosome.permutation.swap_rows(first, second);
}
}