Skip to content

Commit

Permalink
Replace cvrp::PDShift::compute_gain()
Browse files Browse the repository at this point in the history
  • Loading branch information
krypt-n committed Sep 10, 2021
1 parent e42c3b3 commit b88b11e
Showing 1 changed file with 14 additions and 72 deletions.
86 changes: 14 additions & 72 deletions src/problems/cvrp/operators/pd_shift.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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<Gain> 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<Index> 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<Gain>::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;
Expand Down

0 comments on commit b88b11e

Please sign in to comment.