diff --git a/CMakeLists.txt b/CMakeLists.txt index 2edbfcd3398e760a527838bae587fbdf8a664670..80c9ac985e391ef04812f47cf720329c8cbea4f5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,6 +34,7 @@ add_executable(pdptw src/mains/main.cpp src/lns/modification/route/insert_route.cpp src/lns/modification/route/remove_route.cpp src/output/solution_checker.cpp + src/lns/operators/sorting_strategy.cpp src/lns/operators/destruction/random_destroy.cpp src/lns/operators/selector/operator_selection.cpp src/utils.cpp diff --git a/src/lns/operators/destruction/random_destroy.h b/src/lns/operators/destruction/random_destroy.h index c7e6185adf29fe165641dd7f804335173d9c6cfc..5c813a5d2522fc539bb4208434984686dd95462b 100644 --- a/src/lns/operators/destruction/random_destroy.h +++ b/src/lns/operators/destruction/random_destroy.h @@ -5,9 +5,7 @@ class RandomDestroy : public DestructionOperator { public: - explicit RandomDestroy(int numberOfPairsToDestroy) - : numberOfPairsToDestroy(numberOfPairsToDestroy) - {} + explicit RandomDestroy(int numberOfPairsToDestroy) : numberOfPairsToDestroy(numberOfPairsToDestroy) {} /** * This operator removes numberOfPairsToDestroy pairs randomly in the solution. diff --git a/src/lns/operators/sorting_strategy.cpp b/src/lns/operators/sorting_strategy.cpp new file mode 100644 index 0000000000000000000000000000000000000000..08a16296a5fa064b68a205e56a014a14147ae289 --- /dev/null +++ b/src/lns/operators/sorting_strategy.cpp @@ -0,0 +1,13 @@ +#include "sorting_strategy.h" + +#include "utils.h" + +#include <algorithm> +#include <ranges> + +std::vector<int> const &sorting_strategy::Shuffle::sortRequests() const +{ + auto &bank = getSolution().getPairBank(); + std::ranges::shuffle(bank, util::getRawRandom()); + return bank; +} \ No newline at end of file diff --git a/src/lns/operators/sorting_strategy.h b/src/lns/operators/sorting_strategy.h new file mode 100644 index 0000000000000000000000000000000000000000..55f46b4b31607b80c409add57aff9e068f03ea0b --- /dev/null +++ b/src/lns/operators/sorting_strategy.h @@ -0,0 +1,37 @@ +#pragma once + +#include "lns/solution/solution.h" + +namespace sorting_strategy +{ + /** + * Interface for sorting the requests in the request bank. Does modify directly the solution request bank + */ + class SortingStrategy + { + public: + explicit SortingStrategy(Solution &solution) : solution(solution) {} + + Solution &getSolution() const { return solution; } + + virtual std::vector<int> const &sortRequests() const = 0; + virtual ~SortingStrategy() = default; + + private: + // non const to sort in place + Solution &solution; + }; + + /** + * Shuffle the requests + */ + class Shuffle : public SortingStrategy + { + using SortingStrategy::SortingStrategy; + + public: + std::vector<int> const &sortRequests() const override; + }; + + +}// namespace sorting_strategy \ No newline at end of file diff --git a/src/lns/solution/solution.cpp b/src/lns/solution/solution.cpp index f996203ae4d26b110a5fa32f97f89e6678ef313f..f24844fe15642d69d03b8bd858150d923d819c7f 100644 --- a/src/lns/solution/solution.cpp +++ b/src/lns/solution/solution.cpp @@ -83,6 +83,16 @@ Solution::PairBank const &Solution::getBank() const return pairBank; } +Solution::PairBank const &Solution::getPairBank() const +{ + return pairBank; +} + +Solution::PairBank &Solution::getPairBank() +{ + return pairBank; +} + std::vector<Route> const &Solution::getRoutes() const { return routes; diff --git a/src/lns/solution/solution.h b/src/lns/solution/solution.h index 31affb5ed7557ac59ca156a72232c74bcfbef4f7..d5c1cd55f9f28c2d228cc4109865ef69bb3354fa 100644 --- a/src/lns/solution/solution.h +++ b/src/lns/solution/solution.h @@ -54,6 +54,8 @@ public: PairBank const &getBank() const; + PairBank const &getPairBank() const; + PairBank &getPairBank(); std::vector<Route> const &getRoutes() const; Route const &getRoute(int routeIndex) const; PDPTWData const &getData() const; diff --git a/src/utils.cpp b/src/utils.cpp index 6ab381b9b788f2b9551f92f5e70d27581a79ee7a..9b61c3ff12cdf64552c0de9a7b47c320e38e3aa5 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -1,4 +1,5 @@ #include "utils.h" + #include "config.h" #include <random> @@ -10,7 +11,6 @@ namespace// anonymous namespace std::uniform_real_distribution<> distribution(0, 1); }// namespace - double util::getRandom() { if (!seedSet) [[unlikely]] @@ -30,3 +30,13 @@ unsigned int util::getRandomInt(unsigned int min, unsigned int max) } return std::uniform_int_distribution<>(min, max)(randomGenerator); } + +std::mt19937_64 &util::getRawRandom() +{ + if (!seedSet) [[unlikely]] + { + randomGenerator.seed(RANDOM_SEED); + seedSet = true; + } + return randomGenerator; +} diff --git a/src/utils.h b/src/utils.h index 323fe4f2b22c3fa8941a7e2b9f851a9b13871a23..f5048cd223f2f53b093dc8ae173d626046ce9cef 100644 --- a/src/utils.h +++ b/src/utils.h @@ -19,6 +19,9 @@ namespace util * @return a random integer number between min (included) and max (included) */ unsigned int getRandomInt(unsigned int min, unsigned int max); - + /** + * @return the random generator directly, it is a deterministic random + */ + std::mt19937_64 &getRawRandom(); }// namespace util