From 296770023ffe0bcec0b9cc3fe35d8c1aa4a13ca6 Mon Sep 17 00:00:00 2001 From: awenjb <126257927+awenjb@users.noreply.github.com> Date: Mon, 17 Mar 2025 10:03:04 +0100 Subject: [PATCH] Adapt sorting strategies Requests are divided into two locations; we are adapting current sorting methods to this criterion. - Sort by width: take the average of the two time windows (pickup and delivery) - End: take the end of the delivery - Start: take the start of the pickup - Distance: take the average of the two locations --- src/lns/operators/sorting_strategy.cpp | 36 ++++++++++++++++++-------- src/lns/operators/sorting_strategy.h | 20 +++++++------- 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/src/lns/operators/sorting_strategy.cpp b/src/lns/operators/sorting_strategy.cpp index babc1a8..beca6f5 100644 --- a/src/lns/operators/sorting_strategy.cpp +++ b/src/lns/operators/sorting_strategy.cpp @@ -1,19 +1,17 @@ #include "sorting_strategy.h" -#include "input/pdptw_data.h" #include "input/data.h" +#include "input/pdptw_data.h" #include "utils.h" #include <algorithm> #include <ranges> - double getDistanceToDepot(PDPTWData const &data, int pairID) { return data::TravelTime(data, 0, pairID); } - std::vector<int> const &sorting_strategy::Shuffle::sortPairs() const { auto &bank = getSolution().getPairBank(); @@ -32,15 +30,17 @@ std::vector<int> const &sorting_strategy::Demand::sortPairs() const return bank; } -// Following sorting strategy are based on the pickup, TO DO, sort based on the pickup and the delivery - - std::vector<int> const &sorting_strategy::Close::sortPairs() const { auto &bank = getSolution().getPairBank(); // Pair ID = Pickup ID std::sort(bank.begin(), bank.end(), [&](int a, int b) { - return getDistanceToDepot(getSolution().getData(), a) < getDistanceToDepot(getSolution().getData(), b); + return (getDistanceToDepot(getSolution().getData(), a) + + getDistanceToDepot(getSolution().getData(), getSolution().getData().getLocation(a).getPair())) / + 2 < + (getDistanceToDepot(getSolution().getData(), b) + + getDistanceToDepot(getSolution().getData(), getSolution().getData().getLocation(b).getPair())) / + 2; }); return bank; } @@ -50,7 +50,12 @@ std::vector<int> const &sorting_strategy::Far::sortPairs() const auto &bank = getSolution().getPairBank(); // Pair ID = Pickup ID std::sort(bank.begin(), bank.end(), [&](int a, int b) { - return getDistanceToDepot(getSolution().getData(), a) > getDistanceToDepot(getSolution().getData(), b); + return (getDistanceToDepot(getSolution().getData(), a) + + getDistanceToDepot(getSolution().getData(), getSolution().getData().getLocation(a).getPair())) / + 2 > + (getDistanceToDepot(getSolution().getData(), b) + + getDistanceToDepot(getSolution().getData(), getSolution().getData().getLocation(b).getPair())) / + 2; }); return bank; } @@ -60,7 +65,12 @@ std::vector<int> const &sorting_strategy::TimeWindowWidth::sortPairs() const auto &bank = getSolution().getPairBank(); // Pair ID = Pickup ID std::sort(bank.begin(), bank.end(), [&](int a, int b) { - return getSolution().getData().getLocation(a).getTimeWindow().getWidth() < getSolution().getData().getLocation(b).getTimeWindow().getWidth(); + const Location &locA = getSolution().getData().getLocation(a); + const Location &locB = getSolution().getData().getLocation(b); + return locA.getTimeWindow().getWidth() + + getSolution().getData().getLocation(locA.getPair()).getTimeWindow().getWidth() / 2 < + locB.getTimeWindow().getWidth() + + getSolution().getData().getLocation(locB.getPair()).getTimeWindow().getWidth() / 2; }); return bank; } @@ -70,7 +80,8 @@ std::vector<int> const &sorting_strategy::TimeWindowStart::sortPairs() const auto &bank = getSolution().getPairBank(); // Pair ID = Pickup ID std::sort(bank.begin(), bank.end(), [&](int a, int b) { - return getSolution().getData().getLocation(a).getTimeWindow().getStart() < getSolution().getData().getLocation(b).getTimeWindow().getStart(); + return getSolution().getData().getLocation(a).getTimeWindow().getStart() < + getSolution().getData().getLocation(b).getTimeWindow().getStart(); }); return bank; } @@ -80,7 +91,10 @@ std::vector<int> const &sorting_strategy::TimeWindowEnd::sortPairs() const auto &bank = getSolution().getPairBank(); // Pair ID = Pickup ID std::sort(bank.begin(), bank.end(), [&](int a, int b) { - return getSolution().getData().getLocation(a).getTimeWindow().getEnd() > getSolution().getData().getLocation(b).getTimeWindow().getEnd(); + const Location &locA = getSolution().getData().getLocation(a); + const Location &locB = getSolution().getData().getLocation(b); + return getSolution().getData().getLocation(locA.getPair()).getTimeWindow().getEnd() > + getSolution().getData().getLocation(locB.getPair()).getTimeWindow().getEnd(); }); return bank; } \ No newline at end of file diff --git a/src/lns/operators/sorting_strategy.h b/src/lns/operators/sorting_strategy.h index 6624619..041bfff 100644 --- a/src/lns/operators/sorting_strategy.h +++ b/src/lns/operators/sorting_strategy.h @@ -4,7 +4,7 @@ #include "lns/solution/solution.h" /** - * A type of sorting strategy for the bank of pairs + * A type of sorting strategy for the bank of pairs. */ enum class SortingStrategyType { @@ -20,7 +20,7 @@ enum class SortingStrategyType namespace sorting_strategy { /** - * Interface for sorting the requests in the request bank. Does modify directly the solution request bank + * Interface for sorting the requests in the request bank. Does modify directly the solution request bank. */ class SortingStrategy { @@ -33,12 +33,12 @@ namespace sorting_strategy virtual ~SortingStrategy() = default; private: - // non const to sort in place + // non const to sort in place. Solution &solution; }; /** - * Shuffle the requests + * Shuffle the requests. */ class Shuffle : public SortingStrategy { @@ -48,7 +48,7 @@ namespace sorting_strategy }; /** - * Sort the bank by decreasing order of demand + * Sort the bank by decreasing order of demand. */ class Demand : public SortingStrategy { @@ -58,7 +58,7 @@ namespace sorting_strategy }; /** - * Sort the bank by increasing distance from the depot + * Sort the bank by increasing distance from the depot. */ class Close : public SortingStrategy { @@ -68,7 +68,7 @@ namespace sorting_strategy }; /** - * Sort the bank by decreasing distance from the depot + * Sort the bank by decreasing distance from the depot. */ class Far : public SortingStrategy { @@ -78,7 +78,7 @@ namespace sorting_strategy }; /** - * Sort the bank by increasing time window width + * Sort the bank by increasing time window width. */ class TimeWindowWidth : public SortingStrategy { @@ -88,7 +88,7 @@ namespace sorting_strategy }; /** - * Sort the bank by inscreasing time window start + * Sort the bank by inscreasing time window start (compare the pickup time window). */ class TimeWindowStart : public SortingStrategy { @@ -98,7 +98,7 @@ namespace sorting_strategy }; /** - * Sort the bank by decreasing time window end + * Sort the bank by decreasing time window end (compare the delivery time window). */ class TimeWindowEnd : public SortingStrategy { -- GitLab