Skip to content

Commit

Permalink
Give a go at applying sparsification for Swap*.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoupey committed Oct 18, 2023
1 parent 18dbc7c commit 8f548cf
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 16 deletions.
3 changes: 2 additions & 1 deletion src/algorithms/local_search/local_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1650,7 +1650,8 @@ void LocalSearch<Route,
source,
_sol[target],
target,
best_gains[source][target]);
best_gains[source][target],
_sparsification_threshold);

if (best_gains[source][target] < r.gain()) {
best_gains[source][target] = r.gain();
Expand Down
15 changes: 12 additions & 3 deletions src/algorithms/local_search/swap_star_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,11 @@ SwapChoice compute_best_swap_star_choice(const Input& input,
const Route& source,
const Index t_vehicle,
const Route& target,
const Eval& best_known_gain) {
const Eval& best_known_gain,
Cost sparsification_threshold) {
// Preprocessing phase.
const Cost addition_cost_threshold = sparsification_threshold / 2;

std::vector<ThreeInsertions> top_insertions_in_target(source.route.size(),
empty_three_insertions);
for (unsigned s_rank = 0; s_rank < source.route.size(); ++s_rank) {
Expand All @@ -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);
}
}

Expand All @@ -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);
}
}

Expand Down
13 changes: 10 additions & 3 deletions src/algorithms/local_search/top_insertions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ void update_insertions(ThreeInsertions& insertions, InsertionOption&& option) {
template <class Route>
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;
Expand All @@ -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));
}

Expand All @@ -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
3 changes: 2 additions & 1 deletion src/algorithms/local_search/top_insertions.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ constexpr ThreeInsertions
template <class Route>
ThreeInsertions find_top_3_insertions(const Input& input,
Index j,
const Route& r);
const Route& r,
Cost addition_cost_threshold);

} // namespace vroom::ls

Expand Down
9 changes: 6 additions & 3 deletions src/problems/cvrp/operators/swap_star.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand All @@ -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;
}
Expand Down
4 changes: 3 additions & 1 deletion src/problems/cvrp/operators/swap_star.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down
9 changes: 6 additions & 3 deletions src/problems/vrptw/operators/swap_star.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<RawRoute&>(tw_s_route),
s_vehicle,
static_cast<RawRoute&>(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) {
}
Expand All @@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion src/problems/vrptw/operators/swap_star.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down

0 comments on commit 8f548cf

Please sign in to comment.