From 88a1c34dc99bc09b2ad1e74d1866993a3b05ef0a Mon Sep 17 00:00:00 2001 From: awenjb <126257927+awenjb@users.noreply.github.com> Date: Thu, 20 Mar 2025 13:21:17 +0100 Subject: [PATCH] Add CleanEmptyRoute operator --- CMakeLists.txt | 1 + src/config.h | 2 +- src/lns/lns.cpp | 2 +- src/lns/modification/route/remove_route.cpp | 22 +++++++++++++------ src/lns/modification/route/remove_route.h | 3 +++ .../destruction/clean_empty_route.cpp | 16 ++++++++++++++ .../operators/destruction/clean_empty_route.h | 15 +++++++++++++ src/mains/main.cpp | 21 +++++++++++------- src/mains/main_interface.cpp | 7 +++++- 9 files changed, 71 insertions(+), 18 deletions(-) create mode 100644 src/lns/operators/destruction/clean_empty_route.cpp create mode 100644 src/lns/operators/destruction/clean_empty_route.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 673f879..888b4e7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,6 +38,7 @@ add_executable(pdptw src/mains/main.cpp src/output/solution_checker.cpp src/output/solution_exporter.cpp src/lns/operators/sorting_strategy.cpp + src/lns/operators/destruction/clean_empty_route.cpp src/lns/operators/destruction/random_destroy.cpp src/lns/operators/destruction/string_removal.cpp src/lns/operators/reconstruction/enumerate.cpp diff --git a/src/config.h b/src/config.h index 574c5e4..886eae3 100644 --- a/src/config.h +++ b/src/config.h @@ -4,4 +4,4 @@ const int EXCLUSION_PENALTY = 100; const int RANDOM_SEED = 100; -const int NUMBER_VEHICLE = 10; \ No newline at end of file +const int NUMBER_VEHICLE = 20; \ No newline at end of file diff --git a/src/lns/lns.cpp b/src/lns/lns.cpp index 5670a48..c82a637 100644 --- a/src/lns/lns.cpp +++ b/src/lns/lns.cpp @@ -80,7 +80,7 @@ output::LnsOutput lns::runLns(Solution const &initialSolution, OperatorSelector Solution actualSolution = initialSolution; LnsRuntimeData runtime = {actualSolution}; - // temporary + // temporary fixed iteration int iterationMax = 200; while (iterationMax > 0) { diff --git a/src/lns/modification/route/remove_route.cpp b/src/lns/modification/route/remove_route.cpp index aa08131..410a8f3 100644 --- a/src/lns/modification/route/remove_route.cpp +++ b/src/lns/modification/route/remove_route.cpp @@ -18,16 +18,19 @@ RemoveRoute::RemoveRoute(int routeIndex, std::vector<int> &&removedPairID) void RemoveRoute::modifySolution(Solution &solution) { std::vector<Route> &routes = solution.getRoutes(); - std::vector<int> const &locationIDs = routes.at(routeIndex).getRoute(); + if (routes.at(routeIndex).getRoute().empty()) + { + std::vector<int> const &locationIDs = routes.at(routeIndex).getRoute(); - // update removedPairID - removedPairID.reserve(routes.at(routeIndex).getSize()); + // update removedPairID + removedPairID.reserve(routes.at(routeIndex).getSize()); - for (int id : locationIDs) - { - if (solution.getData().getLocation(id).getLocType() == LocType::PICKUP) + for (int id: locationIDs) { - removedPairID.push_back(id); + if (solution.getData().getLocation(id).getLocType() == LocType::PICKUP) + { + removedPairID.push_back(id); + } } } routes.erase(routes.begin() + routeIndex); @@ -46,4 +49,9 @@ int RemoveRoute::getRouteIndex() const std::vector<int> const &RemoveRoute::getDeletedPairs() const { return removedPairID; +} + +ModificationApplyVariant RemoveRoute::asApplyVariant() const +{ + return *this; } \ No newline at end of file diff --git a/src/lns/modification/route/remove_route.h b/src/lns/modification/route/remove_route.h index 08fb830..81c0281 100644 --- a/src/lns/modification/route/remove_route.h +++ b/src/lns/modification/route/remove_route.h @@ -36,5 +36,8 @@ public: */ std::vector<int> const &getDeletedPairs() const override; + ModificationApplyVariant asApplyVariant() const override; + + int getRouteIndex() const; }; \ No newline at end of file diff --git a/src/lns/operators/destruction/clean_empty_route.cpp b/src/lns/operators/destruction/clean_empty_route.cpp new file mode 100644 index 0000000..aaeeede --- /dev/null +++ b/src/lns/operators/destruction/clean_empty_route.cpp @@ -0,0 +1,16 @@ +#include "clean_empty_route.h" +#include "lns/modification/route/remove_route.h" + +CleanEmptyRoute::CleanEmptyRoute() {} + +void CleanEmptyRoute::destroySolution(Solution &solution) const +{ + for (int routeIndex = solution.getRoutes().size() - 1; routeIndex >= 0; --routeIndex) + { + if (solution.getRoute(routeIndex).getRoute().empty()) + { + RemoveRoute remRoute = RemoveRoute(routeIndex); + solution.applyDestructSolution(remRoute); + } + } +} \ No newline at end of file diff --git a/src/lns/operators/destruction/clean_empty_route.h b/src/lns/operators/destruction/clean_empty_route.h new file mode 100644 index 0000000..ec7c934 --- /dev/null +++ b/src/lns/operators/destruction/clean_empty_route.h @@ -0,0 +1,15 @@ +#pragma once + +#include "lns/operators/abstract_operator.h" + +class CleanEmptyRoute : public DestructionOperator +{ +public: + explicit CleanEmptyRoute(); + + /** + * This operator removes empty routes from the solution. + */ + void destroySolution(Solution &solution) const override; + +}; diff --git a/src/mains/main.cpp b/src/mains/main.cpp index 6228eab..ddbdd01 100644 --- a/src/mains/main.cpp +++ b/src/mains/main.cpp @@ -13,6 +13,7 @@ #include "lns/modification/route/insert_route.h" #include "lns/modification/route/remove_route.h" #include "lns/operators/abstract_operator.h" +#include "lns/operators/destruction/clean_empty_route.h" #include "lns/operators/destruction/random_destroy.h" #include "lns/operators/destruction/string_removal.h" #include "lns/operators/reconstruction/enumerate.h" @@ -47,13 +48,17 @@ void simpleLNS(PDPTWData const &data, Solution &startingSolution) ThresholdAcceptance acceptor(0.05); // lns operators - SimpleOperatorSelector RandomDestroy_ShuffleBestInsert; - addAllReconstructor(RandomDestroy_ShuffleBestInsert); - RandomDestroy_ShuffleBestInsert.addDestructor(RandomDestroy(pairs)); + SimpleOperatorSelector RandomDestroy_BestInsert; + addAllReconstructor(RandomDestroy_BestInsert); + RandomDestroy_BestInsert.addDestructor(RandomDestroy(pairs)); + RandomDestroy_BestInsert.addDestructor(StringRemoval(10,10)); + RandomDestroy_BestInsert.addDestructor(CleanEmptyRoute()); SimpleOperatorSelector largeSelector; addAllReconstructor(largeSelector); - largeSelector.addDestructor(RandomDestroy(pairs)); + largeSelector.addDestructor(RandomDestroy(manyPairs)); + largeSelector.addDestructor(StringRemoval(10,10)); + largeSelector.addDestructor(CleanEmptyRoute()); // SimpleOperatorSelector veryLargeSelector; // addAllReconstructor(veryLargeSelector); @@ -68,8 +73,8 @@ void simpleLNS(PDPTWData const &data, Solution &startingSolution) // lastSelector.addDestructor(RandomDestroy(manyPairs)); std::vector<SmallLargeOperatorSelector::StepSelector> selectors; - selectors.emplace_back(10, std::move(RandomDestroy_ShuffleBestInsert)); - // selectors.emplace_back(100, std::move(largeSelector)); + selectors.emplace_back(10, std::move(RandomDestroy_BestInsert)); + selectors.emplace_back(50, std::move(largeSelector)); // selectors.emplace_back(2, std::move(veryLargeSelector)); // selectors.emplace_back(2, std::move(hugeSelector)); // selectors.emplace_back(2, std::move(lastSelector)); @@ -78,7 +83,6 @@ void simpleLNS(PDPTWData const &data, Solution &startingSolution) // run lns output::LnsOutput result = lns::runLns(startingSolution, smallLargeSelector, acceptor); - result.getBestSolution().print(); std::cout << result.getNumberOfIteration() << " " << result.getTimeSpent() << std::endl; } @@ -90,7 +94,7 @@ int main(int argc, char **argv) /////////////////////////////////////////////////////////////////////// //std::string filepath = "/home/a24jacqb/Documents/Code/pdptw-main/data_in/n100/bar-n100-1.json"; - std::string filepath = "/home/a24jacqb/Documents/Code/pdptw-main/data_in/pdp_100/lc101.json"; + std::string filepath = "/home/a24jacqb/Documents/Code/pdptw-main/data_in/pdp_100/lrc201.json"; //std::string filepath = "/home/a24jacqb/Documents/Code/pdptw-main/data_in/Nantes_1.json"; //std::string filepath = "/home/a24jacqb/Documents/Code/pdptw-main/data_in/n5000/bar-n5000-1.json"; @@ -103,6 +107,7 @@ int main(int argc, char **argv) simpleLNS(data, startingSolution); + /// // std::cout << "===== TEST ===== \n"; // Solution testSolution = Solution::emptySolution(data); diff --git a/src/mains/main_interface.cpp b/src/mains/main_interface.cpp index aeae80f..2024a19 100644 --- a/src/mains/main_interface.cpp +++ b/src/mains/main_interface.cpp @@ -8,7 +8,12 @@ void addAllReconstructor(SimpleOperatorSelector &selector) { - selector.addReconstructor(ListHeuristicCostOriented(SortingStrategyType::SHUFFLE, EnumerationType::ALL_INSERT_PAIR)); + selector.addReconstructor(ListHeuristicCostOriented(SortingStrategyType::SHUFFLE, EnumerationType::ALL_INSERT_PAIR), 1); + selector.addReconstructor(ListHeuristicCostOriented(SortingStrategyType::FAR, EnumerationType::ALL_INSERT_PAIR), 1); + selector.addReconstructor(ListHeuristicCostOriented(SortingStrategyType::CLOSE, EnumerationType::ALL_INSERT_PAIR), 1); + selector.addReconstructor(ListHeuristicCostOriented(SortingStrategyType::TWWIDTH, EnumerationType::ALL_INSERT_PAIR), 1); + selector.addReconstructor(ListHeuristicCostOriented(SortingStrategyType::TWSTART, EnumerationType::ALL_INSERT_PAIR), 1); + selector.addReconstructor(ListHeuristicCostOriented(SortingStrategyType::TWEND, EnumerationType::ALL_INSERT_PAIR), 1); } int mainInterface(int argc, char **argv, std::function<void(PDPTWData &, Solution &)> function) -- GitLab