From 8f548cfbefdaf67aa13f7efbe2b8c49885dede8d Mon Sep 17 00:00:00 2001 From: jcoupey Date: Wed, 18 Oct 2023 14:48:46 +0200 Subject: [PATCH] Give a go at applying sparsification for Swap*. --- src/algorithms/local_search/local_search.cpp | 3 ++- src/algorithms/local_search/swap_star_utils.h | 15 ++++++++++++--- src/algorithms/local_search/top_insertions.cpp | 13 ++++++++++--- src/algorithms/local_search/top_insertions.h | 3 ++- src/problems/cvrp/operators/swap_star.cpp | 9 ++++++--- src/problems/cvrp/operators/swap_star.h | 4 +++- src/problems/vrptw/operators/swap_star.cpp | 9 ++++++--- src/problems/vrptw/operators/swap_star.h | 3 ++- 8 files changed, 43 insertions(+), 16 deletions(-) diff --git a/src/algorithms/local_search/local_search.cpp b/src/algorithms/local_search/local_search.cpp index e9c9ee787..9d6c9e9c4 100644 --- a/src/algorithms/local_search/local_search.cpp +++ b/src/algorithms/local_search/local_search.cpp @@ -1650,7 +1650,8 @@ void LocalSearch top_insertions_in_target(source.route.size(), empty_three_insertions); for (unsigned s_rank = 0; s_rank < source.route.size(); ++s_rank) { @@ -186,7 +189,10 @@ SwapChoice compute_best_swap_star_choice(const Input& input, if (input.jobs[source_job_rank].type == JOB_TYPE::SINGLE && input.vehicle_ok_with_job(t_vehicle, source_job_rank)) { top_insertions_in_target[s_rank] = - find_top_3_insertions(input, source_job_rank, target); + find_top_3_insertions(input, + source_job_rank, + target, + addition_cost_threshold); } } @@ -198,7 +204,10 @@ SwapChoice compute_best_swap_star_choice(const Input& input, if (input.jobs[target_job_rank].type == JOB_TYPE::SINGLE && input.vehicle_ok_with_job(s_vehicle, target_job_rank)) { top_insertions_in_source[t_rank] = - find_top_3_insertions(input, target_job_rank, source); + find_top_3_insertions(input, + target_job_rank, + source, + addition_cost_threshold); } } diff --git a/src/algorithms/local_search/top_insertions.cpp b/src/algorithms/local_search/top_insertions.cpp index 0daa0d03f..6b3b125e1 100644 --- a/src/algorithms/local_search/top_insertions.cpp +++ b/src/algorithms/local_search/top_insertions.cpp @@ -33,7 +33,8 @@ void update_insertions(ThreeInsertions& insertions, InsertionOption&& option) { template ThreeInsertions find_top_3_insertions(const Input& input, Index j, - const Route& r) { + const Route& r, + Cost addition_cost_threshold) { const auto& v = input.vehicles[r.vehicle_rank]; auto best_insertions = empty_three_insertions; @@ -42,6 +43,10 @@ ThreeInsertions find_top_3_insertions(const Input& input, InsertionOption current_insert = {utils::addition_cost(input, j, v, r.route, rank), rank}; + if (current_insert.cost.cost > addition_cost_threshold) { + continue; + } + update_insertions(best_insertions, std::move(current_insert)); } @@ -50,10 +55,12 @@ ThreeInsertions find_top_3_insertions(const Input& input, template ThreeInsertions find_top_3_insertions(const Input& input, Index j, - const RawRoute& r); + const RawRoute& r, + Cost addition_cost_threshold); template ThreeInsertions find_top_3_insertions(const Input& input, Index j, - const TWRoute& r); + const TWRoute& r, + Cost addition_cost_threshold); } // namespace vroom::ls diff --git a/src/algorithms/local_search/top_insertions.h b/src/algorithms/local_search/top_insertions.h index 94128bfd1..28b7d9dae 100644 --- a/src/algorithms/local_search/top_insertions.h +++ b/src/algorithms/local_search/top_insertions.h @@ -28,7 +28,8 @@ constexpr ThreeInsertions template ThreeInsertions find_top_3_insertions(const Input& input, Index j, - const Route& r); + const Route& r, + Cost addition_cost_threshold); } // namespace vroom::ls diff --git a/src/problems/cvrp/operators/swap_star.cpp b/src/problems/cvrp/operators/swap_star.cpp index 62e521e40..99adf45b0 100644 --- a/src/problems/cvrp/operators/swap_star.cpp +++ b/src/problems/cvrp/operators/swap_star.cpp @@ -17,7 +17,8 @@ SwapStar::SwapStar(const Input& input, Index s_vehicle, RawRoute& t_route, Index t_vehicle, - const Eval& best_known_gain) + const Eval& best_known_gain, + Cost sparsification_threshold) // Use dummy 0 values for unused ranks. : Operator(OperatorName::SwapStar, input, @@ -28,7 +29,8 @@ SwapStar::SwapStar(const Input& input, t_route, t_vehicle, 0), - _best_known_gain(best_known_gain) { + _best_known_gain(best_known_gain), + _sparsification_threshold(sparsification_threshold) { assert(s_vehicle != t_vehicle); assert(s_route.size() >= 1); assert(t_route.size() >= 1); @@ -42,7 +44,8 @@ void SwapStar::compute_gain() { source, t_vehicle, target, - _best_known_gain); + _best_known_gain, + _sparsification_threshold); if (choice.gain.cost > 0) { stored_gain = choice.gain; } diff --git a/src/problems/cvrp/operators/swap_star.h b/src/problems/cvrp/operators/swap_star.h index fdce5f378..dc0974998 100644 --- a/src/problems/cvrp/operators/swap_star.h +++ b/src/problems/cvrp/operators/swap_star.h @@ -18,6 +18,7 @@ namespace vroom::cvrp { class SwapStar : public ls::Operator { protected: const Eval _best_known_gain; + const Cost _sparsification_threshold; ls::SwapChoice choice; void compute_gain() override; @@ -29,7 +30,8 @@ class SwapStar : public ls::Operator { Index s_vehicle, RawRoute& t_route, Index t_vehicle, - const Eval& best_known_gain); + const Eval& best_known_gain, + Cost sparsification_threshold); bool is_valid() override; diff --git a/src/problems/vrptw/operators/swap_star.cpp b/src/problems/vrptw/operators/swap_star.cpp index 736d9bf3b..c2cecdac3 100644 --- a/src/problems/vrptw/operators/swap_star.cpp +++ b/src/problems/vrptw/operators/swap_star.cpp @@ -17,14 +17,16 @@ SwapStar::SwapStar(const Input& input, Index s_vehicle, TWRoute& tw_t_route, Index t_vehicle, - const Eval& best_known_gain) + const Eval& best_known_gain, + Cost sparsification_threshold) : cvrp::SwapStar(input, sol_state, static_cast(tw_s_route), s_vehicle, static_cast(tw_t_route), t_vehicle, - best_known_gain), + best_known_gain, + sparsification_threshold), _tw_s_route(tw_s_route), _tw_t_route(tw_t_route) { } @@ -38,7 +40,8 @@ void SwapStar::compute_gain() { _tw_s_route, t_vehicle, _tw_t_route, - _best_known_gain); + _best_known_gain, + _sparsification_threshold); if (choice.gain.cost > 0) { stored_gain = choice.gain; } diff --git a/src/problems/vrptw/operators/swap_star.h b/src/problems/vrptw/operators/swap_star.h index 5e59d1f6d..195fb0710 100644 --- a/src/problems/vrptw/operators/swap_star.h +++ b/src/problems/vrptw/operators/swap_star.h @@ -28,7 +28,8 @@ class SwapStar : public cvrp::SwapStar { Index s_vehicle, TWRoute& tw_t_route, Index t_vehicle, - const Eval& best_known_gain); + const Eval& best_known_gain, + Cost sparsification_threshold); void apply() override; };