96 lines
4.5 KiB
C++
96 lines
4.5 KiB
C++
|
|
#include "InterfaceClassic.hpp"
|
||
|
|
#include "../Solver/solverAPI.h"
|
||
|
|
#include "../../General/Logs/IOHelper.h"
|
||
|
|
#include "../../General/Model/STFInstanceUtils.hpp"
|
||
|
|
|
||
|
|
namespace interfacelib {
|
||
|
|
void InterfaceClassic::directory(const std::filesystem::directory_entry &entry, const std::string &solver) {
|
||
|
|
std::filesystem::directory_iterator iter(entry);
|
||
|
|
std::filesystem::directory_iterator iterCount(entry);
|
||
|
|
unsigned int nbFiles = std::count_if(iterCount, {}, [](const std::filesystem::directory_entry &entry) {
|
||
|
|
return std::filesystem::is_regular_file(entry);
|
||
|
|
});
|
||
|
|
unsigned int count = 0;
|
||
|
|
loggerlib::Logger::systemNotify(loggerlib::LOGGER_INFO, std::to_string(nbFiles) + " instances trouvees");
|
||
|
|
//modellib::STFInstanceUtils::write_in_file("Comparaison_ISO_SWAP.csv", "\n Instance Name;Score;H1;H1;H2;H2;H3;H3;H4;H4;diag;sw;cp\n");
|
||
|
|
for (auto &ientry: iter) {
|
||
|
|
if (!ientry.is_directory()) {
|
||
|
|
configlib::Configuration::init("configuration.json");
|
||
|
|
file(ientry, solver);
|
||
|
|
|
||
|
|
count++;
|
||
|
|
double per = ((double) count / (double) nbFiles) * 100.0;
|
||
|
|
std::stringstream stream;
|
||
|
|
stream << std::fixed << std::setprecision(2) << per;
|
||
|
|
loggerlib::Logger::systemNotify(loggerlib::LOGGER_INFO, stream.str() + "%");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
loggerlib::Logger::systemNotify(loggerlib::LOGGER_INFO, std::string("Finished"));
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
void InterfaceClassic::file(const std::filesystem::directory_entry &entry, const std::string &solver) {
|
||
|
|
std::ifstream file(entry.path());
|
||
|
|
modellib::STFInstance instance = modellib::STFInstance::createSTF(file);
|
||
|
|
file.close();
|
||
|
|
std::string filename = entry.path().filename().string().substr(0, entry.path().filename().string().find_last_of('.'));
|
||
|
|
if (instance.isValid() && configlib::Configuration::Global.SAVE_INPUT) {
|
||
|
|
auto json = instance.to_json();
|
||
|
|
auto jsonstr = json.dump(2);
|
||
|
|
loggerlib::IOHelper::dump(configlib::Configuration::Global.INPUT_LOCATION, jsonstr, filename);
|
||
|
|
}
|
||
|
|
loggerlib::Logger::systemNotify(loggerlib::LOGGER_INFO, std::string("Trying to launch solver on : ") +
|
||
|
|
entry.path().filename().string());
|
||
|
|
|
||
|
|
//modellib::STFInstanceUtils::write_in_file("Comparaison_ISO_SWAP.csv", entry.path().filename().string() + ";");
|
||
|
|
|
||
|
|
bool la = false;
|
||
|
|
if(solverlib::SolverAPI::apiFunctions.find(solver) != solverlib::SolverAPI::apiFunctions.end())
|
||
|
|
{
|
||
|
|
if(solver == "-HEURISTIC")
|
||
|
|
instance.init();
|
||
|
|
la = true;
|
||
|
|
solverlib::SolverAPI::apiFunctions[solver](instance, filename);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
loggerlib::Logger::systemNotify(loggerlib::LOGGER_ERRORS, std::string("Cannot find specified solver in the API : ") + solver);
|
||
|
|
if (instance.isValid() && configlib::Configuration::Global.SAVE_OUTPUT) {
|
||
|
|
auto todump = instance.to_json();
|
||
|
|
todump = todump.at("planifications");
|
||
|
|
auto todumpstr = todump.dump(2);
|
||
|
|
if(configlib::Configuration::Global.OUTPUT_FILENAME)
|
||
|
|
{
|
||
|
|
loggerlib::IOHelper::dump(configlib::Configuration::Global.OUTPUT_LOCATION,
|
||
|
|
todumpstr, filename+"_p.json");
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
loggerlib::IOHelper::dump(configlib::Configuration::Global.OUTPUT_LOCATION,
|
||
|
|
todumpstr);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if(la && (solver == "-HEURISTIC"))
|
||
|
|
instance.clean();
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
void InterfaceClassic::redirect(const std::string &mode, const std::string &path, const std::string &solver) {
|
||
|
|
std::filesystem::directory_entry entry(path);
|
||
|
|
if (entry.exists()) {
|
||
|
|
if (solver.empty() && (mode == "--solve"))
|
||
|
|
loggerlib::FileLogger::systemNotify(loggerlib::LOGGER_INFO,
|
||
|
|
"WARNING solver argument is not set");
|
||
|
|
if (entry.is_directory()) {
|
||
|
|
if (mode == "--solve")
|
||
|
|
directory(entry, solver);
|
||
|
|
} else {
|
||
|
|
if (mode == "--solve")
|
||
|
|
file(entry, solver);
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
loggerlib::FileLogger::systemNotify(loggerlib::LOGGER_ERRORS, "Cannot find specified folder or instance:" + entry.path().string());
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|