Skip to content
Snippets Groups Projects
Commit 29677002 authored by awenjb's avatar awenjb
Browse files

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
parent abc43935
No related branches found
No related tags found
No related merge requests found
#include "sorting_strategy.h" #include "sorting_strategy.h"
#include "input/pdptw_data.h"
#include "input/data.h" #include "input/data.h"
#include "input/pdptw_data.h"
#include "utils.h" #include "utils.h"
#include <algorithm> #include <algorithm>
#include <ranges> #include <ranges>
double getDistanceToDepot(PDPTWData const &data, int pairID) double getDistanceToDepot(PDPTWData const &data, int pairID)
{ {
return data::TravelTime(data, 0, pairID); return data::TravelTime(data, 0, pairID);
} }
std::vector<int> const &sorting_strategy::Shuffle::sortPairs() const std::vector<int> const &sorting_strategy::Shuffle::sortPairs() const
{ {
auto &bank = getSolution().getPairBank(); auto &bank = getSolution().getPairBank();
...@@ -32,15 +30,17 @@ std::vector<int> const &sorting_strategy::Demand::sortPairs() const ...@@ -32,15 +30,17 @@ std::vector<int> const &sorting_strategy::Demand::sortPairs() const
return bank; 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 std::vector<int> const &sorting_strategy::Close::sortPairs() const
{ {
auto &bank = getSolution().getPairBank(); auto &bank = getSolution().getPairBank();
// Pair ID = Pickup ID // Pair ID = Pickup ID
std::sort(bank.begin(), bank.end(), [&](int a, int b) { 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; return bank;
} }
...@@ -50,7 +50,12 @@ std::vector<int> const &sorting_strategy::Far::sortPairs() const ...@@ -50,7 +50,12 @@ std::vector<int> const &sorting_strategy::Far::sortPairs() const
auto &bank = getSolution().getPairBank(); auto &bank = getSolution().getPairBank();
// Pair ID = Pickup ID // Pair ID = Pickup ID
std::sort(bank.begin(), bank.end(), [&](int a, int b) { 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; return bank;
} }
...@@ -60,7 +65,12 @@ std::vector<int> const &sorting_strategy::TimeWindowWidth::sortPairs() const ...@@ -60,7 +65,12 @@ std::vector<int> const &sorting_strategy::TimeWindowWidth::sortPairs() const
auto &bank = getSolution().getPairBank(); auto &bank = getSolution().getPairBank();
// Pair ID = Pickup ID // Pair ID = Pickup ID
std::sort(bank.begin(), bank.end(), [&](int a, int b) { 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; return bank;
} }
...@@ -70,7 +80,8 @@ std::vector<int> const &sorting_strategy::TimeWindowStart::sortPairs() const ...@@ -70,7 +80,8 @@ std::vector<int> const &sorting_strategy::TimeWindowStart::sortPairs() const
auto &bank = getSolution().getPairBank(); auto &bank = getSolution().getPairBank();
// Pair ID = Pickup ID // Pair ID = Pickup ID
std::sort(bank.begin(), bank.end(), [&](int a, int b) { 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; return bank;
} }
...@@ -80,7 +91,10 @@ std::vector<int> const &sorting_strategy::TimeWindowEnd::sortPairs() const ...@@ -80,7 +91,10 @@ std::vector<int> const &sorting_strategy::TimeWindowEnd::sortPairs() const
auto &bank = getSolution().getPairBank(); auto &bank = getSolution().getPairBank();
// Pair ID = Pickup ID // Pair ID = Pickup ID
std::sort(bank.begin(), bank.end(), [&](int a, int b) { 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; return bank;
} }
\ No newline at end of file
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
#include "lns/solution/solution.h" #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 enum class SortingStrategyType
{ {
...@@ -20,7 +20,7 @@ enum class SortingStrategyType ...@@ -20,7 +20,7 @@ enum class SortingStrategyType
namespace sorting_strategy 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 class SortingStrategy
{ {
...@@ -33,12 +33,12 @@ namespace sorting_strategy ...@@ -33,12 +33,12 @@ namespace sorting_strategy
virtual ~SortingStrategy() = default; virtual ~SortingStrategy() = default;
private: private:
// non const to sort in place // non const to sort in place.
Solution &solution; Solution &solution;
}; };
/** /**
* Shuffle the requests * Shuffle the requests.
*/ */
class Shuffle : public SortingStrategy class Shuffle : public SortingStrategy
{ {
...@@ -48,7 +48,7 @@ namespace sorting_strategy ...@@ -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 class Demand : public SortingStrategy
{ {
...@@ -58,7 +58,7 @@ namespace sorting_strategy ...@@ -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 class Close : public SortingStrategy
{ {
...@@ -68,7 +68,7 @@ namespace sorting_strategy ...@@ -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 class Far : public SortingStrategy
{ {
...@@ -78,7 +78,7 @@ namespace sorting_strategy ...@@ -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 class TimeWindowWidth : public SortingStrategy
{ {
...@@ -88,7 +88,7 @@ namespace sorting_strategy ...@@ -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 class TimeWindowStart : public SortingStrategy
{ {
...@@ -98,7 +98,7 @@ namespace sorting_strategy ...@@ -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 class TimeWindowEnd : public SortingStrategy
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment