From f9c72b06d8567d3bf267574fd7ae950b845468b0 Mon Sep 17 00:00:00 2001 From: Florian OMNES Date: Fri, 20 Dec 2024 14:01:47 +0100 Subject: [PATCH 1/5] Add const qualifier, call set_solver earlier --- src/cpp/lpnamer/main/ProblemGeneration.cpp | 4 ++-- .../include/antares-xpansion/lpnamer/main/ProblemGeneration.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cpp/lpnamer/main/ProblemGeneration.cpp b/src/cpp/lpnamer/main/ProblemGeneration.cpp index 2c006e3a4..8586537ac 100644 --- a/src/cpp/lpnamer/main/ProblemGeneration.cpp +++ b/src/cpp/lpnamer/main/ProblemGeneration.cpp @@ -83,6 +83,8 @@ std::filesystem::path ProblemGeneration::updateProblems() { const auto archive_path = options_.ArchivePath(); std::filesystem::path study_dir; + set_solver(study_dir, logger.get()); + if (mode_ == SimulationInputMode::ARCHIVE) { xpansion_output_dir = options_.deduceXpansionDirIfEmpty(xpansion_output_dir, archive_path); @@ -123,8 +125,6 @@ std::filesystem::path ProblemGeneration::updateProblems() { auto weights_file = options_.WeightsFile(); auto unnamed_problems = options_.UnnamedProblems(); - set_solver(study_dir, logger.get()); - RunProblemGeneration(xpansion_output_dir, master_formulation, additionalConstraintFilename_l, archive_path, logger, log_file_path, weights_file, unnamed_problems); diff --git a/src/cpp/lpnamer/main/include/antares-xpansion/lpnamer/main/ProblemGeneration.h b/src/cpp/lpnamer/main/include/antares-xpansion/lpnamer/main/ProblemGeneration.h index e74caa310..076ba23f7 100644 --- a/src/cpp/lpnamer/main/include/antares-xpansion/lpnamer/main/ProblemGeneration.h +++ b/src/cpp/lpnamer/main/include/antares-xpansion/lpnamer/main/ProblemGeneration.h @@ -24,7 +24,7 @@ class ProblemGeneration { explicit ProblemGeneration(ProblemGenerationOptions& options); virtual ~ProblemGeneration() = default; std::filesystem::path updateProblems(); - ProblemGenerationOptions& options_; + const ProblemGenerationOptions& options_; private: virtual void RunProblemGeneration( From 2817f49a378fd0a3cde7de43005db541c5c00aab Mon Sep 17 00:00:00 2001 From: Florian OMNES Date: Fri, 20 Dec 2024 16:13:41 +0100 Subject: [PATCH 2/5] Use updated antares-solver API (output path changes) [ANT-2561] --- src/cpp/lpnamer/main/ProblemGeneration.cpp | 50 ++++++++++++++++--- .../lpnamer/main/ProblemGeneration.h | 10 ++-- .../ProblemGenerationExeOptionsTest.cpp | 8 +-- 3 files changed, 53 insertions(+), 15 deletions(-) diff --git a/src/cpp/lpnamer/main/ProblemGeneration.cpp b/src/cpp/lpnamer/main/ProblemGeneration.cpp index 8586537ac..d19494d01 100644 --- a/src/cpp/lpnamer/main/ProblemGeneration.cpp +++ b/src/cpp/lpnamer/main/ProblemGeneration.cpp @@ -61,10 +61,19 @@ ProblemGeneration::ProblemGeneration(ProblemGenerationOptions& options) } } -std::filesystem::path ProblemGeneration::performAntaresSimulation() { +static std::string lowerCase(std::string data) { + std::transform(data.begin(), data.end(), data.begin(), + [](unsigned char c) { return std::tolower(c); }); + return data; +} + +void ProblemGeneration::performAntaresSimulation( + const std::filesystem::path& output) { Antares::Solver::Optimization::OptimizationOptions optOptions; + + optOptions.ortoolsSolver = lowerCase(solver_name_); auto results = - Antares::API::PerformSimulation(options_.StudyPath(), optOptions); + Antares::API::PerformSimulation(options_.StudyPath(), output, optOptions); // Add parallel // Handle errors @@ -74,7 +83,32 @@ std::filesystem::path ProblemGeneration::performAntaresSimulation() { } lps_ = std::move(results.antares_problems); - return {results.simulationPath}; +} + +static std::string getCurrentTimestamp() { + // Get the current time point + auto now = std::chrono::system_clock::now(); + + // Convert to time_t for formatting + std::time_t now_time_t = std::chrono::system_clock::to_time_t(now); + + // Convert to tm structure + std::tm now_tm; +#ifdef _WIN32 + localtime_s(&now_tm, &now_time_t); // Windows-specific +#else + localtime_r(&now_time_t, &now_tm); // POSIX-specific +#endif + + // Format the timestamp + std::ostringstream oss; + oss << std::put_time(&now_tm, "%Y%m%d-%H%Meco"); + return oss.str(); +} + +// Useful only for "API" mode +std::filesystem::path generateOutputName(const std::filesystem::path& study) { + return study / "output" / getCurrentTimestamp(); } std::filesystem::path ProblemGeneration::updateProblems() { @@ -83,8 +117,6 @@ std::filesystem::path ProblemGeneration::updateProblems() { const auto archive_path = options_.ArchivePath(); std::filesystem::path study_dir; - set_solver(study_dir, logger.get()); - if (mode_ == SimulationInputMode::ARCHIVE) { xpansion_output_dir = options_.deduceXpansionDirIfEmpty(xpansion_output_dir, archive_path); @@ -94,8 +126,8 @@ std::filesystem::path ProblemGeneration::updateProblems() { } if (mode_ == SimulationInputMode::ANTARES_API) { - simulation_dir_ = performAntaresSimulation(); study_dir = options_.StudyPath(); + simulation_dir_ = generateOutputName(study_dir); } if (mode_ == SimulationInputMode::FILE) { @@ -119,6 +151,12 @@ std::filesystem::path ProblemGeneration::updateProblems() { auto logger = ProblemGenerationLog::BuildLogger(log_file_path, std::cout, "Problem Generation"s); + set_solver(study_dir, logger.get()); + + if (mode_ == SimulationInputMode::ANTARES_API) { + performAntaresSimulation(simulation_dir_); + } + auto master_formulation = options_.MasterFormulation(); auto additionalConstraintFilename_l = options_.AdditionalConstraintsFilename(); diff --git a/src/cpp/lpnamer/main/include/antares-xpansion/lpnamer/main/ProblemGeneration.h b/src/cpp/lpnamer/main/include/antares-xpansion/lpnamer/main/ProblemGeneration.h index 076ba23f7..6a7275edc 100644 --- a/src/cpp/lpnamer/main/include/antares-xpansion/lpnamer/main/ProblemGeneration.h +++ b/src/cpp/lpnamer/main/include/antares-xpansion/lpnamer/main/ProblemGeneration.h @@ -7,9 +7,10 @@ #include #include -#include #include +#include +// clang-format off #include "antares-xpansion/lpnamer/input_reader/MpsTxtWriter.h" #include "antares-xpansion/lpnamer/model/Problem.h" #include "antares-xpansion/lpnamer/model/SimulationInputMode.h" @@ -18,6 +19,7 @@ #include "antares-xpansion/lpnamer/helper/ProblemGenerationLogger.h" #include "ProblemGenerationOptions.h" #include "antares-xpansion/multisolver_interface/SolverAbstract.h" +// clang-format on class ProblemGeneration { public: @@ -49,11 +51,13 @@ class ProblemGeneration { const std::vector& mpsList, std::filesystem::path& lpDir_, std::shared_ptr reader, const Antares::Solver::LpsFromAntares& lps); - virtual void set_solver(std::filesystem::path study_dir, ProblemGenerationLog::ProblemGenerationLogger* logger); + virtual void set_solver( + std::filesystem::path study_dir, + ProblemGenerationLog::ProblemGenerationLogger* logger); Antares::Solver::LpsFromAntares lps_; std::optional mode_; - virtual std::filesystem::path performAntaresSimulation(); + virtual void performAntaresSimulation(const std::filesystem::path& output); std::filesystem::path simulation_dir_; std::string solver_name_; }; diff --git a/tests/cpp/lp_namer/ProblemGenerationExeOptionsTest.cpp b/tests/cpp/lp_namer/ProblemGenerationExeOptionsTest.cpp index 61d5534d8..713c08a43 100644 --- a/tests/cpp/lp_namer/ProblemGenerationExeOptionsTest.cpp +++ b/tests/cpp/lp_namer/ProblemGenerationExeOptionsTest.cpp @@ -50,14 +50,10 @@ class ProblemGenerationSpyAndMock : public ProblemGeneration { } private: - std::filesystem::path performAntaresSimulation() override { - return options_.StudyPath() / "simulation"; - } + void performAntaresSimulation(const std::filesystem::path&) override {} void set_solver( std::filesystem::path study_dir, - ProblemGenerationLog::ProblemGenerationLogger* logger) override { - - } + ProblemGenerationLog::ProblemGenerationLogger* logger) override {} public: std::filesystem::path xpansion_output_dir_; From efa9e573184c4b00cbfe979392a3d5123cbffdf1 Mon Sep 17 00:00:00 2001 From: Florian OMNES Date: Thu, 2 Jan 2025 10:29:42 +0100 Subject: [PATCH 3/5] Use conversion function instead of lowerCase --- src/cpp/lpnamer/main/ProblemGeneration.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/cpp/lpnamer/main/ProblemGeneration.cpp b/src/cpp/lpnamer/main/ProblemGeneration.cpp index d19494d01..6f1d75623 100644 --- a/src/cpp/lpnamer/main/ProblemGeneration.cpp +++ b/src/cpp/lpnamer/main/ProblemGeneration.cpp @@ -61,17 +61,17 @@ ProblemGeneration::ProblemGeneration(ProblemGenerationOptions& options) } } -static std::string lowerCase(std::string data) { - std::transform(data.begin(), data.end(), data.begin(), - [](unsigned char c) { return std::tolower(c); }); - return data; +static std::string solverXpansionToSimulator(const std::string& in) { + if (in == "Xpress") return "xpress"; + if (in == "Cbc") return "coin"; + throw std::invalid_argument("Invalid solver"); } void ProblemGeneration::performAntaresSimulation( const std::filesystem::path& output) { Antares::Solver::Optimization::OptimizationOptions optOptions; - optOptions.ortoolsSolver = lowerCase(solver_name_); + optOptions.ortoolsSolver = solverXpansionToSimulator(solver_name_); auto results = Antares::API::PerformSimulation(options_.StudyPath(), output, optOptions); // Add parallel From 26f3828718b277f3257662019133bcc599e6f67c Mon Sep 17 00:00:00 2001 From: Jason Marechal Date: Fri, 3 Jan 2025 10:49:22 +0100 Subject: [PATCH 4/5] update rc-9 --- antares-version.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/antares-version.json b/antares-version.json index 616c1bc7b..5a57da495 100644 --- a/antares-version.json +++ b/antares-version.json @@ -1,5 +1,5 @@ { - "antares_version": "9.2.0-rc7", + "antares_version": "9.2.0-rc9", "antares_version_executable": "9.2", "antares_xpansion_version": "1.4.0", "minizip_ng_version": "3.0.6", From a9535bb80cd7ea23c226ee9155cab1ee8bcb9631 Mon Sep 17 00:00:00 2001 From: Thomas Bittar Date: Fri, 3 Jan 2025 18:09:57 +0100 Subject: [PATCH 5/5] Implement case-insensitive handling for Xpansion solver names to be passed to Simulator --- src/cpp/lpnamer/main/ProblemGeneration.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/cpp/lpnamer/main/ProblemGeneration.cpp b/src/cpp/lpnamer/main/ProblemGeneration.cpp index 6f1d75623..ee7024def 100644 --- a/src/cpp/lpnamer/main/ProblemGeneration.cpp +++ b/src/cpp/lpnamer/main/ProblemGeneration.cpp @@ -61,9 +61,19 @@ ProblemGeneration::ProblemGeneration(ProblemGenerationOptions& options) } } +static std::string lowerCase(std::string data) { + std::transform(data.begin(), data.end(), data.begin(), + [](unsigned char c) { return std::tolower(c); }); + return data; +} + static std::string solverXpansionToSimulator(const std::string& in) { - if (in == "Xpress") return "xpress"; - if (in == "Cbc") return "coin"; + // in could be Cbc or CBC depending on whether it is defined or not in the + // settings file + // Use lowerCase in any case to be robust to these subtleties + std::string lower_case_in = lowerCase(in); + if (lower_case_in == "xpress") return "xpress"; + if (lower_case_in == "cbc" || lower_case_in == "coin") return "coin"; throw std::invalid_argument("Invalid solver"); }