From 552bc452de76fd6760c9946c641a2de8217ff1d2 Mon Sep 17 00:00:00 2001 From: Lia Lenckowski Date: Thu, 28 Nov 2024 15:30:02 +0100 Subject: small minimum hamilt. path improvement: during & after 2-opt refinement, rotate longest edge to border --- src/tsp_approx.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/tsp_approx.rs') diff --git a/src/tsp_approx.rs b/src/tsp_approx.rs index d8c30fe..a23a726 100644 --- a/src/tsp_approx.rs +++ b/src/tsp_approx.rs @@ -377,6 +377,23 @@ fn refine_2_opt(dist_cache: &DistCache, tour: Vec) -> (bool, Vec) (improved, tour_flat) } +fn rotate_towards_optimum(dist_cache: &DistCache, tour: &mut [usize]) { + let mut max_dist = dist_cache.dist(tour[0], tour[tour.len() - 1]); + let mut max_dist_at = tour.len() - 1; + + for i in 0..tour.len() - 2 { + let next_dist = dist_cache.dist(tour[i], tour[i + 1]); + if next_dist > max_dist { + max_dist = next_dist; + max_dist_at = i; + } + } + + if max_dist_at != tour.len() - 1 { + tour.rotate_left(max_dist_at + 1); + } +} + fn get_dist_cache(embeds: &[M], hash_seed: &Option) -> DistCache where M: MetricElem, @@ -424,6 +441,7 @@ where }; for _ in 0..refinements { + rotate_towards_optimum(&dc, &mut tour); let res = refine_2_opt(&dc, tour); tour = res.1; if !res.0 { @@ -431,6 +449,8 @@ where } } + rotate_towards_optimum(&dc, &mut tour); + let mut total_dist = 0.; for i in 0..tour.len() - 1 { let (a, b) = (tour[i], tour[i + 1]); -- cgit v1.2.3-70-g09d2