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

Add new selector

- new selector for two phase algorithm with route minimisation then classical SLNS
parent 6d12b765
No related branches found
No related tags found
No related merge requests found
...@@ -47,6 +47,7 @@ add_executable(pdptw src/mains/main.cpp ...@@ -47,6 +47,7 @@ add_executable(pdptw src/mains/main.cpp
src/lns/operators/reconstruction/list_heuristic_cost_oriented.cpp src/lns/operators/reconstruction/list_heuristic_cost_oriented.cpp
src/lns/operators/selector/operator_selector.cpp src/lns/operators/selector/operator_selector.cpp
src/lns/operators/selector/small_large_selector.cpp src/lns/operators/selector/small_large_selector.cpp
src/lns/operators/selector/min_small_large_selector.cpp
src/lns/lns.cpp src/lns/lns.cpp
src/utils.cpp src/utils.cpp
src/output/run.cpp src/output/run.cpp
......
...@@ -101,7 +101,7 @@ def save_to_json(output_dir, filename, size, capacity, depot, locations, distanc ...@@ -101,7 +101,7 @@ def save_to_json(output_dir, filename, size, capacity, depot, locations, distanc
"distance_matrix": distance_matrix "distance_matrix": distance_matrix
}) })
output_path = os.path.join(output_dir, os.path.basename(filename) + ".json") output_path = os.path.join(output_dir, os.path.basename(filename)+".json")
with open(output_path, "w") as f: with open(output_path, "w") as f:
json.dump(data, f, indent=4) json.dump(data, f, indent=4)
...@@ -152,7 +152,7 @@ def read_day_requests(csv_file: str, target_day: str) -> list[Location]: ...@@ -152,7 +152,7 @@ def read_day_requests(csv_file: str, target_day: str) -> list[Location]:
return locations, depot return locations, depot
locations, depot = read_day_requests("julia/clean_LCN_01.csv", "03/10/2023") locations, depot = read_day_requests("julia/clean_LCN_01.csv", "31/10/2023")
size = len(locations) + 1 size = len(locations) + 1
capacity = 275 capacity = 275
...@@ -162,4 +162,4 @@ copyLocations.insert(0, depot) ...@@ -162,4 +162,4 @@ copyLocations.insert(0, depot)
distance_matrix, time_matrix = compute_matrix(copyLocations) distance_matrix, time_matrix = compute_matrix(copyLocations)
save_to_json("data_in/Nantes/", "Nantes_03_10_2023", size, capacity, depot, locations, time_matrix) save_to_json("data_in/Nantes/", "Nantes_31_10_2023", size, capacity, depot, locations, time_matrix)
This diff is collapsed.
This diff is collapsed.
...@@ -13,10 +13,19 @@ int const ROUTE_PENALTY = 0; ...@@ -13,10 +13,19 @@ int const ROUTE_PENALTY = 0;
int const NUMBER_VEHICLE = 50; int const NUMBER_VEHICLE = 50;
// Number of iterations for the algorithm or simulation to run. // Number of iterations for the algorithm or simulation to run.
int const NUMBER_ITERATION = 10000; int const NUMBER_ITERATION = 100000;
// % of NUMBER_ITERATION given to the first phase (route minimisation)
double const FIRST_PHASE_ITERATION = 0.3;
// % of NUMBER_ITERATION
// If no new best solution is found within LNS_FREQUENCY small iterations, then the next iteration will be a large iteration.
double const LNS_FREQUENCY = 0.05;
// Flags // Flags
bool const TWO_PHASE_ALGORITHM = true;
// Flag indicating whether the random seed has been set (true means it is set). // Flag indicating whether the random seed has been set (true means it is set).
bool const SEED_SET = true; bool const SEED_SET = true;
...@@ -27,7 +36,7 @@ int const RANDOM_SEED = 100; ...@@ -27,7 +36,7 @@ int const RANDOM_SEED = 100;
bool const PRINT = true; bool const PRINT = true;
// Flag indicating whether the final solution is stored. // Flag indicating whether the final solution is stored.
bool const STORE_SOLUTION = true; bool const STORE_SOLUTION = false;
// Flag indicating whether the solution is stored with complete information or not. // Flag indicating whether the solution is stored with complete information or not.
bool const COMPLETE_STORE = true; bool const COMPLETE_STORE = true;
......
...@@ -63,7 +63,5 @@ void BankFocusStringRemoval::destroySolution(Solution &solution) const ...@@ -63,7 +63,5 @@ void BankFocusStringRemoval::destroySolution(Solution &solution) const
RemoveRoute remove = RemoveRoute(util::getRandomInt(1, solution.getRoutes().size()) - 1); RemoveRoute remove = RemoveRoute(util::getRandomInt(1, solution.getRoutes().size()) - 1);
solution.applyDestructSolution(remove); solution.applyDestructSolution(remove);
} }
std::cout << "After preparation :" << std::endl;
solution.print();
BankFocusSISRsRuin(solution, maxCardinalityOfString, averageNumberRemovedElement); BankFocusSISRsRuin(solution, maxCardinalityOfString, averageNumberRemovedElement);
} }
\ No newline at end of file
#include "min_small_large_selector.h"
#include "config.h"
#include <algorithm>
#include <spdlog/spdlog.h>
MinSmallLargeOperatorSelector::MinSmallLargeOperatorSelector(std::vector<SimpleOperatorSelector> selectorsList,
int totalIteration)
: totalIteration(totalIteration), iterationAtCurrentStep(0), SLNSIteration(0), selectorStep(0), operatorList(std::move(selectorsList))
{}
OperatorPair MinSmallLargeOperatorSelector::getOperatorPair()
{
int minToSLNS = totalIteration * FIRST_PHASE_ITERATION;
int smallToLarge = totalIteration * LNS_FREQUENCY;
// first phase (assume this phase use a route minimisation operator)
if (iterationAtCurrentStep <= minToSLNS)
{
//std::cout << "Phase mini" << std::endl;
selectorStep = 0;
}
else
{
// second phase (small and large neighborhood search)
if (SLNSIteration < smallToLarge)
{
// Small
// std::cout << "Phase small" << std::endl;
selectorStep = 1;
}
else
{
// Large
// std::cout << "Phase large" << std::endl;
selectorStep = 2;
}
++SLNSIteration;
}
SimpleOperatorSelector &selector = operatorList.at(selectorStep);
OperatorPair pair = selector.getOperatorPair();
++iterationAtCurrentStep;
return pair;
}
void MinSmallLargeOperatorSelector::betterSolutionFound()
{
if (selectorStep == 0)
{
std::cout << "in min phase" << std::endl;
}
if (selectorStep == 1)
{
std::cout << "in small phase" << std::endl;
}
if (selectorStep == 2)
{
std::cout << "in large phase" << std::endl;
}
SLNSIteration = 0;
}
#pragma once
#include "lns/operators/selector/operator_selector.h"
/**
* Selector for a two phased algorithm.
* First phase minimise the number of routes.
* Second phase minimise the size of each routes.
*/
class MinSmallLargeOperatorSelector : public OperatorSelector
{
public:
private:
int totalIteration;
// the number of call to getOperatorPair (ie. lns iteration) done with the current step
unsigned int iterationAtCurrentStep;
unsigned int SLNSIteration;
// the index of the selector used, called step
unsigned int selectorStep;
// vector of all the selector and the number of iterations for each
std::vector<SimpleOperatorSelector> operatorList;
public:
/**
* Does a certain amount of iteration with an initial type of selector.
* Then finish the execution with a classical small and large algorirthm.
*/
explicit MinSmallLargeOperatorSelector(std::vector<SimpleOperatorSelector> operatorList, int totalIterarion);
OperatorPair getOperatorPair() override;
void betterSolutionFound() override;
};
#include "config.h"
#include "input/data.h" #include "input/data.h"
#include "input/json_parser.h" #include "input/json_parser.h"
#include "input/location.h" #include "input/location.h"
...@@ -20,16 +21,16 @@ ...@@ -20,16 +21,16 @@
#include "lns/operators/destruction/string_removal.h" #include "lns/operators/destruction/string_removal.h"
#include "lns/operators/reconstruction/enumerate.h" #include "lns/operators/reconstruction/enumerate.h"
#include "lns/operators/reconstruction/list_heuristic_cost_oriented.h" #include "lns/operators/reconstruction/list_heuristic_cost_oriented.h"
#include "lns/operators/selector/min_small_large_selector.h"
#include "lns/operators/selector/operator_selector.h" #include "lns/operators/selector/operator_selector.h"
#include "lns/operators/selector/small_large_selector.h" #include "lns/operators/selector/small_large_selector.h"
#include "lns/operators/sorting_strategy.h" #include "lns/operators/sorting_strategy.h"
#include "lns/solution/solution.h" #include "lns/solution/solution.h"
#include "mains/main_interface.h" #include "mains/main_interface.h"
#include "output/run.h"
#include "output/solution_checker.h" #include "output/solution_checker.h"
#include "output/solution_exporter.h" #include "output/solution_exporter.h"
#include "types.h" #include "types.h"
#include "output/run.h"
#include "config.h"
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
...@@ -55,42 +56,57 @@ void simpleLNS(PDPTWData const &data, Solution &startingSolution) ...@@ -55,42 +56,57 @@ void simpleLNS(PDPTWData const &data, Solution &startingSolution)
ThresholdAcceptance acceptor(0.05); ThresholdAcceptance acceptor(0.05);
// lns operators // lns operators
SimpleOperatorSelector RandomDestroy_BestInsert; SimpleOperatorSelector smallSelector;
addAllReconstructor(RandomDestroy_BestInsert); addAllReconstructor(smallSelector);
RandomDestroy_BestInsert.addDestructor(RandomDestroy(pairs)); smallSelector.addDestructor(RandomDestroy(pairs));
RandomDestroy_BestInsert.addDestructor(StringRemoval(10, 10)); smallSelector.addDestructor(StringRemoval(10, 10));
SimpleOperatorSelector largeSelector; SimpleOperatorSelector largeSelector;
addAllReconstructor(largeSelector); addAllReconstructor(largeSelector);
largeSelector.addDestructor(RandomDestroy(manyPairs)); largeSelector.addDestructor(RandomDestroy(manyPairs));
largeSelector.addDestructor(StringRemoval(10, 10)); largeSelector.addDestructor(StringRemoval(10, 10));
std::optional<output::LnsOutput> result;
std::vector<SmallLargeOperatorSelector::StepSelector> selectors; if (TWO_PHASE_ALGORITHM)
selectors.emplace_back(10, std::move(RandomDestroy_BestInsert));
selectors.emplace_back(50, std::move(largeSelector));
SmallLargeOperatorSelector smallLargeSelector(std::move(selectors));
// run lns
output::LnsOutput result = lns::runLns(startingSolution, smallLargeSelector, acceptor);
if (STORE_SOLUTION)
{ {
output::exportToJson(result); // route min operators
SimpleOperatorSelector routeMinSelector;
addAllReconstructor(routeMinSelector);
routeMinSelector.addDestructor(BankFocusStringRemoval(10, 10));
//routeMinSelector.addDestructor(StringRemoval(10, 10));
std::vector<SimpleOperatorSelector> operatorList;
operatorList.emplace_back(std::move(routeMinSelector));
operatorList.emplace_back(std::move(smallSelector));
operatorList.emplace_back(std::move(largeSelector));
MinSmallLargeOperatorSelector minSmallLargeSelector(std::move(operatorList), NUMBER_ITERATION);
// run lns
output::LnsOutput result = lns::runLns(startingSolution, minSmallLargeSelector, acceptor);
} }
else
{
std::vector<SmallLargeOperatorSelector::StepSelector> selectors;
selectors.emplace_back(10, std::move(smallSelector));
selectors.emplace_back(50, std::move(largeSelector));
// Solution sol = result.getBestSolution(); SmallLargeOperatorSelector smallLargeSelector(std::move(selectors));
// sol.print(); // run lns
// // try reduce the number of routes output::LnsOutput result = lns::runLns(startingSolution, smallLargeSelector, acceptor);
// BankFocusStringRemoval rem = BankFocusStringRemoval(10,10); }
// rem.destroySolution(sol);
// std::cout << "final print" << std::endl;
// sol.print();
if (result.has_value())
{
if (PRINT)
{
result->getBestSolution().print();
}
if (STORE_SOLUTION)
{
output::exportToJson(*result);
}
}
} }
int main(int argc, char **argv) int main(int argc, char **argv)
...@@ -100,10 +116,10 @@ int main(int argc, char **argv) ...@@ -100,10 +116,10 @@ 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/n100/bar-n100-1.json";
// std::string filepath = "/home/a24jacqb/Documents/Code/pdptw-main/data_in/pdp_100/lc103.json"; std::string filepath = "/home/a24jacqb/Documents/Code/pdptw-main/data_in/pdp_100/lc103.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/Nantes_1.json";
//std::string filepath = "/home/a24jacqb/Documents/Code/pdptw-main/data_in/n5000/bar-n5000-1.json"; //std::string filepath = "/home/a24jacqb/Documents/Code/pdptw-main/data_in/n5000/bar-n5000-1.json";
std::string filepath = "/home/a24jacqb/Documents/Code/pdptw-main/data_in/Nantes/Nantes_03_10_2023"; //std::string filepath = "/home/a24jacqb/Documents/Code/pdptw-main/data_in/Nantes/Nantes_31_10_2023.json";
PDPTWData data = parsing::parseJson(filepath); PDPTWData data = parsing::parseJson(filepath);
Solution startingSolution = Solution::emptySolution(data); Solution startingSolution = Solution::emptySolution(data);
...@@ -113,6 +129,5 @@ int main(int argc, char **argv) ...@@ -113,6 +129,5 @@ int main(int argc, char **argv)
// runAllInDirectory(path, simpleLNS); // runAllInDirectory(path, simpleLNS);
return 0; return 0;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment