From 98eb8ce716f5a14f77ac81fae1633bfadb0fae64 Mon Sep 17 00:00:00 2001 From: Rutherther Date: Fri, 31 Oct 2025 11:17:35 +0100 Subject: [PATCH] fix(tsp): reverse properly in reverse subsequence perturbatio --- codes/tsp_hw01/src/tsp.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/codes/tsp_hw01/src/tsp.rs b/codes/tsp_hw01/src/tsp.rs index 23c40d4e4cbca7e81d061c89e7c5ee319b35ebf4..f35838658ab6282f60fea71d62f65e11920f9b97 100644 --- a/codes/tsp_hw01/src/tsp.rs +++ b/codes/tsp_hw01/src/tsp.rs @@ -295,13 +295,15 @@ where let start = first.min(second); let end = first.max(second); - // Only to half of the interval to swap - let end = (end + start) / 2; - - for (i, j) in (start..=end).zip(end..=start) { - chromosome.permutation.swap_rows(i, j); + // Reverse the subsequence between start and end (inclusive) + let mut left = start; + let mut right = end; + + while left < right { + chromosome.permutation.swap_rows(left, right); + left += 1; + right -= 1; } - } }