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

Add code to run multiple instances

- add code to run multiple instances and store the result in a specific directory
- add new config
- add a complete json output
parent 7c427502
No related branches found
No related tags found
No related merge requests found
......@@ -49,6 +49,7 @@ add_executable(pdptw src/mains/main.cpp
src/lns/operators/selector/small_large_selector.cpp
src/lns/lns.cpp
src/utils.cpp
src/output/run.cpp
)
target_link_libraries(pdptw PRIVATE CLI11::CLI11 nlohmann_json::nlohmann_json spdlog::spdlog)
......
#include <string>
// Parameters
// Penalty for excluding a certain pickup-delivery pair, aims to minimize the number of excluded pairs.
......@@ -26,3 +28,9 @@ bool const PRINT = true;
// Flag indicating whether the final solution is stored.
bool const STORE_SOLUTION = true;
// Flag indicating whether the solution is stored with complete information or not.
bool const COMPLETE_STORE = true;
// Directories
std::string const OUTPUT_DIRECTORY = "./../../output/100_complete";
\ No newline at end of file
......@@ -28,6 +28,8 @@
#include "output/solution_checker.h"
#include "output/solution_exporter.h"
#include "types.h"
#include "output/run.h"
#include "config.h"
#include <filesystem>
#include <fstream>
......@@ -73,7 +75,10 @@ void simpleLNS(PDPTWData const &data, Solution &startingSolution)
// run lns
output::LnsOutput result = lns::runLns(startingSolution, smallLargeSelector, acceptor);
result.getBestSolution().print();
if (STORE_SOLUTION)
{
output::exportToJson(result);
}
}
int main(int argc, char **argv)
......@@ -87,9 +92,12 @@ int main(int argc, char **argv)
//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";
PDPTWData data = parsing::parseJson(filepath);
Solution startingSolution = Solution::emptySolution(data);
simpleLNS(data, startingSolution);
// PDPTWData data = parsing::parseJson(filepath);
// Solution startingSolution = Solution::emptySolution(data);
// simpleLNS(data, startingSolution);
std::string path = "/home/a24jacqb/Documents/Code/pdptw-main/data_in/pdp_100";
runAllInDirectory(path, simpleLNS);
return 0;
}
......@@ -20,8 +20,3 @@ unsigned long output::LnsOutput::getTimeSpent() const
output::LnsOutput::LnsOutput(Solution &&bestSolution, unsigned int numberOfIteration, unsigned long timeSpent)
: bestSolution(std::move(bestSolution)), numberOfIteration(numberOfIteration), timeSpent(timeSpent)
{}
void output::LnsOutput::writeSolutionToFile() const
{
output::exportToJson(getBestSolution());
}
\ No newline at end of file
......@@ -15,6 +15,5 @@ namespace output
unsigned int getNumberOfIteration() const;
Solution const &getBestSolution() const;
unsigned long getTimeSpent() const;
void writeSolutionToFile() const;
};
}// namespace output
\ No newline at end of file
#include "run.h"
#include "input/json_parser.h"
#include "lns/lns.h"
namespace fs = std::filesystem;
std::vector<std::string> getFilesInDirectory(std::string const &directoryPath)
{
std::vector<std::string> filePaths;
try
{
for (auto const &entry: fs::directory_iterator(directoryPath))
{
if (entry.is_regular_file())
{
filePaths.push_back(entry.path().string());
}
}
} catch (std::filesystem::filesystem_error const &e)
{
std::cerr << "Error : " << e.what() << std::endl;
}
return filePaths;
}
void runAllInDirectory(std::string const &directoryPath, std::function<void(PDPTWData &, Solution &)> function)
{
std::vector<std::string> files = getFilesInDirectory(directoryPath);
for (std::string file: files)
{
PDPTWData data = parsing::parseJson(file);
Solution startingSolution = Solution::emptySolution(data);
function(data, startingSolution);
}
}
#pragma once
#include "input/pdptw_data.h"
#include "lns/solution/solution.h"
#include <functional>
#include <iostream>
#include <vector>
#include <string>
#include <filesystem>
std::vector<std::string> getFilesInDirectory(const std::string& directoryPath);
void runAllInDirectory(const std::string& directoryPath, std::function<void(PDPTWData &, Solution &)> function);
\ No newline at end of file
#include "solution_exporter.h"
#include "config.h"
std::string getCurrentDate()
{
std::time_t t = std::time(nullptr);
......@@ -30,17 +32,35 @@ nlohmann::ordered_json output::getMinimalJson(Solution const &solution)
return jsonSolution;
}
nlohmann::ordered_json output::getCompleteJson(Solution const &solution)
nlohmann::ordered_json output::getCompleteJson(Solution const &solution, int iteration, double time)
{
nlohmann::ordered_json jsonSolution;
// TO DO
nlohmann::ordered_json jsonRoutes = nlohmann::ordered_json::array();
int routeID = 0;
for (auto const &route: solution.getRoutes())
{
jsonRoutes.push_back(routeToJson(routeID, route));
++routeID;
}
jsonSolution["InstanceName"] = solution.getData().getDataName();
jsonSolution["Authors"] = "...";
jsonSolution["Date"] = getCurrentDate();
jsonSolution["Reference"] = "...";
jsonSolution["Cost"] = std::ceil(solution.getRawCost() * 100.0) / 100.0;
jsonSolution["Time"] = time;
jsonSolution["Iteration"] = iteration;
jsonSolution["Unfullfilled"] = solution.getPairBank().size();
jsonSolution["routes"] = jsonRoutes;
return jsonSolution;
}
void output::exportToJson(Solution const &solution)
void output::exportToJson(output::LnsOutput result)
{
std::string directory = "./../../output";
std::string filename = directory + "/" + solution.getData().getDataName() + "_sol.json";
std::string directory = OUTPUT_DIRECTORY;
std::string filename = directory + "/" + result.getBestSolution().getData().getDataName() + "_sol.json";
if (!std::filesystem::exists(directory))
{
......@@ -55,8 +75,18 @@ void output::exportToJson(Solution const &solution)
return;
}
nlohmann::ordered_json jsonData = output::getMinimalJson(solution);
file << jsonData.dump();
nlohmann::ordered_json jsonData;
if (COMPLETE_STORE)
{
jsonData =
output::getCompleteJson(result.getBestSolution(), result.getNumberOfIteration(), result.getTimeSpent());
}
else
{
jsonData = output::getMinimalJson(result.getBestSolution());
}
file << jsonData.dump(4);
file.close();
std::cout << "Solution exported" << std::endl;
}
......
#pragma once
#include "lns/solution/solution.h"
#include "output/lns_output.h"
#include <nlohmann/json.hpp>
#include <ctime>
#include <ctime>
#include <fstream>
#include <nlohmann/json.hpp>
namespace output
{
......@@ -17,13 +17,13 @@ namespace output
/**
* Get a complete json representation of a solution (heavier)
*/
nlohmann::ordered_json getCompleteJson(Solution const &solution);
nlohmann::ordered_json getCompleteJson(Solution const &solution, int iteration, double time);
nlohmann::ordered_json routeToJson(int routeID, const Route& route);
nlohmann::ordered_json routeToJson(int routeID, Route const &route);
void exportToJson(Solution const &solution);
void exportToJson(output::LnsOutput result);
}// namespace output
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment