From 78790e62489fd2e92bd5f7a2a5e43ed21b71471e Mon Sep 17 00:00:00 2001 From: Abdoulbari Zakir Date: Sun, 12 Nov 2023 17:25:27 +0100 Subject: [PATCH 01/27] set multi threads log dir --- src/solver/utils/ortools_utils.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/solver/utils/ortools_utils.cpp b/src/solver/utils/ortools_utils.cpp index fc31bf5be7..2eff603748 100644 --- a/src/solver/utils/ortools_utils.cpp +++ b/src/solver/utils/ortools_utils.cpp @@ -58,6 +58,10 @@ MPSolver* ProblemSimplexeNommeConverter::Convert() if (problemeSimplexe_->SolverLogs()) { solver->EnableOutput(); + // TODO won't work in ci, needs ortools update + // see https://github.com/rte-france/or-tools/pull/112 + // std::filesystem::path log_file = logs.logfile().c_str(); + // solver->set_solver_logs_directory(log_file.parent_path()); } return solver; From 151dedd9ca5a23e55107889864ceb7b301582c2e Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Thu, 16 Nov 2023 11:58:37 +0100 Subject: [PATCH 02/27] [skip ci] will not work thank u @guilpier-code --- src/solver/utils/ortools_utils.cpp | 19 ++++++++++++++----- src/solver/utils/ortools_utils.h | 3 +++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/solver/utils/ortools_utils.cpp b/src/solver/utils/ortools_utils.cpp index 2eff603748..19d3090cf2 100644 --- a/src/solver/utils/ortools_utils.cpp +++ b/src/solver/utils/ortools_utils.cpp @@ -4,6 +4,7 @@ #include #include #include +#include using namespace operations_research; @@ -41,6 +42,18 @@ ProblemSimplexeNommeConverter::ProblemSimplexeNommeConverter( variableNameManager_.SetTarget(problemeSimplexe_->VariableNames()); constraintNameManager_.SetTarget(problemeSimplexe_->ConstraintNames()); } + + // TODO won't work in ci, needs ortools update + // see https://github.com/rte-france/or-tools/pull/112 + std::filesystem::path log_file = logs.logfile().c_str(); + auto log_directory = log_file.parent_path(); + auto myid = std::this_thread::get_id(); + std::stringstream ss; + ss << myid; + auto log_file = log_directory / (std::string("thread_") + ss.str() + ".log"); + // TODO + log_writer_.open(log_file, std::ofstream::out | std::ofstream::app); + log_streams.push_back(&log_writer_); } MPSolver* ProblemSimplexeNommeConverter::Convert() @@ -57,11 +70,7 @@ MPSolver* ProblemSimplexeNommeConverter::Convert() CopyMatrix(solver); if (problemeSimplexe_->SolverLogs()) { - solver->EnableOutput(); - // TODO won't work in ci, needs ortools update - // see https://github.com/rte-france/or-tools/pull/112 - // std::filesystem::path log_file = logs.logfile().c_str(); - // solver->set_solver_logs_directory(log_file.parent_path()); + solver->EnableOutput(&log_streams); } return solver; diff --git a/src/solver/utils/ortools_utils.h b/src/solver/utils/ortools_utils.h index 3ea49adce9..0b7053b0db 100644 --- a/src/solver/utils/ortools_utils.h +++ b/src/solver/utils/ortools_utils.h @@ -97,6 +97,9 @@ class ProblemSimplexeNommeConverter void CopyRows(MPSolver* solver); void TuneSolverSpecificOptions(MPSolver* solver) const; void CopyMatrix(const MPSolver* solver); + std::ofstream log_writer_; // one per thread + + std::vector log_streams; }; } // namespace Optimization } // namespace Antares From 6da7fde8c6f209176e8f219020118bc2209bc2d0 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Thu, 16 Nov 2023 12:19:50 +0100 Subject: [PATCH 03/27] fstream-->ostream ptr --- src/solver/utils/ortools_utils.cpp | 4 ++-- src/solver/utils/ortools_utils.h | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/solver/utils/ortools_utils.cpp b/src/solver/utils/ortools_utils.cpp index 19d3090cf2..2854a2f6eb 100644 --- a/src/solver/utils/ortools_utils.cpp +++ b/src/solver/utils/ortools_utils.cpp @@ -50,9 +50,9 @@ ProblemSimplexeNommeConverter::ProblemSimplexeNommeConverter( auto myid = std::this_thread::get_id(); std::stringstream ss; ss << myid; - auto log_file = log_directory / (std::string("thread_") + ss.str() + ".log"); + auto log_file_per_thread = log_directory / (std::string("thread_") + ss.str() + ".log"); // TODO - log_writer_.open(log_file, std::ofstream::out | std::ofstream::app); + log_writer_.open(log_file_per_thread, std::ofstream::out | std::ofstream::app); log_streams.push_back(&log_writer_); } diff --git a/src/solver/utils/ortools_utils.h b/src/solver/utils/ortools_utils.h index 0b7053b0db..ac956335af 100644 --- a/src/solver/utils/ortools_utils.h +++ b/src/solver/utils/ortools_utils.h @@ -2,6 +2,7 @@ #include #include +#include #include From d0697c40727c2ee80643fbebeb16fac17b7ac86e Mon Sep 17 00:00:00 2001 From: Abdoulbari Zakir Date: Thu, 16 Nov 2023 13:00:34 +0100 Subject: [PATCH 04/27] [skip ci] move streams creation --- .../opt_appel_solveur_lineaire.cpp | 16 ++++++++- src/solver/utils/ortools_utils.cpp | 35 ++++++++++--------- src/solver/utils/ortools_utils.h | 9 ++--- 3 files changed, 39 insertions(+), 21 deletions(-) diff --git a/src/solver/optimisation/opt_appel_solveur_lineaire.cpp b/src/solver/optimisation/opt_appel_solveur_lineaire.cpp index ed361d6206..0ad7929f3a 100644 --- a/src/solver/optimisation/opt_appel_solveur_lineaire.cpp +++ b/src/solver/optimisation/opt_appel_solveur_lineaire.cpp @@ -225,7 +225,8 @@ static SimplexResult OPT_TryToCallSimplex( { solver = ORTOOLS_ConvertIfNeeded(options.solverName, &Probleme, solver); } - const std::string filename = createMPSfilename(optPeriodStringGenerator, optimizationNumber); + + const std::string filename = createMPSfilename(optPeriodStringGenerator, optimizationNumber); mpsWriterFactory mps_writer_factory(problemeHebdo->ExportMPS, problemeHebdo->exportMPSOnError, @@ -241,6 +242,13 @@ static SimplexResult OPT_TryToCallSimplex( if (options.useOrtools) { const bool keepBasis = (optimizationNumber == PREMIERE_OPTIMISATION); + std::ofstream log_writer; // one per thread + + std::vector log_streams; + if (Probleme.SolverLogs()) + { + setOrtoolsSolverLogs(solver, log_writer, log_streams); + } solver = ORTOOLS_Simplexe(&Probleme, solver, keepBasis); if (solver != nullptr) { @@ -373,7 +381,13 @@ bool OPT_AppelDuSimplexe(const OptimizationOptions& options, Probleme.SetUseNamedProblems(true); auto MPproblem = std::shared_ptr(ProblemSimplexeNommeConverter(options.solverName, &Probleme).Convert()); + std::ofstream log_writer; // one per thread + std::vector log_streams; + if (Probleme.SolverLogs()) + { + setOrtoolsSolverLogs(MPproblem.get(), log_writer, log_streams); + } auto analyzer = makeUnfeasiblePbAnalyzer(); analyzer->run(MPproblem.get()); analyzer->printReport(); diff --git a/src/solver/utils/ortools_utils.cpp b/src/solver/utils/ortools_utils.cpp index 2854a2f6eb..da2b730066 100644 --- a/src/solver/utils/ortools_utils.cpp +++ b/src/solver/utils/ortools_utils.cpp @@ -32,17 +32,11 @@ namespace Antares { namespace Optimization { -ProblemSimplexeNommeConverter::ProblemSimplexeNommeConverter( - const std::string& solverName, - const Antares::Optimization::PROBLEME_SIMPLEXE_NOMME* problemeSimplexe) - : solverName_(solverName), problemeSimplexe_(problemeSimplexe) -{ - if (problemeSimplexe_->UseNamedProblems()) - { - variableNameManager_.SetTarget(problemeSimplexe_->VariableNames()); - constraintNameManager_.SetTarget(problemeSimplexe_->ConstraintNames()); - } +void setOrtoolsSolverLogs(MPSolver* solver, + std::ofstream& log_writer, // one per thread + std::vector& log_streams) +{ // TODO won't work in ci, needs ortools update // see https://github.com/rte-france/or-tools/pull/112 std::filesystem::path log_file = logs.logfile().c_str(); @@ -52,8 +46,21 @@ ProblemSimplexeNommeConverter::ProblemSimplexeNommeConverter( ss << myid; auto log_file_per_thread = log_directory / (std::string("thread_") + ss.str() + ".log"); // TODO - log_writer_.open(log_file_per_thread, std::ofstream::out | std::ofstream::app); - log_streams.push_back(&log_writer_); + log_writer.open(log_file_per_thread, std::ofstream::out | std::ofstream::app); + log_streams.push_back(&log_writer); + solver->EnableOutput(&log_streams); +} + +ProblemSimplexeNommeConverter::ProblemSimplexeNommeConverter( + const std::string& solverName, + const Antares::Optimization::PROBLEME_SIMPLEXE_NOMME* problemeSimplexe) : + solverName_(solverName), problemeSimplexe_(problemeSimplexe) +{ + if (problemeSimplexe_->UseNamedProblems()) + { + variableNameManager_.SetTarget(problemeSimplexe_->VariableNames()); + constraintNameManager_.SetTarget(problemeSimplexe_->ConstraintNames()); + } } MPSolver* ProblemSimplexeNommeConverter::Convert() @@ -68,10 +75,6 @@ MPSolver* ProblemSimplexeNommeConverter::Convert() CopyRows(solver); CopyMatrix(solver); - if (problemeSimplexe_->SolverLogs()) - { - solver->EnableOutput(&log_streams); - } return solver; } diff --git a/src/solver/utils/ortools_utils.h b/src/solver/utils/ortools_utils.h index ac956335af..82ea14496c 100644 --- a/src/solver/utils/ortools_utils.h +++ b/src/solver/utils/ortools_utils.h @@ -48,8 +48,12 @@ namespace Antares { namespace Optimization { +void setOrtoolsSolverLogs(MPSolver* solver, + std::ofstream& log_writer, // one per thread -class Nomenclature + std::vector& log_streams) +; + class Nomenclature { public: Nomenclature() = delete; @@ -98,9 +102,6 @@ class ProblemSimplexeNommeConverter void CopyRows(MPSolver* solver); void TuneSolverSpecificOptions(MPSolver* solver) const; void CopyMatrix(const MPSolver* solver); - std::ofstream log_writer_; // one per thread - - std::vector log_streams; }; } // namespace Optimization } // namespace Antares From b6e472014ee5cef94ebe88393c7280cffb14c332 Mon Sep 17 00:00:00 2001 From: Abdoulbari Zakir Date: Thu, 16 Nov 2023 17:19:08 +0100 Subject: [PATCH 05/27] dirty --- .../opt_appel_solveur_lineaire.cpp | 57 ++++++++++++------- src/solver/utils/ortools_utils.cpp | 25 ++++---- src/solver/utils/ortools_utils.h | 18 ++++-- 3 files changed, 58 insertions(+), 42 deletions(-) diff --git a/src/solver/optimisation/opt_appel_solveur_lineaire.cpp b/src/solver/optimisation/opt_appel_solveur_lineaire.cpp index 0ad7929f3a..373aeda4f8 100644 --- a/src/solver/optimisation/opt_appel_solveur_lineaire.cpp +++ b/src/solver/optimisation/opt_appel_solveur_lineaire.cpp @@ -97,15 +97,15 @@ struct SimplexResult mpsWriterFactory mps_writer_factory; }; -static SimplexResult OPT_TryToCallSimplex( - const OptimizationOptions& options, - PROBLEME_HEBDO* problemeHebdo, - Optimization::PROBLEME_SIMPLEXE_NOMME& Probleme, - const int NumIntervalle, - const int optimizationNumber, - const OptPeriodStringGenerator& optPeriodStringGenerator, - bool PremierPassage, - IResultWriter& writer) +static SimplexResult OPT_TryToCallSimplex(const OptimizationOptions& options, + PROBLEME_HEBDO* problemeHebdo, + Optimization::PROBLEME_SIMPLEXE_NOMME& Probleme, + const int NumIntervalle, + const int optimizationNumber, + const OptPeriodStringGenerator& optPeriodStringGenerator, + bool PremierPassage, + IResultWriter& writer, + OrtoolsLogHandler& ortools_logger) { const auto& ProblemeAResoudre = problemeHebdo->ProblemeAResoudre; auto ProbSpx @@ -242,12 +242,12 @@ static SimplexResult OPT_TryToCallSimplex( if (options.useOrtools) { const bool keepBasis = (optimizationNumber == PREMIERE_OPTIMISATION); - std::ofstream log_writer; // one per thread + // std::ofstream log_writer; // one per thread - std::vector log_streams; + // std::vector log_streams; if (Probleme.SolverLogs()) { - setOrtoolsSolverLogs(solver, log_writer, log_streams); + setOrtoolsSolverLogs(solver, ortools_logger); } solver = ORTOOLS_Simplexe(&Probleme, solver, keepBasis); if (solver != nullptr) @@ -316,16 +316,30 @@ bool OPT_AppelDuSimplexe(const OptimizationOptions& options, problemeHebdo->solverLogs); bool PremierPassage = true; - - struct SimplexResult simplexResult = - OPT_TryToCallSimplex(options, problemeHebdo, Probleme, NumIntervalle, optimizationNumber, - optPeriodStringGenerator, PremierPassage, writer); + // tmp + OrtoolsLogHandler ortools_logger; + struct SimplexResult simplexResult = OPT_TryToCallSimplex(options, + problemeHebdo, + Probleme, + NumIntervalle, + optimizationNumber, + optPeriodStringGenerator, + PremierPassage, + writer, + ortools_logger); if (!simplexResult.success) { PremierPassage = false; - simplexResult = OPT_TryToCallSimplex(options, problemeHebdo, Probleme, NumIntervalle, optimizationNumber, - optPeriodStringGenerator, PremierPassage, writer); + simplexResult = OPT_TryToCallSimplex(options, + problemeHebdo, + Probleme, + NumIntervalle, + optimizationNumber, + optPeriodStringGenerator, + PremierPassage, + writer, + ortools_logger); } long long solveTime = simplexResult.solveTime; @@ -380,13 +394,12 @@ bool OPT_AppelDuSimplexe(const OptimizationOptions& options, Probleme.SetUseNamedProblems(true); - auto MPproblem = std::shared_ptr(ProblemSimplexeNommeConverter(options.solverName, &Probleme).Convert()); - std::ofstream log_writer; // one per thread + auto MPproblem = std::shared_ptr( + ProblemSimplexeNommeConverter(options.solverName, &Probleme).Convert()); - std::vector log_streams; if (Probleme.SolverLogs()) { - setOrtoolsSolverLogs(MPproblem.get(), log_writer, log_streams); + setOrtoolsSolverLogs(MPproblem.get(), ortools_logger); } auto analyzer = makeUnfeasiblePbAnalyzer(); analyzer->run(MPproblem.get()); diff --git a/src/solver/utils/ortools_utils.cpp b/src/solver/utils/ortools_utils.cpp index da2b730066..2a9701873b 100644 --- a/src/solver/utils/ortools_utils.cpp +++ b/src/solver/utils/ortools_utils.cpp @@ -32,23 +32,20 @@ namespace Antares { namespace Optimization { -void setOrtoolsSolverLogs(MPSolver* solver, - std::ofstream& log_writer, // one per thread - - std::vector& log_streams) +void setOrtoolsSolverLogs(MPSolver* solver, OrtoolsLogHandler& ortools_logger) { // TODO won't work in ci, needs ortools update // see https://github.com/rte-france/or-tools/pull/112 - std::filesystem::path log_file = logs.logfile().c_str(); - auto log_directory = log_file.parent_path(); - auto myid = std::this_thread::get_id(); - std::stringstream ss; - ss << myid; - auto log_file_per_thread = log_directory / (std::string("thread_") + ss.str() + ".log"); - // TODO - log_writer.open(log_file_per_thread, std::ofstream::out | std::ofstream::app); - log_streams.push_back(&log_writer); - solver->EnableOutput(&log_streams); + // std::filesystem::path log_file = logs.logfile().c_str(); + // auto log_directory = log_file.parent_path(); + // auto myid = std::this_thread::get_id(); + // std::stringstream ss; + // ss << myid; + // auto log_file_per_thread = log_directory / (std::string("thread_") + ss.str() + ".log"); + // // TODO + // log_writer.open(log_file_per_thread, std::ofstream::out | std::ofstream::app); + // log_streams.push_back(&log_writer); + solver->EnableOutput(&ortools_logger); } ProblemSimplexeNommeConverter::ProblemSimplexeNommeConverter( diff --git a/src/solver/utils/ortools_utils.h b/src/solver/utils/ortools_utils.h index 82ea14496c..6045acfc69 100644 --- a/src/solver/utils/ortools_utils.h +++ b/src/solver/utils/ortools_utils.h @@ -12,6 +12,16 @@ using namespace operations_research; +class OrtoolsLogHandler : public LogHandlerInterface +{ +public: + // tmp test with std::cout + explicit OrtoolsLogHandler() = default; + void message(const char* msg) override + { + std::cout << msg << std::endl; + } +}; void ORTOOLS_EcrireJeuDeDonneesLineaireAuFormatMPS(MPSolver* solver, Antares::Solver::IResultWriter& writer, const std::string& filename); @@ -48,12 +58,8 @@ namespace Antares { namespace Optimization { -void setOrtoolsSolverLogs(MPSolver* solver, - std::ofstream& log_writer, // one per thread - - std::vector& log_streams) -; - class Nomenclature +void setOrtoolsSolverLogs(MPSolver* solver, OrtoolsLogHandler &ortools_logger); +class Nomenclature { public: Nomenclature() = delete; From 1586ab55a729eabb2939ee80497e4c2ec68b1e47 Mon Sep 17 00:00:00 2001 From: Abdoulbari Zakir Date: Thu, 16 Nov 2023 17:33:35 +0100 Subject: [PATCH 06/27] [skip] better --- src/solver/utils/ortools_utils.cpp | 21 ++++++++++----------- src/solver/utils/ortools_utils.h | 30 ++++++++++++++++++++---------- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/solver/utils/ortools_utils.cpp b/src/solver/utils/ortools_utils.cpp index 2a9701873b..820255b252 100644 --- a/src/solver/utils/ortools_utils.cpp +++ b/src/solver/utils/ortools_utils.cpp @@ -34,20 +34,19 @@ namespace Optimization { void setOrtoolsSolverLogs(MPSolver* solver, OrtoolsLogHandler& ortools_logger) { - // TODO won't work in ci, needs ortools update - // see https://github.com/rte-france/or-tools/pull/112 - // std::filesystem::path log_file = logs.logfile().c_str(); - // auto log_directory = log_file.parent_path(); - // auto myid = std::this_thread::get_id(); - // std::stringstream ss; - // ss << myid; - // auto log_file_per_thread = log_directory / (std::string("thread_") + ss.str() + ".log"); - // // TODO - // log_writer.open(log_file_per_thread, std::ofstream::out | std::ofstream::app); - // log_streams.push_back(&log_writer); solver->EnableOutput(&ortools_logger); } +OrtoolsLogHandler::OrtoolsLogHandler() +{ + std::filesystem::path log_file = logs.logfile().c_str(); + auto log_directory = log_file.parent_path(); + auto myid = std::this_thread::get_id(); + std::stringstream ss; + ss << myid; + auto log_file_per_thread = log_directory / (std::string("thread_") + ss.str() + ".log"); + log_writer_.open(log_file_per_thread, std::ofstream::out | std::ofstream::app); +} ProblemSimplexeNommeConverter::ProblemSimplexeNommeConverter( const std::string& solverName, const Antares::Optimization::PROBLEME_SIMPLEXE_NOMME* problemeSimplexe) : diff --git a/src/solver/utils/ortools_utils.h b/src/solver/utils/ortools_utils.h index 6045acfc69..890f80eaf6 100644 --- a/src/solver/utils/ortools_utils.h +++ b/src/solver/utils/ortools_utils.h @@ -12,16 +12,6 @@ using namespace operations_research; -class OrtoolsLogHandler : public LogHandlerInterface -{ -public: - // tmp test with std::cout - explicit OrtoolsLogHandler() = default; - void message(const char* msg) override - { - std::cout << msg << std::endl; - } -}; void ORTOOLS_EcrireJeuDeDonneesLineaireAuFormatMPS(MPSolver* solver, Antares::Solver::IResultWriter& writer, const std::string& filename); @@ -58,6 +48,26 @@ namespace Antares { namespace Optimization { + +class OrtoolsLogHandler : public LogHandlerInterface +{ +public: + // tmp test with std::cout + explicit OrtoolsLogHandler(); + void message(const char* msg) override + { + log_writer_ << msg << std::endl; + } + +private: + // TODO won't work in ci, needs ortools update + // see https://github.com/rte-france/or-tools/pull/112 + + // // TODO + std::ofstream log_writer_; + // log_writer.open(log_file_per_thread, std::ofstream::out | std::ofstream::app); + // log_streams.push_back(&log_writer); +}; void setOrtoolsSolverLogs(MPSolver* solver, OrtoolsLogHandler &ortools_logger); class Nomenclature { From 81ef7b078ee5fc56f7547ad6e1e3132bb4f3ba8f Mon Sep 17 00:00:00 2001 From: abdoulbari zakir <32519851+a-zakir@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:01:18 +0100 Subject: [PATCH 07/27] suggestion by @flomnes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Florian Omnès <26088210+flomnes@users.noreply.github.com> --- src/solver/optimisation/opt_appel_solveur_lineaire.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/solver/optimisation/opt_appel_solveur_lineaire.cpp b/src/solver/optimisation/opt_appel_solveur_lineaire.cpp index 373aeda4f8..589d6838e2 100644 --- a/src/solver/optimisation/opt_appel_solveur_lineaire.cpp +++ b/src/solver/optimisation/opt_appel_solveur_lineaire.cpp @@ -399,7 +399,7 @@ bool OPT_AppelDuSimplexe(const OptimizationOptions& options, if (Probleme.SolverLogs()) { - setOrtoolsSolverLogs(MPproblem.get(), ortools_logger); + MPproblem->EnableOutput(&ortools_logger); } auto analyzer = makeUnfeasiblePbAnalyzer(); analyzer->run(MPproblem.get()); From c93538961826d214faa35ee63e0bc4b99b3d2814 Mon Sep 17 00:00:00 2001 From: Abdoulbari Zakir Date: Fri, 17 Nov 2023 11:48:00 +0100 Subject: [PATCH 08/27] direct call --- src/solver/optimisation/opt_appel_solveur_lineaire.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/solver/optimisation/opt_appel_solveur_lineaire.cpp b/src/solver/optimisation/opt_appel_solveur_lineaire.cpp index 589d6838e2..14336b6663 100644 --- a/src/solver/optimisation/opt_appel_solveur_lineaire.cpp +++ b/src/solver/optimisation/opt_appel_solveur_lineaire.cpp @@ -242,12 +242,10 @@ static SimplexResult OPT_TryToCallSimplex(const OptimizationOptions& options, if (options.useOrtools) { const bool keepBasis = (optimizationNumber == PREMIERE_OPTIMISATION); - // std::ofstream log_writer; // one per thread - // std::vector log_streams; if (Probleme.SolverLogs()) { - setOrtoolsSolverLogs(solver, ortools_logger); + solver->EnableOutput(&ortools_logger); } solver = ORTOOLS_Simplexe(&Probleme, solver, keepBasis); if (solver != nullptr) From b2a612ad37e8f6ea840173bb75a594637360a179 Mon Sep 17 00:00:00 2001 From: Abdoulbari Zakir Date: Fri, 17 Nov 2023 11:48:29 +0100 Subject: [PATCH 09/27] delete deprecated method --- src/solver/utils/ortools_utils.cpp | 4 ---- src/solver/utils/ortools_utils.h | 1 - 2 files changed, 5 deletions(-) diff --git a/src/solver/utils/ortools_utils.cpp b/src/solver/utils/ortools_utils.cpp index 820255b252..ef8e62edcf 100644 --- a/src/solver/utils/ortools_utils.cpp +++ b/src/solver/utils/ortools_utils.cpp @@ -32,10 +32,6 @@ namespace Antares { namespace Optimization { -void setOrtoolsSolverLogs(MPSolver* solver, OrtoolsLogHandler& ortools_logger) -{ - solver->EnableOutput(&ortools_logger); -} OrtoolsLogHandler::OrtoolsLogHandler() { diff --git a/src/solver/utils/ortools_utils.h b/src/solver/utils/ortools_utils.h index 890f80eaf6..dfb649c145 100644 --- a/src/solver/utils/ortools_utils.h +++ b/src/solver/utils/ortools_utils.h @@ -68,7 +68,6 @@ class OrtoolsLogHandler : public LogHandlerInterface // log_writer.open(log_file_per_thread, std::ofstream::out | std::ofstream::app); // log_streams.push_back(&log_writer); }; -void setOrtoolsSolverLogs(MPSolver* solver, OrtoolsLogHandler &ortools_logger); class Nomenclature { public: From 277564235e6e916af6bef87fc914540e1ac8b2ef Mon Sep 17 00:00:00 2001 From: Abdoulbari Zakir Date: Fri, 17 Nov 2023 16:55:06 +0100 Subject: [PATCH 10/27] [skip ci] set field width for incoming msg --- src/solver/utils/ortools_utils.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/solver/utils/ortools_utils.h b/src/solver/utils/ortools_utils.h index dfb649c145..e394d7ad52 100644 --- a/src/solver/utils/ortools_utils.h +++ b/src/solver/utils/ortools_utils.h @@ -54,9 +54,9 @@ class OrtoolsLogHandler : public LogHandlerInterface public: // tmp test with std::cout explicit OrtoolsLogHandler(); - void message(const char* msg) override + void message(const char* msg, int nLen = 0) override { - log_writer_ << msg << std::endl; + log_writer_ << std::setw(nLen) << msg << std::endl; } private: From a69f2dfc7832e7e241ed71c390b0676485dc3a67 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Fri, 17 Nov 2023 19:04:42 +0100 Subject: [PATCH 11/27] test SiriusCallback --- src/solver/optimisation/opt_appel_solveur_lineaire.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/solver/optimisation/opt_appel_solveur_lineaire.cpp b/src/solver/optimisation/opt_appel_solveur_lineaire.cpp index 373aeda4f8..f83d13bf18 100644 --- a/src/solver/optimisation/opt_appel_solveur_lineaire.cpp +++ b/src/solver/optimisation/opt_appel_solveur_lineaire.cpp @@ -36,7 +36,7 @@ extern "C" { #include "spx_definition_arguments.h" #include "spx_fonctions.h" - +#include "sirius_callback.h" #include "srs_api.h" } @@ -96,7 +96,11 @@ struct SimplexResult long long solveTime = 0; mpsWriterFactory mps_writer_factory; }; - +int SiriusCallback_function(void* caller, const char* sMsg, int nLen, SIRIUS_LOGLEVEL log_level) +{ + auto* stdcout = reinterpret_cast(caller); + (*stdcout) << sMsg; +} static SimplexResult OPT_TryToCallSimplex(const OptimizationOptions& options, PROBLEME_HEBDO* problemeHebdo, Optimization::PROBLEME_SIMPLEXE_NOMME& Probleme, @@ -220,6 +224,8 @@ static SimplexResult OPT_TryToCallSimplex(const OptimizationOptions& options, Probleme.CoutsReduits = ProblemeAResoudre->CoutsReduits.data(); Probleme.NombreDeContraintesCoupes = 0; + Probleme.callback = SiriusCallback_function; + Probleme.caller = &std::cout; if (options.useOrtools) { From 5769816ee73a323a87ee39660186d0a69bdf0c58 Mon Sep 17 00:00:00 2001 From: Abdoulbari Zakir Date: Fri, 17 Nov 2023 19:07:04 +0100 Subject: [PATCH 12/27] [skip ci] fix --- src/solver/optimisation/opt_appel_solveur_lineaire.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/solver/optimisation/opt_appel_solveur_lineaire.cpp b/src/solver/optimisation/opt_appel_solveur_lineaire.cpp index c47be24780..853dc2c293 100644 --- a/src/solver/optimisation/opt_appel_solveur_lineaire.cpp +++ b/src/solver/optimisation/opt_appel_solveur_lineaire.cpp @@ -100,6 +100,7 @@ int SiriusCallback_function(void* caller, const char* sMsg, int nLen, SIRIUS_LOG { auto* stdcout = reinterpret_cast(caller); (*stdcout) << sMsg; + return 0; } static SimplexResult OPT_TryToCallSimplex(const OptimizationOptions& options, PROBLEME_HEBDO* problemeHebdo, From 8c615aeaa85df49283f9d19830422858a4afc12d Mon Sep 17 00:00:00 2001 From: Abdoulbari Zakir Date: Fri, 17 Nov 2023 19:58:16 +0100 Subject: [PATCH 13/27] test Sirius callback: ok --- src/solver/optimisation/opt_appel_solveur_lineaire.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/solver/optimisation/opt_appel_solveur_lineaire.cpp b/src/solver/optimisation/opt_appel_solveur_lineaire.cpp index 853dc2c293..a53ca4b031 100644 --- a/src/solver/optimisation/opt_appel_solveur_lineaire.cpp +++ b/src/solver/optimisation/opt_appel_solveur_lineaire.cpp @@ -225,8 +225,12 @@ static SimplexResult OPT_TryToCallSimplex(const OptimizationOptions& options, Probleme.CoutsReduits = ProblemeAResoudre->CoutsReduits.data(); Probleme.NombreDeContraintesCoupes = 0; - Probleme.callback = SiriusCallback_function; - Probleme.caller = &std::cout; + // this must be done in Sirius + // auto call_back = (callback_function)Probleme.callback; + // call_back = SiriusCallback_function; + // SPXsetcbmessage(ProbSpx, SiriusCallback_function, &std::cout, SIRIUS_INFO); + SPLXsetcbmessage(&Probleme, SiriusCallback_function, &std::cout, SIRIUS_INFO); + // Probleme.caller = &std::cout; if (options.useOrtools) { From c8d8f827156faa716899bbac4d10804e85279aa9 Mon Sep 17 00:00:00 2001 From: Abdoulbari Zakir Date: Thu, 23 Nov 2023 12:38:00 +0100 Subject: [PATCH 14/27] adaptation for COIN solvers --- .../opt_appel_solveur_lineaire.cpp | 7 +- src/solver/utils/ortools_utils.cpp | 74 +++++++++++++++---- src/solver/utils/ortools_utils.h | 28 ++++++- 3 files changed, 91 insertions(+), 18 deletions(-) diff --git a/src/solver/optimisation/opt_appel_solveur_lineaire.cpp b/src/solver/optimisation/opt_appel_solveur_lineaire.cpp index a53ca4b031..b50188607f 100644 --- a/src/solver/optimisation/opt_appel_solveur_lineaire.cpp +++ b/src/solver/optimisation/opt_appel_solveur_lineaire.cpp @@ -229,8 +229,9 @@ static SimplexResult OPT_TryToCallSimplex(const OptimizationOptions& options, // auto call_back = (callback_function)Probleme.callback; // call_back = SiriusCallback_function; // SPXsetcbmessage(ProbSpx, SiriusCallback_function, &std::cout, SIRIUS_INFO); - SPLXsetcbmessage(&Probleme, SiriusCallback_function, &std::cout, SIRIUS_INFO); - // Probleme.caller = &std::cout; + /* postponed */ // SPLXsetcbmessage(&Probleme, SiriusCallback_function, &std::cout, + // SIRIUS_INFO); + // Probleme.caller = &std::cout; if (options.useOrtools) { @@ -326,7 +327,7 @@ bool OPT_AppelDuSimplexe(const OptimizationOptions& options, bool PremierPassage = true; // tmp - OrtoolsLogHandler ortools_logger; + OrtoolsLogHandler ortools_logger(options.solverName); struct SimplexResult simplexResult = OPT_TryToCallSimplex(options, problemeHebdo, Probleme, diff --git a/src/solver/utils/ortools_utils.cpp b/src/solver/utils/ortools_utils.cpp index ef8e62edcf..85b1c1e751 100644 --- a/src/solver/utils/ortools_utils.cpp +++ b/src/solver/utils/ortools_utils.cpp @@ -33,16 +33,63 @@ namespace Antares namespace Optimization { -OrtoolsLogHandler::OrtoolsLogHandler() -{ - std::filesystem::path log_file = logs.logfile().c_str(); - auto log_directory = log_file.parent_path(); - auto myid = std::this_thread::get_id(); - std::stringstream ss; - ss << myid; - auto log_file_per_thread = log_directory / (std::string("thread_") + ss.str() + ".log"); - log_writer_.open(log_file_per_thread, std::ofstream::out | std::ofstream::app); +OrtoolsLogHandler::OrtoolsLogHandler(const std::string& solver_name) : solver_name_(solver_name) +{ + init(); } + +OrtoolsLogHandler& OrtoolsLogHandler::operator=(const OrtoolsLogHandler& other) +{ + if (this == &other) + { + return *this; + } + init(); + return *this; +} +void OrtoolsLogHandler::init() +{ + if (solver_name_ == COIN) + { + std::filesystem::path log_file = logs.logfile().c_str(); + +#ifdef __linux__ + if (log_file.empty() || (file_pointer = fopen(log_file.string().c_str(), "a+")) == nullptr) +#elif _WIN32 + if (log_file.empty() + || (file_pointer = _fsopen(log_file.string().c_str(), "a+", _SH_DENYNO)) == nullptr) +#endif + { + // logs.error() + std::cerr << "Invalid log file name passed as parameter: " + << std::quoted(log_file.string()) << std::endl; + } + else + { + setvbuf(file_pointer, nullptr, _IONBF, 0); + } + } + else + { + std::filesystem::path log_file = logs.logfile().c_str(); + auto log_directory = log_file.parent_path(); + auto myid = std::this_thread::get_id(); + std::stringstream ss; + ss << myid; + auto log_file_per_thread = log_directory / (std::string("thread_") + ss.str() + ".log"); + log_writer_.open(log_file_per_thread, std::ofstream::out | std::ofstream::app); + } +} + +OrtoolsLogHandler::~OrtoolsLogHandler() +{ + if (file_pointer) + { + fclose(file_pointer); + file_pointer = nullptr; + } +} + ProblemSimplexeNommeConverter::ProblemSimplexeNommeConverter( const std::string& solverName, const Antares::Optimization::PROBLEME_SIMPLEXE_NOMME* problemeSimplexe) : @@ -380,12 +427,11 @@ void ORTOOLS_LibererProbleme(MPSolver* solver) { delete solver; } - const std::map OrtoolsUtils::solverMap - = {{"xpress", {"xpress_lp", "xpress"}}, - {"sirius", {"sirius_lp", "sirius"}}, - {"coin", {"clp", "cbc"}}, - {"glpk", {"glpk_lp", "glpk"}}}; + = {{XPRESS, {XPRESS_LP, XPRESS}}, + {SIRIUS, {SIRIUS_LP, SIRIUS}}, + {COIN, {CLP, CBC}}, + {GLPK, {GLPK_LP, GLPK}}}; std::list getAvailableOrtoolsSolverName() { diff --git a/src/solver/utils/ortools_utils.h b/src/solver/utils/ortools_utils.h index e394d7ad52..1d616411dd 100644 --- a/src/solver/utils/ortools_utils.h +++ b/src/solver/utils/ortools_utils.h @@ -12,6 +12,16 @@ using namespace operations_research; +const std::string XPRESS = "xpress"; +const std::string XPRESS_LP = "xpress_lp"; +const std::string SIRIUS = "sirius"; +const std::string SIRIUS_LP = "sirius_lp"; +const std::string GLPK = "glpk"; +const std::string GLPK_LP = "glpk_lp"; +const std::string COIN = "coin"; +const std::string CBC = "cbc"; +const std::string CLP = "clp"; + void ORTOOLS_EcrireJeuDeDonneesLineaireAuFormatMPS(MPSolver* solver, Antares::Solver::IResultWriter& writer, const std::string& filename); @@ -53,13 +63,27 @@ class OrtoolsLogHandler : public LogHandlerInterface { public: // tmp test with std::cout - explicit OrtoolsLogHandler(); + explicit OrtoolsLogHandler(const std::string& solverName); + + explicit OrtoolsLogHandler(const OrtoolsLogHandler& other) : + OrtoolsLogHandler(other.solver_name_) + { + } + + OrtoolsLogHandler& operator=(const OrtoolsLogHandler& other); + ~OrtoolsLogHandler(); void message(const char* msg, int nLen = 0) override { log_writer_ << std::setw(nLen) << msg << std::endl; } + FILE* where_to_write() + { + return file_pointer; + } + private: + void init(); // TODO won't work in ci, needs ortools update // see https://github.com/rte-france/or-tools/pull/112 @@ -67,6 +91,8 @@ class OrtoolsLogHandler : public LogHandlerInterface std::ofstream log_writer_; // log_writer.open(log_file_per_thread, std::ofstream::out | std::ofstream::app); // log_streams.push_back(&log_writer); + FILE* file_pointer = nullptr; + std::string solver_name_; }; class Nomenclature { From 1535d8c3dc34917329fc0f09bda7f62694708304 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Thu, 23 Nov 2023 14:57:05 +0100 Subject: [PATCH 15/27] bug --- src/solver/utils/ortools_utils.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/solver/utils/ortools_utils.cpp b/src/solver/utils/ortools_utils.cpp index 85b1c1e751..2502a1be9f 100644 --- a/src/solver/utils/ortools_utils.cpp +++ b/src/solver/utils/ortools_utils.cpp @@ -44,6 +44,7 @@ OrtoolsLogHandler& OrtoolsLogHandler::operator=(const OrtoolsLogHandler& other) { return *this; } + solver_name_ = other.solver_name_; init(); return *this; } From b1dce6947f86c4bbdda865b6db188e59c1ee7c3f Mon Sep 17 00:00:00 2001 From: Abdoulbari Zakir Date: Thu, 23 Nov 2023 15:10:32 +0100 Subject: [PATCH 16/27] set log files per thread --- src/solver/utils/ortools_utils.cpp | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/solver/utils/ortools_utils.cpp b/src/solver/utils/ortools_utils.cpp index 85b1c1e751..54dafd398d 100644 --- a/src/solver/utils/ortools_utils.cpp +++ b/src/solver/utils/ortools_utils.cpp @@ -49,20 +49,27 @@ OrtoolsLogHandler& OrtoolsLogHandler::operator=(const OrtoolsLogHandler& other) } void OrtoolsLogHandler::init() { + std::filesystem::path log_file = logs.logfile().c_str(); + auto log_directory = log_file.parent_path(); + auto myid = std::this_thread::get_id(); + std::stringstream ss; + ss << myid; + auto log_file_per_thread = log_directory / (std::string("thread_") + ss.str() + ".log"); + if (solver_name_ == COIN) { - std::filesystem::path log_file = logs.logfile().c_str(); - #ifdef __linux__ - if (log_file.empty() || (file_pointer = fopen(log_file.string().c_str(), "a+")) == nullptr) + if (log_file_per_thread.empty() + || (file_pointer = fopen(log_file_per_thread.string().c_str(), "a+")) == nullptr) #elif _WIN32 - if (log_file.empty() - || (file_pointer = _fsopen(log_file.string().c_str(), "a+", _SH_DENYNO)) == nullptr) + if (log_file_per_thread.empty() + || (file_pointer = _fsopen(log_file_per_thread.string().c_str(), "a+", _SH_DENYNO)) + == nullptr) #endif { // logs.error() std::cerr << "Invalid log file name passed as parameter: " - << std::quoted(log_file.string()) << std::endl; + << std::quoted(log_file_per_thread.string()) << std::endl; } else { @@ -71,12 +78,6 @@ void OrtoolsLogHandler::init() } else { - std::filesystem::path log_file = logs.logfile().c_str(); - auto log_directory = log_file.parent_path(); - auto myid = std::this_thread::get_id(); - std::stringstream ss; - ss << myid; - auto log_file_per_thread = log_directory / (std::string("thread_") + ss.str() + ".log"); log_writer_.open(log_file_per_thread, std::ofstream::out | std::ofstream::app); } } From 30853d1a92c2c93d6bfa02dbe024baf7942d2e0d Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Thu, 23 Nov 2023 15:11:18 +0100 Subject: [PATCH 17/27] tmp Revert "bug" This reverts commit 1535d8c3dc34917329fc0f09bda7f62694708304. --- src/solver/utils/ortools_utils.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/solver/utils/ortools_utils.cpp b/src/solver/utils/ortools_utils.cpp index 2502a1be9f..85b1c1e751 100644 --- a/src/solver/utils/ortools_utils.cpp +++ b/src/solver/utils/ortools_utils.cpp @@ -44,7 +44,6 @@ OrtoolsLogHandler& OrtoolsLogHandler::operator=(const OrtoolsLogHandler& other) { return *this; } - solver_name_ = other.solver_name_; init(); return *this; } From df4a67185bd8bdc26e18a4264cfb40edfa888c47 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Thu, 23 Nov 2023 15:38:02 +0100 Subject: [PATCH 18/27] copy log files --- .../opt_appel_solveur_lineaire.cpp | 5 +-- src/solver/utils/ortools_utils.cpp | 31 +++++++++++++------ src/solver/utils/ortools_utils.h | 9 ++++-- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/solver/optimisation/opt_appel_solveur_lineaire.cpp b/src/solver/optimisation/opt_appel_solveur_lineaire.cpp index b50188607f..4a9f8b9ed4 100644 --- a/src/solver/optimisation/opt_appel_solveur_lineaire.cpp +++ b/src/solver/optimisation/opt_appel_solveur_lineaire.cpp @@ -327,7 +327,7 @@ bool OPT_AppelDuSimplexe(const OptimizationOptions& options, bool PremierPassage = true; // tmp - OrtoolsLogHandler ortools_logger(options.solverName); + OrtoolsLogHandler ortools_logger(options.solverName, logs.logfile().c_str()); struct SimplexResult simplexResult = OPT_TryToCallSimplex(options, problemeHebdo, Probleme, @@ -421,6 +421,7 @@ bool OPT_AppelDuSimplexe(const OptimizationOptions& options, return false; } - + // copy log file(s) + ortools_logger.copy_log(writer); return true; } diff --git a/src/solver/utils/ortools_utils.cpp b/src/solver/utils/ortools_utils.cpp index 54dafd398d..16d26c195d 100644 --- a/src/solver/utils/ortools_utils.cpp +++ b/src/solver/utils/ortools_utils.cpp @@ -33,7 +33,9 @@ namespace Antares namespace Optimization { -OrtoolsLogHandler::OrtoolsLogHandler(const std::string& solver_name) : solver_name_(solver_name) +OrtoolsLogHandler::OrtoolsLogHandler(const std::string& solver_name, + const std::filesystem::path& logs_directory) : + solver_name_(solver_name), logs_directory_(logs_directory) { init(); } @@ -44,32 +46,33 @@ OrtoolsLogHandler& OrtoolsLogHandler::operator=(const OrtoolsLogHandler& other) { return *this; } + + solver_name_ = other.solver_name_; + logs_directory_ = other.logs_directory_; init(); return *this; } void OrtoolsLogHandler::init() { - std::filesystem::path log_file = logs.logfile().c_str(); - auto log_directory = log_file.parent_path(); auto myid = std::this_thread::get_id(); std::stringstream ss; ss << myid; - auto log_file_per_thread = log_directory / (std::string("thread_") + ss.str() + ".log"); + log_file_per_thread_ = log_directory_ / (std::string("thread_") + ss.str() + ".log"); if (solver_name_ == COIN) { #ifdef __linux__ - if (log_file_per_thread.empty() - || (file_pointer = fopen(log_file_per_thread.string().c_str(), "a+")) == nullptr) + if (log_file_per_thread_.empty() + || (file_pointer = fopen(log_file_per_thread_.string().c_str(), "a+")) == nullptr) #elif _WIN32 - if (log_file_per_thread.empty() - || (file_pointer = _fsopen(log_file_per_thread.string().c_str(), "a+", _SH_DENYNO)) + if (log_file_per_thread_.empty() + || (file_pointer = _fsopen(log_file_per_thread_.string().c_str(), "a+", _SH_DENYNO)) == nullptr) #endif { // logs.error() std::cerr << "Invalid log file name passed as parameter: " - << std::quoted(log_file_per_thread.string()) << std::endl; + << std::quoted(log_file_per_thread_.string()) << std::endl; } else { @@ -78,7 +81,7 @@ void OrtoolsLogHandler::init() } else { - log_writer_.open(log_file_per_thread, std::ofstream::out | std::ofstream::app); + log_writer_.open(log_file_per_thread_, std::ofstream::out | std::ofstream::app); } } @@ -91,6 +94,14 @@ OrtoolsLogHandler::~OrtoolsLogHandler() } } +void OrtoolsLogHandler::copy_log(Solver::IResultWriter& writer) const +{ + auto log_file_per_thread = log_file_per_thread_.string().c_str(); + auto tmp_path_log_per_thread = generateTempPath(log_file_per_thread); + writer.addEntryFromFile(log_file_per_thread, tmp_path_log_per_thread); + removeTemporaryFile(tmp_path_log_per_thread); +} + ProblemSimplexeNommeConverter::ProblemSimplexeNommeConverter( const std::string& solverName, const Antares::Optimization::PROBLEME_SIMPLEXE_NOMME* problemeSimplexe) : diff --git a/src/solver/utils/ortools_utils.h b/src/solver/utils/ortools_utils.h index 1d616411dd..8ddeb71c9d 100644 --- a/src/solver/utils/ortools_utils.h +++ b/src/solver/utils/ortools_utils.h @@ -63,10 +63,11 @@ class OrtoolsLogHandler : public LogHandlerInterface { public: // tmp test with std::cout - explicit OrtoolsLogHandler(const std::string& solverName); + explicit OrtoolsLogHandler(const std::string& solverName, + const std::filesystem::path& logs_directory); explicit OrtoolsLogHandler(const OrtoolsLogHandler& other) : - OrtoolsLogHandler(other.solver_name_) + OrtoolsLogHandler(other.solver_name_, other.logs_directory_) { } @@ -82,6 +83,8 @@ class OrtoolsLogHandler : public LogHandlerInterface return file_pointer; } + void copy_log(Solver::IResultWriter& writer) const; + private: void init(); // TODO won't work in ci, needs ortools update @@ -93,6 +96,8 @@ class OrtoolsLogHandler : public LogHandlerInterface // log_streams.push_back(&log_writer); FILE* file_pointer = nullptr; std::string solver_name_; + std::filesystem::path& log_directory_ = "."; + std::filesystem::path& log_file_per_thread_ = ""; }; class Nomenclature { From 3a24ba165b9f8cf8644b234cae1be0c3c4b8af2b Mon Sep 17 00:00:00 2001 From: Abdoulbari Zakir Date: Thu, 23 Nov 2023 15:39:44 +0100 Subject: [PATCH 19/27] remove & --- src/solver/utils/ortools_utils.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/solver/utils/ortools_utils.h b/src/solver/utils/ortools_utils.h index 8ddeb71c9d..b1af40e1a0 100644 --- a/src/solver/utils/ortools_utils.h +++ b/src/solver/utils/ortools_utils.h @@ -96,8 +96,8 @@ class OrtoolsLogHandler : public LogHandlerInterface // log_streams.push_back(&log_writer); FILE* file_pointer = nullptr; std::string solver_name_; - std::filesystem::path& log_directory_ = "."; - std::filesystem::path& log_file_per_thread_ = ""; + std::filesystem::path log_directory_ = "."; + std::filesystem::path log_file_per_thread_ = ""; }; class Nomenclature { From 14b115030e07b96943998f48255c1023b2bb293c Mon Sep 17 00:00:00 2001 From: Abdoulbari Zakir Date: Thu, 23 Nov 2023 15:44:15 +0100 Subject: [PATCH 20/27] fix --- src/solver/utils/ortools_utils.cpp | 6 +++--- src/solver/utils/ortools_utils.h | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/solver/utils/ortools_utils.cpp b/src/solver/utils/ortools_utils.cpp index 16d26c195d..e4ed1647eb 100644 --- a/src/solver/utils/ortools_utils.cpp +++ b/src/solver/utils/ortools_utils.cpp @@ -34,8 +34,8 @@ namespace Optimization { OrtoolsLogHandler::OrtoolsLogHandler(const std::string& solver_name, - const std::filesystem::path& logs_directory) : - solver_name_(solver_name), logs_directory_(logs_directory) + const std::filesystem::path& log_directory) : + solver_name_(solver_name), log_directory_(log_directory) { init(); } @@ -48,7 +48,7 @@ OrtoolsLogHandler& OrtoolsLogHandler::operator=(const OrtoolsLogHandler& other) } solver_name_ = other.solver_name_; - logs_directory_ = other.logs_directory_; + log_directory_ = other.log_directory_; init(); return *this; } diff --git a/src/solver/utils/ortools_utils.h b/src/solver/utils/ortools_utils.h index b1af40e1a0..2df29a9d52 100644 --- a/src/solver/utils/ortools_utils.h +++ b/src/solver/utils/ortools_utils.h @@ -64,10 +64,10 @@ class OrtoolsLogHandler : public LogHandlerInterface public: // tmp test with std::cout explicit OrtoolsLogHandler(const std::string& solverName, - const std::filesystem::path& logs_directory); + const std::filesystem::path& log_directory); explicit OrtoolsLogHandler(const OrtoolsLogHandler& other) : - OrtoolsLogHandler(other.solver_name_, other.logs_directory_) + OrtoolsLogHandler(other.solver_name_, other.log_directory_) { } From 00926bc404fc62292b289dad372e391084a5c558 Mon Sep 17 00:00:00 2001 From: Abdoulbari Zakir Date: Thu, 23 Nov 2023 16:13:37 +0100 Subject: [PATCH 21/27] ok --- src/solver/optimisation/opt_appel_solveur_lineaire.cpp | 4 +++- src/solver/utils/ortools_utils.cpp | 6 ++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/solver/optimisation/opt_appel_solveur_lineaire.cpp b/src/solver/optimisation/opt_appel_solveur_lineaire.cpp index 4a9f8b9ed4..1e585ec2b9 100644 --- a/src/solver/optimisation/opt_appel_solveur_lineaire.cpp +++ b/src/solver/optimisation/opt_appel_solveur_lineaire.cpp @@ -327,7 +327,9 @@ bool OPT_AppelDuSimplexe(const OptimizationOptions& options, bool PremierPassage = true; // tmp - OrtoolsLogHandler ortools_logger(options.solverName, logs.logfile().c_str()); + std::filesystem::path log_file = logs.logfile().c_str(); + auto log_directory = log_file.parent_path(); + OrtoolsLogHandler ortools_logger(options.solverName, log_directory); struct SimplexResult simplexResult = OPT_TryToCallSimplex(options, problemeHebdo, Probleme, diff --git a/src/solver/utils/ortools_utils.cpp b/src/solver/utils/ortools_utils.cpp index e4ed1647eb..2ba68547d1 100644 --- a/src/solver/utils/ortools_utils.cpp +++ b/src/solver/utils/ortools_utils.cpp @@ -96,10 +96,8 @@ OrtoolsLogHandler::~OrtoolsLogHandler() void OrtoolsLogHandler::copy_log(Solver::IResultWriter& writer) const { - auto log_file_per_thread = log_file_per_thread_.string().c_str(); - auto tmp_path_log_per_thread = generateTempPath(log_file_per_thread); - writer.addEntryFromFile(log_file_per_thread, tmp_path_log_per_thread); - removeTemporaryFile(tmp_path_log_per_thread); + writer.addEntryFromFile(log_file_per_thread_.filename().string().c_str(), + log_file_per_thread_.string().c_str()); } ProblemSimplexeNommeConverter::ProblemSimplexeNommeConverter( From 0a44ba7663e16b5bd32a4bb6b8c2668b6631a550 Mon Sep 17 00:00:00 2001 From: Abdoulbari Zakir Date: Thu, 23 Nov 2023 16:26:50 +0100 Subject: [PATCH 22/27] clean&clear --- .../optimisation/opt_appel_solveur_lineaire.cpp | 15 --------------- src/solver/utils/ortools_utils.h | 8 ++------ 2 files changed, 2 insertions(+), 21 deletions(-) diff --git a/src/solver/optimisation/opt_appel_solveur_lineaire.cpp b/src/solver/optimisation/opt_appel_solveur_lineaire.cpp index 1e585ec2b9..74ae570ca7 100644 --- a/src/solver/optimisation/opt_appel_solveur_lineaire.cpp +++ b/src/solver/optimisation/opt_appel_solveur_lineaire.cpp @@ -36,7 +36,6 @@ extern "C" { #include "spx_definition_arguments.h" #include "spx_fonctions.h" -#include "sirius_callback.h" #include "srs_api.h" } @@ -96,12 +95,6 @@ struct SimplexResult long long solveTime = 0; mpsWriterFactory mps_writer_factory; }; -int SiriusCallback_function(void* caller, const char* sMsg, int nLen, SIRIUS_LOGLEVEL log_level) -{ - auto* stdcout = reinterpret_cast(caller); - (*stdcout) << sMsg; - return 0; -} static SimplexResult OPT_TryToCallSimplex(const OptimizationOptions& options, PROBLEME_HEBDO* problemeHebdo, Optimization::PROBLEME_SIMPLEXE_NOMME& Probleme, @@ -225,13 +218,6 @@ static SimplexResult OPT_TryToCallSimplex(const OptimizationOptions& options, Probleme.CoutsReduits = ProblemeAResoudre->CoutsReduits.data(); Probleme.NombreDeContraintesCoupes = 0; - // this must be done in Sirius - // auto call_back = (callback_function)Probleme.callback; - // call_back = SiriusCallback_function; - // SPXsetcbmessage(ProbSpx, SiriusCallback_function, &std::cout, SIRIUS_INFO); - /* postponed */ // SPLXsetcbmessage(&Probleme, SiriusCallback_function, &std::cout, - // SIRIUS_INFO); - // Probleme.caller = &std::cout; if (options.useOrtools) { @@ -326,7 +312,6 @@ bool OPT_AppelDuSimplexe(const OptimizationOptions& options, problemeHebdo->solverLogs); bool PremierPassage = true; - // tmp std::filesystem::path log_file = logs.logfile().c_str(); auto log_directory = log_file.parent_path(); OrtoolsLogHandler ortools_logger(options.solverName, log_directory); diff --git a/src/solver/utils/ortools_utils.h b/src/solver/utils/ortools_utils.h index 2df29a9d52..1b0c9fe680 100644 --- a/src/solver/utils/ortools_utils.h +++ b/src/solver/utils/ortools_utils.h @@ -62,7 +62,8 @@ namespace Optimization class OrtoolsLogHandler : public LogHandlerInterface { public: - // tmp test with std::cout + // TODO won't work in ci, needs ortools update + // see https://github.com/rte-france/or-tools/pull/112 explicit OrtoolsLogHandler(const std::string& solverName, const std::filesystem::path& log_directory); @@ -87,13 +88,8 @@ class OrtoolsLogHandler : public LogHandlerInterface private: void init(); - // TODO won't work in ci, needs ortools update - // see https://github.com/rte-france/or-tools/pull/112 - // // TODO std::ofstream log_writer_; - // log_writer.open(log_file_per_thread, std::ofstream::out | std::ofstream::app); - // log_streams.push_back(&log_writer); FILE* file_pointer = nullptr; std::string solver_name_; std::filesystem::path log_directory_ = "."; From 568e8f02c3e6523b07158c82ca3867e1090b0751 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Mon, 11 Dec 2023 14:58:38 +0100 Subject: [PATCH 23/27] update log handler --- src/solver/utils/ortools_utils.cpp | 12 ++++++------ src/solver/utils/ortools_utils.h | 5 +++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/solver/utils/ortools_utils.cpp b/src/solver/utils/ortools_utils.cpp index 2ba68547d1..3da6c8cf6c 100644 --- a/src/solver/utils/ortools_utils.cpp +++ b/src/solver/utils/ortools_utils.cpp @@ -63,10 +63,10 @@ void OrtoolsLogHandler::init() { #ifdef __linux__ if (log_file_per_thread_.empty() - || (file_pointer = fopen(log_file_per_thread_.string().c_str(), "a+")) == nullptr) + || (file_pointer_ = fopen(log_file_per_thread_.string().c_str(), "a+")) == nullptr) #elif _WIN32 if (log_file_per_thread_.empty() - || (file_pointer = _fsopen(log_file_per_thread_.string().c_str(), "a+", _SH_DENYNO)) + || (file_pointer_ = _fsopen(log_file_per_thread_.string().c_str(), "a+", _SH_DENYNO)) == nullptr) #endif { @@ -76,7 +76,7 @@ void OrtoolsLogHandler::init() } else { - setvbuf(file_pointer, nullptr, _IONBF, 0); + setvbuf(file_pointer_, nullptr, _IONBF, 0); } } else @@ -87,10 +87,10 @@ void OrtoolsLogHandler::init() OrtoolsLogHandler::~OrtoolsLogHandler() { - if (file_pointer) + if (file_pointer_) { - fclose(file_pointer); - file_pointer = nullptr; + fclose(file_pointer_); + file_pointer_ = nullptr; } } diff --git a/src/solver/utils/ortools_utils.h b/src/solver/utils/ortools_utils.h index 1b0c9fe680..a7a0da65f8 100644 --- a/src/solver/utils/ortools_utils.h +++ b/src/solver/utils/ortools_utils.h @@ -81,7 +81,7 @@ class OrtoolsLogHandler : public LogHandlerInterface FILE* where_to_write() { - return file_pointer; + return file_pointer_; } void copy_log(Solver::IResultWriter& writer) const; @@ -90,11 +90,12 @@ class OrtoolsLogHandler : public LogHandlerInterface void init(); std::ofstream log_writer_; - FILE* file_pointer = nullptr; + FILE* file_pointer_ = nullptr; std::string solver_name_; std::filesystem::path log_directory_ = "."; std::filesystem::path log_file_per_thread_ = ""; }; + class Nomenclature { public: From ae4c29fd40a7c21b3de8eccccfa0f8bb6257b25f Mon Sep 17 00:00:00 2001 From: abdoulbari zakir <32519851+a-zakir@users.noreply.github.com> Date: Thu, 14 Dec 2023 16:17:19 +0100 Subject: [PATCH 24/27] update log handler (#1793) --- src/solver/utils/ortools_utils.cpp | 12 ++++++------ src/solver/utils/ortools_utils.h | 5 +++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/solver/utils/ortools_utils.cpp b/src/solver/utils/ortools_utils.cpp index 2ba68547d1..3da6c8cf6c 100644 --- a/src/solver/utils/ortools_utils.cpp +++ b/src/solver/utils/ortools_utils.cpp @@ -63,10 +63,10 @@ void OrtoolsLogHandler::init() { #ifdef __linux__ if (log_file_per_thread_.empty() - || (file_pointer = fopen(log_file_per_thread_.string().c_str(), "a+")) == nullptr) + || (file_pointer_ = fopen(log_file_per_thread_.string().c_str(), "a+")) == nullptr) #elif _WIN32 if (log_file_per_thread_.empty() - || (file_pointer = _fsopen(log_file_per_thread_.string().c_str(), "a+", _SH_DENYNO)) + || (file_pointer_ = _fsopen(log_file_per_thread_.string().c_str(), "a+", _SH_DENYNO)) == nullptr) #endif { @@ -76,7 +76,7 @@ void OrtoolsLogHandler::init() } else { - setvbuf(file_pointer, nullptr, _IONBF, 0); + setvbuf(file_pointer_, nullptr, _IONBF, 0); } } else @@ -87,10 +87,10 @@ void OrtoolsLogHandler::init() OrtoolsLogHandler::~OrtoolsLogHandler() { - if (file_pointer) + if (file_pointer_) { - fclose(file_pointer); - file_pointer = nullptr; + fclose(file_pointer_); + file_pointer_ = nullptr; } } diff --git a/src/solver/utils/ortools_utils.h b/src/solver/utils/ortools_utils.h index 1b0c9fe680..a7a0da65f8 100644 --- a/src/solver/utils/ortools_utils.h +++ b/src/solver/utils/ortools_utils.h @@ -81,7 +81,7 @@ class OrtoolsLogHandler : public LogHandlerInterface FILE* where_to_write() { - return file_pointer; + return file_pointer_; } void copy_log(Solver::IResultWriter& writer) const; @@ -90,11 +90,12 @@ class OrtoolsLogHandler : public LogHandlerInterface void init(); std::ofstream log_writer_; - FILE* file_pointer = nullptr; + FILE* file_pointer_ = nullptr; std::string solver_name_; std::filesystem::path log_directory_ = "."; std::filesystem::path log_file_per_thread_ = ""; }; + class Nomenclature { public: From f1cb6aa1adf554e4d676d7c2a2cd3825f65e5170 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Fri, 15 Dec 2023 17:22:14 +0100 Subject: [PATCH 25/27] remove old code & update --- src/solver/utils/ortools_utils.cpp | 34 +----------------------------- src/solver/utils/ortools_utils.h | 9 +------- 2 files changed, 2 insertions(+), 41 deletions(-) diff --git a/src/solver/utils/ortools_utils.cpp b/src/solver/utils/ortools_utils.cpp index 3da6c8cf6c..ff43b20f65 100644 --- a/src/solver/utils/ortools_utils.cpp +++ b/src/solver/utils/ortools_utils.cpp @@ -59,39 +59,7 @@ void OrtoolsLogHandler::init() ss << myid; log_file_per_thread_ = log_directory_ / (std::string("thread_") + ss.str() + ".log"); - if (solver_name_ == COIN) - { -#ifdef __linux__ - if (log_file_per_thread_.empty() - || (file_pointer_ = fopen(log_file_per_thread_.string().c_str(), "a+")) == nullptr) -#elif _WIN32 - if (log_file_per_thread_.empty() - || (file_pointer_ = _fsopen(log_file_per_thread_.string().c_str(), "a+", _SH_DENYNO)) - == nullptr) -#endif - { - // logs.error() - std::cerr << "Invalid log file name passed as parameter: " - << std::quoted(log_file_per_thread_.string()) << std::endl; - } - else - { - setvbuf(file_pointer_, nullptr, _IONBF, 0); - } - } - else - { - log_writer_.open(log_file_per_thread_, std::ofstream::out | std::ofstream::app); - } -} - -OrtoolsLogHandler::~OrtoolsLogHandler() -{ - if (file_pointer_) - { - fclose(file_pointer_); - file_pointer_ = nullptr; - } + log_writer_.open(log_file_per_thread_, std::ofstream::out | std::ofstream::app); } void OrtoolsLogHandler::copy_log(Solver::IResultWriter& writer) const diff --git a/src/solver/utils/ortools_utils.h b/src/solver/utils/ortools_utils.h index a7a0da65f8..15928cd243 100644 --- a/src/solver/utils/ortools_utils.h +++ b/src/solver/utils/ortools_utils.h @@ -73,24 +73,17 @@ class OrtoolsLogHandler : public LogHandlerInterface } OrtoolsLogHandler& operator=(const OrtoolsLogHandler& other); - ~OrtoolsLogHandler(); void message(const char* msg, int nLen = 0) override { log_writer_ << std::setw(nLen) << msg << std::endl; } - FILE* where_to_write() - { - return file_pointer_; - } - - void copy_log(Solver::IResultWriter& writer) const; + void copy_log(Solver::IResultWriter& writer) const; private: void init(); std::ofstream log_writer_; - FILE* file_pointer_ = nullptr; std::string solver_name_; std::filesystem::path log_directory_ = "."; std::filesystem::path log_file_per_thread_ = ""; From e63468ccb2af139c1de97b2b35453c7fe884a63c Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Tue, 19 Dec 2023 11:35:05 +0100 Subject: [PATCH 26/27] use numSpace and thread number --- .../adequacy_patch_weekly_optimization.cpp | 6 ++++-- .../optimisation/opt_appel_solveur_lineaire.cpp | 5 +++-- src/solver/optimisation/opt_fonctions.h | 12 ++++++++---- .../optimisation/opt_optimisation_hebdo.cpp | 6 ++++-- .../optimisation/opt_optimisation_lineaire.cpp | 14 ++++++++------ .../opt_pilotage_optimisation_lineaire.cpp | 5 +++-- src/solver/optimisation/weekly_optimization.cpp | 3 ++- src/solver/simulation/adequacy.cpp | 5 +++-- src/solver/simulation/common-eco-adq.cpp | 14 ++++++++++---- src/solver/simulation/common-eco-adq.h | 3 ++- src/solver/simulation/economy.cpp | 2 +- src/solver/utils/ortools_utils.cpp | 17 +++++++++++------ src/solver/utils/ortools_utils.h | 4 +++- 13 files changed, 62 insertions(+), 34 deletions(-) diff --git a/src/solver/optimisation/adequacy_patch_local_matching/adequacy_patch_weekly_optimization.cpp b/src/solver/optimisation/adequacy_patch_local_matching/adequacy_patch_weekly_optimization.cpp index 9f0193912d..b1371c5255 100644 --- a/src/solver/optimisation/adequacy_patch_local_matching/adequacy_patch_weekly_optimization.cpp +++ b/src/solver/optimisation/adequacy_patch_local_matching/adequacy_patch_weekly_optimization.cpp @@ -49,7 +49,8 @@ AdequacyPatchOptimization::AdequacyPatchOptimization(const Antares::Data::Study& void AdequacyPatchOptimization::solve() { problemeHebdo_->adequacyPatchRuntimeData->AdequacyFirstStep = true; - OPT_OptimisationHebdomadaire(options_, problemeHebdo_, adqPatchParams_, writer_); + OPT_OptimisationHebdomadaire( + options_, problemeHebdo_, adqPatchParams_, writer_, thread_number_); problemeHebdo_->adequacyPatchRuntimeData->AdequacyFirstStep = false; for (uint32_t pays = 0; pays < problemeHebdo_->NombreDePays; ++pays) @@ -63,7 +64,8 @@ void AdequacyPatchOptimization::solve() problemeHebdo_->ResultatsHoraires[pays].ValeursHorairesDENS.end(), 0); } - OPT_OptimisationHebdomadaire(options_, problemeHebdo_, adqPatchParams_, writer_); + OPT_OptimisationHebdomadaire( + options_, problemeHebdo_, adqPatchParams_, writer_, thread_number_); } } // namespace Antares::Solver::Optimization diff --git a/src/solver/optimisation/opt_appel_solveur_lineaire.cpp b/src/solver/optimisation/opt_appel_solveur_lineaire.cpp index 74ae570ca7..6b8e5e115f 100644 --- a/src/solver/optimisation/opt_appel_solveur_lineaire.cpp +++ b/src/solver/optimisation/opt_appel_solveur_lineaire.cpp @@ -300,7 +300,8 @@ bool OPT_AppelDuSimplexe(const OptimizationOptions& options, int NumIntervalle, const int optimizationNumber, const OptPeriodStringGenerator& optPeriodStringGenerator, - IResultWriter& writer) + IResultWriter& writer, + uint thread_number) { const auto& ProblemeAResoudre = problemeHebdo->ProblemeAResoudre; Optimization::PROBLEME_SIMPLEXE_NOMME Probleme(ProblemeAResoudre->NomDesVariables, @@ -314,7 +315,7 @@ bool OPT_AppelDuSimplexe(const OptimizationOptions& options, bool PremierPassage = true; std::filesystem::path log_file = logs.logfile().c_str(); auto log_directory = log_file.parent_path(); - OrtoolsLogHandler ortools_logger(options.solverName, log_directory); + OrtoolsLogHandler ortools_logger(options.solverName, log_directory, thread_number); struct SimplexResult simplexResult = OPT_TryToCallSimplex(options, problemeHebdo, Probleme, diff --git a/src/solver/optimisation/opt_fonctions.h b/src/solver/optimisation/opt_fonctions.h index e2807f0c8d..620d04a05d 100644 --- a/src/solver/optimisation/opt_fonctions.h +++ b/src/solver/optimisation/opt_fonctions.h @@ -41,7 +41,8 @@ using OptimizationOptions = Antares::Solver::Optimization::OptimizationOptions; void OPT_OptimisationHebdomadaire(const OptimizationOptions& options, PROBLEME_HEBDO*, const AdqPatchParams&, - Antares::Solver::IResultWriter& writer); + Antares::Solver::IResultWriter& writer, + uint thread_number); void OPT_NumeroDeJourDuPasDeTemps(PROBLEME_HEBDO*); void OPT_NumeroDIntervalleOptimiseDuPasDeTemps(PROBLEME_HEBDO*); void OPT_ConstruireLaListeDesVariablesOptimiseesDuProblemeLineaire(PROBLEME_HEBDO*); @@ -75,7 +76,8 @@ bool ADQ_PATCH_CSR(PROBLEME_ANTARES_A_RESOUDRE&, bool OPT_PilotageOptimisationLineaire(const OptimizationOptions& options, PROBLEME_HEBDO*, const AdqPatchParams&, - Antares::Solver::IResultWriter& writer); + Antares::Solver::IResultWriter& writer, + uint thread_number); void OPT_VerifierPresenceReserveJmoins1(PROBLEME_HEBDO*); bool OPT_PilotageOptimisationQuadratique(PROBLEME_HEBDO*); @@ -89,13 +91,15 @@ bool OPT_AppelDuSimplexe(const OptimizationOptions& options, int, const int, const OptPeriodStringGenerator&, - Antares::Solver::IResultWriter& writer); + Antares::Solver::IResultWriter& writer, + uint thread_number); void OPT_LiberationProblemesSimplexe(const OptimizationOptions& options, const PROBLEME_HEBDO*); bool OPT_OptimisationLineaire(const OptimizationOptions& options, PROBLEME_HEBDO*, const AdqPatchParams&, - Antares::Solver::IResultWriter& writer); + Antares::Solver::IResultWriter& writer, + uint thread_number); void OPT_RestaurerLesDonnees(const PROBLEME_HEBDO*); /*------------------------------*/ diff --git a/src/solver/optimisation/opt_optimisation_hebdo.cpp b/src/solver/optimisation/opt_optimisation_hebdo.cpp index a84f334942..8755ed4e3b 100644 --- a/src/solver/optimisation/opt_optimisation_hebdo.cpp +++ b/src/solver/optimisation/opt_optimisation_hebdo.cpp @@ -48,11 +48,13 @@ using Antares::Solver::Optimization::OptimizationOptions; void OPT_OptimisationHebdomadaire(const OptimizationOptions& options, PROBLEME_HEBDO* pProblemeHebdo, const AdqPatchParams& adqPatchParams, - Solver::IResultWriter& writer) + Solver::IResultWriter& writer, + uint thread_number) { if (pProblemeHebdo->TypeDOptimisation == OPTIMISATION_LINEAIRE) { - if (!OPT_PilotageOptimisationLineaire(options, pProblemeHebdo, adqPatchParams, writer)) + if (!OPT_PilotageOptimisationLineaire( + options, pProblemeHebdo, adqPatchParams, writer, thread_number)) { logs.error() << "Linear optimization failed"; throw UnfeasibleProblemError("Linear optimization failed"); diff --git a/src/solver/optimisation/opt_optimisation_lineaire.cpp b/src/solver/optimisation/opt_optimisation_lineaire.cpp index b124098e98..d335c25dfc 100644 --- a/src/solver/optimisation/opt_optimisation_lineaire.cpp +++ b/src/solver/optimisation/opt_optimisation_lineaire.cpp @@ -66,7 +66,8 @@ bool runWeeklyOptimization(const OptimizationOptions& options, PROBLEME_HEBDO* problemeHebdo, const AdqPatchParams& adqPatchParams, Solver::IResultWriter& writer, - int optimizationNumber) + int optimizationNumber, + uint thread_number) { const int NombreDePasDeTempsPourUneOptimisation = problemeHebdo->NombreDePasDeTempsPourUneOptimisation; @@ -107,7 +108,8 @@ bool runWeeklyOptimization(const OptimizationOptions& options, numeroDeLIntervalle, optimizationNumber, *optPeriodStringGenerator, - writer)) + writer, + thread_number)) return false; if (problemeHebdo->ExportMPS != Data::mpsExportStatus::NO_EXPORT @@ -135,11 +137,11 @@ void runThermalHeuristic(PROBLEME_HEBDO* problemeHebdo) } } // namespace - bool OPT_OptimisationLineaire(const OptimizationOptions& options, PROBLEME_HEBDO* problemeHebdo, const AdqPatchParams& adqPatchParams, - Solver::IResultWriter& writer) + Solver::IResultWriter& writer, + uint thread_number) { if (!problemeHebdo->OptimisationAuPasHebdomadaire) { @@ -162,7 +164,7 @@ bool OPT_OptimisationLineaire(const OptimizationOptions& options, OPT_ConstruireLaMatriceDesContraintesDuProblemeLineaire(problemeHebdo, writer); bool ret = runWeeklyOptimization( - options, problemeHebdo, adqPatchParams, writer, PREMIERE_OPTIMISATION); + options, problemeHebdo, adqPatchParams, writer, PREMIERE_OPTIMISATION, thread_number); // We only need the 2nd optimization when NOT solving with integer variables // We also skip the 2nd optimization in the hidden 'Expansion' mode @@ -172,7 +174,7 @@ bool OPT_OptimisationLineaire(const OptimizationOptions& options, // We need to adjust some stuff before running the 2nd optimisation runThermalHeuristic(problemeHebdo); return runWeeklyOptimization( - options, problemeHebdo, adqPatchParams, writer, DEUXIEME_OPTIMISATION); + options, problemeHebdo, adqPatchParams, writer, DEUXIEME_OPTIMISATION, thread_number); } return ret; } diff --git a/src/solver/optimisation/opt_pilotage_optimisation_lineaire.cpp b/src/solver/optimisation/opt_pilotage_optimisation_lineaire.cpp index 49a3a489b6..f62139b7f4 100644 --- a/src/solver/optimisation/opt_pilotage_optimisation_lineaire.cpp +++ b/src/solver/optimisation/opt_pilotage_optimisation_lineaire.cpp @@ -37,7 +37,8 @@ using Antares::Solver::Optimization::OptimizationOptions; bool OPT_PilotageOptimisationLineaire(const OptimizationOptions& options, PROBLEME_HEBDO* problemeHebdo, const AdqPatchParams& adqPatchParams, - Solver::IResultWriter& writer) + Solver::IResultWriter& writer, + uint thread_number) { if (!problemeHebdo->LeProblemeADejaEteInstancie) { @@ -83,5 +84,5 @@ bool OPT_PilotageOptimisationLineaire(const OptimizationOptions& options, OPT_InitialiserNombreMinEtMaxDeGroupesCoutsDeDemarrage(problemeHebdo); } - return OPT_OptimisationLineaire(options, problemeHebdo, adqPatchParams, writer); + return OPT_OptimisationLineaire(options, problemeHebdo, adqPatchParams, writer, thread_number); } diff --git a/src/solver/optimisation/weekly_optimization.cpp b/src/solver/optimisation/weekly_optimization.cpp index 1734c5a0da..be834e48c5 100644 --- a/src/solver/optimisation/weekly_optimization.cpp +++ b/src/solver/optimisation/weekly_optimization.cpp @@ -41,7 +41,8 @@ DefaultWeeklyOptimization::DefaultWeeklyOptimization(const OptimizationOptions& void DefaultWeeklyOptimization::solve() { - OPT_OptimisationHebdomadaire(options_, problemeHebdo_, adqPatchParams_, writer_); + OPT_OptimisationHebdomadaire( + options_, problemeHebdo_, adqPatchParams_, writer_, thread_number_); } } // namespace Antares::Solver::Optimization diff --git a/src/solver/simulation/adequacy.cpp b/src/solver/simulation/adequacy.cpp index 0d311813a7..fe18732fef 100644 --- a/src/solver/simulation/adequacy.cpp +++ b/src/solver/simulation/adequacy.cpp @@ -199,7 +199,8 @@ bool Adequacy::year(Progression::Task& progression, OPT_OptimisationHebdomadaire(createOptimizationOptions(study), &pProblemesHebdo[numSpace], study.parameters.adqPatchParams, - resultWriter); + resultWriter, + numSpace); computingHydroLevels(study.areas, pProblemesHebdo[numSpace], false); @@ -382,7 +383,7 @@ void Adequacy::simulationEnd() if (!preproOnly && study.runtime->interconnectionsCount() > 0) { auto balance = retrieveBalance(study, variables); - ComputeFlowQuad(study, pProblemesHebdo[0], balance, pNbWeeks); + ComputeFlowQuad(study, pProblemesHebdo[0], balance, pNbWeeks, 0); } } diff --git a/src/solver/simulation/common-eco-adq.cpp b/src/solver/simulation/common-eco-adq.cpp index 4d8f4a1f11..88ee739648 100644 --- a/src/solver/simulation/common-eco-adq.cpp +++ b/src/solver/simulation/common-eco-adq.cpp @@ -44,7 +44,8 @@ namespace Antares::Solver::Simulation static void RecalculDesEchangesMoyens(Data::Study& study, PROBLEME_HEBDO& problem, const std::vector& balance, - int PasDeTempsDebut) + int PasDeTempsDebut, + uint thread_number) { for (uint i = 0; i < (uint)problem.NombreDePasDeTemps; i++) { @@ -95,7 +96,11 @@ static void RecalculDesEchangesMoyens(Data::Study& study, try { NullResultWriter resultWriter; - OPT_OptimisationHebdomadaire(createOptimizationOptions(study), &problem, study.parameters.adqPatchParams, resultWriter); + OPT_OptimisationHebdomadaire(createOptimizationOptions(study), + &problem, + study.parameters.adqPatchParams, + resultWriter, + thread_number); } catch (Data::UnfeasibleProblemError&) { @@ -197,7 +202,8 @@ bool ShouldUseQuadraticOptimisation(const Data::Study& study) void ComputeFlowQuad(Data::Study& study, PROBLEME_HEBDO& problem, const std::vector& balance, - uint nbWeeks) + uint nbWeeks, + uint thread_number) { uint startTime = study.calendar.days[study.parameters.simulationDays.first].hours.first; @@ -210,7 +216,7 @@ void ComputeFlowQuad(Data::Study& study, for (uint w = 0; w != nbWeeks; ++w) { int PasDeTempsDebut = startTime + (w * problem.NombreDePasDeTemps); - RecalculDesEchangesMoyens(study, problem, balance, PasDeTempsDebut); + RecalculDesEchangesMoyens(study, problem, balance, PasDeTempsDebut, thread_number); } } else diff --git a/src/solver/simulation/common-eco-adq.h b/src/solver/simulation/common-eco-adq.h index 3d1f47c3b0..c7b1c66181 100644 --- a/src/solver/simulation/common-eco-adq.h +++ b/src/solver/simulation/common-eco-adq.h @@ -89,7 +89,8 @@ bool ShouldUseQuadraticOptimisation(const Data::Study& study); void ComputeFlowQuad(Data::Study& study, PROBLEME_HEBDO& problem, const std::vector& balance, - unsigned int nbWeeks); + unsigned int nbWeeks, + uint thread_number); /*! ** \brief Hydro Remix diff --git a/src/solver/simulation/economy.cpp b/src/solver/simulation/economy.cpp index 85effa779a..3c1e93632e 100644 --- a/src/solver/simulation/economy.cpp +++ b/src/solver/simulation/economy.cpp @@ -260,7 +260,7 @@ void Economy::simulationEnd() if (!preproOnly && study.runtime->interconnectionsCount() > 0) { auto balance = retrieveBalance(study, variables); - ComputeFlowQuad(study, pProblemesHebdo[0], balance, pNbWeeks); + ComputeFlowQuad(study, pProblemesHebdo[0], balance, pNbWeeks, 0); } } diff --git a/src/solver/utils/ortools_utils.cpp b/src/solver/utils/ortools_utils.cpp index ff43b20f65..5cf4a50f28 100644 --- a/src/solver/utils/ortools_utils.cpp +++ b/src/solver/utils/ortools_utils.cpp @@ -34,8 +34,9 @@ namespace Optimization { OrtoolsLogHandler::OrtoolsLogHandler(const std::string& solver_name, - const std::filesystem::path& log_directory) : - solver_name_(solver_name), log_directory_(log_directory) + const std::filesystem::path& log_directory, + uint thread_number) : + solver_name_(solver_name), log_directory_(log_directory), thread_number_(thread_number) { init(); } @@ -49,15 +50,19 @@ OrtoolsLogHandler& OrtoolsLogHandler::operator=(const OrtoolsLogHandler& other) solver_name_ = other.solver_name_; log_directory_ = other.log_directory_; + thread_number_ = other.thread_number_; init(); return *this; } void OrtoolsLogHandler::init() { - auto myid = std::this_thread::get_id(); - std::stringstream ss; - ss << myid; - log_file_per_thread_ = log_directory_ / (std::string("thread_") + ss.str() + ".log"); + // auto myid = std::this_thread::get_id(); + // std::stringstream ss; + // ss << myid; + std::ostringstream name; + ss << "thread_" << thread_number_ << ".log"; + + log_file_per_thread_ = log_directory_ / name.str(); log_writer_.open(log_file_per_thread_, std::ofstream::out | std::ofstream::app); } diff --git a/src/solver/utils/ortools_utils.h b/src/solver/utils/ortools_utils.h index 15928cd243..20b4527c9d 100644 --- a/src/solver/utils/ortools_utils.h +++ b/src/solver/utils/ortools_utils.h @@ -65,7 +65,8 @@ class OrtoolsLogHandler : public LogHandlerInterface // TODO won't work in ci, needs ortools update // see https://github.com/rte-france/or-tools/pull/112 explicit OrtoolsLogHandler(const std::string& solverName, - const std::filesystem::path& log_directory); + const std::filesystem::path& log_directory, + uint thread_number); explicit OrtoolsLogHandler(const OrtoolsLogHandler& other) : OrtoolsLogHandler(other.solver_name_, other.log_directory_) @@ -87,6 +88,7 @@ class OrtoolsLogHandler : public LogHandlerInterface std::string solver_name_; std::filesystem::path log_directory_ = "."; std::filesystem::path log_file_per_thread_ = ""; + uint thread_number_ = 0; }; class Nomenclature From becdf7f12a1a18f94188f8e34e4d2e0cf8230206 Mon Sep 17 00:00:00 2001 From: Abdoulbari Zakir Date: Tue, 19 Dec 2023 16:22:41 +0100 Subject: [PATCH 27/27] update & fix --- src/solver/utils/ortools_utils.cpp | 2 +- src/solver/utils/ortools_utils.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/solver/utils/ortools_utils.cpp b/src/solver/utils/ortools_utils.cpp index 5cf4a50f28..b8fc5dfd37 100644 --- a/src/solver/utils/ortools_utils.cpp +++ b/src/solver/utils/ortools_utils.cpp @@ -60,7 +60,7 @@ void OrtoolsLogHandler::init() // std::stringstream ss; // ss << myid; std::ostringstream name; - ss << "thread_" << thread_number_ << ".log"; + name << "thread_" << thread_number_ << ".log"; log_file_per_thread_ = log_directory_ / name.str(); diff --git a/src/solver/utils/ortools_utils.h b/src/solver/utils/ortools_utils.h index 20b4527c9d..024587a94b 100644 --- a/src/solver/utils/ortools_utils.h +++ b/src/solver/utils/ortools_utils.h @@ -69,7 +69,7 @@ class OrtoolsLogHandler : public LogHandlerInterface uint thread_number); explicit OrtoolsLogHandler(const OrtoolsLogHandler& other) : - OrtoolsLogHandler(other.solver_name_, other.log_directory_) + OrtoolsLogHandler(other.solver_name_, other.log_directory_, other.thread_number_) { }