From b88b11e7a937d17a1325cc9874a9b28d694767e1 Mon Sep 17 00:00:00 2001 From: mfunk Date: Fri, 10 Sep 2021 14:25:07 +0200 Subject: [PATCH] Replace cvrp::PDShift::compute_gain() --- src/problems/cvrp/operators/pd_shift.cpp | 86 ++++-------------------- 1 file changed, 14 insertions(+), 72 deletions(-) diff --git a/src/problems/cvrp/operators/pd_shift.cpp b/src/problems/cvrp/operators/pd_shift.cpp index 2c8e2461..f5570876 100644 --- a/src/problems/cvrp/operators/pd_shift.cpp +++ b/src/problems/cvrp/operators/pd_shift.cpp @@ -10,6 +10,8 @@ All rights reserved (see LICENSE). #include "problems/cvrp/operators/pd_shift.h" #include "utils/helpers.h" +#include "algorithms/local_search/insertion_search.h" + namespace vroom { namespace cvrp { @@ -37,79 +39,19 @@ PDShift::PDShift(const Input& input, } void PDShift::compute_gain() { - const auto& v = _input.vehicles[t_vehicle]; - - // For source vehicle, we consider the cost of removing P&D already - // stored in remove_gain. - - // For target vehicle, we go through all insertion options for - // pickup and delivery target ranks, storing best ranks along the - // way. - - std::vector t_d_gains(t_route.size() + 1); - for (unsigned t_d_rank = 0; t_d_rank <= t_route.size(); ++t_d_rank) { - t_d_gains[t_d_rank] = - -utils::addition_cost(_input, s_route[_s_d_rank], v, t_route, t_d_rank); - } - for (unsigned t_p_rank = 0; t_p_rank <= t_route.size(); ++t_p_rank) { - Gain t_p_gain = - -utils::addition_cost(_input, s_route[_s_p_rank], v, t_route, t_p_rank); - - if (_remove_gain + t_p_gain < stored_gain) { - // Even without delivery insertion, the gain is lower than best - // known stored gain. - continue; - } - - if (!target - .is_valid_addition_for_load(_input, - _input.jobs[s_route[_s_p_rank]].pickup, - t_p_rank)) { - continue; - } - - std::vector modified_with_pd({s_route[_s_p_rank]}); - Amount modified_delivery = _input.zero_amount(); - - for (unsigned t_d_rank = t_p_rank; t_d_rank <= t_route.size(); ++t_d_rank) { - Gain target_gain; - if (t_p_rank == t_d_rank) { - target_gain = -utils::addition_cost(_input, - s_route[_s_p_rank], - v, - t_route, - t_p_rank, - t_p_rank + 1); - } else { - target_gain = t_p_gain + t_d_gains[t_d_rank]; - modified_with_pd.push_back(t_route[t_d_rank - 1]); - const auto& new_modified_job = _input.jobs[t_route[t_d_rank - 1]]; - if (new_modified_job.type == JOB_TYPE::SINGLE) { - modified_delivery += new_modified_job.delivery; - } - } - - if (_remove_gain + target_gain > stored_gain) { - modified_with_pd.push_back(s_route[_s_d_rank]); - bool valid = - target - .is_valid_addition_for_capacity_inclusion(_input, - modified_delivery, - modified_with_pd.begin(), - modified_with_pd.end(), - t_p_rank, - t_d_rank); - modified_with_pd.pop_back(); - - if (valid) { - _valid = true; - stored_gain = _remove_gain + target_gain; - _best_t_p_rank = t_p_rank; - _best_t_d_rank = t_d_rank; - } - } - } + ls::RouteInsertion rs = + ls::compute_best_insertion_pd(_input, + s_route[_s_p_rank], + t_vehicle, + target, + _remove_gain - stored_gain); + + if (rs.cost < std::numeric_limits::max()) { + _valid = true; + stored_gain = _remove_gain - rs.cost; + _best_t_p_rank = rs.pickup_rank; + _best_t_d_rank = rs.delivery_rank; } gain_computed = true;