From 038f5f6ceb88eac60cdbd3b6cbda7ed975cc8073 Mon Sep 17 00:00:00 2001 From: Rutherther Date: Tue, 28 Oct 2025 16:27:57 +0100 Subject: [PATCH] refactor: use swap_rows in SwapPerturbaiton instead of swapping by indices --- codes/tsp_hw01/src/tsp.rs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/codes/tsp_hw01/src/tsp.rs b/codes/tsp_hw01/src/tsp.rs index 4bbc06afa34ae1513099a6eeece727848c94bcd5..69809af67368eb5c697bbb02aafd333734286831 100644 --- a/codes/tsp_hw01/src/tsp.rs +++ b/codes/tsp_hw01/src/tsp.rs @@ -229,6 +229,12 @@ pub struct SwapPerturbation { _phantom: PhantomData } +impl SwapPerturbation { + pub fn new() -> Self { + Self { _phantom: PhantomData } + } +} + impl PerturbationOperator for SwapPerturbation where D: Dim, @@ -238,15 +244,8 @@ where type Chromosome = NodePermutation; 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); } }