From 3423dfc4e50af0f0e48cfa510c784b4a0f204c65 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Mon, 20 Nov 2023 12:59:22 +0100 Subject: [PATCH 01/68] start Math log --- src/cpp/benders/logger/CMakeLists.txt | 3 +- src/cpp/benders/logger/MathLogger.cpp | 15 ++++++ .../logger/include/logger/MathLogger.h | 50 +++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 src/cpp/benders/logger/MathLogger.cpp create mode 100644 src/cpp/benders/logger/include/logger/MathLogger.h diff --git a/src/cpp/benders/logger/CMakeLists.txt b/src/cpp/benders/logger/CMakeLists.txt index cd0bf4f45..d322c811a 100644 --- a/src/cpp/benders/logger/CMakeLists.txt +++ b/src/cpp/benders/logger/CMakeLists.txt @@ -5,7 +5,8 @@ add_library (logger_lib User.cpp UserFile.cpp CandidateLog.cpp - IterationResultLog.cpp ) + IterationResultLog.cpp + MathLogger.cpp ) target_link_libraries (logger_lib PUBLIC diff --git a/src/cpp/benders/logger/MathLogger.cpp b/src/cpp/benders/logger/MathLogger.cpp new file mode 100644 index 000000000..eca640be7 --- /dev/null +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -0,0 +1,15 @@ +#include "logger/MathLogger.h" + +#include + +#include "LoggerUtils.h" +void MathLogger::write_header() {} + +MathLoggerFile::MathLoggerFile(const std::filesystem::path &filename) + : MathLogger(&file_stream_) { + file_stream_.open(filename, std::ofstream::out | std::ofstream::app); + if (file_stream_.fail()) { + std::cerr << PrefixMessage(LogUtils::LOGLEVEL::ERR, data.CONTEXT) + << "Invalid file name passed as parameter" << std::endl; + } +} \ No newline at end of file diff --git a/src/cpp/benders/logger/include/logger/MathLogger.h b/src/cpp/benders/logger/include/logger/MathLogger.h new file mode 100644 index 000000000..bfb51cc9e --- /dev/null +++ b/src/cpp/benders/logger/include/logger/MathLogger.h @@ -0,0 +1,50 @@ + +#pragma once + +#include +#include + +struct MathLoggerData { + int iteration; + double lower_bound; + double upper_bound; + double best_upper_bound; + double optimality_gap; + double relative_gap; + int max_simplexiter; + int min_simplexiter; + int deletedcut; + double time_master; + double time_subproblems; + double alpha; + const std::string CONTEXT = "Benders"; +}; + +class LogDestination { + public: + explicit LogDestination(std::ostream* stream) : stream_(stream) {} + template + std::ostream& operator<<(const T& obj) { + // write obj to stream + return (*stream_) << T; + } + + private: + std::ostream* stream_; +}; + +struct MathLogger { + explicit MathLogger(std::ostream* stream) : log_destination(stream) {} + void write_header(); + + MathLoggerData data; + LogDestination log_destination; +}; + +class MathLoggerFile : public MathLogger { + public: + explicit MathLoggerFile(const std::filesystem::path& log_file); + + private: + std::ofstream file_stream_; +}; \ No newline at end of file From f184ff70e673e5da40884e3b34a0260aae073b6e Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Mon, 20 Nov 2023 13:35:31 +0100 Subject: [PATCH 02/68] add overloaded method --- src/cpp/benders/logger/MathLogger.cpp | 20 ++++++++++++++- .../logger/include/logger/MathLogger.h | 25 +++++++++++++++---- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/cpp/benders/logger/MathLogger.cpp b/src/cpp/benders/logger/MathLogger.cpp index eca640be7..3a30af686 100644 --- a/src/cpp/benders/logger/MathLogger.cpp +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -1,9 +1,27 @@ #include "logger/MathLogger.h" +#include #include #include "LoggerUtils.h" -void MathLogger::write_header() {} + +void MathLogger::write_header() { + log_destination << std::setw(10) << "ITE"; + log_destination << std::setw(20) << "LB"; + log_destination << std::setw(20) << "LEV"; + log_destination << std::setw(20) << "UB"; + log_destination << std::setw(20) << "BESTUB"; + log_destination << std::setw(15) << "GAP"; + + log_destination << std::setw(15) << "MINSIMPLEX"; + log_destination << std::setw(15) << "MAXSIMPLEX"; + + log_destination << std::setw(15) << "DELETEDCUT"; + log_destination << std::setw(15) << "TIMEMASTER"; + log_destination << std::setw(15) << "TIMESLAVES"; + + log_destination << std::endl; +} MathLoggerFile::MathLoggerFile(const std::filesystem::path &filename) : MathLogger(&file_stream_) { diff --git a/src/cpp/benders/logger/include/logger/MathLogger.h b/src/cpp/benders/logger/include/logger/MathLogger.h index bfb51cc9e..2696ebb0f 100644 --- a/src/cpp/benders/logger/include/logger/MathLogger.h +++ b/src/cpp/benders/logger/include/logger/MathLogger.h @@ -3,6 +3,7 @@ #include #include +#include struct MathLoggerData { int iteration; @@ -16,23 +17,37 @@ struct MathLoggerData { int deletedcut; double time_master; double time_subproblems; - double alpha; + // double alpha; const std::string CONTEXT = "Benders"; }; class LogDestination { public: explicit LogDestination(std::ostream* stream) : stream_(stream) {} - template - std::ostream& operator<<(const T& obj) { + + // for std::endl + std::ostream& operator<<(std::ostream& (*function)(std::ostream&)) { + // write obj to stream + return function(*stream_); + } + + // for std::endl + std::ostream& operator<<(const std::_Smanip& smanip) { // write obj to stream - return (*stream_) << T; + return (*stream_) << smanip; } + template + std::ostream& operator<<(const T& obj); + private: std::ostream* stream_; }; - +template +std::ostream& LogDestination::operator<<(const T& obj) { + // write obj to stream + return (*stream_) << T; +} struct MathLogger { explicit MathLogger(std::ostream* stream) : log_destination(stream) {} void write_header(); From 4cfd573e0aeecec4f7b1d07d9d688434e184a78c Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Mon, 20 Nov 2023 15:05:27 +0100 Subject: [PATCH 03/68] fix platform specific standard --- src/cpp/benders/logger/MathLogger.cpp | 28 +++++++++++-------- .../logger/include/logger/MathLogger.h | 12 ++++---- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/cpp/benders/logger/MathLogger.cpp b/src/cpp/benders/logger/MathLogger.cpp index 3a30af686..66049b95f 100644 --- a/src/cpp/benders/logger/MathLogger.cpp +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -2,23 +2,29 @@ #include #include +#include #include "LoggerUtils.h" +std::string Indent(int size) { + std::stringstream ss; + ss << std::setw(size); + return ss.str(); +} void MathLogger::write_header() { - log_destination << std::setw(10) << "ITE"; - log_destination << std::setw(20) << "LB"; - log_destination << std::setw(20) << "LEV"; - log_destination << std::setw(20) << "UB"; - log_destination << std::setw(20) << "BESTUB"; - log_destination << std::setw(15) << "GAP"; + log_destination << Indent(10) << "ITE"; + log_destination << Indent(20) << "LB"; + log_destination << Indent(20) << "LEV"; + log_destination << Indent(20) << "UB"; + log_destination << Indent(20) << "BESTUB"; + log_destination << Indent(15) << "GAP"; - log_destination << std::setw(15) << "MINSIMPLEX"; - log_destination << std::setw(15) << "MAXSIMPLEX"; + log_destination << Indent(15) << "MINSIMPLEX"; + log_destination << Indent(15) << "MAXSIMPLEX"; - log_destination << std::setw(15) << "DELETEDCUT"; - log_destination << std::setw(15) << "TIMEMASTER"; - log_destination << std::setw(15) << "TIMESLAVES"; + log_destination << Indent(15) << "DELETEDCUT"; + log_destination << Indent(15) << "TIMEMASTER"; + log_destination << Indent(15) << "TIMESLAVES"; log_destination << std::endl; } diff --git a/src/cpp/benders/logger/include/logger/MathLogger.h b/src/cpp/benders/logger/include/logger/MathLogger.h index 2696ebb0f..c3267c9c1 100644 --- a/src/cpp/benders/logger/include/logger/MathLogger.h +++ b/src/cpp/benders/logger/include/logger/MathLogger.h @@ -31,11 +31,11 @@ class LogDestination { return function(*stream_); } - // for std::endl - std::ostream& operator<<(const std::_Smanip& smanip) { - // write obj to stream - return (*stream_) << smanip; - } + // // for std::endl + // std::ostream& operator<<(const std::_Smanip& smanip) { + // // write obj to stream + // return (*stream_) << smanip; + // } template std::ostream& operator<<(const T& obj); @@ -46,7 +46,7 @@ class LogDestination { template std::ostream& LogDestination::operator<<(const T& obj) { // write obj to stream - return (*stream_) << T; + return (*stream_) << obj; } struct MathLogger { explicit MathLogger(std::ostream* stream) : log_destination(stream) {} From d3527c431c8c48f55cca37edd8cd22f62b33b37a Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Mon, 20 Nov 2023 15:20:15 +0100 Subject: [PATCH 04/68] add MathloggerDriver --- src/cpp/benders/logger/MathLogger.cpp | 13 ++++++++++++- .../benders/logger/include/logger/MathLogger.h | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/cpp/benders/logger/MathLogger.cpp b/src/cpp/benders/logger/MathLogger.cpp index 66049b95f..35f1e970e 100644 --- a/src/cpp/benders/logger/MathLogger.cpp +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -1,7 +1,6 @@ #include "logger/MathLogger.h" #include -#include #include #include "LoggerUtils.h" @@ -36,4 +35,16 @@ MathLoggerFile::MathLoggerFile(const std::filesystem::path &filename) std::cerr << PrefixMessage(LogUtils::LOGLEVEL::ERR, data.CONTEXT) << "Invalid file name passed as parameter" << std::endl; } +} + +void MathLoggerDriver::write_header() { + for (auto logger : math_loggers_) { + logger->write_header(); + } +} + +void MathLoggerDriver::add_logger(MathLogger* logger) { + if (logger) { + math_loggers_.push_back(logger); + } } \ No newline at end of file diff --git a/src/cpp/benders/logger/include/logger/MathLogger.h b/src/cpp/benders/logger/include/logger/MathLogger.h index c3267c9c1..6c2653d98 100644 --- a/src/cpp/benders/logger/include/logger/MathLogger.h +++ b/src/cpp/benders/logger/include/logger/MathLogger.h @@ -4,6 +4,8 @@ #include #include #include +#include +#include struct MathLoggerData { int iteration; @@ -62,4 +64,19 @@ class MathLoggerFile : public MathLogger { private: std::ofstream file_stream_; +}; + +class MathLoggerOstream : public MathLogger { + public: + explicit MathLoggerOstream() : MathLogger(&std::cout) {} +}; + +class MathLoggerDriver { + public: + MathLoggerDriver() = default; + void write_header(); + void MathLoggerDriver::add_logger(MathLogger* logger); + + private: + std::list math_loggers_; }; \ No newline at end of file From d341b1ba0801a060caa82011456980ee7432893a Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Mon, 20 Nov 2023 15:30:54 +0100 Subject: [PATCH 05/68] print basic data --- src/cpp/benders/logger/MathLogger.cpp | 37 ++++++++++++++++++- .../logger/include/logger/MathLogger.h | 5 +-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/cpp/benders/logger/MathLogger.cpp b/src/cpp/benders/logger/MathLogger.cpp index 35f1e970e..f7f7f4018 100644 --- a/src/cpp/benders/logger/MathLogger.cpp +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -28,11 +28,46 @@ void MathLogger::write_header() { log_destination << std::endl; } +void MathLogger::Print(const MathLoggerData& data) { + log_destination << Indent(10) << data.iteration; + if (data.lower_bound == -1e20) + log_destination << Indent(20) << "-INF"; + else + log_destination << Indent(20) << std::scientific << std::setprecision(10) + << data.lower_bound; + if (data.upper_bound == +1e20) + log_destination << Indent(20) << "+INF"; + else + log_destination << Indent(20) << std::scientific << std::setprecision(10) + << data.upper_bound; + if (data.best_upper_bound == +1e20) + log_destination << Indent(20) << "+INF"; + else + log_destination << Indent(20) << std::scientific << std::setprecision(10) + << data.best_upper_bound; + log_destination << Indent(15) << std::scientific << std::setprecision(2) + << data.best_upper_bound - data.lower_bound; + + log_destination << Indent(15) << std::scientific << std::setprecision(2) + << (data.best_upper_bound - data.lower_bound) / + data.best_upper_bound; + + log_destination << Indent(15) << data.min_simplexiter; + log_destination << Indent(15) << data.max_simplexiter; + + log_destination << Indent(15) << data.deletedcut; + log_destination << Indent(15) << std::setprecision(2) << data.time_master; + log_destination << Indent(15) << std::setprecision(2) + << data.time_subproblems; + + log_destination << std::endl; +} + MathLoggerFile::MathLoggerFile(const std::filesystem::path &filename) : MathLogger(&file_stream_) { file_stream_.open(filename, std::ofstream::out | std::ofstream::app); if (file_stream_.fail()) { - std::cerr << PrefixMessage(LogUtils::LOGLEVEL::ERR, data.CONTEXT) + std::cerr << PrefixMessage(LogUtils::LOGLEVEL::ERR, MATHLOGGERCONTEXT) << "Invalid file name passed as parameter" << std::endl; } } diff --git a/src/cpp/benders/logger/include/logger/MathLogger.h b/src/cpp/benders/logger/include/logger/MathLogger.h index 6c2653d98..2fe77229d 100644 --- a/src/cpp/benders/logger/include/logger/MathLogger.h +++ b/src/cpp/benders/logger/include/logger/MathLogger.h @@ -6,6 +6,7 @@ #include #include #include +const std::string MATHLOGGERCONTEXT = "Benders"; struct MathLoggerData { int iteration; @@ -20,7 +21,6 @@ struct MathLoggerData { double time_master; double time_subproblems; // double alpha; - const std::string CONTEXT = "Benders"; }; class LogDestination { @@ -53,8 +53,7 @@ std::ostream& LogDestination::operator<<(const T& obj) { struct MathLogger { explicit MathLogger(std::ostream* stream) : log_destination(stream) {} void write_header(); - - MathLoggerData data; + void Print(const MathLoggerData& data); LogDestination log_destination; }; From f28101cc5318fcc8c0ee175d43ee92d7d569ef9a Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Mon, 20 Nov 2023 16:44:35 +0100 Subject: [PATCH 06/68] add math logger in benders --- src/cpp/benders/factories/BendersFactory.cpp | 8 ++++++ .../factories/include/LoggerFactories.h | 25 ++++++++++++++++++- .../logger/include/logger/MathLogger.h | 2 ++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/cpp/benders/factories/BendersFactory.cpp b/src/cpp/benders/factories/BendersFactory.cpp index 35df81627..6ffdda383 100644 --- a/src/cpp/benders/factories/BendersFactory.cpp +++ b/src/cpp/benders/factories/BendersFactory.cpp @@ -17,6 +17,7 @@ int RunBenders(char** argv, const std::filesystem::path& options_file, const BENDERSMETHOD& method) { // Read options, needed to have options.OUTPUTROOT Logger logger; + const MathLoggerDriver* math_log_driver = nullptr; try { /* code */ @@ -34,12 +35,17 @@ int RunBenders(char** argv, const std::filesystem::path& options_file, auto log_reports_name = std::filesystem::path(options.OUTPUTROOT) / "reportbenders.txt"; + auto math_logs_file = + std::filesystem::path(options.OUTPUTROOT) / "math_log.txt"; + Writer writer; if (world.rank() == 0) { auto logger_factory = FileAndStdoutLoggerFactory(log_reports_name); + auto math_log_factory = MathLoggerFactory(false, math_logs_file); logger = logger_factory.get_logger(); + math_log_driver = &math_log_factory.get_logger(); writer = build_json_writer(options.JSON_FILE, options.RESUME); if (Benders::StartUp startup; startup.StudyAlreadyAchievedCriterion(options, writer, logger)) @@ -47,6 +53,8 @@ int RunBenders(char** argv, const std::filesystem::path& options_file, } else { logger = build_void_logger(); writer = build_void_writer(); + auto math_log_factory = MathLoggerFactory(); + math_log_driver = &math_log_factory.get_logger(); } pBendersBase benders; diff --git a/src/cpp/benders/factories/include/LoggerFactories.h b/src/cpp/benders/factories/include/LoggerFactories.h index fcd3086ed..ba137075d 100644 --- a/src/cpp/benders/factories/include/LoggerFactories.h +++ b/src/cpp/benders/factories/include/LoggerFactories.h @@ -7,6 +7,7 @@ #include "ILogger.h" #include "SimulationOptions.h" #include "logger/Master.h" +#include "logger/MathLogger.h" #include "logger/UserFile.h" Logger build_void_logger(); @@ -29,7 +30,29 @@ class FileAndStdoutLoggerFactory { masterLogger->addLogger(loggerUser); logger = masterLogger; } - inline Logger get_logger() const { return logger; } }; + +class MathLoggerFactory { + private: + MathLoggerDriver math_Logger_driver; + MathLoggerFile math_logger_file_; + MathLoggerOstream math_Logger_ostream_; + + public: + explicit MathLoggerFactory( + bool console_log, const std::filesystem::path &math_logs_file_path = "") { + if (math_logs_file_path != "") { + math_logger_file_ = MathLoggerFile(math_logs_file_path); + math_Logger_driver.add_logger(&math_logger_file_); + } + if (console_log) { + math_Logger_driver.add_logger(&math_Logger_ostream_); + } + } + explicit MathLoggerFactory() = default; + inline const MathLoggerDriver &get_logger() const { + return math_Logger_driver; + } +}; #endif // ANTARESXPANSION_LOGGERFACTORIES_H diff --git a/src/cpp/benders/logger/include/logger/MathLogger.h b/src/cpp/benders/logger/include/logger/MathLogger.h index 2fe77229d..1bef8b10e 100644 --- a/src/cpp/benders/logger/include/logger/MathLogger.h +++ b/src/cpp/benders/logger/include/logger/MathLogger.h @@ -52,6 +52,7 @@ std::ostream& LogDestination::operator<<(const T& obj) { } struct MathLogger { explicit MathLogger(std::ostream* stream) : log_destination(stream) {} + explicit MathLogger() : log_destination(&std::cout) {} void write_header(); void Print(const MathLoggerData& data); LogDestination log_destination; @@ -60,6 +61,7 @@ struct MathLogger { class MathLoggerFile : public MathLogger { public: explicit MathLoggerFile(const std::filesystem::path& log_file); + using MathLogger::MathLogger; private: std::ofstream file_stream_; From 3710fbbd85c10c59d8d0e1e0ae3137d67393bc67 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Mon, 20 Nov 2023 17:11:38 +0100 Subject: [PATCH 07/68] Benders take mathlooger --- .../benders_by_batch/BendersByBatch.cpp | 9 +-- .../benders_by_batch/include/BendersByBatch.h | 4 +- src/cpp/benders/benders_core/BendersBase.cpp | 6 +- .../benders_core/BendersMathLogger.cpp | 19 ++++++ src/cpp/benders/benders_core/CMakeLists.txt | 4 +- .../benders_core/include/BendersBase.h | 5 +- .../benders_core/include/BendersMathLogger.h | 67 +++++++++++++++++++ src/cpp/benders/benders_mpi/BendersMPI.cpp | 5 +- .../benders/benders_mpi/include/BendersMPI.h | 3 +- .../benders_sequential/BendersSequential.cpp | 8 ++- .../include/BendersSequential.h | 5 +- src/cpp/benders/factories/BendersFactory.cpp | 12 ++-- .../factories/include/LoggerFactories.h | 4 +- src/cpp/benders/logger/MathLogger.cpp | 12 ---- .../logger/include/logger/MathLogger.h | 67 +------------------ tests/cpp/benders/benders_sequential_test.cpp | 10 +-- 16 files changed, 132 insertions(+), 108 deletions(-) create mode 100644 src/cpp/benders/benders_core/BendersMathLogger.cpp create mode 100644 src/cpp/benders/benders_core/include/BendersMathLogger.h diff --git a/src/cpp/benders/benders_by_batch/BendersByBatch.cpp b/src/cpp/benders/benders_by_batch/BendersByBatch.cpp index fb2a72826..a0814fcd7 100644 --- a/src/cpp/benders/benders_by_batch/BendersByBatch.cpp +++ b/src/cpp/benders/benders_by_batch/BendersByBatch.cpp @@ -8,10 +8,11 @@ #include "BatchCollection.h" #include "RandomBatchShuffler.h" #include "glog/logging.h" -BendersByBatch::BendersByBatch(BendersBaseOptions const &options, Logger logger, - Writer writer, mpi::environment &env, - mpi::communicator &world) - : BendersMpi(options, logger, writer, env, world) {} +BendersByBatch::BendersByBatch( + BendersBaseOptions const &options, Logger logger, Writer writer, + mpi::environment &env, mpi::communicator &world, + std::shared_ptr mathLoggerDriver) + : BendersMpi(options, logger, writer, env, world, mathLoggerDriver) {} void BendersByBatch::InitializeProblems() { MatchProblemToId(); diff --git a/src/cpp/benders/benders_by_batch/include/BendersByBatch.h b/src/cpp/benders/benders_by_batch/include/BendersByBatch.h index 9590f2726..06af08e98 100644 --- a/src/cpp/benders/benders_by_batch/include/BendersByBatch.h +++ b/src/cpp/benders/benders_by_batch/include/BendersByBatch.h @@ -9,8 +9,8 @@ class BendersByBatch : public BendersMpi { public: BendersByBatch(BendersBaseOptions const &options, Logger logger, - Writer writer, mpi::environment &env, - mpi::communicator &world); + Writer writer, mpi::environment &env, mpi::communicator &world, + std::shared_ptr mathLoggerDriver); ~BendersByBatch() override = default; void Run() override; void BuildCut(const std::vector &batch_sub_problems, diff --git a/src/cpp/benders/benders_core/BendersBase.cpp b/src/cpp/benders/benders_core/BendersBase.cpp index 71e239603..f722e3a06 100644 --- a/src/cpp/benders/benders_core/BendersBase.cpp +++ b/src/cpp/benders/benders_core/BendersBase.cpp @@ -13,12 +13,14 @@ #include "solver_utils.h" BendersBase::BendersBase(BendersBaseOptions options, Logger logger, - Writer writer) + Writer writer, + std::shared_ptr mathLoggerDriver) : _options(std::move(options)), _csv_file_path(std::filesystem::path(_options.OUTPUTROOT) / (_options.CSV_NAME + ".csv")), _logger(std::move(logger)), - _writer(std::move(writer)) {} + _writer(std::move(writer)), + mathLoggerDriver_(mathLoggerDriver) {} /*! * \brief Initialize set of data used in the loop diff --git a/src/cpp/benders/benders_core/BendersMathLogger.cpp b/src/cpp/benders/benders_core/BendersMathLogger.cpp new file mode 100644 index 000000000..cc1e25834 --- /dev/null +++ b/src/cpp/benders/benders_core/BendersMathLogger.cpp @@ -0,0 +1,19 @@ +#include "BendersMathLogger.h" + +void MathLoggerDriver::add_logger(MathLogger* logger) { + if (logger) { + math_loggers_.push_back(logger); + } +} + +void MathLoggerDriver::Print(const MathLoggerData& data) { + for (auto logger : math_loggers_) { + logger->Print(data); + } +} + +void MathLoggerDriver::write_header() { + for (auto logger : math_loggers_) { + logger->write_header(); + } +} diff --git a/src/cpp/benders/benders_core/CMakeLists.txt b/src/cpp/benders/benders_core/CMakeLists.txt index 05f6af399..c889a00db 100644 --- a/src/cpp/benders/benders_core/CMakeLists.txt +++ b/src/cpp/benders/benders_core/CMakeLists.txt @@ -26,7 +26,9 @@ add_library (benders_core STATIC ${CMAKE_CURRENT_SOURCE_DIR}/LastIterationWriter.cpp ${CMAKE_CURRENT_SOURCE_DIR}/LastIterationReader.cpp ${CMAKE_CURRENT_SOURCE_DIR}/LastIterationPrinter.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/StartUp.cpp) + ${CMAKE_CURRENT_SOURCE_DIR}/StartUp.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/BendersMathLogger.cpp + ) get_target_property(JSON_INC_PATH jsoncpp_lib INTERFACE_INCLUDE_DIRECTORIES) diff --git a/src/cpp/benders/benders_core/include/BendersBase.h b/src/cpp/benders/benders_core/include/BendersBase.h index 19b38c97c..9b0ee2470 100644 --- a/src/cpp/benders/benders_core/include/BendersBase.h +++ b/src/cpp/benders/benders_core/include/BendersBase.h @@ -3,6 +3,7 @@ #include #include +#include "BendersMathLogger.h" #include "BendersStructsDatas.h" #include "ILogger.h" #include "OutputWriter.h" @@ -29,7 +30,8 @@ auto selectPolicy(lambda f, bool shouldParallelize) { class BendersBase { public: virtual ~BendersBase() = default; - BendersBase(BendersBaseOptions options, Logger logger, Writer writer); + BendersBase(BendersBaseOptions options, Logger logger, Writer writer, + std::shared_ptr mathLoggerDriver); virtual void launch() = 0; void set_log_file(const std::filesystem::path &log_name); [[nodiscard]] std::filesystem::path log_name() const { return _log_name; } @@ -40,6 +42,7 @@ class BendersBase { CurrentIterationData _data; VariableMap master_variable_map; CouplingMap coupling_map; + std::shared_ptr mathLoggerDriver_; protected: virtual void free() = 0; diff --git a/src/cpp/benders/benders_core/include/BendersMathLogger.h b/src/cpp/benders/benders_core/include/BendersMathLogger.h new file mode 100644 index 000000000..991188932 --- /dev/null +++ b/src/cpp/benders/benders_core/include/BendersMathLogger.h @@ -0,0 +1,67 @@ +#pragma once + +#include +#include +#include +const std::string MATHLOGGERCONTEXT = "Benders"; + +struct MathLoggerData { + int iteration; + double lower_bound; + double upper_bound; + double best_upper_bound; + double optimality_gap; + double relative_gap; + int max_simplexiter; + int min_simplexiter; + int deletedcut; + double time_master; + double time_subproblems; + // double alpha; +}; + +class LogDestination { + public: + explicit LogDestination(std::ostream* stream) : stream_(stream) {} + + // for std::endl + std::ostream& operator<<(std::ostream& (*function)(std::ostream&)) { + // write obj to stream + return function(*stream_); + } + + // // for std::endl + // std::ostream& operator<<(const std::_Smanip& smanip) { + // // write obj to stream + // return (*stream_) << smanip; + // } + + template + std::ostream& operator<<(const T& obj); + + private: + std::ostream* stream_; +}; +template +std::ostream& LogDestination::operator<<(const T& obj) { + // write obj to stream + return (*stream_) << obj; +} +struct MathLogger { + explicit MathLogger(std::ostream* stream) : log_destination(stream) {} + explicit MathLogger() : log_destination(&std::cout) {} + void write_header(); + void Print(const MathLoggerData& data); + LogDestination log_destination; +}; + +class MathLoggerDriver { + public: + MathLoggerDriver() = default; + void write_header(); + void MathLoggerDriver::add_logger(MathLogger* logger); + void Print(const MathLoggerData& data); + + private: + std::list math_loggers_; +}; diff --git a/src/cpp/benders/benders_mpi/BendersMPI.cpp b/src/cpp/benders/benders_mpi/BendersMPI.cpp index 5c86a6f35..075021955 100644 --- a/src/cpp/benders/benders_mpi/BendersMPI.cpp +++ b/src/cpp/benders/benders_mpi/BendersMPI.cpp @@ -9,8 +9,9 @@ BendersMpi::BendersMpi(BendersBaseOptions const &options, Logger logger, Writer writer, mpi::environment &env, - mpi::communicator &world) - : BendersBase(options, logger, std::move(writer)), + mpi::communicator &world, + std::shared_ptr mathLoggerDriver) + : BendersBase(options, logger, std::move(writer), mathLoggerDriver), _env(env), _world(world) {} diff --git a/src/cpp/benders/benders_mpi/include/BendersMPI.h b/src/cpp/benders/benders_mpi/include/BendersMPI.h index 702bc6d4a..0a31a8d82 100644 --- a/src/cpp/benders/benders_mpi/include/BendersMPI.h +++ b/src/cpp/benders/benders_mpi/include/BendersMPI.h @@ -19,7 +19,8 @@ class BendersMpi : public BendersBase { public: ~BendersMpi() override = default; BendersMpi(BendersBaseOptions const &options, Logger logger, Writer writer, - mpi::environment &env, mpi::communicator &world); + mpi::environment &env, mpi::communicator &world, + std::shared_ptr mathLoggerDriver); void launch() override; virtual std::string BendersName() const { return "Benders mpi"; } diff --git a/src/cpp/benders/benders_sequential/BendersSequential.cpp b/src/cpp/benders/benders_sequential/BendersSequential.cpp index e595ba17d..e1be67864 100644 --- a/src/cpp/benders/benders_sequential/BendersSequential.cpp +++ b/src/cpp/benders/benders_sequential/BendersSequential.cpp @@ -17,9 +17,11 @@ * \param options : set of options fixed by the user */ -BendersSequential::BendersSequential(BendersBaseOptions const &options, - Logger logger, Writer writer) - : BendersBase(options, std::move(logger), std::move(writer)) {} +BendersSequential::BendersSequential( + BendersBaseOptions const &options, Logger logger, Writer writer, + std::shared_ptr mathLoggerDriver) + : BendersBase(options, std::move(logger), std::move(writer), + mathLoggerDriver) {} void BendersSequential::InitializeProblems() { MatchProblemToId(); diff --git a/src/cpp/benders/benders_sequential/include/BendersSequential.h b/src/cpp/benders/benders_sequential/include/BendersSequential.h index 0e7a90e99..141226e1e 100644 --- a/src/cpp/benders/benders_sequential/include/BendersSequential.h +++ b/src/cpp/benders/benders_sequential/include/BendersSequential.h @@ -11,8 +11,9 @@ */ class BendersSequential : public BendersBase { public: - explicit BendersSequential(BendersBaseOptions const &options, Logger logger, - Writer writer); + explicit BendersSequential( + BendersBaseOptions const &options, Logger logger, Writer writer, + std::shared_ptr mathLoggerDriver); virtual ~BendersSequential() = default; virtual void launch(); virtual void BuildCut(); diff --git a/src/cpp/benders/factories/BendersFactory.cpp b/src/cpp/benders/factories/BendersFactory.cpp index 6ffdda383..87cd07cb3 100644 --- a/src/cpp/benders/factories/BendersFactory.cpp +++ b/src/cpp/benders/factories/BendersFactory.cpp @@ -17,7 +17,7 @@ int RunBenders(char** argv, const std::filesystem::path& options_file, const BENDERSMETHOD& method) { // Read options, needed to have options.OUTPUTROOT Logger logger; - const MathLoggerDriver* math_log_driver = nullptr; + std::shared_ptr math_log_driver; try { /* code */ @@ -45,7 +45,7 @@ int RunBenders(char** argv, const std::filesystem::path& options_file, auto math_log_factory = MathLoggerFactory(false, math_logs_file); logger = logger_factory.get_logger(); - math_log_driver = &math_log_factory.get_logger(); + math_log_driver = math_log_factory.get_logger(); writer = build_json_writer(options.JSON_FILE, options.RESUME); if (Benders::StartUp startup; startup.StudyAlreadyAchievedCriterion(options, writer, logger)) @@ -54,16 +54,16 @@ int RunBenders(char** argv, const std::filesystem::path& options_file, logger = build_void_logger(); writer = build_void_writer(); auto math_log_factory = MathLoggerFactory(); - math_log_driver = &math_log_factory.get_logger(); + math_log_driver = math_log_factory.get_logger(); } pBendersBase benders; if (method == BENDERSMETHOD::BENDERS) { benders = std::make_shared(benders_options, logger, writer, - env, world); + env, world, math_log_driver); } else if (method == BENDERSMETHOD::BENDERSBYBATCH) { - benders = std::make_shared(benders_options, logger, - writer, env, world); + benders = std::make_shared( + benders_options, logger, writer, env, world, math_log_driver); } else { auto err_msg = "Error only benders or benders-by-batch allowed!"; logger->display_message(err_msg); diff --git a/src/cpp/benders/factories/include/LoggerFactories.h b/src/cpp/benders/factories/include/LoggerFactories.h index ba137075d..148be35b4 100644 --- a/src/cpp/benders/factories/include/LoggerFactories.h +++ b/src/cpp/benders/factories/include/LoggerFactories.h @@ -51,8 +51,8 @@ class MathLoggerFactory { } } explicit MathLoggerFactory() = default; - inline const MathLoggerDriver &get_logger() const { - return math_Logger_driver; + std::shared_ptr get_logger() { + return std::make_shared(math_Logger_driver); } }; #endif // ANTARESXPANSION_LOGGERFACTORIES_H diff --git a/src/cpp/benders/logger/MathLogger.cpp b/src/cpp/benders/logger/MathLogger.cpp index f7f7f4018..a89468f17 100644 --- a/src/cpp/benders/logger/MathLogger.cpp +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -71,15 +71,3 @@ MathLoggerFile::MathLoggerFile(const std::filesystem::path &filename) << "Invalid file name passed as parameter" << std::endl; } } - -void MathLoggerDriver::write_header() { - for (auto logger : math_loggers_) { - logger->write_header(); - } -} - -void MathLoggerDriver::add_logger(MathLogger* logger) { - if (logger) { - math_loggers_.push_back(logger); - } -} \ No newline at end of file diff --git a/src/cpp/benders/logger/include/logger/MathLogger.h b/src/cpp/benders/logger/include/logger/MathLogger.h index 1bef8b10e..f1e433007 100644 --- a/src/cpp/benders/logger/include/logger/MathLogger.h +++ b/src/cpp/benders/logger/include/logger/MathLogger.h @@ -1,63 +1,8 @@ #pragma once - #include -#include -#include -#include -#include -const std::string MATHLOGGERCONTEXT = "Benders"; - -struct MathLoggerData { - int iteration; - double lower_bound; - double upper_bound; - double best_upper_bound; - double optimality_gap; - double relative_gap; - int max_simplexiter; - int min_simplexiter; - int deletedcut; - double time_master; - double time_subproblems; - // double alpha; -}; - -class LogDestination { - public: - explicit LogDestination(std::ostream* stream) : stream_(stream) {} - - // for std::endl - std::ostream& operator<<(std::ostream& (*function)(std::ostream&)) { - // write obj to stream - return function(*stream_); - } - - // // for std::endl - // std::ostream& operator<<(const std::_Smanip& smanip) { - // // write obj to stream - // return (*stream_) << smanip; - // } - - template - std::ostream& operator<<(const T& obj); - - private: - std::ostream* stream_; -}; -template -std::ostream& LogDestination::operator<<(const T& obj) { - // write obj to stream - return (*stream_) << obj; -} -struct MathLogger { - explicit MathLogger(std::ostream* stream) : log_destination(stream) {} - explicit MathLogger() : log_destination(&std::cout) {} - void write_header(); - void Print(const MathLoggerData& data); - LogDestination log_destination; -}; +#include "BendersMathLogger.h" class MathLoggerFile : public MathLogger { public: explicit MathLoggerFile(const std::filesystem::path& log_file); @@ -71,13 +16,3 @@ class MathLoggerOstream : public MathLogger { public: explicit MathLoggerOstream() : MathLogger(&std::cout) {} }; - -class MathLoggerDriver { - public: - MathLoggerDriver() = default; - void write_header(); - void MathLoggerDriver::add_logger(MathLogger* logger); - - private: - std::list math_loggers_; -}; \ No newline at end of file diff --git a/tests/cpp/benders/benders_sequential_test.cpp b/tests/cpp/benders/benders_sequential_test.cpp index acf7f2c29..eeafff567 100644 --- a/tests/cpp/benders/benders_sequential_test.cpp +++ b/tests/cpp/benders/benders_sequential_test.cpp @@ -42,9 +42,10 @@ class BendersSequentialDouble : public BendersSequential { bool _setDataPreRelaxationCall = false; bool _setDataPostRelaxationCall = false; - explicit BendersSequentialDouble(BendersBaseOptions const &options, - Logger &logger, Writer writer) - : BendersSequential(options, logger, writer){}; + explicit BendersSequentialDouble( + BendersBaseOptions const &options, Logger &logger, Writer writer, + std::shared_ptr mathLoggerDriver) + : BendersSequential(options, logger, writer, mathLoggerDriver){}; void init_data() override { BendersBase::init_data(); @@ -131,6 +132,7 @@ class BendersSequentialDouble : public BendersSequential { class BendersSequentialTest : public ::testing::Test { public: Logger logger; + std::shared_ptr mathLoggerDriver; Writer writer; const std::filesystem::path data_test_dir = "data_test"; const std::filesystem::path mps_dir = data_test_dir / "mps"; @@ -196,7 +198,7 @@ class BendersSequentialTest : public ::testing::Test { double sep_param) { BendersBaseOptions options = init_benders_options( master_formulation, max_iter, relaxed_gap, sep_param); - return BendersSequentialDouble(options, logger, writer); + return BendersSequentialDouble(options, logger, writer, mathLoggerDriver); } std::vector get_nb_units_col_types( From e2e9a91a0b2972598b42f0a369a2f2cf2b5ab7b1 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Mon, 20 Nov 2023 18:12:18 +0100 Subject: [PATCH 08/68] print maths log --- .../benders/benders_core/BendersMathLogger.cpp | 2 +- .../benders_core/BendersStructsDatas.cpp | 17 +++++++++++++++++ .../benders_core/include/BendersMathLogger.h | 4 ++-- .../benders_core/include/BendersStructsDatas.h | 5 ++++- src/cpp/benders/benders_mpi/BendersMPI.cpp | 2 ++ .../benders/factories/include/LoggerFactories.h | 10 +++++----- src/cpp/benders/logger/MathLogger.cpp | 7 +------ src/python/antares_xpansion/config_loader.py | 2 +- 8 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/cpp/benders/benders_core/BendersMathLogger.cpp b/src/cpp/benders/benders_core/BendersMathLogger.cpp index cc1e25834..79686d262 100644 --- a/src/cpp/benders/benders_core/BendersMathLogger.cpp +++ b/src/cpp/benders/benders_core/BendersMathLogger.cpp @@ -1,6 +1,6 @@ #include "BendersMathLogger.h" -void MathLoggerDriver::add_logger(MathLogger* logger) { +void MathLoggerDriver::add_logger(std::shared_ptr logger) { if (logger) { math_loggers_.push_back(logger); } diff --git a/src/cpp/benders/benders_core/BendersStructsDatas.cpp b/src/cpp/benders/benders_core/BendersStructsDatas.cpp index bea2aff25..f5a0911bc 100644 --- a/src/cpp/benders/benders_core/BendersStructsDatas.cpp +++ b/src/cpp/benders/benders_core/BendersStructsDatas.cpp @@ -8,3 +8,20 @@ Point WorkerMasterData::get_x_cut() const { return *_x_cut; } Point WorkerMasterData::get_min_invest() const { return *_min_invest; } Point WorkerMasterData::get_max_invest() const { return *_max_invest; } + +MathLoggerData MathLoggerDataFromCurrentIterationData( + const CurrentIterationData& data) { + return {/*.iteration =*/data.it, + /*.lower_bound =*/data.lb, + /*.upper_bound =*/data.ub, + /*.best_upper_bound =*/data.best_ub, + /*.optimality_gap =*/0, + /*.relative_gap =*/0, + /*.max_simplexiter =*/0, + /*.min_simplexiter =*/0, + /*.deletedcut =*/data.deletedcut, + /*.time_master =*/data.timer_master, + /*.time_subproblems =*/data.subproblems_cputime + + }; +} \ No newline at end of file diff --git a/src/cpp/benders/benders_core/include/BendersMathLogger.h b/src/cpp/benders/benders_core/include/BendersMathLogger.h index 991188932..cba3c9d8d 100644 --- a/src/cpp/benders/benders_core/include/BendersMathLogger.h +++ b/src/cpp/benders/benders_core/include/BendersMathLogger.h @@ -59,9 +59,9 @@ class MathLoggerDriver { public: MathLoggerDriver() = default; void write_header(); - void MathLoggerDriver::add_logger(MathLogger* logger); + void MathLoggerDriver::add_logger(std::shared_ptr logger); void Print(const MathLoggerData& data); private: - std::list math_loggers_; + std::list> math_loggers_; }; diff --git a/src/cpp/benders/benders_core/include/BendersStructsDatas.h b/src/cpp/benders/benders_core/include/BendersStructsDatas.h index c3a8e4468..6305b6292 100644 --- a/src/cpp/benders/benders_core/include/BendersStructsDatas.h +++ b/src/cpp/benders/benders_core/include/BendersStructsDatas.h @@ -1,9 +1,10 @@ #pragma once +#include "BendersMathLogger.h" +#include "ILogger.h" #include "SubproblemCut.h" #include "Worker.h" #include "common.h" -#include "ILogger.h" struct CurrentIterationData { double subproblems_walltime; @@ -33,6 +34,8 @@ struct CurrentIterationData { bool is_in_initial_relaxation; int number_of_subproblem_resolved; }; +MathLoggerData MathLoggerDataFromCurrentIterationData( + const CurrentIterationData& data); /*! * \class WorkerMasterData * \brief Class use to store trace information during the algorithm run diff --git a/src/cpp/benders/benders_mpi/BendersMPI.cpp b/src/cpp/benders/benders_mpi/BendersMPI.cpp index 075021955..c967cf999 100644 --- a/src/cpp/benders/benders_mpi/BendersMPI.cpp +++ b/src/cpp/benders/benders_mpi/BendersMPI.cpp @@ -95,6 +95,7 @@ void BendersMpi::solve_master_and_create_trace() { ComputeXCut(); _logger->log_iteration_candidates(bendersDataToLogData(_data)); + mathLoggerDriver_->Print(MathLoggerDataFromCurrentIterationData(_data)); } /*! @@ -225,6 +226,7 @@ void BendersMpi::free() { */ void BendersMpi::Run() { PreRunInitialization(); + mathLoggerDriver_->write_header(); while (!_data.stop) { Timer timer_master; ++_data.it; diff --git a/src/cpp/benders/factories/include/LoggerFactories.h b/src/cpp/benders/factories/include/LoggerFactories.h index 148be35b4..da78b8904 100644 --- a/src/cpp/benders/factories/include/LoggerFactories.h +++ b/src/cpp/benders/factories/include/LoggerFactories.h @@ -36,18 +36,18 @@ class FileAndStdoutLoggerFactory { class MathLoggerFactory { private: MathLoggerDriver math_Logger_driver; - MathLoggerFile math_logger_file_; - MathLoggerOstream math_Logger_ostream_; + std::shared_ptr math_logger_file_; + std::shared_ptr math_Logger_ostream_; public: explicit MathLoggerFactory( bool console_log, const std::filesystem::path &math_logs_file_path = "") { if (math_logs_file_path != "") { - math_logger_file_ = MathLoggerFile(math_logs_file_path); - math_Logger_driver.add_logger(&math_logger_file_); + math_logger_file_ = std::make_shared(math_logs_file_path); + math_Logger_driver.add_logger(math_logger_file_); } if (console_log) { - math_Logger_driver.add_logger(&math_Logger_ostream_); + math_Logger_driver.add_logger(math_Logger_ostream_); } } explicit MathLoggerFactory() = default; diff --git a/src/cpp/benders/logger/MathLogger.cpp b/src/cpp/benders/logger/MathLogger.cpp index a89468f17..8e9d71b15 100644 --- a/src/cpp/benders/logger/MathLogger.cpp +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -4,16 +4,11 @@ #include #include "LoggerUtils.h" -std::string Indent(int size) { - std::stringstream ss; - ss << std::setw(size); - return ss.str(); -} +std::string Indent(int size) { return std::string(size, ' '); } void MathLogger::write_header() { log_destination << Indent(10) << "ITE"; log_destination << Indent(20) << "LB"; - log_destination << Indent(20) << "LEV"; log_destination << Indent(20) << "UB"; log_destination << Indent(20) << "BESTUB"; log_destination << Indent(15) << "GAP"; diff --git a/src/python/antares_xpansion/config_loader.py b/src/python/antares_xpansion/config_loader.py index d4b560a3e..5401fd51d 100644 --- a/src/python/antares_xpansion/config_loader.py +++ b/src/python/antares_xpansion/config_loader.py @@ -537,7 +537,7 @@ def update_last_study_with_sensitivity_results(self): def is_antares_study_output(self, study: Path): _, ext = os.path.splitext(study) - return ext == ".zip" or os.path.isdir(study) and '-Xpansion' not in study.name + return ext == ".zip" or (os.path.isdir(study) and '-Xpansion' in study.name) def last_modified_study(self, root_dir:Path)-> Path: list_dir = os.listdir(root_dir) From 847b9db9f69793abeb013064508e3fa245ef6ecf Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Mon, 20 Nov 2023 18:35:30 +0100 Subject: [PATCH 09/68] add missing include --- src/cpp/benders/benders_core/include/BendersMathLogger.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cpp/benders/benders_core/include/BendersMathLogger.h b/src/cpp/benders/benders_core/include/BendersMathLogger.h index cba3c9d8d..1d9b81385 100644 --- a/src/cpp/benders/benders_core/include/BendersMathLogger.h +++ b/src/cpp/benders/benders_core/include/BendersMathLogger.h @@ -3,6 +3,7 @@ #include #include #include +#include const std::string MATHLOGGERCONTEXT = "Benders"; struct MathLoggerData { From b2680f0354fa6fa389b65ef2fb8c79c59d102f05 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Mon, 20 Nov 2023 18:44:38 +0100 Subject: [PATCH 10/68] fix --- src/cpp/benders/benders_core/include/BendersMathLogger.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cpp/benders/benders_core/include/BendersMathLogger.h b/src/cpp/benders/benders_core/include/BendersMathLogger.h index 1d9b81385..e1f54060f 100644 --- a/src/cpp/benders/benders_core/include/BendersMathLogger.h +++ b/src/cpp/benders/benders_core/include/BendersMathLogger.h @@ -60,7 +60,7 @@ class MathLoggerDriver { public: MathLoggerDriver() = default; void write_header(); - void MathLoggerDriver::add_logger(std::shared_ptr logger); + void add_logger(std::shared_ptr logger); void Print(const MathLoggerData& data); private: From ef378578acde908779e467f5b12ab8f5f8b9ae9f Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Wed, 22 Nov 2023 18:17:25 +0100 Subject: [PATCH 11/68] print more data --- .../benders_by_batch/BendersByBatch.cpp | 1 + src/cpp/benders/benders_core/BendersBase.cpp | 13 ++++++++ .../benders_core/BendersMathLogger.cpp | 2 +- .../benders_core/BendersStructsDatas.cpp | 16 --------- .../benders_core/include/BendersBase.h | 2 ++ .../benders_core/include/BendersMathLogger.h | 19 ++--------- .../include/BendersStructsDatas.h | 5 ++- src/cpp/benders/benders_mpi/BendersMPI.cpp | 7 ++-- src/cpp/benders/logger/MathLogger.cpp | 33 ++++++++++--------- 9 files changed, 44 insertions(+), 54 deletions(-) diff --git a/src/cpp/benders/benders_by_batch/BendersByBatch.cpp b/src/cpp/benders/benders_by_batch/BendersByBatch.cpp index a0814fcd7..ce507b1fd 100644 --- a/src/cpp/benders/benders_by_batch/BendersByBatch.cpp +++ b/src/cpp/benders/benders_by_batch/BendersByBatch.cpp @@ -230,6 +230,7 @@ void BendersByBatch::BuildCut( for (const auto &subproblem_map : gathered_subproblem_map) { for (auto &&[_, subproblem_data] : subproblem_map) { SetSubproblemCost(GetSubproblemCost() + subproblem_data.subproblem_cost); + BoundSimplexIterations(subproblem_data.simplex_iter); } } for (const auto &subproblem_map : gathered_subproblem_map) { diff --git a/src/cpp/benders/benders_core/BendersBase.cpp b/src/cpp/benders/benders_core/BendersBase.cpp index f722e3a06..6ce2b9b2d 100644 --- a/src/cpp/benders/benders_core/BendersBase.cpp +++ b/src/cpp/benders/benders_core/BendersBase.cpp @@ -775,6 +775,19 @@ void BendersBase::SetSubproblemCost(const double &subproblem_cost) { _data.subproblem_cost = subproblem_cost; } +/*! +* \brief Update maximum and minimum of simplex iterations +* +* \param subproblem_iterations : number of iterations done with the subproblem +* +*/ +void BendersBase::BoundSimplexIterations(int subproblem_iterations){ + + _data.max_simplexiter = (_data.max_simplexiter < subproblem_iterations) ? subproblem_iterations : _data.max_simplexiter; + _data.min_simplexiter = (_data.min_simplexiter > subproblem_iterations) ? subproblem_iterations : _data.min_simplexiter; + +} + bool BendersBase::IsResumeMode() const { return _options.RESUME; } void BendersBase::UpdateMaxNumberIterationResumeMode( diff --git a/src/cpp/benders/benders_core/BendersMathLogger.cpp b/src/cpp/benders/benders_core/BendersMathLogger.cpp index 79686d262..0819fbeec 100644 --- a/src/cpp/benders/benders_core/BendersMathLogger.cpp +++ b/src/cpp/benders/benders_core/BendersMathLogger.cpp @@ -6,7 +6,7 @@ void MathLoggerDriver::add_logger(std::shared_ptr logger) { } } -void MathLoggerDriver::Print(const MathLoggerData& data) { +void MathLoggerDriver::Print(const CurrentIterationData& data) { for (auto logger : math_loggers_) { logger->Print(data); } diff --git a/src/cpp/benders/benders_core/BendersStructsDatas.cpp b/src/cpp/benders/benders_core/BendersStructsDatas.cpp index f5a0911bc..9b5e8dc8a 100644 --- a/src/cpp/benders/benders_core/BendersStructsDatas.cpp +++ b/src/cpp/benders/benders_core/BendersStructsDatas.cpp @@ -9,19 +9,3 @@ Point WorkerMasterData::get_min_invest() const { return *_min_invest; } Point WorkerMasterData::get_max_invest() const { return *_max_invest; } -MathLoggerData MathLoggerDataFromCurrentIterationData( - const CurrentIterationData& data) { - return {/*.iteration =*/data.it, - /*.lower_bound =*/data.lb, - /*.upper_bound =*/data.ub, - /*.best_upper_bound =*/data.best_ub, - /*.optimality_gap =*/0, - /*.relative_gap =*/0, - /*.max_simplexiter =*/0, - /*.min_simplexiter =*/0, - /*.deletedcut =*/data.deletedcut, - /*.time_master =*/data.timer_master, - /*.time_subproblems =*/data.subproblems_cputime - - }; -} \ No newline at end of file diff --git a/src/cpp/benders/benders_core/include/BendersBase.h b/src/cpp/benders/benders_core/include/BendersBase.h index 9b0ee2470..7e8db7631 100644 --- a/src/cpp/benders/benders_core/include/BendersBase.h +++ b/src/cpp/benders/benders_core/include/BendersBase.h @@ -139,6 +139,8 @@ class BendersBase { return cumulative_number_of_subproblem_resolved_before_resume; } + void BoundSimplexIterations(int subproblem_iteration); + SolverLogManager solver_log_manager_; private: diff --git a/src/cpp/benders/benders_core/include/BendersMathLogger.h b/src/cpp/benders/benders_core/include/BendersMathLogger.h index e1f54060f..2d0e2f028 100644 --- a/src/cpp/benders/benders_core/include/BendersMathLogger.h +++ b/src/cpp/benders/benders_core/include/BendersMathLogger.h @@ -4,22 +4,9 @@ #include #include #include +#include "BendersStructsDatas.h" const std::string MATHLOGGERCONTEXT = "Benders"; -struct MathLoggerData { - int iteration; - double lower_bound; - double upper_bound; - double best_upper_bound; - double optimality_gap; - double relative_gap; - int max_simplexiter; - int min_simplexiter; - int deletedcut; - double time_master; - double time_subproblems; - // double alpha; -}; class LogDestination { public: @@ -52,7 +39,7 @@ struct MathLogger { explicit MathLogger(std::ostream* stream) : log_destination(stream) {} explicit MathLogger() : log_destination(&std::cout) {} void write_header(); - void Print(const MathLoggerData& data); + void Print(const CurrentIterationData& data); LogDestination log_destination; }; @@ -61,7 +48,7 @@ class MathLoggerDriver { MathLoggerDriver() = default; void write_header(); void add_logger(std::shared_ptr logger); - void Print(const MathLoggerData& data); + void Print(const CurrentIterationData& data); private: std::list> math_loggers_; diff --git a/src/cpp/benders/benders_core/include/BendersStructsDatas.h b/src/cpp/benders/benders_core/include/BendersStructsDatas.h index 6305b6292..05ec3deae 100644 --- a/src/cpp/benders/benders_core/include/BendersStructsDatas.h +++ b/src/cpp/benders/benders_core/include/BendersStructsDatas.h @@ -1,6 +1,5 @@ #pragma once -#include "BendersMathLogger.h" #include "ILogger.h" #include "SubproblemCut.h" #include "Worker.h" @@ -33,9 +32,9 @@ struct CurrentIterationData { StoppingCriterion stopping_criterion; bool is_in_initial_relaxation; int number_of_subproblem_resolved; + int min_simplexiter; + int max_simplexiter; }; -MathLoggerData MathLoggerDataFromCurrentIterationData( - const CurrentIterationData& data); /*! * \class WorkerMasterData * \brief Class use to store trace information during the algorithm run diff --git a/src/cpp/benders/benders_mpi/BendersMPI.cpp b/src/cpp/benders/benders_mpi/BendersMPI.cpp index c967cf999..b5abb9a18 100644 --- a/src/cpp/benders/benders_mpi/BendersMPI.cpp +++ b/src/cpp/benders/benders_mpi/BendersMPI.cpp @@ -95,7 +95,6 @@ void BendersMpi::solve_master_and_create_trace() { ComputeXCut(); _logger->log_iteration_candidates(bendersDataToLogData(_data)); - mathLoggerDriver_->Print(MathLoggerDataFromCurrentIterationData(_data)); } /*! @@ -155,6 +154,7 @@ void BendersMpi::master_build_cuts( for (const auto &subproblem_data_map : gathered_subproblem_map) { for (auto &&[_, subproblem_data] : subproblem_data_map) { SetSubproblemCost(GetSubproblemCost() + subproblem_data.subproblem_cost); + BoundSimplexIterations(subproblem_data.simplex_iter); } } @@ -244,7 +244,10 @@ void BendersMpi::Run() { step_4_update_best_solution(_world.rank(), timer_master); } _data.stop |= _exceptionRaised; - + if(Rank()==rank_0){ + + mathLoggerDriver_->Print(_data); + } broadcast(_world, _data.is_in_initial_relaxation, rank_0); broadcast(_world, _data.stop, rank_0); if (_world.rank() == rank_0) { diff --git a/src/cpp/benders/logger/MathLogger.cpp b/src/cpp/benders/logger/MathLogger.cpp index 8e9d71b15..df3f58fc0 100644 --- a/src/cpp/benders/logger/MathLogger.cpp +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -11,49 +11,50 @@ void MathLogger::write_header() { log_destination << Indent(20) << "LB"; log_destination << Indent(20) << "UB"; log_destination << Indent(20) << "BESTUB"; - log_destination << Indent(15) << "GAP"; + log_destination << Indent(15) << "ABSOLUTE GAP"; + log_destination << Indent(15) << "RELATIVE GAP"; log_destination << Indent(15) << "MINSIMPLEX"; log_destination << Indent(15) << "MAXSIMPLEX"; - log_destination << Indent(15) << "DELETEDCUT"; + // log_destination << Indent(15) << "DELETEDCUT"; log_destination << Indent(15) << "TIMEMASTER"; log_destination << Indent(15) << "TIMESLAVES"; log_destination << std::endl; } -void MathLogger::Print(const MathLoggerData& data) { - log_destination << Indent(10) << data.iteration; - if (data.lower_bound == -1e20) +void MathLogger::Print(const CurrentIterationData& data) { + log_destination << Indent(10) << data.it; + if (data.lb == -1e20) log_destination << Indent(20) << "-INF"; else log_destination << Indent(20) << std::scientific << std::setprecision(10) - << data.lower_bound; - if (data.upper_bound == +1e20) + << data.lb; + if (data.ub == +1e20) log_destination << Indent(20) << "+INF"; else log_destination << Indent(20) << std::scientific << std::setprecision(10) - << data.upper_bound; - if (data.best_upper_bound == +1e20) + << data.ub; + if (data.best_ub == +1e20) log_destination << Indent(20) << "+INF"; else log_destination << Indent(20) << std::scientific << std::setprecision(10) - << data.best_upper_bound; + << data.best_ub; log_destination << Indent(15) << std::scientific << std::setprecision(2) - << data.best_upper_bound - data.lower_bound; + << data.best_ub - data.lb; log_destination << Indent(15) << std::scientific << std::setprecision(2) - << (data.best_upper_bound - data.lower_bound) / - data.best_upper_bound; + << (data.best_ub - data.lb) / + data.best_ub; log_destination << Indent(15) << data.min_simplexiter; log_destination << Indent(15) << data.max_simplexiter; - log_destination << Indent(15) << data.deletedcut; - log_destination << Indent(15) << std::setprecision(2) << data.time_master; + // log_destination << Indent(15) << data.deletedcut; + log_destination << Indent(15) << std::setprecision(2) << data.timer_master; log_destination << Indent(15) << std::setprecision(2) - << data.time_subproblems; + << data.subproblems_walltime; log_destination << std::endl; } From 896fddefa435c4e332ed61a4068b305ac4c798dd Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Fri, 24 Nov 2023 10:57:57 +0100 Subject: [PATCH 12/68] Reset Simplex Iterations Bounds --- src/cpp/benders/benders_core/BendersBase.cpp | 5 +++++ src/cpp/benders/benders_core/include/BendersBase.h | 1 + src/cpp/benders/benders_mpi/BendersMPI.cpp | 3 ++- src/cpp/benders/logger/MathLogger.cpp | 1 - 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/cpp/benders/benders_core/BendersBase.cpp b/src/cpp/benders/benders_core/BendersBase.cpp index 6ce2b9b2d..675ae4f98 100644 --- a/src/cpp/benders/benders_core/BendersBase.cpp +++ b/src/cpp/benders/benders_core/BendersBase.cpp @@ -788,6 +788,11 @@ void BendersBase::BoundSimplexIterations(int subproblem_iterations){ } +void BendersBase::ResetSimplexIterationsBounds() +{ + _data.max_simplexiter = 0; + _data.min_simplexiter = std::numeric_limits::max(); +} bool BendersBase::IsResumeMode() const { return _options.RESUME; } void BendersBase::UpdateMaxNumberIterationResumeMode( diff --git a/src/cpp/benders/benders_core/include/BendersBase.h b/src/cpp/benders/benders_core/include/BendersBase.h index 7e8db7631..59855fdfb 100644 --- a/src/cpp/benders/benders_core/include/BendersBase.h +++ b/src/cpp/benders/benders_core/include/BendersBase.h @@ -140,6 +140,7 @@ class BendersBase { } void BoundSimplexIterations(int subproblem_iteration); + void ResetSimplexIterationsBounds(); SolverLogManager solver_log_manager_; diff --git a/src/cpp/benders/benders_mpi/BendersMPI.cpp b/src/cpp/benders/benders_mpi/BendersMPI.cpp index b5abb9a18..40fd923ae 100644 --- a/src/cpp/benders/benders_mpi/BendersMPI.cpp +++ b/src/cpp/benders/benders_mpi/BendersMPI.cpp @@ -230,6 +230,7 @@ void BendersMpi::Run() { while (!_data.stop) { Timer timer_master; ++_data.it; + ResetSimplexIterationsBounds(); /*Solve Master problem, get optimal value and cost and send it to process*/ step_1_solve_master(); @@ -244,7 +245,7 @@ void BendersMpi::Run() { step_4_update_best_solution(_world.rank(), timer_master); } _data.stop |= _exceptionRaised; - if(Rank()==rank_0){ + if(Rank() == rank_0){ mathLoggerDriver_->Print(_data); } diff --git a/src/cpp/benders/logger/MathLogger.cpp b/src/cpp/benders/logger/MathLogger.cpp index df3f58fc0..335136f41 100644 --- a/src/cpp/benders/logger/MathLogger.cpp +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -17,7 +17,6 @@ void MathLogger::write_header() { log_destination << Indent(15) << "MINSIMPLEX"; log_destination << Indent(15) << "MAXSIMPLEX"; - // log_destination << Indent(15) << "DELETEDCUT"; log_destination << Indent(15) << "TIMEMASTER"; log_destination << Indent(15) << "TIMESLAVES"; From 3616fcfb610da486f5916079ed50b61d2b16bc34 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Fri, 24 Nov 2023 17:55:20 +0100 Subject: [PATCH 13/68] introducing MathlogImplementation --- cmake/dependencies/CMakeLists.txt | 2 +- .../benders_by_batch/BendersByBatch.cpp | 3 + .../benders_core/BendersMathLogger.cpp | 3 +- .../benders_core/SimulationOptions.cpp | 2 +- .../benders_core/include/BendersMathLogger.h | 77 +++++++++++++++-- src/cpp/benders/benders_core/include/common.h | 2 + src/cpp/benders/benders_mpi/BendersMPI.cpp | 10 ++- src/cpp/benders/factories/BendersFactory.cpp | 2 +- .../factories/include/BendersFactory.h | 2 +- .../factories/include/LoggerFactories.h | 23 ++--- src/cpp/benders/logger/MathLogger.cpp | 83 ++++++++++++++----- .../logger/include/logger/MathLogger.h | 14 ++-- 12 files changed, 170 insertions(+), 53 deletions(-) diff --git a/cmake/dependencies/CMakeLists.txt b/cmake/dependencies/CMakeLists.txt index e65c3c3b1..b8ee01495 100644 --- a/cmake/dependencies/CMakeLists.txt +++ b/cmake/dependencies/CMakeLists.txt @@ -9,7 +9,7 @@ if (NOT antares-solver_FOUND) set(REPOSITORY "https://github.com/AntaresSimulatorTeam/Antares_Simulator.git") set(TAG "v${ANTARES_VERSION_TAG}") - set(CMAKE_ARGS "-DBUILD_UI=OFF -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} -DDEPS_INSTALL_DIR=${DEPS_INSTALL_DIR} -DBUILD_not_system=OFF -DBUILD_ortools=ON") + set(CMAKE_ARGS "-DBUILD_UI=OFF -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} -DDEPS_INSTALL_DIR=${DEPS_INSTALL_DIR} -DBUILD_not_system=OFF -DBUILD_ortools=ON -DCMAKE_PREFIX_PATH=${DEPS_INSTALL_DIR}/../antares-xpansion/vcpkg_installed") if (CMAKE_BUILD_TYPE STREQUAL "Debug") set(ANTARES_BUILD_TYPE "debug") diff --git a/src/cpp/benders/benders_by_batch/BendersByBatch.cpp b/src/cpp/benders/benders_by_batch/BendersByBatch.cpp index ce507b1fd..64690e037 100644 --- a/src/cpp/benders/benders_by_batch/BendersByBatch.cpp +++ b/src/cpp/benders/benders_by_batch/BendersByBatch.cpp @@ -125,6 +125,7 @@ void BendersByBatch::MasterLoop() { _logger->LogSubproblemsSolvingCumulativeCpuTime( GetSubproblemsCumulativeCpuTime()); _logger->LogSubproblemsSolvingWalltime(GetSubproblemsWalltime()); + mathLoggerDriver_->Print(_data); _logger->display_message( "\\________________________________________________________________" "________"); @@ -136,6 +137,7 @@ void BendersByBatch::SeparationLoop() { batch_counter_ = 0; while (misprice_ && batch_counter_ < number_of_batch_) { _data.it++; + ResetSimplexIterationsBounds(); _logger->log_at_initialization(_data.it + GetNumIterationsBeforeRestart()); ComputeXCut(); @@ -143,6 +145,7 @@ void BendersByBatch::SeparationLoop() { BroadcastXCut(); UpdateRemainingEpsilon(); SolveBatches(); + if (Rank() == rank_0) { UpdateTrace(); SaveCurrentBendersData(); diff --git a/src/cpp/benders/benders_core/BendersMathLogger.cpp b/src/cpp/benders/benders_core/BendersMathLogger.cpp index 0819fbeec..a39d04962 100644 --- a/src/cpp/benders/benders_core/BendersMathLogger.cpp +++ b/src/cpp/benders/benders_core/BendersMathLogger.cpp @@ -1,6 +1,7 @@ #include "BendersMathLogger.h" -void MathLoggerDriver::add_logger(std::shared_ptr logger) { +void MathLoggerDriver::add_logger( + std::shared_ptr logger) { if (logger) { math_loggers_.push_back(logger); } diff --git a/src/cpp/benders/benders_core/SimulationOptions.cpp b/src/cpp/benders/benders_core/SimulationOptions.cpp index 22b64c9cc..d88d0cc9a 100644 --- a/src/cpp/benders/benders_core/SimulationOptions.cpp +++ b/src/cpp/benders/benders_core/SimulationOptions.cpp @@ -112,7 +112,7 @@ void SimulationOptions::set_weights() { void SimulationOptions::print(std::ostream &stream) const { #define BENDERS_OPTIONS_MACRO(name__, type__, default__, \ deserialization_method__) \ - stream << std::setw(30) << #name__ << std::setw(50) << name__ << std::endl; + stream << std::setw(30) << #name__ << std::setw(50)< #include #include + #include "BendersStructsDatas.h" +#include "common.h" const std::string MATHLOGGERCONTEXT = "Benders"; - +inline std::string Indent(int size) { return std::string(size, ' '); } +const std::string ITE = Indent(10) + "ITE"; +const std::string LB = Indent(20) + "LB"; +const std::string UB = Indent(20) + "UB"; +const std::string BESTUB = Indent(20) + "BESTUB"; +const std::string ABSOLUTE_GAP = Indent(15) + "ABSOLUTE GAP"; +const std::string RELATIVE_GAP = Indent(15) + "RELATIVE GAP"; +const std::string MINSIMPLEX = Indent(15) + "MINSIMPLEX"; +const std::string MAXSIMPLEX = Indent(15) + "MAXSIMPLEX"; +const std::string TIMEMASTER = Indent(15) + "TIMEMASTER"; +const std::string SUB_PROBLEMS_TIME_CPU = + Indent(15) + "SUB-PROBLEMS TIME (CPU)"; +const std::string SUB_PROBLEMS_TIME_WALL = + Indent(15) + "SUB-PROBLEMS TIME (WALL)"; +const std::string TIME_NOT_DOING_MASTER_OR_SUB_PROBLEMS_WALL = + Indent(15) + "TIME NOT DOING MASTER OR SUB-PROBLEMS (WALL)"; class LogDestination { public: explicit LogDestination(std::ostream* stream) : stream_(stream) {} @@ -35,21 +52,69 @@ std::ostream& LogDestination::operator<<(const T& obj) { // write obj to stream return (*stream_) << obj; } + struct MathLogger { - explicit MathLogger(std::ostream* stream) : log_destination(stream) {} - explicit MathLogger() : log_destination(&std::cout) {} + explicit MathLogger(std::ostream* stream) : log_destination(stream) { + setHeadersList(); + } + explicit MathLogger() : log_destination(&std::cout) { setHeadersList(); } void write_header(); - void Print(const CurrentIterationData& data); + virtual void Print(const CurrentIterationData& data) = 0; + virtual void setHeadersList() = 0; + LogDestination log_destination; + std::list headers; +}; + +struct MathLoggerBase : public MathLogger { + using MathLogger::MathLogger; + void Print(const CurrentIterationData& data) override; + void setHeadersList() override; +}; + +struct MathLoggerBendersByBatch : public MathLogger { + using MathLogger::MathLogger; + void Print(const CurrentIterationData& data) override; + void setHeadersList() override; +}; + +class MathLoggerImplementation : public MathLogger { + public: + explicit MathLoggerImplementation(const BENDERSMETHOD& method, + std::ostream* stream) { + if (method == BENDERSMETHOD::BENDERS) { + implementation_ = std::make_shared(stream); + } else if (method == BENDERSMETHOD::BENDERSBYBATCH) { + implementation_ = std::make_shared(stream); + } + // else + } + explicit MathLoggerImplementation(const BENDERSMETHOD& method) { + if (method == BENDERSMETHOD::BENDERS) { + implementation_ = std::make_shared(); + } else if (method == BENDERSMETHOD::BENDERSBYBATCH) { + implementation_ = std::make_shared(); + } + // else } + } + + virtual void Print(const CurrentIterationData& data) { + implementation_->Print(data); + } + virtual void setHeadersList() { implementation_->setHeadersList(); } + + private: + std::shared_ptr implementation_; }; class MathLoggerDriver { public: MathLoggerDriver() = default; void write_header(); - void add_logger(std::shared_ptr logger); + + void add_logger(std::shared_ptr logger); void Print(const CurrentIterationData& data); private: - std::list> math_loggers_; + std::list> math_loggers_; }; diff --git a/src/cpp/benders/benders_core/include/common.h b/src/cpp/benders/benders_core/include/common.h index b12ab95d3..728e25910 100644 --- a/src/cpp/benders/benders_core/include/common.h +++ b/src/cpp/benders/benders_core/include/common.h @@ -49,6 +49,8 @@ typedef std::vector ActiveCutStorage; typedef std::pair mps_coupling; typedef std::list mps_coupling_list; +enum class BENDERSMETHOD { BENDERS, BENDERSBYBATCH, MERGEMPS }; + struct Predicate { bool operator()(PointPtr const &lhs, PointPtr const &rhs) const { return *lhs < *rhs; diff --git a/src/cpp/benders/benders_mpi/BendersMPI.cpp b/src/cpp/benders/benders_mpi/BendersMPI.cpp index 40fd923ae..a2cd043ad 100644 --- a/src/cpp/benders/benders_mpi/BendersMPI.cpp +++ b/src/cpp/benders/benders_mpi/BendersMPI.cpp @@ -138,6 +138,7 @@ void BendersMpi::gather_subproblems_cut_package_and_build_cuts( Reduce(GetSubproblemsCpuTime(), cumulative_subproblems_timer_per_iter, std::plus(), rank_0); SetSubproblemsCumulativeCpuTime(cumulative_subproblems_timer_per_iter); + // only rank_0 receive non-emtpy gathered_subproblem_map master_build_cuts(gathered_subproblem_map); } } @@ -154,6 +155,7 @@ void BendersMpi::master_build_cuts( for (const auto &subproblem_data_map : gathered_subproblem_map) { for (auto &&[_, subproblem_data] : subproblem_data_map) { SetSubproblemCost(GetSubproblemCost() + subproblem_data.subproblem_cost); + // compute delta_cut >= options.CUT_MASTER_TOL; BoundSimplexIterations(subproblem_data.simplex_iter); } } @@ -245,10 +247,11 @@ void BendersMpi::Run() { step_4_update_best_solution(_world.rank(), timer_master); } _data.stop |= _exceptionRaised; - if(Rank() == rank_0){ - - mathLoggerDriver_->Print(_data); + + if (Rank() == rank_0) { + mathLoggerDriver_->Print(_data); } + broadcast(_world, _data.is_in_initial_relaxation, rank_0); broadcast(_world, _data.stop, rank_0); if (_world.rank() == rank_0) { @@ -281,6 +284,7 @@ void BendersMpi::PreRunInitialization() { OpenCsvFile(); } } + mathLoggerDriver_->write_header(); } void BendersMpi::launch() { build_input_map(); diff --git a/src/cpp/benders/factories/BendersFactory.cpp b/src/cpp/benders/factories/BendersFactory.cpp index 87cd07cb3..2266be0b4 100644 --- a/src/cpp/benders/factories/BendersFactory.cpp +++ b/src/cpp/benders/factories/BendersFactory.cpp @@ -42,7 +42,7 @@ int RunBenders(char** argv, const std::filesystem::path& options_file, if (world.rank() == 0) { auto logger_factory = FileAndStdoutLoggerFactory(log_reports_name); - auto math_log_factory = MathLoggerFactory(false, math_logs_file); + auto math_log_factory = MathLoggerFactory(method, false, math_logs_file); logger = logger_factory.get_logger(); math_log_driver = math_log_factory.get_logger(); diff --git a/src/cpp/benders/factories/include/BendersFactory.h b/src/cpp/benders/factories/include/BendersFactory.h index a3e310f52..db304e415 100644 --- a/src/cpp/benders/factories/include/BendersFactory.h +++ b/src/cpp/benders/factories/include/BendersFactory.h @@ -5,7 +5,7 @@ #include "BendersSequential.h" #include "ILogger.h" #include "OutputWriter.h" -enum class BENDERSMETHOD { BENDERS, BENDERSBYBATCH, MERGEMPS }; +#include "common.h" class BendersMainFactory { private: diff --git a/src/cpp/benders/factories/include/LoggerFactories.h b/src/cpp/benders/factories/include/LoggerFactories.h index da78b8904..142cfd8f3 100644 --- a/src/cpp/benders/factories/include/LoggerFactories.h +++ b/src/cpp/benders/factories/include/LoggerFactories.h @@ -4,6 +4,7 @@ #include +#include "BendersFactory.h" #include "ILogger.h" #include "SimulationOptions.h" #include "logger/Master.h" @@ -36,23 +37,25 @@ class FileAndStdoutLoggerFactory { class MathLoggerFactory { private: MathLoggerDriver math_Logger_driver; - std::shared_ptr math_logger_file_; - std::shared_ptr math_Logger_ostream_; public: explicit MathLoggerFactory( - bool console_log, const std::filesystem::path &math_logs_file_path = "") { + const BENDERSMETHOD &method, bool console_log, + const std::filesystem::path &math_logs_file_path = "") { if (math_logs_file_path != "") { - math_logger_file_ = std::make_shared(math_logs_file_path); - math_Logger_driver.add_logger(math_logger_file_); - } - if (console_log) { - math_Logger_driver.add_logger(math_Logger_ostream_); - } + auto math_logger_file = + std::make_shared(method, math_logs_file_path); + math_Logger_driver.add_logger(math_logger_file); + + if (console_log) { + auto math_Logger_ostream = std::make_shared(method); + + math_Logger_driver.add_logger(math_Logger_ostream); + } } explicit MathLoggerFactory() = default; std::shared_ptr get_logger() { return std::make_shared(math_Logger_driver); } -}; + }; #endif // ANTARESXPANSION_LOGGERFACTORIES_H diff --git a/src/cpp/benders/logger/MathLogger.cpp b/src/cpp/benders/logger/MathLogger.cpp index 335136f41..8fbfe387f 100644 --- a/src/cpp/benders/logger/MathLogger.cpp +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -3,27 +3,28 @@ #include #include -#include "LoggerUtils.h" -std::string Indent(int size) { return std::string(size, ' '); } +void MathLoggerBase::setHeadersList() { + headers.clear(); + headers = {ITE, + LB, + UB, + BESTUB, + ABSOLUTE_GAP, + RELATIVE_GAP, + MINSIMPLEX, + MAXSIMPLEX, + TIMEMASTER, + SUB_PROBLEMS_TIME_CPU, + SUB_PROBLEMS_TIME_WALL}; +} void MathLogger::write_header() { - log_destination << Indent(10) << "ITE"; - log_destination << Indent(20) << "LB"; - log_destination << Indent(20) << "UB"; - log_destination << Indent(20) << "BESTUB"; - log_destination << Indent(15) << "ABSOLUTE GAP"; - log_destination << Indent(15) << "RELATIVE GAP"; - - log_destination << Indent(15) << "MINSIMPLEX"; - log_destination << Indent(15) << "MAXSIMPLEX"; - - log_destination << Indent(15) << "TIMEMASTER"; - log_destination << Indent(15) << "TIMESLAVES"; - + for (const auto& header : headers) { + log_destination << header; + } log_destination << std::endl; } - -void MathLogger::Print(const CurrentIterationData& data) { +void MathLoggerBase::Print(const CurrentIterationData& data) { log_destination << Indent(10) << data.it; if (data.lb == -1e20) log_destination << Indent(20) << "-INF"; @@ -44,25 +45,61 @@ void MathLogger::Print(const CurrentIterationData& data) { << data.best_ub - data.lb; log_destination << Indent(15) << std::scientific << std::setprecision(2) - << (data.best_ub - data.lb) / - data.best_ub; + << (data.best_ub - data.lb) / data.best_ub; log_destination << Indent(15) << data.min_simplexiter; log_destination << Indent(15) << data.max_simplexiter; // log_destination << Indent(15) << data.deletedcut; log_destination << Indent(15) << std::setprecision(2) << data.timer_master; + log_destination << Indent(15) << std::setprecision(2) + << data.subproblems_cputime; log_destination << Indent(15) << std::setprecision(2) << data.subproblems_walltime; log_destination << std::endl; } -MathLoggerFile::MathLoggerFile(const std::filesystem::path &filename) - : MathLogger(&file_stream_) { - file_stream_.open(filename, std::ofstream::out | std::ofstream::app); +void MathLoggerBendersByBatch::setHeadersList() { + headers.clear(); + headers = {ITE, + LB, + MINSIMPLEX, + MAXSIMPLEX, + TIMEMASTER, + SUB_PROBLEMS_TIME_CPU, + SUB_PROBLEMS_TIME_WALL, + TIME_NOT_DOING_MASTER_OR_SUB_PROBLEMS_WALL}; +} + +void MathLoggerBendersByBatch::Print(const CurrentIterationData& data) { + log_destination << Indent(10) << data.it; + if (data.lb == -1e20) + log_destination << Indent(20) << "-INF"; + else + log_destination << Indent(20) << std::scientific << std::setprecision(10) + << data.lb; + + log_destination << Indent(15) << data.min_simplexiter; + log_destination << Indent(15) << data.max_simplexiter; + + // log_destination << Indent(15) << data.deletedcut; + log_destination << Indent(15) << std::setprecision(2) << data.timer_master; + log_destination << Indent(15) << std::setprecision(2) + << data.subproblems_cumulative_cputime; + log_destination << Indent(15) << std::setprecision(2) + << data.subproblems_walltime; + + log_destination << std::endl; +} + +MathLoggerFile::MathLoggerFile(const BENDERSMETHOD& method, + const std::filesystem::path& filename) + : MathLoggerImplementation(method, &file_stream_) { + // TODO restart case????????????? + file_stream_.open(filename, std::ofstream::out); if (file_stream_.fail()) { std::cerr << PrefixMessage(LogUtils::LOGLEVEL::ERR, MATHLOGGERCONTEXT) << "Invalid file name passed as parameter" << std::endl; } -} +} \ No newline at end of file diff --git a/src/cpp/benders/logger/include/logger/MathLogger.h b/src/cpp/benders/logger/include/logger/MathLogger.h index f1e433007..2564d3d5b 100644 --- a/src/cpp/benders/logger/include/logger/MathLogger.h +++ b/src/cpp/benders/logger/include/logger/MathLogger.h @@ -3,16 +3,18 @@ #include #include "BendersMathLogger.h" -class MathLoggerFile : public MathLogger { +#include "LoggerUtils.h" + +class MathLoggerFile : public MathLoggerImplementation { public: - explicit MathLoggerFile(const std::filesystem::path& log_file); - using MathLogger::MathLogger; + explicit MathLoggerFile(const BENDERSMETHOD& method, + const std::filesystem::path& log_file); private: std::ofstream file_stream_; }; - -class MathLoggerOstream : public MathLogger { +class MathLoggerOstream : public MathLoggerImplementation { public: - explicit MathLoggerOstream() : MathLogger(&std::cout) {} + explicit MathLoggerOstream(const BENDERSMETHOD& method) + : MathLoggerImplementation(method, &std::cout) {} }; From ff73b8905f16884adad8dbee9fc3c8a24f370817 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Fri, 24 Nov 2023 18:02:04 +0100 Subject: [PATCH 14/68] fix --- src/cpp/benders/factories/include/LoggerFactories.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cpp/benders/factories/include/LoggerFactories.h b/src/cpp/benders/factories/include/LoggerFactories.h index 142cfd8f3..7cbaa10af 100644 --- a/src/cpp/benders/factories/include/LoggerFactories.h +++ b/src/cpp/benders/factories/include/LoggerFactories.h @@ -46,6 +46,7 @@ class MathLoggerFactory { auto math_logger_file = std::make_shared(method, math_logs_file_path); math_Logger_driver.add_logger(math_logger_file); + } if (console_log) { auto math_Logger_ostream = std::make_shared(method); From 53adca3451e9bb6aa5abdc233a5555abc207adf6 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Fri, 24 Nov 2023 18:11:30 +0100 Subject: [PATCH 15/68] fix --- .../benders/benders_core/include/BendersMathLogger.h | 12 ++++-------- src/cpp/benders/logger/MathLogger.cpp | 1 + 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/cpp/benders/benders_core/include/BendersMathLogger.h b/src/cpp/benders/benders_core/include/BendersMathLogger.h index 62c52233c..ee312a28e 100644 --- a/src/cpp/benders/benders_core/include/BendersMathLogger.h +++ b/src/cpp/benders/benders_core/include/BendersMathLogger.h @@ -54,10 +54,8 @@ std::ostream& LogDestination::operator<<(const T& obj) { } struct MathLogger { - explicit MathLogger(std::ostream* stream) : log_destination(stream) { - setHeadersList(); - } - explicit MathLogger() : log_destination(&std::cout) { setHeadersList(); } + explicit MathLogger(std::ostream* stream) : log_destination(stream) {} + explicit MathLogger() : log_destination(&std::cout) {} void write_header(); virtual void Print(const CurrentIterationData& data) = 0; virtual void setHeadersList() = 0; @@ -98,10 +96,8 @@ class MathLoggerImplementation : public MathLogger { // else } } - virtual void Print(const CurrentIterationData& data) { - implementation_->Print(data); - } - virtual void setHeadersList() { implementation_->setHeadersList(); } + void Print(const CurrentIterationData& data) { implementation_->Print(data); } + void setHeadersList() override { implementation_->setHeadersList(); } private: std::shared_ptr implementation_; diff --git a/src/cpp/benders/logger/MathLogger.cpp b/src/cpp/benders/logger/MathLogger.cpp index 8fbfe387f..91a6652fa 100644 --- a/src/cpp/benders/logger/MathLogger.cpp +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -19,6 +19,7 @@ void MathLoggerBase::setHeadersList() { } void MathLogger::write_header() { + setHeadersList(); for (const auto& header : headers) { log_destination << header; } From 7307b1368eb91cc67185241afe55a9db91fe36c8 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Fri, 24 Nov 2023 18:44:34 +0100 Subject: [PATCH 16/68] ok --- .../benders_core/include/BendersMathLogger.h | 16 +++++++-- src/cpp/benders/logger/MathLogger.cpp | 34 +++++++------------ 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/cpp/benders/benders_core/include/BendersMathLogger.h b/src/cpp/benders/benders_core/include/BendersMathLogger.h index ee312a28e..66097afd8 100644 --- a/src/cpp/benders/benders_core/include/BendersMathLogger.h +++ b/src/cpp/benders/benders_core/include/BendersMathLogger.h @@ -58,21 +58,28 @@ struct MathLogger { explicit MathLogger() : log_destination(&std::cout) {} void write_header(); virtual void Print(const CurrentIterationData& data) = 0; + virtual std::list Headers() const { return headers_; } + LogDestination log_destination; virtual void setHeadersList() = 0; - LogDestination log_destination; - std::list headers; + protected: + void setHeadersList(const std::list& headers); + + private: + std::list headers_; }; struct MathLoggerBase : public MathLogger { using MathLogger::MathLogger; void Print(const CurrentIterationData& data) override; + void setHeadersList() override; }; struct MathLoggerBendersByBatch : public MathLogger { using MathLogger::MathLogger; void Print(const CurrentIterationData& data) override; + void setHeadersList() override; }; @@ -97,7 +104,12 @@ class MathLoggerImplementation : public MathLogger { } void Print(const CurrentIterationData& data) { implementation_->Print(data); } + + protected: void setHeadersList() override { implementation_->setHeadersList(); } + std::list Headers() const override { + return implementation_->Headers(); + } private: std::shared_ptr implementation_; diff --git a/src/cpp/benders/logger/MathLogger.cpp b/src/cpp/benders/logger/MathLogger.cpp index 91a6652fa..7ab2685ac 100644 --- a/src/cpp/benders/logger/MathLogger.cpp +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -4,23 +4,19 @@ #include void MathLoggerBase::setHeadersList() { - headers.clear(); - headers = {ITE, - LB, - UB, - BESTUB, - ABSOLUTE_GAP, - RELATIVE_GAP, - MINSIMPLEX, - MAXSIMPLEX, - TIMEMASTER, - SUB_PROBLEMS_TIME_CPU, - SUB_PROBLEMS_TIME_WALL}; + MathLogger::setHeadersList({ITE, LB, UB, BESTUB, ABSOLUTE_GAP, RELATIVE_GAP, + MINSIMPLEX, MAXSIMPLEX, TIMEMASTER, + SUB_PROBLEMS_TIME_CPU, SUB_PROBLEMS_TIME_WALL}); +} + +void MathLogger::setHeadersList(const std::list& headers) { + headers_.clear(); + headers_ = headers; } void MathLogger::write_header() { setHeadersList(); - for (const auto& header : headers) { + for (const auto& header : Headers()) { log_destination << header; } log_destination << std::endl; @@ -62,15 +58,9 @@ void MathLoggerBase::Print(const CurrentIterationData& data) { } void MathLoggerBendersByBatch::setHeadersList() { - headers.clear(); - headers = {ITE, - LB, - MINSIMPLEX, - MAXSIMPLEX, - TIMEMASTER, - SUB_PROBLEMS_TIME_CPU, - SUB_PROBLEMS_TIME_WALL, - TIME_NOT_DOING_MASTER_OR_SUB_PROBLEMS_WALL}; + MathLogger::setHeadersList({ITE, LB, MINSIMPLEX, MAXSIMPLEX, TIMEMASTER, + SUB_PROBLEMS_TIME_CPU, SUB_PROBLEMS_TIME_WALL, + TIME_NOT_DOING_MASTER_OR_SUB_PROBLEMS_WALL}); } void MathLoggerBendersByBatch::Print(const CurrentIterationData& data) { From 707b6e504f516ea7e5fab170b7cdd174252243d8 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Fri, 24 Nov 2023 18:57:53 +0100 Subject: [PATCH 17/68] set log destination --- .../benders_core/include/BendersMathLogger.h | 10 ++- src/cpp/benders/logger/MathLogger.cpp | 78 ++++++++++--------- 2 files changed, 47 insertions(+), 41 deletions(-) diff --git a/src/cpp/benders/benders_core/include/BendersMathLogger.h b/src/cpp/benders/benders_core/include/BendersMathLogger.h index 66097afd8..2674d90da 100644 --- a/src/cpp/benders/benders_core/include/BendersMathLogger.h +++ b/src/cpp/benders/benders_core/include/BendersMathLogger.h @@ -54,12 +54,12 @@ std::ostream& LogDestination::operator<<(const T& obj) { } struct MathLogger { - explicit MathLogger(std::ostream* stream) : log_destination(stream) {} - explicit MathLogger() : log_destination(&std::cout) {} + explicit MathLogger(std::ostream* stream) : log_destination_(stream) {} + explicit MathLogger() : log_destination_(&std::cout) {} void write_header(); virtual void Print(const CurrentIterationData& data) = 0; virtual std::list Headers() const { return headers_; } - LogDestination log_destination; + virtual LogDestination& TheLogDestination() { return log_destination_; } virtual void setHeadersList() = 0; protected: @@ -67,6 +67,7 @@ struct MathLogger { private: std::list headers_; + LogDestination log_destination_; }; struct MathLoggerBase : public MathLogger { @@ -110,6 +111,9 @@ class MathLoggerImplementation : public MathLogger { std::list Headers() const override { return implementation_->Headers(); } + virtual LogDestination& TheLogDestination() { + return implementation_->TheLogDestination(); + } private: std::shared_ptr implementation_; diff --git a/src/cpp/benders/logger/MathLogger.cpp b/src/cpp/benders/logger/MathLogger.cpp index 7ab2685ac..4fb2f2001 100644 --- a/src/cpp/benders/logger/MathLogger.cpp +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -17,44 +17,45 @@ void MathLogger::setHeadersList(const std::list& headers) { void MathLogger::write_header() { setHeadersList(); for (const auto& header : Headers()) { - log_destination << header; + TheLogDestination() << header; } - log_destination << std::endl; + TheLogDestination() << std::endl; } void MathLoggerBase::Print(const CurrentIterationData& data) { - log_destination << Indent(10) << data.it; + TheLogDestination() << Indent(10) << data.it; if (data.lb == -1e20) - log_destination << Indent(20) << "-INF"; + TheLogDestination() << Indent(20) << "-INF"; else - log_destination << Indent(20) << std::scientific << std::setprecision(10) - << data.lb; + TheLogDestination() << Indent(20) << std::scientific + << std::setprecision(10) << data.lb; if (data.ub == +1e20) - log_destination << Indent(20) << "+INF"; + TheLogDestination() << Indent(20) << "+INF"; else - log_destination << Indent(20) << std::scientific << std::setprecision(10) - << data.ub; + TheLogDestination() << Indent(20) << std::scientific + << std::setprecision(10) << data.ub; if (data.best_ub == +1e20) - log_destination << Indent(20) << "+INF"; + TheLogDestination() << Indent(20) << "+INF"; else - log_destination << Indent(20) << std::scientific << std::setprecision(10) - << data.best_ub; - log_destination << Indent(15) << std::scientific << std::setprecision(2) - << data.best_ub - data.lb; + TheLogDestination() << Indent(20) << std::scientific + << std::setprecision(10) << data.best_ub; + TheLogDestination() << Indent(15) << std::scientific << std::setprecision(2) + << data.best_ub - data.lb; - log_destination << Indent(15) << std::scientific << std::setprecision(2) - << (data.best_ub - data.lb) / data.best_ub; + TheLogDestination() << Indent(15) << std::scientific << std::setprecision(2) + << (data.best_ub - data.lb) / data.best_ub; - log_destination << Indent(15) << data.min_simplexiter; - log_destination << Indent(15) << data.max_simplexiter; + TheLogDestination() << Indent(15) << data.min_simplexiter; + TheLogDestination() << Indent(15) << data.max_simplexiter; - // log_destination << Indent(15) << data.deletedcut; - log_destination << Indent(15) << std::setprecision(2) << data.timer_master; - log_destination << Indent(15) << std::setprecision(2) - << data.subproblems_cputime; - log_destination << Indent(15) << std::setprecision(2) - << data.subproblems_walltime; + // TheLogDestination() << Indent(15) << data.deletedcut; + TheLogDestination() << Indent(15) << std::setprecision(2) + << data.timer_master; + TheLogDestination() << Indent(15) << std::setprecision(2) + << data.subproblems_cputime; + TheLogDestination() << Indent(15) << std::setprecision(2) + << data.subproblems_walltime; - log_destination << std::endl; + TheLogDestination() << std::endl; } void MathLoggerBendersByBatch::setHeadersList() { @@ -64,24 +65,25 @@ void MathLoggerBendersByBatch::setHeadersList() { } void MathLoggerBendersByBatch::Print(const CurrentIterationData& data) { - log_destination << Indent(10) << data.it; + TheLogDestination() << Indent(10) << data.it; if (data.lb == -1e20) - log_destination << Indent(20) << "-INF"; + TheLogDestination() << Indent(20) << "-INF"; else - log_destination << Indent(20) << std::scientific << std::setprecision(10) - << data.lb; + TheLogDestination() << Indent(20) << std::scientific + << std::setprecision(10) << data.lb; - log_destination << Indent(15) << data.min_simplexiter; - log_destination << Indent(15) << data.max_simplexiter; + TheLogDestination() << Indent(15) << data.min_simplexiter; + TheLogDestination() << Indent(15) << data.max_simplexiter; - // log_destination << Indent(15) << data.deletedcut; - log_destination << Indent(15) << std::setprecision(2) << data.timer_master; - log_destination << Indent(15) << std::setprecision(2) - << data.subproblems_cumulative_cputime; - log_destination << Indent(15) << std::setprecision(2) - << data.subproblems_walltime; + // TheLogDestination() << Indent(15) << data.deletedcut; + TheLogDestination() << Indent(15) << std::setprecision(2) + << data.timer_master; + TheLogDestination() << Indent(15) << std::setprecision(2) + << data.subproblems_cumulative_cputime; + TheLogDestination() << Indent(15) << std::setprecision(2) + << data.subproblems_walltime; - log_destination << std::endl; + TheLogDestination() << std::endl; } MathLoggerFile::MathLoggerFile(const BENDERSMETHOD& method, From 52984bd43e24a5c8f3d472da48b134ea7a544938 Mon Sep 17 00:00:00 2001 From: abdoulbari zakir <32519851+a-zakir@users.noreply.github.com> Date: Tue, 12 Dec 2023 16:06:28 +0100 Subject: [PATCH 18/68] merge fork change (#729) * @tbittar remarks * split behaviour and data holders * rename data header * add "other time" * align columns * align headers and data * fix master time * add CUMULATIVE nb of sub-pblm * add num of subproblem solved / iteration * add "expert" option for logs * update * to fit terminal width * add expert_logs option in python code * update doc * remove unused function --- .../get-started/settings-definition.md | 1 + .../benders_by_batch/BendersByBatch.cpp | 18 +-- src/cpp/benders/benders_core/BendersBase.cpp | 14 +- .../benders_core/BendersMathLogger.cpp | 62 ++++++++ .../benders_core/SimulationOptions.cpp | 1 + .../benders_core/include/BendersBase.h | 4 +- .../benders_core/include/BendersMathLogger.h | 112 +++++++++------ .../include/BendersStructsDatas.h | 3 +- .../include/SimulationOptions.hxx | 5 +- src/cpp/benders/benders_core/include/common.h | 1 + src/cpp/benders/benders_mpi/BendersMPI.cpp | 28 ++-- .../benders/benders_mpi/include/BendersMPI.h | 2 +- src/cpp/benders/factories/BendersFactory.cpp | 24 ++-- .../factories/include/LoggerFactories.h | 22 +-- src/cpp/benders/logger/Master.cpp | 4 +- src/cpp/benders/logger/MathLogger.cpp | 132 +++++++++--------- src/cpp/benders/logger/User.cpp | 2 +- src/cpp/benders/logger/UserFile.cpp | 2 +- .../benders/logger/include/logger/Master.h | 2 +- .../logger/include/logger/MathLogger.h | 9 +- src/cpp/benders/logger/include/logger/User.h | 2 +- .../benders/logger/include/logger/UserFile.h | 2 +- src/cpp/xpansion_interfaces/ILogger.h | 21 ++- src/python/antares_xpansion/config_loader.py | 17 ++- src/python/antares_xpansion/input_checker.py | 31 ++++ .../antares_xpansion/optimisation_keys.py | 4 + src/python/antares_xpansion/xpansionConfig.py | 5 + tests/cpp/TestDoubles/LoggerStub.h | 2 +- tests/cpp/logger/logger_test.cpp | 6 +- 29 files changed, 357 insertions(+), 181 deletions(-) diff --git a/docs/user-guide/get-started/settings-definition.md b/docs/user-guide/get-started/settings-definition.md index 1f71e190c..f7448926c 100644 --- a/docs/user-guide/get-started/settings-definition.md +++ b/docs/user-guide/get-started/settings-definition.md @@ -21,6 +21,7 @@ The following section lists the configurable parameters. If the user does not sp |[`separation_parameter`](#separation_parameter) | `0.5` | Step size for the in-out separation | |[`relaxed_optimality_gap`](#relaxed_optimality_gap) | `1e-5` | Threshold to switch from relaxed to integer master | |[`batch_size`](#batch_size) | `0` | Number of subproblems per batch | +|[`expert_logs`](#expert_logs) | `false` | display pure optimization data (/!\ the corresponding option(`EXPERT_LOGS`) for benders is `true` by default ) | The format is a standard `.ini` and should follow this template: ```ini diff --git a/src/cpp/benders/benders_by_batch/BendersByBatch.cpp b/src/cpp/benders/benders_by_batch/BendersByBatch.cpp index 64690e037..0e8aba252 100644 --- a/src/cpp/benders/benders_by_batch/BendersByBatch.cpp +++ b/src/cpp/benders/benders_by_batch/BendersByBatch.cpp @@ -76,8 +76,8 @@ void BendersByBatch::MasterLoop() { random_batch_permutation_.resize(number_of_batch_); batch_counter_ = 0; current_batch_id_ = 0; - - _data.number_of_subproblem_resolved = 0; + _data.number_of_subproblem_solved = 0; + _data.cumulative_number_of_subproblem_solved = 0; cumulative_subproblems_timer_per_iter_ = 0; first_unsolved_batch_ = 0; while (!_data.stop) { @@ -113,22 +113,22 @@ void BendersByBatch::MasterLoop() { random_batch_permutation_.size(), rank_0); SeparationLoop(); if (Rank() == rank_0) { - _data.elapsed_time = GetBendersTime(); _data.stop = ShouldBendersStop(); } BroadCast(_data.stop, rank_0); BroadCast(batch_counter_, rank_0); SetSubproblemsCumulativeCpuTime(cumulative_subproblems_timer_per_iter_); - _logger->cumulative_number_of_sub_problem_resolved( - _data.number_of_subproblem_resolved + - GetNumOfSubProblemsResolvedBeforeResume()); + _logger->cumulative_number_of_sub_problem_solved( + _data.cumulative_number_of_subproblem_solved + + GetNumOfSubProblemsSolvedBeforeResume()); _logger->LogSubproblemsSolvingCumulativeCpuTime( GetSubproblemsCumulativeCpuTime()); _logger->LogSubproblemsSolvingWalltime(GetSubproblemsWalltime()); - mathLoggerDriver_->Print(_data); _logger->display_message( "\\________________________________________________________________" "________"); + _data.elapsed_time = GetBendersTime(); + mathLoggerDriver_->Print(_data); } } void BendersByBatch::SeparationLoop() { @@ -144,6 +144,7 @@ void BendersByBatch::SeparationLoop() { _logger->log_iteration_candidates(bendersDataToLogData(_data)); BroadcastXCut(); UpdateRemainingEpsilon(); + _data.number_of_subproblem_solved = 0; SolveBatches(); if (Rank() == rank_0) { @@ -199,7 +200,8 @@ void BendersByBatch::SolveBatches() { Reduce(GetSubproblemsCpuTime(), cumulative_subproblems_timer_per_iter_, std::plus(), rank_0); if (Rank() == rank_0) { - _data.number_of_subproblem_resolved += batch_sub_problems.size(); + _data.number_of_subproblem_solved += batch_sub_problems.size(); + _data.cumulative_number_of_subproblem_solved += batch_sub_problems.size(); remaining_epsilon_ -= batch_subproblems_costs_contribution_in_gap; } diff --git a/src/cpp/benders/benders_core/BendersBase.cpp b/src/cpp/benders/benders_core/BendersBase.cpp index 675ae4f98..ad461521b 100644 --- a/src/cpp/benders/benders_core/BendersBase.cpp +++ b/src/cpp/benders/benders_core/BendersBase.cpp @@ -36,7 +36,7 @@ void BendersBase::init_data() { _data.best_it = 0; _data.stopping_criterion = StoppingCriterion::empty; _data.is_in_initial_relaxation = false; - _data.number_of_subproblem_resolved = 0; + _data.cumulative_number_of_subproblem_solved = 0; } void BendersBase::OpenCsvFile() { @@ -469,7 +469,7 @@ LogData BendersBase::FinalLogData() const { result.subproblem_cost = best_iteration_data.subproblem_cost; result.invest_cost = best_iteration_data.invest_cost; result.cumulative_number_of_subproblem_resolved = - _data.number_of_subproblem_resolved + + _data.cumulative_number_of_subproblem_solved + cumulative_number_of_subproblem_resolved_before_resume; return result; @@ -527,7 +527,7 @@ Output::Iteration BendersBase::iteration( masterDataPtr_l->_invest_cost + masterDataPtr_l->_operational_cost; iteration.candidates = candidates_data(masterDataPtr_l); iteration.cumulative_number_of_subproblem_resolved = - _data.number_of_subproblem_resolved + + _data.cumulative_number_of_subproblem_solved + cumulative_number_of_subproblem_resolved_before_resume; return iteration; } @@ -653,7 +653,7 @@ LogData BendersBase::bendersDataToLogData( data.elapsed_time, data.timer_master, data.subproblems_walltime, - data.number_of_subproblem_resolved + + data.cumulative_number_of_subproblem_solved + cumulative_number_of_subproblem_resolved_before_resume}; } void BendersBase::set_log_file(const std::filesystem::path &log_file) { @@ -813,7 +813,7 @@ LogData BendersBase::GetBestIterationData() const { } void BendersBase::ChecksResumeMode() { - benders_timer = Timer(); + ITE_TIMEr = Timer(); if (IsResumeMode()) { auto reader = LastIterationReader(LastIterationFile()); LogData last_iter; @@ -829,7 +829,7 @@ void BendersBase::ChecksResumeMode() { LastIterationPrinter(_logger, best_iteration_data, last_iter); restart_data_printer.Print(); UpdateMaxNumberIterationResumeMode(last_iter.it); - benders_timer = Timer(last_iter.benders_elapsed_time); + ITE_TIMEr = Timer(last_iter.benders_elapsed_time); _data.stop = ShouldBendersStop(); iterations_before_resume = last_iter.it; cumulative_number_of_subproblem_resolved_before_resume = @@ -856,7 +856,7 @@ void BendersBase::EndWritingInOutputFile() const { _writer->write_duration(_data.elapsed_time); SaveSolutionInOutputFile(); } -double BendersBase::GetBendersTime() const { return benders_timer.elapsed(); } +double BendersBase::GetBendersTime() const { return ITE_TIMEr.elapsed(); } void BendersBase::write_basis() const { const auto filename(std::filesystem::path(_options.OUTPUTROOT) / (_options.LAST_MASTER_BASIS)); diff --git a/src/cpp/benders/benders_core/BendersMathLogger.cpp b/src/cpp/benders/benders_core/BendersMathLogger.cpp index a39d04962..bca9d460e 100644 --- a/src/cpp/benders/benders_core/BendersMathLogger.cpp +++ b/src/cpp/benders/benders_core/BendersMathLogger.cpp @@ -1,5 +1,61 @@ #include "BendersMathLogger.h" +// #ifdef _WIN32 +// #include +// #endif + +HeadersManager::HeadersManager(HEADERSTYPE type, const BENDERSMETHOD& method) { + if (type == HEADERSTYPE::SHORT) { + headers_list.push_back("ITE"); + headers_list.push_back("LB"); + if (method == BENDERSMETHOD::BENDERS) { + headers_list.push_back("UB"); + headers_list.push_back("BESTUB"); + headers_list.push_back("AGAP"); + headers_list.push_back("RGAP"); + } + headers_list.push_back("MinSpx"); + headers_list.push_back("MaxSpx"); + if (method == BENDERSMETHOD::BENDERSBYBATCH) { + headers_list.push_back("NbSubPbSolv"); + } + headers_list.push_back("IteTime"); + headers_list.push_back("MasterTime"); + // headers_list.push_back("SubPbCpuTime"); + headers_list.push_back("SPWallTime"); + } else { + headers_list.push_back("ITERATION"); + headers_list.push_back("LB"); + if (method == BENDERSMETHOD::BENDERS) { + headers_list.push_back("UB"); + headers_list.push_back("BESTUB"); + headers_list.push_back("ABSOLUTE GAP"); + headers_list.push_back("RELATIVE GAP"); + } + headers_list.push_back("MINSIMPLEX"); + headers_list.push_back("MAXSIMPLEX"); + headers_list.push_back("NUMBER OF SUBPROBLEMS SOLVED"); + headers_list.push_back("CUMULATIVE NUMBER OF SUBPROBLEMS SOLVED "); + headers_list.push_back("ITERATION TIME"); + headers_list.push_back("MASTER TIME"); + headers_list.push_back("SUB-PROBLEMS TIME (CPU)"); + headers_list.push_back("SUB-PROBLEMS TIME (WALL)"); + headers_list.push_back("TIME NOT DOING MASTER OR SUB-PROBLEMS (WALL)"); + } +} + +LogDestination::LogDestination(std::ostream* stream, std::streamsize width) + : stream_(stream), width_(width) { + // _COORD coordinates; + // coordinates.X = 1000; + // coordinates.Y = 1000; + + // if (0 == SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), + // coordinates)) { + // std::cout << "could not resize the console screen\n"; + // // return -1; + // } +} void MathLoggerDriver::add_logger( std::shared_ptr logger) { if (logger) { @@ -18,3 +74,9 @@ void MathLoggerDriver::write_header() { logger->write_header(); } } + +void MathLoggerDriver::display_message(const std::string& str) { + for (auto logger : math_loggers_) { + logger->display_message(str); + } +} diff --git a/src/cpp/benders/benders_core/SimulationOptions.cpp b/src/cpp/benders/benders_core/SimulationOptions.cpp index d88d0cc9a..d59440231 100644 --- a/src/cpp/benders/benders_core/SimulationOptions.cpp +++ b/src/cpp/benders/benders_core/SimulationOptions.cpp @@ -167,6 +167,7 @@ BendersBaseOptions SimulationOptions::get_benders_options() const { result.LAST_MASTER_MPS = LAST_MASTER_MPS; result.LAST_MASTER_BASIS = LAST_MASTER_BASIS; result.BATCH_SIZE = BATCH_SIZE; + result.EXPERT_LOGS = EXPERT_LOGS; return result; } \ No newline at end of file diff --git a/src/cpp/benders/benders_core/include/BendersBase.h b/src/cpp/benders/benders_core/include/BendersBase.h index 59855fdfb..c50866fb6 100644 --- a/src/cpp/benders/benders_core/include/BendersBase.h +++ b/src/cpp/benders/benders_core/include/BendersBase.h @@ -135,7 +135,7 @@ class BendersBase { BendersBaseOptions Options() const { return _options; } virtual void UpdateStoppingCriterion(); virtual bool ShouldRelaxationStop() const; - int GetNumOfSubProblemsResolvedBeforeResume() { + int GetNumOfSubProblemsSolvedBeforeResume() { return cumulative_number_of_subproblem_resolved_before_resume; } @@ -177,7 +177,7 @@ class BendersBase { LogData best_iteration_data; int iterations_before_resume = 0; int cumulative_number_of_subproblem_resolved_before_resume = 0; - Timer benders_timer; + Timer ITE_TIMEr; public: Logger _logger; diff --git a/src/cpp/benders/benders_core/include/BendersMathLogger.h b/src/cpp/benders/benders_core/include/BendersMathLogger.h index 2674d90da..4844b7580 100644 --- a/src/cpp/benders/benders_core/include/BendersMathLogger.h +++ b/src/cpp/benders/benders_core/include/BendersMathLogger.h @@ -6,28 +6,23 @@ #include #include "BendersStructsDatas.h" +#include "ILogger.h" #include "common.h" const std::string MATHLOGGERCONTEXT = "Benders"; -inline std::string Indent(int size) { return std::string(size, ' '); } -const std::string ITE = Indent(10) + "ITE"; -const std::string LB = Indent(20) + "LB"; -const std::string UB = Indent(20) + "UB"; -const std::string BESTUB = Indent(20) + "BESTUB"; -const std::string ABSOLUTE_GAP = Indent(15) + "ABSOLUTE GAP"; -const std::string RELATIVE_GAP = Indent(15) + "RELATIVE GAP"; -const std::string MINSIMPLEX = Indent(15) + "MINSIMPLEX"; -const std::string MAXSIMPLEX = Indent(15) + "MAXSIMPLEX"; -const std::string TIMEMASTER = Indent(15) + "TIMEMASTER"; -const std::string SUB_PROBLEMS_TIME_CPU = - Indent(15) + "SUB-PROBLEMS TIME (CPU)"; -const std::string SUB_PROBLEMS_TIME_WALL = - Indent(15) + "SUB-PROBLEMS TIME (WALL)"; -const std::string TIME_NOT_DOING_MASTER_OR_SUB_PROBLEMS_WALL = - Indent(15) + "TIME NOT DOING MASTER OR SUB-PROBLEMS (WALL)"; +struct Header { + std::string label; + int pos; +}; + +enum class HEADERSTYPE { SHORT, LONG }; +struct HeadersManager { + explicit HeadersManager(HEADERSTYPE type, const BENDERSMETHOD& method); + std::vector headers_list; +}; class LogDestination { public: - explicit LogDestination(std::ostream* stream) : stream_(stream) {} + explicit LogDestination(std::ostream* stream, std::streamsize width = 40); // for std::endl std::ostream& operator<<(std::ostream& (*function)(std::ostream&)) { @@ -35,39 +30,60 @@ class LogDestination { return function(*stream_); } - // // for std::endl - // std::ostream& operator<<(const std::_Smanip& smanip) { - // // write obj to stream - // return (*stream_) << smanip; - // } - template std::ostream& operator<<(const T& obj); private: std::ostream* stream_; + std::streamsize width_ = 40; }; template std::ostream& LogDestination::operator<<(const T& obj) { // write obj to stream - return (*stream_) << obj; + return (*stream_) << std::left << std::setw(width_) << obj; } -struct MathLogger { - explicit MathLogger(std::ostream* stream) : log_destination_(stream) {} - explicit MathLogger() : log_destination_(&std::cout) {} - void write_header(); +struct MathLoggerBehaviour : public ILoggerBenders { + void write_header() { + setHeadersList(); + for (const auto& header : Headers()) { + LogsDestination() << header; + } + LogsDestination() << std::endl; + } + + virtual void display_message(const std::string& str) { + LogsDestination() << str; + } + virtual void Print(const CurrentIterationData& data) = 0; - virtual std::list Headers() const { return headers_; } - virtual LogDestination& TheLogDestination() { return log_destination_; } + virtual std::vector Headers() const = 0; + virtual LogDestination& LogsDestination() = 0; virtual void setHeadersList() = 0; +}; + +struct MathLogger : public MathLoggerBehaviour { + explicit MathLogger(std::ostream* stream, std::streamsize width = 40, + HEADERSTYPE type = HEADERSTYPE::LONG) + : log_destination_(stream, width), type_(type) {} + + explicit MathLogger(std::streamsize width = 40, + HEADERSTYPE type = HEADERSTYPE::LONG) + : log_destination_(&std::cout, width), type_(type) {} + + virtual void Print(const CurrentIterationData& data) = 0; + std::vector Headers() const override { return headers_; } + virtual LogDestination& LogsDestination() { return log_destination_; } + virtual void setHeadersList() = 0; + HEADERSTYPE HeadersType() const { return type_; } protected: - void setHeadersList(const std::list& headers); + void setHeadersList(const std::vector& headers); private: - std::list headers_; + std::vector headers_; LogDestination log_destination_; + HEADERSTYPE type_; }; struct MathLoggerBase : public MathLogger { @@ -80,26 +96,30 @@ struct MathLoggerBase : public MathLogger { struct MathLoggerBendersByBatch : public MathLogger { using MathLogger::MathLogger; void Print(const CurrentIterationData& data) override; - void setHeadersList() override; }; -class MathLoggerImplementation : public MathLogger { +class MathLoggerImplementation : public MathLoggerBehaviour { public: explicit MathLoggerImplementation(const BENDERSMETHOD& method, - std::ostream* stream) { + std::ostream* stream, + std::streamsize width = 40, + HEADERSTYPE type = HEADERSTYPE::LONG) { if (method == BENDERSMETHOD::BENDERS) { - implementation_ = std::make_shared(stream); + implementation_ = std::make_shared(stream, width, type); } else if (method == BENDERSMETHOD::BENDERSBYBATCH) { - implementation_ = std::make_shared(stream); + implementation_ = + std::make_shared(stream, width, type); } // else } - explicit MathLoggerImplementation(const BENDERSMETHOD& method) { + explicit MathLoggerImplementation(const BENDERSMETHOD& method, + std::streamsize width = 40, + HEADERSTYPE type = HEADERSTYPE::LONG) { if (method == BENDERSMETHOD::BENDERS) { - implementation_ = std::make_shared(); + implementation_ = std::make_shared(width, type); } else if (method == BENDERSMETHOD::BENDERSBYBATCH) { - implementation_ = std::make_shared(); + implementation_ = std::make_shared(width, type); } // else } } @@ -108,25 +128,25 @@ class MathLoggerImplementation : public MathLogger { protected: void setHeadersList() override { implementation_->setHeadersList(); } - std::list Headers() const override { + std::vector Headers() const override { return implementation_->Headers(); } - virtual LogDestination& TheLogDestination() { - return implementation_->TheLogDestination(); + virtual LogDestination& LogsDestination() { + return implementation_->LogsDestination(); } private: std::shared_ptr implementation_; }; -class MathLoggerDriver { +class MathLoggerDriver : public ILoggerBenders { public: MathLoggerDriver() = default; void write_header(); - + void display_message(const std::string& str) override; void add_logger(std::shared_ptr logger); void Print(const CurrentIterationData& data); private: - std::list> math_loggers_; + std::vector> math_loggers_; }; diff --git a/src/cpp/benders/benders_core/include/BendersStructsDatas.h b/src/cpp/benders/benders_core/include/BendersStructsDatas.h index 05ec3deae..22d0f6de9 100644 --- a/src/cpp/benders/benders_core/include/BendersStructsDatas.h +++ b/src/cpp/benders/benders_core/include/BendersStructsDatas.h @@ -31,7 +31,8 @@ struct CurrentIterationData { double elapsed_time; StoppingCriterion stopping_criterion; bool is_in_initial_relaxation; - int number_of_subproblem_resolved; + int number_of_subproblem_solved; + int cumulative_number_of_subproblem_solved; int min_simplexiter; int max_simplexiter; }; diff --git a/src/cpp/benders/benders_core/include/SimulationOptions.hxx b/src/cpp/benders/benders_core/include/SimulationOptions.hxx index 3b216a185..e49a90009 100644 --- a/src/cpp/benders/benders_core/include/SimulationOptions.hxx +++ b/src/cpp/benders/benders_core/include/SimulationOptions.hxx @@ -56,7 +56,7 @@ BENDERS_OPTIONS_MACRO(SOLVER_NAME, std::string, "COIN", asString()) // json file in output/expansion/ BENDERS_OPTIONS_MACRO(JSON_FILE, std::string, ".", asString()) -// last iteration json file in output/expansion/ +// last iteration json file in output/expansion/ BENDERS_OPTIONS_MACRO(LAST_ITERATION_JSON_FILE, std::string, ".", asString()) // TIME_LIMIT BENDERS_OPTIONS_MACRO(TIME_LIMIT, double, 1e12, asDouble()) @@ -72,3 +72,6 @@ BENDERS_OPTIONS_MACRO(LAST_MASTER_BASIS, std::string, "master_last_basis", // BATCH SIZE (Benders by batch) BENDERS_OPTIONS_MACRO(BATCH_SIZE, size_t, 0, asUInt()) + +// which logs must be printed in the console +BENDERS_OPTIONS_MACRO(EXPERT_LOGS, bool, true, asBool()) diff --git a/src/cpp/benders/benders_core/include/common.h b/src/cpp/benders/benders_core/include/common.h index 728e25910..51f956eb7 100644 --- a/src/cpp/benders/benders_core/include/common.h +++ b/src/cpp/benders/benders_core/include/common.h @@ -159,6 +159,7 @@ struct BendersBaseOptions : public BaseOptions { std::string LAST_MASTER_BASIS; size_t BATCH_SIZE; + bool EXPERT_LOGS = true; }; void usage(int argc); diff --git a/src/cpp/benders/benders_mpi/BendersMPI.cpp b/src/cpp/benders/benders_mpi/BendersMPI.cpp index a2cd043ad..49ba18e9c 100644 --- a/src/cpp/benders/benders_mpi/BendersMPI.cpp +++ b/src/cpp/benders/benders_mpi/BendersMPI.cpp @@ -121,10 +121,10 @@ void BendersMpi::step_2_solve_subproblems_and_build_cuts() { check_if_some_proc_had_a_failure(success); gather_subproblems_cut_package_and_build_cuts(subproblem_data_map, walltime); if (Rank() == rank_0) { - _data.number_of_subproblem_resolved += _data.nsubproblem; - _logger->cumulative_number_of_sub_problem_resolved( - _data.number_of_subproblem_resolved + - GetNumOfSubProblemsResolvedBeforeResume()); + _data.cumulative_number_of_subproblem_solved += _data.nsubproblem; + _logger->cumulative_number_of_sub_problem_solved( + _data.cumulative_number_of_subproblem_solved + + GetNumOfSubProblemsSolvedBeforeResume()); } } @@ -193,8 +193,7 @@ void BendersMpi::write_exception_message(const std::exception &ex) const { _logger->display_message(error); } -void BendersMpi::step_4_update_best_solution(int rank, - const Timer &timer_master) { +void BendersMpi::step_4_update_best_solution(int rank) { if (rank == rank_0) { compute_ub(); update_best_ub(); @@ -202,8 +201,6 @@ void BendersMpi::step_4_update_best_solution(int rank, UpdateTrace(); - _data.elapsed_time = GetBendersTime(); - set_timer_master(timer_master.elapsed()); _data.stop = ShouldBendersStop(); } } @@ -228,9 +225,8 @@ void BendersMpi::free() { */ void BendersMpi::Run() { PreRunInitialization(); - mathLoggerDriver_->write_header(); + _data.number_of_subproblem_solved = _data.nsubproblem; while (!_data.stop) { - Timer timer_master; ++_data.it; ResetSimplexIterationsBounds(); @@ -244,17 +240,16 @@ void BendersMpi::Run() { } if (!_exceptionRaised) { - step_4_update_best_solution(_world.rank(), timer_master); + step_4_update_best_solution(_world.rank()); } _data.stop |= _exceptionRaised; - if (Rank() == rank_0) { - mathLoggerDriver_->Print(_data); - } - broadcast(_world, _data.is_in_initial_relaxation, rank_0); broadcast(_world, _data.stop, rank_0); - if (_world.rank() == rank_0) { + + _data.elapsed_time = GetBendersTime(); + mathLoggerDriver_->Print(_data); + if (Rank() == rank_0) { SaveCurrentBendersData(); } } @@ -286,6 +281,7 @@ void BendersMpi::PreRunInitialization() { } mathLoggerDriver_->write_header(); } + void BendersMpi::launch() { build_input_map(); _world.barrier(); diff --git a/src/cpp/benders/benders_mpi/include/BendersMPI.h b/src/cpp/benders/benders_mpi/include/BendersMPI.h index 0a31a8d82..fd3bedaa5 100644 --- a/src/cpp/benders/benders_mpi/include/BendersMPI.h +++ b/src/cpp/benders/benders_mpi/include/BendersMPI.h @@ -35,7 +35,7 @@ class BendersMpi : public BendersBase { private: void step_1_solve_master(); void step_2_solve_subproblems_and_build_cuts(); - void step_4_update_best_solution(int rank, const Timer &timer_master); + void step_4_update_best_solution(int rank); void master_build_cuts( std::vector gathered_subproblem_map); diff --git a/src/cpp/benders/factories/BendersFactory.cpp b/src/cpp/benders/factories/BendersFactory.cpp index 2266be0b4..f42379567 100644 --- a/src/cpp/benders/factories/BendersFactory.cpp +++ b/src/cpp/benders/factories/BendersFactory.cpp @@ -16,6 +16,7 @@ int RunBenders(char** argv, const std::filesystem::path& options_file, mpi::environment& env, mpi::communicator& world, const BENDERSMETHOD& method) { // Read options, needed to have options.OUTPUTROOT + BendersLoggerBase benders_loggers; Logger logger; std::shared_ptr math_log_driver; @@ -36,13 +37,15 @@ int RunBenders(char** argv, const std::filesystem::path& options_file, std::filesystem::path(options.OUTPUTROOT) / "reportbenders.txt"; auto math_logs_file = - std::filesystem::path(options.OUTPUTROOT) / "math_log.txt"; + std::filesystem::path(options.OUTPUTROOT) / "benders_solver.log"; Writer writer; if (world.rank() == 0) { - auto logger_factory = FileAndStdoutLoggerFactory(log_reports_name); - auto math_log_factory = MathLoggerFactory(method, false, math_logs_file); + auto logger_factory = FileAndStdoutLoggerFactory( + log_reports_name, benders_options.EXPERT_LOGS); + auto math_log_factory = MathLoggerFactory( + method, benders_options.EXPERT_LOGS, math_logs_file); logger = logger_factory.get_logger(); math_log_driver = math_log_factory.get_logger(); @@ -56,7 +59,8 @@ int RunBenders(char** argv, const std::filesystem::path& options_file, auto math_log_factory = MathLoggerFactory(); math_log_driver = math_log_factory.get_logger(); } - + benders_loggers.AddLogger(logger); + benders_loggers.AddLogger(math_log_driver); pBendersBase benders; if (method == BENDERSMETHOD::BENDERS) { benders = std::make_shared(benders_options, logger, writer, @@ -66,13 +70,13 @@ int RunBenders(char** argv, const std::filesystem::path& options_file, benders_options, logger, writer, env, world, math_log_driver); } else { auto err_msg = "Error only benders or benders-by-batch allowed!"; - logger->display_message(err_msg); + benders_loggers.display_message(err_msg); std::exit(1); } std::ostringstream oss_l = start_message(options, benders->BendersName()); oss_l << std::endl; - logger->display_message(oss_l.str()); - + benders_loggers.display_message(oss_l.str()); + benders->set_log_file(log_reports_name); writer->write_log_level(options.LOG_LEVEL); @@ -81,18 +85,18 @@ int RunBenders(char** argv, const std::filesystem::path& options_file, benders->launch(); std::stringstream str; str << "Optimization results available in : " << options.JSON_FILE; - logger->display_message(str.str()); + benders_loggers.display_message(str.str()); logger->log_total_duration(benders->execution_time()); } catch (std::exception& e) { std::ostringstream msg; msg << "error: " << e.what() << std::endl; - logger->display_message(msg.str()); + benders_loggers.display_message(msg.str()); mpi::environment::abort(1); return 1; } catch (...) { std::ostringstream msg; msg << "Exception of unknown type!" << std::endl; - logger->display_message(msg.str()); + benders_loggers.display_message(msg.str()); mpi::environment::abort(1); return 1; } diff --git a/src/cpp/benders/factories/include/LoggerFactories.h b/src/cpp/benders/factories/include/LoggerFactories.h index 7cbaa10af..82bb4fe41 100644 --- a/src/cpp/benders/factories/include/LoggerFactories.h +++ b/src/cpp/benders/factories/include/LoggerFactories.h @@ -21,14 +21,18 @@ class FileAndStdoutLoggerFactory { public: explicit FileAndStdoutLoggerFactory( - const std::filesystem::path &report_file_path_string) { + const std::filesystem::path &report_file_path_string, + bool expert_log_at_console) { auto masterLogger = std::make_shared(); auto user_file = std::make_shared(report_file_path_string); - - auto loggerUser = std::make_shared(std::cout); masterLogger->addLogger(user_file); - masterLogger->addLogger(loggerUser); + + if (!expert_log_at_console) { + auto loggerUser = std::make_shared(std::cout); + masterLogger->addLogger(loggerUser); + } + logger = masterLogger; } inline Logger get_logger() const { return logger; } @@ -36,7 +40,7 @@ class FileAndStdoutLoggerFactory { class MathLoggerFactory { private: - MathLoggerDriver math_Logger_driver; + MathLoggerDriver math_logger_driver; public: explicit MathLoggerFactory( @@ -45,18 +49,18 @@ class MathLoggerFactory { if (math_logs_file_path != "") { auto math_logger_file = std::make_shared(method, math_logs_file_path); - math_Logger_driver.add_logger(math_logger_file); + math_logger_driver.add_logger(math_logger_file); } if (console_log) { - auto math_Logger_ostream = std::make_shared(method); + auto math_logger_ostream = std::make_shared(method); - math_Logger_driver.add_logger(math_Logger_ostream); + math_logger_driver.add_logger(math_logger_ostream); } } explicit MathLoggerFactory() = default; std::shared_ptr get_logger() { - return std::make_shared(math_Logger_driver); + return std::make_shared(math_logger_driver); } }; #endif // ANTARESXPANSION_LOGGERFACTORIES_H diff --git a/src/cpp/benders/logger/Master.cpp b/src/cpp/benders/logger/Master.cpp index 410edf8f7..d2e0236b1 100644 --- a/src/cpp/benders/logger/Master.cpp +++ b/src/cpp/benders/logger/Master.cpp @@ -106,9 +106,9 @@ void Master::LogAtSwitchToInteger() { } } -void Master::cumulative_number_of_sub_problem_resolved(int number) { +void Master::cumulative_number_of_sub_problem_solved(int number) { for (auto logger : _loggers) { - logger->cumulative_number_of_sub_problem_resolved(number); + logger->cumulative_number_of_sub_problem_solved(number); } } diff --git a/src/cpp/benders/logger/MathLogger.cpp b/src/cpp/benders/logger/MathLogger.cpp index 4fb2f2001..919529a6c 100644 --- a/src/cpp/benders/logger/MathLogger.cpp +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -3,92 +3,98 @@ #include #include +double getDurationNotDoingMasterOrSubproblems(double interation, double master, + double subproblems) { + return interation - master - subproblems; +} + void MathLoggerBase::setHeadersList() { - MathLogger::setHeadersList({ITE, LB, UB, BESTUB, ABSOLUTE_GAP, RELATIVE_GAP, - MINSIMPLEX, MAXSIMPLEX, TIMEMASTER, - SUB_PROBLEMS_TIME_CPU, SUB_PROBLEMS_TIME_WALL}); + auto type = HeadersType(); + HeadersManager headers_manager(type, BENDERSMETHOD::BENDERS); + MathLogger::setHeadersList(headers_manager.headers_list); } -void MathLogger::setHeadersList(const std::list& headers) { +void MathLogger::setHeadersList(const std::vector& headers) { headers_.clear(); headers_ = headers; } -void MathLogger::write_header() { - setHeadersList(); - for (const auto& header : Headers()) { - TheLogDestination() << header; - } - TheLogDestination() << std::endl; -} void MathLoggerBase::Print(const CurrentIterationData& data) { - TheLogDestination() << Indent(10) << data.it; - if (data.lb == -1e20) - TheLogDestination() << Indent(20) << "-INF"; - else - TheLogDestination() << Indent(20) << std::scientific - << std::setprecision(10) << data.lb; - if (data.ub == +1e20) - TheLogDestination() << Indent(20) << "+INF"; - else - TheLogDestination() << Indent(20) << std::scientific - << std::setprecision(10) << data.ub; - if (data.best_ub == +1e20) - TheLogDestination() << Indent(20) << "+INF"; - else - TheLogDestination() << Indent(20) << std::scientific - << std::setprecision(10) << data.best_ub; - TheLogDestination() << Indent(15) << std::scientific << std::setprecision(2) - << data.best_ub - data.lb; + auto type = HeadersType(); - TheLogDestination() << Indent(15) << std::scientific << std::setprecision(2) - << (data.best_ub - data.lb) / data.best_ub; - - TheLogDestination() << Indent(15) << data.min_simplexiter; - TheLogDestination() << Indent(15) << data.max_simplexiter; - - // TheLogDestination() << Indent(15) << data.deletedcut; - TheLogDestination() << Indent(15) << std::setprecision(2) - << data.timer_master; - TheLogDestination() << Indent(15) << std::setprecision(2) - << data.subproblems_cputime; - TheLogDestination() << Indent(15) << std::setprecision(2) - << data.subproblems_walltime; + LogsDestination() << data.it; + LogsDestination() << std::scientific << std::setprecision(10) << data.lb; + LogsDestination() << std::scientific << std::setprecision(10) << data.ub; + LogsDestination() << std::scientific << std::setprecision(10) << data.best_ub; + LogsDestination() << std::scientific << std::setprecision(2) + << data.best_ub - data.lb; + LogsDestination() << std::scientific << std::setprecision(2) + << (data.best_ub - data.lb) / data.best_ub; + LogsDestination() << data.min_simplexiter; + LogsDestination() << data.max_simplexiter; + if (type == HEADERSTYPE::LONG) { + LogsDestination() << data.number_of_subproblem_solved; + LogsDestination() << data.cumulative_number_of_subproblem_solved; + } - TheLogDestination() << std::endl; + // LogsDestination() << data.deletedcut; + LogsDestination() << std::setprecision(2) << data.elapsed_time; + LogsDestination() << std::setprecision(2) << data.timer_master; + if (type == HEADERSTYPE::LONG) { + LogsDestination() << std::setprecision(2) + << data.subproblems_cumulative_cputime; + } + LogsDestination() << std::setprecision(2) << data.subproblems_walltime; + if (type == HEADERSTYPE::LONG) { + LogsDestination() << std::setprecision(2) + << getDurationNotDoingMasterOrSubproblems( + data.elapsed_time, data.timer_master, + data.subproblems_walltime); + } + LogsDestination() << std::endl; } void MathLoggerBendersByBatch::setHeadersList() { - MathLogger::setHeadersList({ITE, LB, MINSIMPLEX, MAXSIMPLEX, TIMEMASTER, - SUB_PROBLEMS_TIME_CPU, SUB_PROBLEMS_TIME_WALL, - TIME_NOT_DOING_MASTER_OR_SUB_PROBLEMS_WALL}); + auto type = HeadersType(); + HeadersManager headers_manager(type, BENDERSMETHOD::BENDERSBYBATCH); + + MathLogger::setHeadersList(headers_manager.headers_list); } void MathLoggerBendersByBatch::Print(const CurrentIterationData& data) { - TheLogDestination() << Indent(10) << data.it; - if (data.lb == -1e20) - TheLogDestination() << Indent(20) << "-INF"; - else - TheLogDestination() << Indent(20) << std::scientific - << std::setprecision(10) << data.lb; + auto type = HeadersType(); - TheLogDestination() << Indent(15) << data.min_simplexiter; - TheLogDestination() << Indent(15) << data.max_simplexiter; + LogsDestination() << data.it; + LogsDestination() << std::scientific << std::setprecision(10) << data.lb; + LogsDestination() << data.min_simplexiter; + LogsDestination() << data.max_simplexiter; + LogsDestination() << data.number_of_subproblem_solved; - // TheLogDestination() << Indent(15) << data.deletedcut; - TheLogDestination() << Indent(15) << std::setprecision(2) - << data.timer_master; - TheLogDestination() << Indent(15) << std::setprecision(2) + if (type == HEADERSTYPE::LONG) { + LogsDestination() << data.cumulative_number_of_subproblem_solved; + } + LogsDestination() << std::setprecision(2) << data.elapsed_time; + LogsDestination() << std::setprecision(2) << data.timer_master; + if (type == HEADERSTYPE::LONG) { + LogsDestination() << std::setprecision(2) << data.subproblems_cumulative_cputime; - TheLogDestination() << Indent(15) << std::setprecision(2) - << data.subproblems_walltime; + } + LogsDestination() << std::setprecision(2) << data.subproblems_walltime; + if (type == HEADERSTYPE::LONG) { + LogsDestination() << std::setprecision(2) + << getDurationNotDoingMasterOrSubproblems( + data.elapsed_time, data.timer_master, + data.subproblems_walltime); + } - TheLogDestination() << std::endl; + LogsDestination() << std::endl; } MathLoggerFile::MathLoggerFile(const BENDERSMETHOD& method, - const std::filesystem::path& filename) - : MathLoggerImplementation(method, &file_stream_) { + const std::filesystem::path& filename, + std::streamsize width) + : MathLoggerImplementation(method, &file_stream_, width, + HEADERSTYPE::LONG) { // TODO restart case????????????? file_stream_.open(filename, std::ofstream::out); if (file_stream_.fail()) { diff --git a/src/cpp/benders/logger/User.cpp b/src/cpp/benders/logger/User.cpp index 84ce7f4e0..087899a4a 100644 --- a/src/cpp/benders/logger/User.cpp +++ b/src/cpp/benders/logger/User.cpp @@ -121,7 +121,7 @@ void User::LogAtSwitchToInteger() { << "--- Relaxed gap reached, switch master formulation to integer" << std::endl; } -void User::cumulative_number_of_sub_problem_resolved(int number) { +void User::cumulative_number_of_sub_problem_solved(int number) { _stream << PrefixMessage(LogUtils::LOGLEVEL::INFO, CONTEXT) << indent_1 << "cumulative number of subproblem resolutions: " << number << std::endl; diff --git a/src/cpp/benders/logger/UserFile.cpp b/src/cpp/benders/logger/UserFile.cpp index b856f72d7..ba72d62f1 100644 --- a/src/cpp/benders/logger/UserFile.cpp +++ b/src/cpp/benders/logger/UserFile.cpp @@ -144,7 +144,7 @@ void UserFile::LogAtSwitchToInteger() { << std::endl; _file.flush(); } -void UserFile::cumulative_number_of_sub_problem_resolved(int number) { +void UserFile::cumulative_number_of_sub_problem_solved(int number) { _file << PrefixMessage(LogUtils::LOGLEVEL::INFO, CONTEXT) << indent_1 << "cumulative number of call to solver (only for subproblems) : " << number << std::endl; diff --git a/src/cpp/benders/logger/include/logger/Master.h b/src/cpp/benders/logger/include/logger/Master.h index 65110950e..ff4b44aa7 100644 --- a/src/cpp/benders/logger/include/logger/Master.h +++ b/src/cpp/benders/logger/include/logger/Master.h @@ -56,7 +56,7 @@ class Master : public ILogger { const LogData &best_iterations_data) override; void LogAtInitialRelaxation() override; void LogAtSwitchToInteger() override; - void cumulative_number_of_sub_problem_resolved(int number) override; + void cumulative_number_of_sub_problem_solved(int number) override; private: std::list> _loggers; diff --git a/src/cpp/benders/logger/include/logger/MathLogger.h b/src/cpp/benders/logger/include/logger/MathLogger.h index 2564d3d5b..0b9b8bcd9 100644 --- a/src/cpp/benders/logger/include/logger/MathLogger.h +++ b/src/cpp/benders/logger/include/logger/MathLogger.h @@ -8,13 +8,16 @@ class MathLoggerFile : public MathLoggerImplementation { public: explicit MathLoggerFile(const BENDERSMETHOD& method, - const std::filesystem::path& log_file); + const std::filesystem::path& log_file, + std::streamsize width = 45); private: std::ofstream file_stream_; }; class MathLoggerOstream : public MathLoggerImplementation { public: - explicit MathLoggerOstream(const BENDERSMETHOD& method) - : MathLoggerImplementation(method, &std::cout) {} + explicit MathLoggerOstream(const BENDERSMETHOD& method, + std::streamsize width = 20) + : MathLoggerImplementation(method, &std::cout, width, + HEADERSTYPE::SHORT) {} }; diff --git a/src/cpp/benders/logger/include/logger/User.h b/src/cpp/benders/logger/include/logger/User.h index c7218cd7f..721825682 100644 --- a/src/cpp/benders/logger/include/logger/User.h +++ b/src/cpp/benders/logger/include/logger/User.h @@ -48,7 +48,7 @@ class User : public ILogger { const LogData &best_iterations_data) override; void LogAtInitialRelaxation() override; void LogAtSwitchToInteger() override; - void cumulative_number_of_sub_problem_resolved(int number) override; + void cumulative_number_of_sub_problem_solved(int number) override; private: std::ostream &_stream; diff --git a/src/cpp/benders/logger/include/logger/UserFile.h b/src/cpp/benders/logger/include/logger/UserFile.h index d8f9319a8..a0c272e5b 100644 --- a/src/cpp/benders/logger/include/logger/UserFile.h +++ b/src/cpp/benders/logger/include/logger/UserFile.h @@ -47,7 +47,7 @@ class UserFile : public ILogger { const LogData &best_iterations_data) override; void LogAtInitialRelaxation() override; void LogAtSwitchToInteger() override; - void cumulative_number_of_sub_problem_resolved(int number) override; + void cumulative_number_of_sub_problem_solved(int number) override; private: std::ofstream _file; diff --git a/src/cpp/xpansion_interfaces/ILogger.h b/src/cpp/xpansion_interfaces/ILogger.h index 1d829e64d..acf1bbaa4 100644 --- a/src/cpp/xpansion_interfaces/ILogger.h +++ b/src/cpp/xpansion_interfaces/ILogger.h @@ -64,7 +64,24 @@ struct LogData { double subproblem_time; int cumulative_number_of_subproblem_resolved; }; -class ILogger { +struct ILoggerBenders { + virtual void display_message(const std::string &str) = 0; +}; + +struct BendersLoggerBase : public ILoggerBenders { + void display_message(const std::string &str) override { + for (auto logger : loggers) { + logger->display_message(str); + } + } + void AddLogger(std::shared_ptr logger) { + loggers.push_back(logger); + } + + private: + std::vector> loggers; +}; +class ILogger : public ILoggerBenders { public: virtual ~ILogger() = default; @@ -91,7 +108,7 @@ class ILogger { const LogData &best_iterations_data) = 0; virtual void LogAtInitialRelaxation() = 0; virtual void LogAtSwitchToInteger() = 0; - virtual void cumulative_number_of_sub_problem_resolved(int number) = 0; + virtual void cumulative_number_of_sub_problem_solved(int number) = 0; const std::string CONTEXT = "Benders"; }; diff --git a/src/python/antares_xpansion/config_loader.py b/src/python/antares_xpansion/config_loader.py index 5401fd51d..dfc622206 100644 --- a/src/python/antares_xpansion/config_loader.py +++ b/src/python/antares_xpansion/config_loader.py @@ -14,7 +14,7 @@ from antares_xpansion.chronicles_checker import ChronicleChecker from antares_xpansion.logger import step_logger from antares_xpansion.general_data_reader import GeneralDataIniReader -from antares_xpansion.input_checker import check_candidates_file, check_options +from antares_xpansion.input_checker import check_candidates_file, check_options, str_to_bool from antares_xpansion.launcher_options_default_value import LauncherOptionsDefaultValues from antares_xpansion.launcher_options_keys import LauncherOptionsKeys from antares_xpansion.optimisation_keys import OptimisationKeys @@ -333,6 +333,19 @@ def get_batch_size(self): return int(batch_size_str) + def get_expert_logs(self): + """ + return the expert_log option from settings file + """ + expert_logs = self.options.get( + "expert_logs", + self._config.settings_default["expert_logs"], + ) + if (isinstance(expert_logs, bool)): + return expert_logs + else: + return str_to_bool(self._config.settings_default["expert_logs"])[1] + def additional_constraints(self): """ returns path to additional constraints file @@ -485,6 +498,8 @@ def _set_options_for_benders_solver(self): options_values["LAST_MASTER_BASIS"] = self._config.LAST_MASTER_BASIS options_values[OptimisationKeys.batch_size_key() ] = self.get_batch_size() + options_values[OptimisationKeys.expert_logs_key() + ] = self.get_expert_logs() # generate options file for the solver with open(self.options_file_path(), "w") as options_file: json.dump(options_values, options_file, indent=4) diff --git a/src/python/antares_xpansion/input_checker.py b/src/python/antares_xpansion/input_checker.py index cc248a7e2..0bc3a125f 100644 --- a/src/python/antares_xpansion/input_checker.py +++ b/src/python/antares_xpansion/input_checker.py @@ -344,10 +344,22 @@ class NotHandledOption(Exception): class NotHandledValue(Exception): pass +# return ->tuple[is_a_bool: bool, result: bool] + + +def str_to_bool(my_str: str) -> tuple[bool, bool]: + if my_str in ["true", "True", "TRUE", "1"]: + return (True, True) + elif my_str in ["false", "False", "False", "0"]: + return (True, False) + else: + return (False, False) type_str = str type_int = int type_float = float +type_bool = bool + # "option": (type, legal_value(s)) options_types_and_legal_values = { @@ -364,6 +376,7 @@ class NotHandledValue(Exception): "log_level": (type_int, ["0", "1", "2", "3"]), "separation_parameter": (type_float, None), "batch_size": (type_int, None), + "expert_logs": (type_bool, None), } @@ -408,6 +421,14 @@ def _check_setting_option_type(option, value): logger.error( 'check_setting_option_type: Illegal %s option in type, integer is expected .' % option) return False + elif option_type == type_bool: + [is_a_bool, ret] = str_to_bool(value) + if is_a_bool: + return True + else: + logger.error( + 'check_setting_option_type: Illegal %s option in type, boolean is expected .' % option) + return False return isinstance(value, type_str) @@ -440,6 +461,10 @@ class BatchSizeValueError(Exception): pass +class ExpertLogsValueError(Exception): + pass + + def check_options(options): """ checks that a settings file related to an XpansionDriver has the correct format @@ -498,6 +523,8 @@ def _check_batch_size(value) -> bool: raise BatchSizeValueError + + def _check_separation(value) -> bool: if 0 <= float(value) <= 1: return True @@ -560,6 +587,10 @@ def _check_setting_option_value(option, value): elif option == "batch_size": return _check_batch_size(value) + elif option == "expert_logs": + return str_to_bool(value)[0] + + logger.error( 'check_candidate_option_value: Illegal value %s for option %s' % (value, option)) sys.exit(1) diff --git a/src/python/antares_xpansion/optimisation_keys.py b/src/python/antares_xpansion/optimisation_keys.py index fcc6d3a6a..f79f14c5f 100644 --- a/src/python/antares_xpansion/optimisation_keys.py +++ b/src/python/antares_xpansion/optimisation_keys.py @@ -94,3 +94,7 @@ def separation_key(): @staticmethod def batch_size_key(): return "BATCH_SIZE" + + @staticmethod + def expert_logs_key(): + return "EXPERT_LOGS" diff --git a/src/python/antares_xpansion/xpansionConfig.py b/src/python/antares_xpansion/xpansionConfig.py index 01d31e7b7..cf471641b 100644 --- a/src/python/antares_xpansion/xpansionConfig.py +++ b/src/python/antares_xpansion/xpansionConfig.py @@ -160,6 +160,7 @@ def _set_default_settings(self): "log_level": "0", "separation_parameter": "0.5", "batch_size": "0", + "expert_logs": False, } def _set_default_options(self): @@ -180,6 +181,7 @@ def _set_default_options(self): OptimisationKeys.bound_alpha_key(): self.bound_alpha_default_value(), OptimisationKeys.separation_key(): self.separation_default_value(), OptimisationKeys.batch_size_key(): self.batch_size_default_value(), + OptimisationKeys.expert_logs_key(): self.expert_logs_default_value(), } def bound_alpha_default_value(self): @@ -233,6 +235,9 @@ def separation_default_value(self): def batch_size_default_value(self): return "0" + def expert_logs_default_value(self): + return False + def _get_config_values(self): self.default_install_dir = self.config_parameters.default_install_dir diff --git a/tests/cpp/TestDoubles/LoggerStub.h b/tests/cpp/TestDoubles/LoggerStub.h index 395407674..a0a965c31 100644 --- a/tests/cpp/TestDoubles/LoggerStub.h +++ b/tests/cpp/TestDoubles/LoggerStub.h @@ -30,5 +30,5 @@ class LoggerNOOPStub : public ILogger { const LogData& best_iterations_data) override {} void LogAtInitialRelaxation() override {} void LogAtSwitchToInteger() override {} - void cumulative_number_of_sub_problem_resolved(int number) override {} + void cumulative_number_of_sub_problem_solved(int number) override {} }; \ No newline at end of file diff --git a/tests/cpp/logger/logger_test.cpp b/tests/cpp/logger/logger_test.cpp index 78d721a29..faaa8dc4c 100644 --- a/tests/cpp/logger/logger_test.cpp +++ b/tests/cpp/logger/logger_test.cpp @@ -443,7 +443,7 @@ TEST_F(UserLoggerTest, EndLog) { TEST_F(UserLoggerTest, CumulativeNumberOfSubProblemResolved) { auto number(9150); - _logger.cumulative_number_of_sub_problem_resolved(number); + _logger.cumulative_number_of_sub_problem_solved(number); auto logWithoutPrefix = RemovePrefixFromMessage(_stream); std::stringstream expected; expected << " " << indent_1 @@ -619,7 +619,7 @@ class SimpleLoggerMock : public ILogger { void LogAtInitialRelaxation() { _initialRelaxationCall = true; } void LogAtSwitchToInteger() { _switchToIntegerCall = true; } - void cumulative_number_of_sub_problem_resolved(int number) { + void cumulative_number_of_sub_problem_solved(int number) { _cumulativeNumberOfSubProblemResolved = true; } @@ -682,7 +682,7 @@ TEST_F(MasterLoggerTest, EndLog) { TEST_F(MasterLoggerTest, CumulativeNumberOfSubProblemResolved) { LogData logData; - _master.cumulative_number_of_sub_problem_resolved(39); + _master.cumulative_number_of_sub_problem_solved(39); ASSERT_TRUE(_logger->_cumulativeNumberOfSubProblemResolved); ASSERT_TRUE(_logger2->_cumulativeNumberOfSubProblemResolved); } From a2d5b33cb66b98d77541b71bec95e104620b6e7c Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Tue, 12 Dec 2023 16:31:08 +0100 Subject: [PATCH 19/68] fix python typing --- src/python/antares_xpansion/input_checker.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/python/antares_xpansion/input_checker.py b/src/python/antares_xpansion/input_checker.py index 0bc3a125f..b5f5d89ee 100644 --- a/src/python/antares_xpansion/input_checker.py +++ b/src/python/antares_xpansion/input_checker.py @@ -347,7 +347,8 @@ class NotHandledValue(Exception): # return ->tuple[is_a_bool: bool, result: bool] -def str_to_bool(my_str: str) -> tuple[bool, bool]: +# -> tuple[bool, bool]: not working with python <3.9 +def str_to_bool(my_str: str): if my_str in ["true", "True", "TRUE", "1"]: return (True, True) elif my_str in ["false", "False", "False", "0"]: From 71b8f8840c1b65c9a05e4c49321ec4ee28272e87 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Tue, 12 Dec 2023 18:04:09 +0100 Subject: [PATCH 20/68] writing warning in stdout --- src/cpp/multisolver_interface/environment.cc | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/cpp/multisolver_interface/environment.cc b/src/cpp/multisolver_interface/environment.cc index d3f9d1f33..a3120818b 100644 --- a/src/cpp/multisolver_interface/environment.cc +++ b/src/cpp/multisolver_interface/environment.cc @@ -207,7 +207,7 @@ void printXpressBanner(bool error) { XPRSgetbanner(banner); if (error) { - std::cerr << "Xpress banner :\n" << banner << "\n"; + std::cout << "Xpress banner :\n" << banner << "\n"; } else { std::cout << "Xpress banner :\n" << banner << "\n"; } @@ -221,7 +221,7 @@ std::string GetXpressVarFromEnvironmentVariables(const char* XPRESS_var) { getenv_s(&requiredSize, NULL, 0, XPRESS_var); if (requiredSize == 0) { - std::cerr << "[Windows getenv_s function]: " << XPRESS_var + std::cout << "[Windows getenv_s function]: " << XPRESS_var << " doesn't exist!\n"; } else { xpress_home_from_env.resize(requiredSize); @@ -256,11 +256,11 @@ std::vector XpressDynamicLibraryPotentialPaths() { #elif defined(__GNUC__) // Linux potential_paths.push_back((prefix / "/lib/libxprs.so").string()); #else - std::cerr << "OS Not recognized by xpress/environment.cc." + std::cout << "OS Not recognized by xpress/environment.cc." << " You won't be able to use Xpress."; #endif } else { - std::cerr << "Warning: " + std::cout << "Warning: " << "Environment variable " << XPRESSDIR << " undefined.\n"; } @@ -273,7 +273,7 @@ std::vector XpressDynamicLibraryPotentialPaths() { #elif defined(__GNUC__) // Linux potential_paths.push_back("/opt/xpressmp/lib/libxprs.so"); #else - std::cerr << "OS Not recognized by environment.cc." + std::cout << "OS Not recognized by environment.cc." << " You won't be able to use Xpress."; #endif return potential_paths; @@ -363,11 +363,11 @@ bool initXpressEnv(bool verbose, int xpress_oem_license_key) { } return true; } else { - std::cerr << "XpressInterface: Xpress found at " << xpresspath << "\n"; + std::cout << "XpressInterface: Xpress found at " << xpresspath << "\n"; char errmsg[256]; XPRSgetlicerrmsg(errmsg, 256); - std::cerr << "Xpress License error : " << errmsg + std::cout << "Xpress License error : " << errmsg << " (XPRSinit returned code " << code << "). Please check" << " environment variable XPRESS.\n"; @@ -405,7 +405,7 @@ bool initXpressEnv(bool verbose, int xpress_oem_license_key) { // get the license error message XPRSgetlicerrmsg(errmsg, 256); - std::cerr << "Xpress Error Message: " << errmsg << "\n"; + std::cout << "Xpress Error Message: " << errmsg << "\n"; return false; } @@ -414,7 +414,7 @@ bool initXpressEnv(bool verbose, int xpress_oem_license_key) { if (!code) { return true; } else { - std::cerr << "XPRSinit returned code : " << code << "\n"; + std::cout << "XPRSinit returned code : " << code << "\n"; return false; } } From 815273b533cd93d4bbca43c59abc7842811af04d Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Tue, 12 Dec 2023 18:10:38 +0100 Subject: [PATCH 21/68] delete useless print --- src/cpp/multisolver_interface/environment.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/src/cpp/multisolver_interface/environment.cc b/src/cpp/multisolver_interface/environment.cc index a3120818b..7315ce779 100644 --- a/src/cpp/multisolver_interface/environment.cc +++ b/src/cpp/multisolver_interface/environment.cc @@ -323,7 +323,6 @@ bool initXpressEnv(bool verbose, int xpress_oem_license_key) { std::string xpresspath; bool status = LoadXpressDynamicLibrary(xpresspath); if (!status) { - std::cout << "Warning: " << status << "\n"; return false; } From bd0d77f6cf7775a33685bd6c0b879bd023a66090 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Tue, 12 Dec 2023 18:43:09 +0100 Subject: [PATCH 22/68] rename var --- src/cpp/benders/benders_core/BendersBase.cpp | 6 +++--- src/cpp/benders/benders_core/include/BendersBase.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cpp/benders/benders_core/BendersBase.cpp b/src/cpp/benders/benders_core/BendersBase.cpp index ad461521b..d20b068cd 100644 --- a/src/cpp/benders/benders_core/BendersBase.cpp +++ b/src/cpp/benders/benders_core/BendersBase.cpp @@ -813,7 +813,7 @@ LogData BendersBase::GetBestIterationData() const { } void BendersBase::ChecksResumeMode() { - ITE_TIMEr = Timer(); + benders_timer = Timer(); if (IsResumeMode()) { auto reader = LastIterationReader(LastIterationFile()); LogData last_iter; @@ -829,7 +829,7 @@ void BendersBase::ChecksResumeMode() { LastIterationPrinter(_logger, best_iteration_data, last_iter); restart_data_printer.Print(); UpdateMaxNumberIterationResumeMode(last_iter.it); - ITE_TIMEr = Timer(last_iter.benders_elapsed_time); + benders_timer = Timer(last_iter.benders_elapsed_time); _data.stop = ShouldBendersStop(); iterations_before_resume = last_iter.it; cumulative_number_of_subproblem_resolved_before_resume = @@ -856,7 +856,7 @@ void BendersBase::EndWritingInOutputFile() const { _writer->write_duration(_data.elapsed_time); SaveSolutionInOutputFile(); } -double BendersBase::GetBendersTime() const { return ITE_TIMEr.elapsed(); } +double BendersBase::GetBendersTime() const { return benders_timer.elapsed(); } void BendersBase::write_basis() const { const auto filename(std::filesystem::path(_options.OUTPUTROOT) / (_options.LAST_MASTER_BASIS)); diff --git a/src/cpp/benders/benders_core/include/BendersBase.h b/src/cpp/benders/benders_core/include/BendersBase.h index c50866fb6..1c59f2141 100644 --- a/src/cpp/benders/benders_core/include/BendersBase.h +++ b/src/cpp/benders/benders_core/include/BendersBase.h @@ -177,7 +177,7 @@ class BendersBase { LogData best_iteration_data; int iterations_before_resume = 0; int cumulative_number_of_subproblem_resolved_before_resume = 0; - Timer ITE_TIMEr; + Timer benders_timer; public: Logger _logger; From 6f1f42b85004c69b154f2e2fae836c66f3e39b49 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Tue, 12 Dec 2023 18:59:43 +0100 Subject: [PATCH 23/68] avoid dangling refs --- src/cpp/benders/factories/BendersFactory.cpp | 3 +-- src/cpp/benders/factories/include/LoggerFactories.h | 3 +++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/cpp/benders/factories/BendersFactory.cpp b/src/cpp/benders/factories/BendersFactory.cpp index f42379567..235ec3b70 100644 --- a/src/cpp/benders/factories/BendersFactory.cpp +++ b/src/cpp/benders/factories/BendersFactory.cpp @@ -56,8 +56,7 @@ int RunBenders(char** argv, const std::filesystem::path& options_file, } else { logger = build_void_logger(); writer = build_void_writer(); - auto math_log_factory = MathLoggerFactory(); - math_log_driver = math_log_factory.get_logger(); + math_log_driver = MathLoggerFactory::get_void_logger(); } benders_loggers.AddLogger(logger); benders_loggers.AddLogger(math_log_driver); diff --git a/src/cpp/benders/factories/include/LoggerFactories.h b/src/cpp/benders/factories/include/LoggerFactories.h index 82bb4fe41..82008a28d 100644 --- a/src/cpp/benders/factories/include/LoggerFactories.h +++ b/src/cpp/benders/factories/include/LoggerFactories.h @@ -62,5 +62,8 @@ class MathLoggerFactory { std::shared_ptr get_logger() { return std::make_shared(math_logger_driver); } + static std::shared_ptr get_void_logger() { + return std::make_shared(); + } }; #endif // ANTARESXPANSION_LOGGERFACTORIES_H From 30899873a75f46cd96491e4f0a89783db4ab6b93 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Tue, 12 Dec 2023 19:00:20 +0100 Subject: [PATCH 24/68] clarity --- src/cpp/benders/benders_mpi/BendersMPI.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cpp/benders/benders_mpi/BendersMPI.cpp b/src/cpp/benders/benders_mpi/BendersMPI.cpp index 49ba18e9c..18c58a9a6 100644 --- a/src/cpp/benders/benders_mpi/BendersMPI.cpp +++ b/src/cpp/benders/benders_mpi/BendersMPI.cpp @@ -247,9 +247,9 @@ void BendersMpi::Run() { broadcast(_world, _data.is_in_initial_relaxation, rank_0); broadcast(_world, _data.stop, rank_0); - _data.elapsed_time = GetBendersTime(); - mathLoggerDriver_->Print(_data); if (Rank() == rank_0) { + _data.elapsed_time = GetBendersTime(); + mathLoggerDriver_->Print(_data); SaveCurrentBendersData(); } } From e7d74d96cf4f72011837109bb750c0a6f70882ce Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Tue, 12 Dec 2023 19:29:16 +0100 Subject: [PATCH 25/68] update --- src/cpp/benders/benders_by_batch/BendersByBatch.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cpp/benders/benders_by_batch/BendersByBatch.cpp b/src/cpp/benders/benders_by_batch/BendersByBatch.cpp index 6f831aa4a..0c4c09a89 100644 --- a/src/cpp/benders/benders_by_batch/BendersByBatch.cpp +++ b/src/cpp/benders/benders_by_batch/BendersByBatch.cpp @@ -113,6 +113,7 @@ void BendersByBatch::MasterLoop() { random_batch_permutation_.size(), rank_0); SeparationLoop(); if (Rank() == rank_0) { + _data.elapsed_time = GetBendersTime(); _data.stop = ShouldBendersStop(); } BroadCast(_data.stop, rank_0); @@ -127,7 +128,6 @@ void BendersByBatch::MasterLoop() { _logger->display_message( "\\________________________________________________________________" "________"); - _data.elapsed_time = GetBendersTime(); mathLoggerDriver_->Print(_data); } } From e0295d726119c21ecf91dfea25c18d33be823dbe Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Wed, 13 Dec 2023 11:10:27 +0100 Subject: [PATCH 26/68] test --- src/cpp/benders/benders_by_batch/BendersByBatch.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cpp/benders/benders_by_batch/BendersByBatch.cpp b/src/cpp/benders/benders_by_batch/BendersByBatch.cpp index 0c4c09a89..01e709ba0 100644 --- a/src/cpp/benders/benders_by_batch/BendersByBatch.cpp +++ b/src/cpp/benders/benders_by_batch/BendersByBatch.cpp @@ -128,7 +128,7 @@ void BendersByBatch::MasterLoop() { _logger->display_message( "\\________________________________________________________________" "________"); - mathLoggerDriver_->Print(_data); + // mathLoggerDriver_->Print(_data); } } void BendersByBatch::SeparationLoop() { From 43435f3ad1868f4360a1a7b131e40b9a52fd060c Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Wed, 13 Dec 2023 12:06:37 +0100 Subject: [PATCH 27/68] about benders time --- src/cpp/benders/benders_by_batch/BendersByBatch.cpp | 2 +- src/cpp/benders/benders_mpi/BendersMPI.cpp | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/cpp/benders/benders_by_batch/BendersByBatch.cpp b/src/cpp/benders/benders_by_batch/BendersByBatch.cpp index 01e709ba0..0c4c09a89 100644 --- a/src/cpp/benders/benders_by_batch/BendersByBatch.cpp +++ b/src/cpp/benders/benders_by_batch/BendersByBatch.cpp @@ -128,7 +128,7 @@ void BendersByBatch::MasterLoop() { _logger->display_message( "\\________________________________________________________________" "________"); - // mathLoggerDriver_->Print(_data); + mathLoggerDriver_->Print(_data); } } void BendersByBatch::SeparationLoop() { diff --git a/src/cpp/benders/benders_mpi/BendersMPI.cpp b/src/cpp/benders/benders_mpi/BendersMPI.cpp index 18c58a9a6..f02bfe495 100644 --- a/src/cpp/benders/benders_mpi/BendersMPI.cpp +++ b/src/cpp/benders/benders_mpi/BendersMPI.cpp @@ -200,7 +200,7 @@ void BendersMpi::step_4_update_best_solution(int rank) { _logger->log_at_iteration_end(bendersDataToLogData(_data)); UpdateTrace(); - + _data.elapsed_time = GetBendersTime(); _data.stop = ShouldBendersStop(); } } @@ -248,7 +248,6 @@ void BendersMpi::Run() { broadcast(_world, _data.stop, rank_0); if (Rank() == rank_0) { - _data.elapsed_time = GetBendersTime(); mathLoggerDriver_->Print(_data); SaveCurrentBendersData(); } From 2dcc76f549edf486193f4f849f8c268df7b582a3 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Wed, 13 Dec 2023 14:52:56 +0100 Subject: [PATCH 28/68] make log headers identical --- .../benders_core/BendersMathLogger.cpp | 58 +++++++------------ src/cpp/benders/logger/MathLogger.cpp | 24 ++++---- 2 files changed, 32 insertions(+), 50 deletions(-) diff --git a/src/cpp/benders/benders_core/BendersMathLogger.cpp b/src/cpp/benders/benders_core/BendersMathLogger.cpp index bca9d460e..74ea124a9 100644 --- a/src/cpp/benders/benders_core/BendersMathLogger.cpp +++ b/src/cpp/benders/benders_core/BendersMathLogger.cpp @@ -5,42 +5,28 @@ // #endif HeadersManager::HeadersManager(HEADERSTYPE type, const BENDERSMETHOD& method) { - if (type == HEADERSTYPE::SHORT) { - headers_list.push_back("ITE"); - headers_list.push_back("LB"); - if (method == BENDERSMETHOD::BENDERS) { - headers_list.push_back("UB"); - headers_list.push_back("BESTUB"); - headers_list.push_back("AGAP"); - headers_list.push_back("RGAP"); - } - headers_list.push_back("MinSpx"); - headers_list.push_back("MaxSpx"); - if (method == BENDERSMETHOD::BENDERSBYBATCH) { - headers_list.push_back("NbSubPbSolv"); - } - headers_list.push_back("IteTime"); - headers_list.push_back("MasterTime"); - // headers_list.push_back("SubPbCpuTime"); - headers_list.push_back("SPWallTime"); - } else { - headers_list.push_back("ITERATION"); - headers_list.push_back("LB"); - if (method == BENDERSMETHOD::BENDERS) { - headers_list.push_back("UB"); - headers_list.push_back("BESTUB"); - headers_list.push_back("ABSOLUTE GAP"); - headers_list.push_back("RELATIVE GAP"); - } - headers_list.push_back("MINSIMPLEX"); - headers_list.push_back("MAXSIMPLEX"); - headers_list.push_back("NUMBER OF SUBPROBLEMS SOLVED"); - headers_list.push_back("CUMULATIVE NUMBER OF SUBPROBLEMS SOLVED "); - headers_list.push_back("ITERATION TIME"); - headers_list.push_back("MASTER TIME"); - headers_list.push_back("SUB-PROBLEMS TIME (CPU)"); - headers_list.push_back("SUB-PROBLEMS TIME (WALL)"); - headers_list.push_back("TIME NOT DOING MASTER OR SUB-PROBLEMS (WALL)"); + headers_list.push_back("ITE"); + headers_list.push_back("LB"); + if (method == BENDERSMETHOD::BENDERS) { + headers_list.push_back("UB"); + headers_list.push_back("BESTUB"); + headers_list.push_back("AGAP"); + headers_list.push_back("RGAP"); + } + headers_list.push_back("MinSpx"); + headers_list.push_back("MaxSpx"); + if (method == BENDERSMETHOD::BENDERSBYBATCH) { + headers_list.push_back("NbSubPbSolv"); + } + if (type == HEADERSTYPE::LONG) { + headers_list.push_back("CumulNbSubPbSolv"); + } + headers_list.push_back("IteTime"); + headers_list.push_back("MasterTime"); + headers_list.push_back("SPWallTime"); + if (type == HEADERSTYPE::LONG) { + headers_list.push_back("SPCpuTime"); + headers_list.push_back("NotSolvingWallTime"); } } diff --git a/src/cpp/benders/logger/MathLogger.cpp b/src/cpp/benders/logger/MathLogger.cpp index 919529a6c..911eedc5a 100644 --- a/src/cpp/benders/logger/MathLogger.cpp +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -3,8 +3,8 @@ #include #include -double getDurationNotDoingMasterOrSubproblems(double interation, double master, - double subproblems) { +double getDurationNotSolving(double interation, double master, + double subproblems) { return interation - master - subproblems; } @@ -40,16 +40,14 @@ void MathLoggerBase::Print(const CurrentIterationData& data) { // LogsDestination() << data.deletedcut; LogsDestination() << std::setprecision(2) << data.elapsed_time; LogsDestination() << std::setprecision(2) << data.timer_master; + LogsDestination() << std::setprecision(2) << data.subproblems_walltime; if (type == HEADERSTYPE::LONG) { LogsDestination() << std::setprecision(2) << data.subproblems_cumulative_cputime; - } - LogsDestination() << std::setprecision(2) << data.subproblems_walltime; - if (type == HEADERSTYPE::LONG) { LogsDestination() << std::setprecision(2) - << getDurationNotDoingMasterOrSubproblems( - data.elapsed_time, data.timer_master, - data.subproblems_walltime); + << getDurationNotSolving(data.elapsed_time, + data.timer_master, + data.subproblems_walltime); } LogsDestination() << std::endl; } @@ -75,16 +73,14 @@ void MathLoggerBendersByBatch::Print(const CurrentIterationData& data) { } LogsDestination() << std::setprecision(2) << data.elapsed_time; LogsDestination() << std::setprecision(2) << data.timer_master; + LogsDestination() << std::setprecision(2) << data.subproblems_walltime; if (type == HEADERSTYPE::LONG) { LogsDestination() << std::setprecision(2) << data.subproblems_cumulative_cputime; - } - LogsDestination() << std::setprecision(2) << data.subproblems_walltime; - if (type == HEADERSTYPE::LONG) { LogsDestination() << std::setprecision(2) - << getDurationNotDoingMasterOrSubproblems( - data.elapsed_time, data.timer_master, - data.subproblems_walltime); + << getDurationNotSolving(data.elapsed_time, + data.timer_master, + data.subproblems_walltime); } LogsDestination() << std::endl; From 36f50abd45333a198bab9650d863b0900b1ee197 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Wed, 13 Dec 2023 14:54:41 +0100 Subject: [PATCH 29/68] clean --- src/cpp/benders/benders_core/include/BendersMathLogger.h | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/cpp/benders/benders_core/include/BendersMathLogger.h b/src/cpp/benders/benders_core/include/BendersMathLogger.h index 4844b7580..cb3f2d91c 100644 --- a/src/cpp/benders/benders_core/include/BendersMathLogger.h +++ b/src/cpp/benders/benders_core/include/BendersMathLogger.h @@ -10,11 +10,6 @@ #include "common.h" const std::string MATHLOGGERCONTEXT = "Benders"; -struct Header { - std::string label; - int pos; -}; - enum class HEADERSTYPE { SHORT, LONG }; struct HeadersManager { explicit HeadersManager(HEADERSTYPE type, const BENDERSMETHOD& method); @@ -24,9 +19,7 @@ class LogDestination { public: explicit LogDestination(std::ostream* stream, std::streamsize width = 40); - // for std::endl std::ostream& operator<<(std::ostream& (*function)(std::ostream&)) { - // write obj to stream return function(*stream_); } @@ -39,7 +32,6 @@ class LogDestination { }; template std::ostream& LogDestination::operator<<(const T& obj) { - // write obj to stream return (*stream_) << std::left << std::setw(width_) << obj; } From 9313166f4e46f02ad1ca892459dba9a246568469 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Wed, 13 Dec 2023 15:08:11 +0100 Subject: [PATCH 30/68] add check to Logdestination's stream handler --- .../benders_core/BendersMathLogger.cpp | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/cpp/benders/benders_core/BendersMathLogger.cpp b/src/cpp/benders/benders_core/BendersMathLogger.cpp index 74ea124a9..be499bae0 100644 --- a/src/cpp/benders/benders_core/BendersMathLogger.cpp +++ b/src/cpp/benders/benders_core/BendersMathLogger.cpp @@ -1,8 +1,6 @@ #include "BendersMathLogger.h" -// #ifdef _WIN32 -// #include -// #endif +#include "LogUtils.h" HeadersManager::HeadersManager(HEADERSTYPE type, const BENDERSMETHOD& method) { headers_list.push_back("ITE"); @@ -32,15 +30,13 @@ HeadersManager::HeadersManager(HEADERSTYPE type, const BENDERSMETHOD& method) { LogDestination::LogDestination(std::ostream* stream, std::streamsize width) : stream_(stream), width_(width) { - // _COORD coordinates; - // coordinates.X = 1000; - // coordinates.Y = 1000; - - // if (0 == SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), - // coordinates)) { - // std::cout << "could not resize the console screen\n"; - // // return -1; - // } + if (!stream) { + std::cerr << LOGLOCATION + << LogUtils::LogLevelToStr(LogUtils::LOGLEVEL::WARNING) + << "the stream ptr (std::ostream*) is null, &std::cout will be " + "used!\n"; + stream_ = &std::cout; + } } void MathLoggerDriver::add_logger( std::shared_ptr logger) { From 29b8db2cfa5d84acb2484453b8f9f92980207eda Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Wed, 13 Dec 2023 16:06:55 +0100 Subject: [PATCH 31/68] LogDestinationTest --- src/cpp/benders/benders_core/BendersMathLogger.cpp | 4 +--- tests/cpp/logger/logger_test.cpp | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/cpp/benders/benders_core/BendersMathLogger.cpp b/src/cpp/benders/benders_core/BendersMathLogger.cpp index be499bae0..6696ad510 100644 --- a/src/cpp/benders/benders_core/BendersMathLogger.cpp +++ b/src/cpp/benders/benders_core/BendersMathLogger.cpp @@ -31,9 +31,7 @@ HeadersManager::HeadersManager(HEADERSTYPE type, const BENDERSMETHOD& method) { LogDestination::LogDestination(std::ostream* stream, std::streamsize width) : stream_(stream), width_(width) { if (!stream) { - std::cerr << LOGLOCATION - << LogUtils::LogLevelToStr(LogUtils::LOGLEVEL::WARNING) - << "the stream ptr (std::ostream*) is null, &std::cout will be " + std::cerr << "the stream ptr (std::ostream*) is null, &std::cout will be " "used!\n"; stream_ = &std::cout; } diff --git a/tests/cpp/logger/logger_test.cpp b/tests/cpp/logger/logger_test.cpp index faaa8dc4c..faf342150 100644 --- a/tests/cpp/logger/logger_test.cpp +++ b/tests/cpp/logger/logger_test.cpp @@ -3,6 +3,7 @@ #include #include +#include "BendersMathLogger.h" #include "ILogger.h" #include "LogPrefixManip.h" #include "gtest/gtest.h" @@ -733,3 +734,16 @@ TEST_F(MasterLoggerTest, LogSwitchToInteger) { ASSERT_TRUE(_logger->_switchToIntegerCall); ASSERT_TRUE(_logger2->_switchToIntegerCall); } + +TEST(LogDestinationTest, WithNullStreamHandler) { + const std::string expected_msg = + "the stream ptr (std::ostream*) is null, &std::cout will be " + "used!\n"; + + std::stringstream redirectedErrorStream; + std::streambuf* initialBufferCerr = + std::cerr.rdbuf(redirectedErrorStream.rdbuf()); + LogDestination log_dest(nullptr); + std::cerr.rdbuf(initialBufferCerr); + ASSERT_EQ(expected_msg, redirectedErrorStream.str()); +} \ No newline at end of file From 9314057e1c1e867d11f0b51923cab87d08cf09ac Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Wed, 13 Dec 2023 16:13:50 +0100 Subject: [PATCH 32/68] test ok --- tests/cpp/logger/logger_test.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/cpp/logger/logger_test.cpp b/tests/cpp/logger/logger_test.cpp index faf342150..ccf3ac49e 100644 --- a/tests/cpp/logger/logger_test.cpp +++ b/tests/cpp/logger/logger_test.cpp @@ -743,7 +743,24 @@ TEST(LogDestinationTest, WithNullStreamHandler) { std::stringstream redirectedErrorStream; std::streambuf* initialBufferCerr = std::cerr.rdbuf(redirectedErrorStream.rdbuf()); + LogDestination log_dest(nullptr); + std::cerr.rdbuf(initialBufferCerr); + ASSERT_EQ(expected_msg, redirectedErrorStream.str()); +} + +TEST(LogDestinationTest, WithNullStreamHandlerOutputinStdout) { + const std::string expected_msg = "Hello!"; + + std::stringstream redirectedStdout; + std::streambuf* initialBufferCout = std::cout.rdbuf(redirectedStdout.rdbuf()); + + LogDestination log_dest(nullptr, 0); + log_dest << expected_msg; + + std::cout.rdbuf(initialBufferCout); + + ASSERT_EQ(expected_msg, redirectedStdout.str()); } \ No newline at end of file From b959f9f7628fb157b991aaedb8e33dd008e59030 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Wed, 13 Dec 2023 17:55:39 +0100 Subject: [PATCH 33/68] move implementation details --- .../benders_core/BendersMathLogger.cpp | 22 +++++--- .../benders_core/include/BendersMathLogger.h | 19 ++++--- src/cpp/benders/logger/MathLogger.cpp | 10 +--- .../logger/include/logger/MathLogger.h | 3 +- tests/cpp/logger/logger_test.cpp | 50 +++++++++++++------ 5 files changed, 64 insertions(+), 40 deletions(-) diff --git a/src/cpp/benders/benders_core/BendersMathLogger.cpp b/src/cpp/benders/benders_core/BendersMathLogger.cpp index 6696ad510..3efc2f1c8 100644 --- a/src/cpp/benders/benders_core/BendersMathLogger.cpp +++ b/src/cpp/benders/benders_core/BendersMathLogger.cpp @@ -1,6 +1,7 @@ #include "BendersMathLogger.h" #include "LogUtils.h" +#include "LoggerUtils.h" HeadersManager::HeadersManager(HEADERSTYPE type, const BENDERSMETHOD& method) { headers_list.push_back("ITE"); @@ -28,12 +29,21 @@ HeadersManager::HeadersManager(HEADERSTYPE type, const BENDERSMETHOD& method) { } } -LogDestination::LogDestination(std::ostream* stream, std::streamsize width) - : stream_(stream), width_(width) { - if (!stream) { - std::cerr << "the stream ptr (std::ostream*) is null, &std::cout will be " - "used!\n"; - stream_ = &std::cout; +LogDestination::LogDestination(std::streamsize width) + : stream_(&std::cout), width_(width) {} + +LogDestination::LogDestination(const std::filesystem::path& file_path, + std::streamsize width) + : width_(width) { + file_stream_.open(file_path, std::ofstream::out | std::ofstream::app); + if (file_stream_.is_open()) { + stream_ = &file_stream_; + } else { + std::ostringstream err_msg; + err_msg << PrefixMessage(LogUtils::LOGLEVEL::WARNING, MATHLOGGERCONTEXT) + << "Could not open the file: " + << std::quoted(file_path.string().c_str()) << "\n"; + std::cerr << err_msg.str(); } } void MathLoggerDriver::add_logger( diff --git a/src/cpp/benders/benders_core/include/BendersMathLogger.h b/src/cpp/benders/benders_core/include/BendersMathLogger.h index cb3f2d91c..6d0b3db7c 100644 --- a/src/cpp/benders/benders_core/include/BendersMathLogger.h +++ b/src/cpp/benders/benders_core/include/BendersMathLogger.h @@ -17,7 +17,9 @@ struct HeadersManager { }; class LogDestination { public: - explicit LogDestination(std::ostream* stream, std::streamsize width = 40); + explicit LogDestination(std::streamsize width = 40); + explicit LogDestination(const std::filesystem::path& file_path, + std::streamsize width = 40); std::ostream& operator<<(std::ostream& (*function)(std::ostream&)) { return function(*stream_); @@ -27,6 +29,7 @@ class LogDestination { std::ostream& operator<<(const T& obj); private: + std::ofstream file_stream_; std::ostream* stream_; std::streamsize width_ = 40; }; @@ -55,13 +58,14 @@ struct MathLoggerBehaviour : public ILoggerBenders { }; struct MathLogger : public MathLoggerBehaviour { - explicit MathLogger(std::ostream* stream, std::streamsize width = 40, + explicit MathLogger(const std::filesystem::path& file_path, + std::streamsize width = 40, HEADERSTYPE type = HEADERSTYPE::LONG) - : log_destination_(stream, width), type_(type) {} + : log_destination_(file_path, width), type_(type) {} explicit MathLogger(std::streamsize width = 40, HEADERSTYPE type = HEADERSTYPE::LONG) - : log_destination_(&std::cout, width), type_(type) {} + : log_destination_(width), type_(type) {} virtual void Print(const CurrentIterationData& data) = 0; std::vector Headers() const override { return headers_; } @@ -94,14 +98,15 @@ struct MathLoggerBendersByBatch : public MathLogger { class MathLoggerImplementation : public MathLoggerBehaviour { public: explicit MathLoggerImplementation(const BENDERSMETHOD& method, - std::ostream* stream, + const std::filesystem::path& file_path, std::streamsize width = 40, HEADERSTYPE type = HEADERSTYPE::LONG) { if (method == BENDERSMETHOD::BENDERS) { - implementation_ = std::make_shared(stream, width, type); + implementation_ = + std::make_shared(file_path, width, type); } else if (method == BENDERSMETHOD::BENDERSBYBATCH) { implementation_ = - std::make_shared(stream, width, type); + std::make_shared(file_path, width, type); } // else } diff --git a/src/cpp/benders/logger/MathLogger.cpp b/src/cpp/benders/logger/MathLogger.cpp index 911eedc5a..4240fec5f 100644 --- a/src/cpp/benders/logger/MathLogger.cpp +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -89,12 +89,4 @@ void MathLoggerBendersByBatch::Print(const CurrentIterationData& data) { MathLoggerFile::MathLoggerFile(const BENDERSMETHOD& method, const std::filesystem::path& filename, std::streamsize width) - : MathLoggerImplementation(method, &file_stream_, width, - HEADERSTYPE::LONG) { - // TODO restart case????????????? - file_stream_.open(filename, std::ofstream::out); - if (file_stream_.fail()) { - std::cerr << PrefixMessage(LogUtils::LOGLEVEL::ERR, MATHLOGGERCONTEXT) - << "Invalid file name passed as parameter" << std::endl; - } -} \ No newline at end of file + : MathLoggerImplementation(method, filename, width, HEADERSTYPE::LONG) {} \ No newline at end of file diff --git a/src/cpp/benders/logger/include/logger/MathLogger.h b/src/cpp/benders/logger/include/logger/MathLogger.h index 0b9b8bcd9..a2905d80e 100644 --- a/src/cpp/benders/logger/include/logger/MathLogger.h +++ b/src/cpp/benders/logger/include/logger/MathLogger.h @@ -18,6 +18,5 @@ class MathLoggerOstream : public MathLoggerImplementation { public: explicit MathLoggerOstream(const BENDERSMETHOD& method, std::streamsize width = 20) - : MathLoggerImplementation(method, &std::cout, width, - HEADERSTYPE::SHORT) {} + : MathLoggerImplementation(method, width, HEADERSTYPE::SHORT) {} }; diff --git a/tests/cpp/logger/logger_test.cpp b/tests/cpp/logger/logger_test.cpp index ccf3ac49e..94f6e3209 100644 --- a/tests/cpp/logger/logger_test.cpp +++ b/tests/cpp/logger/logger_test.cpp @@ -735,32 +735,50 @@ TEST_F(MasterLoggerTest, LogSwitchToInteger) { ASSERT_TRUE(_logger2->_switchToIntegerCall); } -TEST(LogDestinationTest, WithNullStreamHandler) { - const std::string expected_msg = - "the stream ptr (std::ostream*) is null, &std::cout will be " - "used!\n"; +TEST(LogDestinationTest, WithInvalidEmptyFilePath) { + const std::filesystem::path invalid_file_path(""); + std::ostringstream expected_msg; + expected_msg << " Could not open the file: " + << std::quoted(invalid_file_path.string().c_str()) << "\n"; std::stringstream redirectedErrorStream; std::streambuf* initialBufferCerr = std::cerr.rdbuf(redirectedErrorStream.rdbuf()); - LogDestination log_dest(nullptr); + LogDestination log_dest(invalid_file_path); std::cerr.rdbuf(initialBufferCerr); - - ASSERT_EQ(expected_msg, redirectedErrorStream.str()); + auto err_str = RemovePrefixFromMessage(redirectedErrorStream); + ASSERT_EQ(expected_msg.str(), err_str); } -TEST(LogDestinationTest, WithNullStreamHandlerOutputinStdout) { - const std::string expected_msg = "Hello!"; +// TEST(LogDestinationTest, WithNullStreamHandlerOutputinStdout) { +// const std::string expected_msg = "Hello!"; + +// std::stringstream redirectedStdout; +// std::streambuf* initialBufferCout = +// std::cout.rdbuf(redirectedStdout.rdbuf()); + +// LogDestination log_dest(nullptr, 0); +// log_dest << expected_msg; + +// std::cout.rdbuf(initialBufferCout); + +// ASSERT_EQ(expected_msg, redirectedStdout.str()); +// } + +// TEST(LogDestinationTest, WithInvalidFileStream) { +// std::ofstream invalid_ofstream(""); +// const std::string expected_msg = "Hello!"; - std::stringstream redirectedStdout; - std::streambuf* initialBufferCout = std::cout.rdbuf(redirectedStdout.rdbuf()); +// std::stringstream redirectedStdout; +// std::streambuf* initialBufferCout = +// std::cout.rdbuf(redirectedStdout.rdbuf()); - LogDestination log_dest(nullptr, 0); - log_dest << expected_msg; +// LogDestination log_dest(&invalid_ofstream, 0); +// log_dest << expected_msg; - std::cout.rdbuf(initialBufferCout); +// std::cout.rdbuf(initialBufferCout); - ASSERT_EQ(expected_msg, redirectedStdout.str()); -} \ No newline at end of file +// ASSERT_EQ(expected_msg, redirectedStdout.str()); +// } \ No newline at end of file From 6b9199c5daa479db23a51487be83da5382566151 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Tue, 19 Dec 2023 18:44:38 +0100 Subject: [PATCH 34/68] add test --- .../benders_core/include/BendersMathLogger.h | 1 + tests/cpp/logger/logger_test.cpp | 22 ++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/cpp/benders/benders_core/include/BendersMathLogger.h b/src/cpp/benders/benders_core/include/BendersMathLogger.h index 6d0b3db7c..bb6e68e0c 100644 --- a/src/cpp/benders/benders_core/include/BendersMathLogger.h +++ b/src/cpp/benders/benders_core/include/BendersMathLogger.h @@ -15,6 +15,7 @@ struct HeadersManager { explicit HeadersManager(HEADERSTYPE type, const BENDERSMETHOD& method); std::vector headers_list; }; + class LogDestination { public: explicit LogDestination(std::streamsize width = 40); diff --git a/tests/cpp/logger/logger_test.cpp b/tests/cpp/logger/logger_test.cpp index 94f6e3209..78171bc1a 100644 --- a/tests/cpp/logger/logger_test.cpp +++ b/tests/cpp/logger/logger_test.cpp @@ -752,20 +752,22 @@ TEST(LogDestinationTest, WithInvalidEmptyFilePath) { ASSERT_EQ(expected_msg.str(), err_str); } -// TEST(LogDestinationTest, WithNullStreamHandlerOutputinStdout) { -// const std::string expected_msg = "Hello!"; +TEST(LogDestinationTest, StdoutWithAValidMessage) { + const std::string msg = "Hello!"; + std::streamsize indentation = 25; + const std::string expected_msg = + msg + std::string((size_t)indentation - msg.size(), ' '); -// std::stringstream redirectedStdout; -// std::streambuf* initialBufferCout = -// std::cout.rdbuf(redirectedStdout.rdbuf()); + std::stringstream redirectedStdout; + std::streambuf* initialBufferCout = std::cout.rdbuf(redirectedStdout.rdbuf()); -// LogDestination log_dest(nullptr, 0); -// log_dest << expected_msg; + LogDestination log_dest(indentation); + log_dest << msg; -// std::cout.rdbuf(initialBufferCout); + std::cout.rdbuf(initialBufferCout); -// ASSERT_EQ(expected_msg, redirectedStdout.str()); -// } + ASSERT_EQ(expected_msg, redirectedStdout.str()); +} // TEST(LogDestinationTest, WithInvalidFileStream) { // std::ofstream invalid_ofstream(""); From 249319a20020f36d6d520d1f99ec535fc2687939 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Wed, 20 Dec 2023 11:49:49 +0100 Subject: [PATCH 35/68] test log file content --- tests/cpp/logger/logger_test.cpp | 24 +++++++++++++++++ tests/cpp/tests_utils/RandomDirGenerator.cpp | 28 +++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/tests/cpp/logger/logger_test.cpp b/tests/cpp/logger/logger_test.cpp index 78171bc1a..721f0ff26 100644 --- a/tests/cpp/logger/logger_test.cpp +++ b/tests/cpp/logger/logger_test.cpp @@ -6,6 +6,7 @@ #include "BendersMathLogger.h" #include "ILogger.h" #include "LogPrefixManip.h" +#include "RandomDirGenerator.h" #include "gtest/gtest.h" #include "logger/Master.h" #include "logger/User.h" @@ -769,6 +770,29 @@ TEST(LogDestinationTest, StdoutWithAValidMessage) { ASSERT_EQ(expected_msg, redirectedStdout.str()); } +std::string FileContent(const std::filesystem::path& file) { + std::ifstream file_stream(file); + + std::string content((std::istreambuf_iterator(file_stream)), + (std::istreambuf_iterator())); + return content; +} + +TEST(LogDestinationTest, MessageWithAValidFile) { + const std::string msg = "Hello!"; + std::streamsize indentation = 40; + const std::string expected_msg = + msg + std::string((size_t)indentation - msg.size(), ' '); + + auto log_file = + CreateRandomSubDir(std::filesystem::temp_directory_path()) / "log.txt"; + LogDestination log_dest(log_file); + log_dest << msg; + ASSERT_TRUE(std::filesystem::exists(log_file)); + + ASSERT_EQ(expected_msg, FileContent(log_file)); +} + // TEST(LogDestinationTest, WithInvalidFileStream) { // std::ofstream invalid_ofstream(""); // const std::string expected_msg = "Hello!"; diff --git a/tests/cpp/tests_utils/RandomDirGenerator.cpp b/tests/cpp/tests_utils/RandomDirGenerator.cpp index cd315fc60..375a02e79 100644 --- a/tests/cpp/tests_utils/RandomDirGenerator.cpp +++ b/tests/cpp/tests_utils/RandomDirGenerator.cpp @@ -1,4 +1,10 @@ #include "RandomDirGenerator.h" + +#include +#ifdef _WIN32 +#include +#endif + std::string timeToStr(const std::time_t& time_p) { struct tm local_time; #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) @@ -29,9 +35,29 @@ std::filesystem::path GetRandomSubDirPath( return parentDir / (timeToStr(std::time(nullptr)) + "-" + GenerateRandomString(6)); } -std::filesystem::path CreateRandomSubDir( +std::filesystem::path CreateRandomSubDir_( const std::filesystem::path& parentDir) { const auto subDirPath = GetRandomSubDirPath(parentDir); std::filesystem::create_directory(subDirPath); return subDirPath; } +std::filesystem::path CreateRandomSubDir( + const std::filesystem::path& parentDir) { + char template_array[] = "XXXXXX"; +#ifdef __linux__ + auto template_dir = parentDir / template_array; + char* template_dir_array = template_dir.string().data(); + if (auto ret = mkdtemp(template_dir_array); ret != nullptr) { + retrun ret; + } +#elif _WIN32 + if (auto ret = _mktemp_s(template_array); ret == 0) { + auto created_dir = parentDir / template_array; + std::filesystem::create_directory(created_dir); + return created_dir; + } +#endif + else { + return CreateRandomSubDir_(parentDir); + } +} From 8e2af9ccdfff344a5cf8b3ce2c4f738c8f1a2b2f Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Wed, 20 Dec 2023 11:54:10 +0100 Subject: [PATCH 36/68] fix --- tests/cpp/tests_utils/RandomDirGenerator.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cpp/tests_utils/RandomDirGenerator.cpp b/tests/cpp/tests_utils/RandomDirGenerator.cpp index 375a02e79..36168f06c 100644 --- a/tests/cpp/tests_utils/RandomDirGenerator.cpp +++ b/tests/cpp/tests_utils/RandomDirGenerator.cpp @@ -48,7 +48,7 @@ std::filesystem::path CreateRandomSubDir( auto template_dir = parentDir / template_array; char* template_dir_array = template_dir.string().data(); if (auto ret = mkdtemp(template_dir_array); ret != nullptr) { - retrun ret; + return ret; } #elif _WIN32 if (auto ret = _mktemp_s(template_array); ret == 0) { From 9d1bc820d9c1c8faa21eb87a4e1cce53870434d9 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Wed, 20 Dec 2023 14:52:31 +0100 Subject: [PATCH 37/68] small changes --- src/cpp/benders/benders_core/BendersMathLogger.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/cpp/benders/benders_core/BendersMathLogger.cpp b/src/cpp/benders/benders_core/BendersMathLogger.cpp index 3efc2f1c8..fb31473b0 100644 --- a/src/cpp/benders/benders_core/BendersMathLogger.cpp +++ b/src/cpp/benders/benders_core/BendersMathLogger.cpp @@ -14,9 +14,11 @@ HeadersManager::HeadersManager(HEADERSTYPE type, const BENDERSMETHOD& method) { } headers_list.push_back("MinSpx"); headers_list.push_back("MaxSpx"); - if (method == BENDERSMETHOD::BENDERSBYBATCH) { + + if (type == HEADERSTYPE::LONG || method == BENDERSMETHOD::BENDERSBYBATCH) { headers_list.push_back("NbSubPbSolv"); } + if (type == HEADERSTYPE::LONG) { headers_list.push_back("CumulNbSubPbSolv"); } From bc0ab43252bb1988aa9759e4c10a24e8592e9ed1 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Wed, 20 Dec 2023 15:10:26 +0100 Subject: [PATCH 38/68] test passed --- src/cpp/benders/benders_core/BendersMathLogger.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/cpp/benders/benders_core/BendersMathLogger.cpp b/src/cpp/benders/benders_core/BendersMathLogger.cpp index fb31473b0..256f4f6db 100644 --- a/src/cpp/benders/benders_core/BendersMathLogger.cpp +++ b/src/cpp/benders/benders_core/BendersMathLogger.cpp @@ -32,7 +32,9 @@ HeadersManager::HeadersManager(HEADERSTYPE type, const BENDERSMETHOD& method) { } LogDestination::LogDestination(std::streamsize width) - : stream_(&std::cout), width_(width) {} + : stream_(&std::cout), width_(width) { + (*stream_) << std::unitbuf; +} LogDestination::LogDestination(const std::filesystem::path& file_path, std::streamsize width) @@ -40,6 +42,7 @@ LogDestination::LogDestination(const std::filesystem::path& file_path, file_stream_.open(file_path, std::ofstream::out | std::ofstream::app); if (file_stream_.is_open()) { stream_ = &file_stream_; + (*stream_) << std::unitbuf; } else { std::ostringstream err_msg; err_msg << PrefixMessage(LogUtils::LOGLEVEL::WARNING, MATHLOGGERCONTEXT) From 31dd692a466c22b23f2af7ad91235cfae71ad44a Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Wed, 20 Dec 2023 15:33:38 +0100 Subject: [PATCH 39/68] test by batch mathlogger headers --- tests/cpp/logger/logger_test.cpp | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/tests/cpp/logger/logger_test.cpp b/tests/cpp/logger/logger_test.cpp index 721f0ff26..f96268b25 100644 --- a/tests/cpp/logger/logger_test.cpp +++ b/tests/cpp/logger/logger_test.cpp @@ -793,18 +793,21 @@ TEST(LogDestinationTest, MessageWithAValidFile) { ASSERT_EQ(expected_msg, FileContent(log_file)); } -// TEST(LogDestinationTest, WithInvalidFileStream) { -// std::ofstream invalid_ofstream(""); -// const std::string expected_msg = "Hello!"; +TEST(MathLoggerBendersByBatchTest, HeadersListStdOut) { + HEADERSTYPE headers_type = HEADERSTYPE::SHORT; + HeadersManager headers_manager(headers_type, BENDERSMETHOD::BENDERSBYBATCH); + std::streamsize width = 25; -// std::stringstream redirectedStdout; -// std::streambuf* initialBufferCout = -// std::cout.rdbuf(redirectedStdout.rdbuf()); - -// LogDestination log_dest(&invalid_ofstream, 0); -// log_dest << expected_msg; - -// std::cout.rdbuf(initialBufferCout); + std::ostringstream expected_msg; + for (const auto& header : headers_manager.headers_list) { + expected_msg << std::setw(width) << std::left << header; + } + expected_msg << std::endl; + std::stringstream redirectedStdout; + std::streambuf* initialBufferCout = std::cout.rdbuf(redirectedStdout.rdbuf()); + MathLoggerBendersByBatch benders_batch_logger(width); + benders_batch_logger.write_header(); + std::cout.rdbuf(initialBufferCout); -// ASSERT_EQ(expected_msg, redirectedStdout.str()); -// } \ No newline at end of file + ASSERT_EQ(expected_msg.str(), redirectedStdout.str()); +} \ No newline at end of file From 7b4c9f4eeda9702ba428d78dad22364cfd9f7c69 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Wed, 20 Dec 2023 15:40:26 +0100 Subject: [PATCH 40/68] test bbb LONG headers --- .../benders_core/include/BendersMathLogger.h | 2 +- tests/cpp/logger/logger_test.cpp | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/cpp/benders/benders_core/include/BendersMathLogger.h b/src/cpp/benders/benders_core/include/BendersMathLogger.h index bb6e68e0c..346573fcf 100644 --- a/src/cpp/benders/benders_core/include/BendersMathLogger.h +++ b/src/cpp/benders/benders_core/include/BendersMathLogger.h @@ -65,7 +65,7 @@ struct MathLogger : public MathLoggerBehaviour { : log_destination_(file_path, width), type_(type) {} explicit MathLogger(std::streamsize width = 40, - HEADERSTYPE type = HEADERSTYPE::LONG) + HEADERSTYPE type = HEADERSTYPE::SHORT) : log_destination_(width), type_(type) {} virtual void Print(const CurrentIterationData& data) = 0; diff --git a/tests/cpp/logger/logger_test.cpp b/tests/cpp/logger/logger_test.cpp index f96268b25..a6476b525 100644 --- a/tests/cpp/logger/logger_test.cpp +++ b/tests/cpp/logger/logger_test.cpp @@ -793,7 +793,7 @@ TEST(LogDestinationTest, MessageWithAValidFile) { ASSERT_EQ(expected_msg, FileContent(log_file)); } -TEST(MathLoggerBendersByBatchTest, HeadersListStdOut) { +TEST(MathLoggerBendersByBatchTest, HeadersListStdOutShort) { HEADERSTYPE headers_type = HEADERSTYPE::SHORT; HeadersManager headers_manager(headers_type, BENDERSMETHOD::BENDERSBYBATCH); std::streamsize width = 25; @@ -810,4 +810,21 @@ TEST(MathLoggerBendersByBatchTest, HeadersListStdOut) { std::cout.rdbuf(initialBufferCout); ASSERT_EQ(expected_msg.str(), redirectedStdout.str()); +} +TEST(MathLoggerBendersByBatchTest, HeadersListFileLong) { + HEADERSTYPE headers_type = HEADERSTYPE::LONG; + HeadersManager headers_manager(headers_type, BENDERSMETHOD::BENDERSBYBATCH); + std::streamsize width = 25; + + std::ostringstream expected_msg; + for (const auto& header : headers_manager.headers_list) { + expected_msg << std::setw(width) << std::left << header; + } + expected_msg << std::endl; + auto log_file = + CreateRandomSubDir(std::filesystem::temp_directory_path()) / "log.txt"; + MathLoggerBendersByBatch benders_batch_logger(log_file, width, headers_type); + benders_batch_logger.write_header(); + + ASSERT_EQ(expected_msg.str(), FileContent(log_file)); } \ No newline at end of file From acfaf420def5038148df62cde79256fd8ae3acb9 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Wed, 20 Dec 2023 15:49:11 +0100 Subject: [PATCH 41/68] HeadersManager test --- tests/cpp/logger/logger_test.cpp | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/cpp/logger/logger_test.cpp b/tests/cpp/logger/logger_test.cpp index a6476b525..89c2f94fa 100644 --- a/tests/cpp/logger/logger_test.cpp +++ b/tests/cpp/logger/logger_test.cpp @@ -793,6 +793,38 @@ TEST(LogDestinationTest, MessageWithAValidFile) { ASSERT_EQ(expected_msg, FileContent(log_file)); } +TEST(MathLoggerHeadersManagerTest, LongBenders) { + HEADERSTYPE headers_type = HEADERSTYPE::LONG; + HeadersManager headers_manager(headers_type, BENDERSMETHOD::BENDERS); + + std::vector expected_headers = {"ITE", + "LB", + "UB", + "BESTUB", + "AGAP", + "RGAP", + "MinSpx", + "MaxSpx", + "NbSubPbSolv", + "CumulNbSubPbSolv", + "IteTime", + "MasterTime", + "SPWallTime", + "SPCpuTime", + "NotSolvingWallTime"}; + ASSERT_EQ(expected_headers, headers_manager.headers_list); +} + +TEST(MathLoggerHeadersManagerTest, ShortBenders) { + HEADERSTYPE headers_type = HEADERSTYPE::SHORT; + HeadersManager headers_manager(headers_type, BENDERSMETHOD::BENDERS); + + std::vector expected_headers = { + "ITE", "LB", "UB", "BESTUB", "AGAP", "RGAP", + "MinSpx", "MaxSpx", "IteTime", "MasterTime", "SPWallTime"}; + ASSERT_EQ(expected_headers, headers_manager.headers_list); +} + TEST(MathLoggerBendersByBatchTest, HeadersListStdOutShort) { HEADERSTYPE headers_type = HEADERSTYPE::SHORT; HeadersManager headers_manager(headers_type, BENDERSMETHOD::BENDERSBYBATCH); @@ -811,6 +843,7 @@ TEST(MathLoggerBendersByBatchTest, HeadersListStdOutShort) { ASSERT_EQ(expected_msg.str(), redirectedStdout.str()); } + TEST(MathLoggerBendersByBatchTest, HeadersListFileLong) { HEADERSTYPE headers_type = HEADERSTYPE::LONG; HeadersManager headers_manager(headers_type, BENDERSMETHOD::BENDERSBYBATCH); From f48ffa3eab76571e22bdc3bb8f18d364ec7987ec Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Wed, 20 Dec 2023 15:53:55 +0100 Subject: [PATCH 42/68] HeadersManager completed --- tests/cpp/logger/logger_test.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/cpp/logger/logger_test.cpp b/tests/cpp/logger/logger_test.cpp index 89c2f94fa..00ca72463 100644 --- a/tests/cpp/logger/logger_test.cpp +++ b/tests/cpp/logger/logger_test.cpp @@ -825,6 +825,34 @@ TEST(MathLoggerHeadersManagerTest, ShortBenders) { ASSERT_EQ(expected_headers, headers_manager.headers_list); } +TEST(MathLoggerHeadersManagerTest, LongBendersByBatch) { + HEADERSTYPE headers_type = HEADERSTYPE::LONG; + HeadersManager headers_manager(headers_type, BENDERSMETHOD::BENDERSBYBATCH); + + std::vector expected_headers = {"ITE", + "LB", + "MinSpx", + "MaxSpx", + "NbSubPbSolv", + "CumulNbSubPbSolv", + "IteTime", + "MasterTime", + "SPWallTime", + "SPCpuTime", + "NotSolvingWallTime"}; + ASSERT_EQ(expected_headers, headers_manager.headers_list); +} + +TEST(MathLoggerHeadersManagerTest, ShortBendersByBatch) { + HEADERSTYPE headers_type = HEADERSTYPE::SHORT; + HeadersManager headers_manager(headers_type, BENDERSMETHOD::BENDERSBYBATCH); + + std::vector expected_headers = { + "ITE", "LB", "MinSpx", "MaxSpx", + "NbSubPbSolv", "IteTime", "MasterTime", "SPWallTime"}; + ASSERT_EQ(expected_headers, headers_manager.headers_list); +} + TEST(MathLoggerBendersByBatchTest, HeadersListStdOutShort) { HEADERSTYPE headers_type = HEADERSTYPE::SHORT; HeadersManager headers_manager(headers_type, BENDERSMETHOD::BENDERSBYBATCH); From ec8f1dd27a77143efed1b8916a24e3e12ec16713 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Wed, 20 Dec 2023 16:24:20 +0100 Subject: [PATCH 43/68] MathLoggerBendersByBatchTest printed data --- tests/cpp/logger/logger_test.cpp | 51 ++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/tests/cpp/logger/logger_test.cpp b/tests/cpp/logger/logger_test.cpp index 00ca72463..38b797bc7 100644 --- a/tests/cpp/logger/logger_test.cpp +++ b/tests/cpp/logger/logger_test.cpp @@ -887,5 +887,56 @@ TEST(MathLoggerBendersByBatchTest, HeadersListFileLong) { MathLoggerBendersByBatch benders_batch_logger(log_file, width, headers_type); benders_batch_logger.write_header(); + ASSERT_EQ(expected_msg.str(), FileContent(log_file)); +} + +TEST(MathLoggerBendersByBatchTest, DataInFileLong) { + HEADERSTYPE headers_type = HEADERSTYPE::LONG; + std::streamsize width = 25; + + CurrentIterationData data; + data.it = 35; + data.lb = 256999; + // data.ub = 222256999; + // data.best_ub = 222256999; + data.min_simplexiter = 3; + data.max_simplexiter = 30; + data.number_of_subproblem_solved = 657; + data.cumulative_number_of_subproblem_solved = 1387; + data.elapsed_time = 1000; + data.timer_master = 10; + data.subproblems_walltime = 16; + data.subproblems_cumulative_cputime = 160; + auto time_not_solving = + data.elapsed_time - data.timer_master - data.subproblems_walltime; + + std::ostringstream expected_msg; + expected_msg << std::left << std::setw(width) << data.it; + expected_msg << std::left << std::setw(width) << std::scientific + << std::setprecision(10) << data.lb; + expected_msg << std::left << std::setw(width) << data.min_simplexiter; + expected_msg << std::left << std::setw(width) << data.max_simplexiter; + expected_msg << std::left << std::setw(width) + << data.number_of_subproblem_solved; + + expected_msg << std::left << std::setw(width) + << data.cumulative_number_of_subproblem_solved; + expected_msg << std::left << std::setw(width) << std::setprecision(2) + << data.elapsed_time; + expected_msg << std::left << std::setw(width) << std::setprecision(2) + << data.timer_master; + expected_msg << std::left << std::setw(width) << std::setprecision(2) + << data.subproblems_walltime; + + expected_msg << std::left << std::setw(width) << std::setprecision(2) + << data.subproblems_cumulative_cputime; + expected_msg << std::left << std::setw(width) << std::setprecision(2) + << time_not_solving; + expected_msg << std::endl; + auto log_file = + CreateRandomSubDir(std::filesystem::temp_directory_path()) / "log.txt"; + MathLoggerBendersByBatch benders_batch_logger(log_file, width, headers_type); + benders_batch_logger.Print(data); + ASSERT_EQ(expected_msg.str(), FileContent(log_file)); } \ No newline at end of file From 7ea33ac0186b2ca33ff6f231d7fbd735ed6e8073 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Wed, 20 Dec 2023 16:26:24 +0100 Subject: [PATCH 44/68] MathLoggerBendersByBatchTest printed datastdout --- tests/cpp/logger/logger_test.cpp | 45 ++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/tests/cpp/logger/logger_test.cpp b/tests/cpp/logger/logger_test.cpp index 38b797bc7..554bff7d6 100644 --- a/tests/cpp/logger/logger_test.cpp +++ b/tests/cpp/logger/logger_test.cpp @@ -938,5 +938,50 @@ TEST(MathLoggerBendersByBatchTest, DataInFileLong) { MathLoggerBendersByBatch benders_batch_logger(log_file, width, headers_type); benders_batch_logger.Print(data); + ASSERT_EQ(expected_msg.str(), FileContent(log_file)); +} + +TEST(MathLoggerBendersByBatchTest, DataInStdOutShort) { + HEADERSTYPE headers_type = HEADERSTYPE::SHORT; + std::streamsize width = 25; + + CurrentIterationData data; + data.it = 35; + data.lb = 256999; + // data.ub = 222256999; + // data.best_ub = 222256999; + data.min_simplexiter = 3; + data.max_simplexiter = 30; + data.number_of_subproblem_solved = 657; + data.cumulative_number_of_subproblem_solved = 1387; + data.elapsed_time = 1000; + data.timer_master = 10; + data.subproblems_walltime = 16; + data.subproblems_cumulative_cputime = 160; + auto time_not_solving = + data.elapsed_time - data.timer_master - data.subproblems_walltime; + + std::ostringstream expected_msg; + expected_msg << std::left << std::setw(width) << data.it; + expected_msg << std::left << std::setw(width) << std::scientific + << std::setprecision(10) << data.lb; + expected_msg << std::left << std::setw(width) << data.min_simplexiter; + expected_msg << std::left << std::setw(width) << data.max_simplexiter; + expected_msg << std::left << std::setw(width) + << data.number_of_subproblem_solved; + + expected_msg << std::left << std::setw(width) << std::setprecision(2) + << data.elapsed_time; + expected_msg << std::left << std::setw(width) << std::setprecision(2) + << data.timer_master; + expected_msg << std::left << std::setw(width) << std::setprecision(2) + << data.subproblems_walltime; + + expected_msg << std::endl; + auto log_file = + CreateRandomSubDir(std::filesystem::temp_directory_path()) / "log.txt"; + MathLoggerBendersByBatch benders_batch_logger(log_file, width, headers_type); + benders_batch_logger.Print(data); + ASSERT_EQ(expected_msg.str(), FileContent(log_file)); } \ No newline at end of file From e8d3564b7c5d3ab77ed00f300b3cb7fd6947a533 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Wed, 20 Dec 2023 16:28:47 +0100 Subject: [PATCH 45/68] MathLoggerBendersByBatchTest printed datastdout --- tests/cpp/logger/logger_test.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/cpp/logger/logger_test.cpp b/tests/cpp/logger/logger_test.cpp index 554bff7d6..9e5403db5 100644 --- a/tests/cpp/logger/logger_test.cpp +++ b/tests/cpp/logger/logger_test.cpp @@ -978,10 +978,11 @@ TEST(MathLoggerBendersByBatchTest, DataInStdOutShort) { << data.subproblems_walltime; expected_msg << std::endl; - auto log_file = - CreateRandomSubDir(std::filesystem::temp_directory_path()) / "log.txt"; - MathLoggerBendersByBatch benders_batch_logger(log_file, width, headers_type); + std::stringstream redirectedStdout; + std::streambuf* initialBufferCout = std::cout.rdbuf(redirectedStdout.rdbuf()); + MathLoggerBendersByBatch benders_batch_logger(width); benders_batch_logger.Print(data); + std::cout.rdbuf(initialBufferCout); - ASSERT_EQ(expected_msg.str(), FileContent(log_file)); + ASSERT_EQ(expected_msg.str(), redirectedStdout.str()); } \ No newline at end of file From a2d6f4147ea4f8ac35caa86a0bde34f5be74d679 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Wed, 20 Dec 2023 16:51:30 +0100 Subject: [PATCH 46/68] MathLoggerBendersBaseTest --- tests/cpp/logger/logger_test.cpp | 114 +++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/tests/cpp/logger/logger_test.cpp b/tests/cpp/logger/logger_test.cpp index 9e5403db5..9972e22e8 100644 --- a/tests/cpp/logger/logger_test.cpp +++ b/tests/cpp/logger/logger_test.cpp @@ -984,5 +984,119 @@ TEST(MathLoggerBendersByBatchTest, DataInStdOutShort) { benders_batch_logger.Print(data); std::cout.rdbuf(initialBufferCout); + ASSERT_EQ(expected_msg.str(), redirectedStdout.str()); +} + +TEST(MathLoggerBendersBaseTest, DataInFileLong) { + HEADERSTYPE headers_type = HEADERSTYPE::LONG; + std::streamsize width = 25; + + CurrentIterationData data; + data.it = 35; + data.lb = 256999; + data.ub = 222256999; + data.best_ub = 22552256999; + data.min_simplexiter = 3; + data.max_simplexiter = 30; + data.number_of_subproblem_solved = 657; + data.cumulative_number_of_subproblem_solved = 1387; + data.elapsed_time = 1000; + data.timer_master = 10; + data.subproblems_walltime = 16; + data.subproblems_cumulative_cputime = 160; + auto time_not_solving = + data.elapsed_time - data.timer_master - data.subproblems_walltime; + + std::ostringstream expected_msg; + expected_msg << std::left << std::setw(width) << data.it; + expected_msg << std::left << std::setw(width) << std::scientific + << std::setprecision(10) << data.lb; + expected_msg << std::left << std::setw(width) << std::scientific + << std::setprecision(10) << data.ub; + expected_msg << std::left << std::setw(width) << std::scientific + << std::setprecision(10) << data.best_ub; + expected_msg << std::left << std::setw(width) << std::scientific + << std::setprecision(2) << data.best_ub - data.lb; + expected_msg << std::left << std::setw(width) << std::scientific + << std::setprecision(2) + << (data.best_ub - data.lb) / data.best_ub; + expected_msg << std::left << std::setw(width) << data.min_simplexiter; + expected_msg << std::left << std::setw(width) << data.max_simplexiter; + expected_msg << std::left << std::setw(width) + << data.number_of_subproblem_solved; + + expected_msg << std::left << std::setw(width) + << data.cumulative_number_of_subproblem_solved; + expected_msg << std::left << std::setw(width) << std::setprecision(2) + << data.elapsed_time; + expected_msg << std::left << std::setw(width) << std::setprecision(2) + << data.timer_master; + expected_msg << std::left << std::setw(width) << std::setprecision(2) + << data.subproblems_walltime; + + expected_msg << std::left << std::setw(width) << std::setprecision(2) + << data.subproblems_cumulative_cputime; + expected_msg << std::left << std::setw(width) << std::setprecision(2) + << time_not_solving; + expected_msg << std::endl; + auto log_file = + CreateRandomSubDir(std::filesystem::temp_directory_path()) / "log.txt"; + MathLoggerBase benders_batch_logger(log_file, width, headers_type); + benders_batch_logger.Print(data); + + ASSERT_EQ(expected_msg.str(), FileContent(log_file)); +} + +TEST(MathLoggerBendersBaseTest, DataInStdOutShort) { + HEADERSTYPE headers_type = HEADERSTYPE::SHORT; + std::streamsize width = 25; + + CurrentIterationData data; + data.it = 35; + data.lb = 256999; + data.ub = 2222569996; + data.best_ub = 22225556999; + data.min_simplexiter = 3; + data.max_simplexiter = 30; + data.number_of_subproblem_solved = 657; + data.cumulative_number_of_subproblem_solved = 1387; + data.elapsed_time = 1000; + data.timer_master = 10; + data.subproblems_walltime = 16; + data.subproblems_cumulative_cputime = 160; + auto time_not_solving = + data.elapsed_time - data.timer_master - data.subproblems_walltime; + + std::ostringstream expected_msg; + expected_msg << std::left << std::setw(width) << data.it; + expected_msg << std::left << std::setw(width) << std::scientific + << std::setprecision(10) << data.lb; + expected_msg << std::left << std::setw(width) << std::scientific + << std::setprecision(10) << data.ub; + expected_msg << std::left << std::setw(width) << std::scientific + << std::setprecision(10) << data.best_ub; + expected_msg << std::left << std::setw(width) << std::scientific + << std::setprecision(2) << data.best_ub - data.lb; + expected_msg << std::left << std::setw(width) << std::scientific + << std::setprecision(2) + << (data.best_ub - data.lb) / data.best_ub; + + expected_msg << std::left << std::setw(width) << data.min_simplexiter; + expected_msg << std::left << std::setw(width) << data.max_simplexiter; + + expected_msg << std::left << std::setw(width) << std::setprecision(2) + << data.elapsed_time; + expected_msg << std::left << std::setw(width) << std::setprecision(2) + << data.timer_master; + expected_msg << std::left << std::setw(width) << std::setprecision(2) + << data.subproblems_walltime; + + expected_msg << std::endl; + std::stringstream redirectedStdout; + std::streambuf* initialBufferCout = std::cout.rdbuf(redirectedStdout.rdbuf()); + MathLoggerBase benders_batch_logger(width); + benders_batch_logger.Print(data); + std::cout.rdbuf(initialBufferCout); + ASSERT_EQ(expected_msg.str(), redirectedStdout.str()); } \ No newline at end of file From 4f40605bd7ac80028e2473b4d02899c9d3fad570 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Wed, 20 Dec 2023 17:01:31 +0100 Subject: [PATCH 47/68] windows --- src/cpp/benders/benders_core/BendersMathLogger.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cpp/benders/benders_core/BendersMathLogger.cpp b/src/cpp/benders/benders_core/BendersMathLogger.cpp index 256f4f6db..d7d411b7e 100644 --- a/src/cpp/benders/benders_core/BendersMathLogger.cpp +++ b/src/cpp/benders/benders_core/BendersMathLogger.cpp @@ -39,7 +39,7 @@ LogDestination::LogDestination(std::streamsize width) LogDestination::LogDestination(const std::filesystem::path& file_path, std::streamsize width) : width_(width) { - file_stream_.open(file_path, std::ofstream::out | std::ofstream::app); + file_stream_.open(file_path, std::ofstream::out | std::ofstream::trunc); if (file_stream_.is_open()) { stream_ = &file_stream_; (*stream_) << std::unitbuf; From fdf3c6238f9e6c7c407d4f91d50c731070b62f20 Mon Sep 17 00:00:00 2001 From: Abdoulbari Zakir Date: Thu, 21 Dec 2023 06:30:33 -0800 Subject: [PATCH 48/68] fix parsing of "expert_logs" --- src/python/antares_xpansion/config_loader.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/python/antares_xpansion/config_loader.py b/src/python/antares_xpansion/config_loader.py index dfc622206..af73004d1 100644 --- a/src/python/antares_xpansion/config_loader.py +++ b/src/python/antares_xpansion/config_loader.py @@ -344,7 +344,7 @@ def get_expert_logs(self): if (isinstance(expert_logs, bool)): return expert_logs else: - return str_to_bool(self._config.settings_default["expert_logs"])[1] + return str_to_bool(expert_logs)[1] def additional_constraints(self): """ From ed5e00b455d7b80494b17b5ba75e87ba71f48dd3 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Fri, 22 Dec 2023 15:20:24 +0100 Subject: [PATCH 49/68] control all logs with log_level option --- .../benders_core/SimulationOptions.cpp | 1 - .../include/SimulationOptions.hxx | 3 --- src/cpp/benders/benders_core/include/common.h | 1 - src/cpp/benders/factories/BendersFactory.cpp | 21 +++++++++++-------- src/python/antares_xpansion/config_loader.py | 16 +------------- src/python/antares_xpansion/input_checker.py | 5 ----- .../antares_xpansion/optimisation_keys.py | 4 ---- src/python/antares_xpansion/xpansionConfig.py | 5 ----- 8 files changed, 13 insertions(+), 43 deletions(-) diff --git a/src/cpp/benders/benders_core/SimulationOptions.cpp b/src/cpp/benders/benders_core/SimulationOptions.cpp index d59440231..d88d0cc9a 100644 --- a/src/cpp/benders/benders_core/SimulationOptions.cpp +++ b/src/cpp/benders/benders_core/SimulationOptions.cpp @@ -167,7 +167,6 @@ BendersBaseOptions SimulationOptions::get_benders_options() const { result.LAST_MASTER_MPS = LAST_MASTER_MPS; result.LAST_MASTER_BASIS = LAST_MASTER_BASIS; result.BATCH_SIZE = BATCH_SIZE; - result.EXPERT_LOGS = EXPERT_LOGS; return result; } \ No newline at end of file diff --git a/src/cpp/benders/benders_core/include/SimulationOptions.hxx b/src/cpp/benders/benders_core/include/SimulationOptions.hxx index e49a90009..df4f4487b 100644 --- a/src/cpp/benders/benders_core/include/SimulationOptions.hxx +++ b/src/cpp/benders/benders_core/include/SimulationOptions.hxx @@ -72,6 +72,3 @@ BENDERS_OPTIONS_MACRO(LAST_MASTER_BASIS, std::string, "master_last_basis", // BATCH SIZE (Benders by batch) BENDERS_OPTIONS_MACRO(BATCH_SIZE, size_t, 0, asUInt()) - -// which logs must be printed in the console -BENDERS_OPTIONS_MACRO(EXPERT_LOGS, bool, true, asBool()) diff --git a/src/cpp/benders/benders_core/include/common.h b/src/cpp/benders/benders_core/include/common.h index 51f956eb7..728e25910 100644 --- a/src/cpp/benders/benders_core/include/common.h +++ b/src/cpp/benders/benders_core/include/common.h @@ -159,7 +159,6 @@ struct BendersBaseOptions : public BaseOptions { std::string LAST_MASTER_BASIS; size_t BATCH_SIZE; - bool EXPERT_LOGS = true; }; void usage(int argc); diff --git a/src/cpp/benders/factories/BendersFactory.cpp b/src/cpp/benders/factories/BendersFactory.cpp index 51d841e07..bb130045c 100644 --- a/src/cpp/benders/factories/BendersFactory.cpp +++ b/src/cpp/benders/factories/BendersFactory.cpp @@ -42,10 +42,11 @@ int RunBenders(char** argv, const std::filesystem::path& options_file, Writer writer; if (world.rank() == 0) { - auto logger_factory = FileAndStdoutLoggerFactory( - log_reports_name, benders_options.EXPERT_LOGS); - auto math_log_factory = MathLoggerFactory( - method, benders_options.EXPERT_LOGS, math_logs_file); + auto benders_log_console = benders_options.LOG_LEVEL > 0; + auto logger_factory = + FileAndStdoutLoggerFactory(log_reports_name, benders_log_console); + auto math_log_factory = + MathLoggerFactory(method, benders_log_console, math_logs_file); logger = logger_factory.get_logger(); math_log_driver = math_log_factory.get_logger(); @@ -58,6 +59,7 @@ int RunBenders(char** argv, const std::filesystem::path& options_file, writer = build_void_writer(); math_log_driver = MathLoggerFactory::get_void_logger(); } + benders_loggers.AddLogger(logger); benders_loggers.AddLogger(math_log_driver); pBendersBase benders; @@ -76,12 +78,13 @@ int RunBenders(char** argv, const std::filesystem::path& options_file, oss_l << std::endl; benders_loggers.display_message(oss_l.str()); - auto solver_log = std::filesystem::path(options.OUTPUTROOT) / - (std::string("solver_log_proc_") + - std::to_string(world.rank()) + ".txt"); - - benders->set_solver_log_file(solver_log); + if (benders_options.LOG_LEVEL > 1) { + auto solver_log = std::filesystem::path(options.OUTPUTROOT) / + (std::string("solver_log_proc_") + + std::to_string(world.rank()) + ".txt"); + benders->set_solver_log_file(solver_log); + } writer->write_log_level(options.LOG_LEVEL); writer->write_master_name(options.MASTER_NAME); writer->write_solver_name(options.SOLVER_NAME); diff --git a/src/python/antares_xpansion/config_loader.py b/src/python/antares_xpansion/config_loader.py index af73004d1..8c89b79be 100644 --- a/src/python/antares_xpansion/config_loader.py +++ b/src/python/antares_xpansion/config_loader.py @@ -14,7 +14,7 @@ from antares_xpansion.chronicles_checker import ChronicleChecker from antares_xpansion.logger import step_logger from antares_xpansion.general_data_reader import GeneralDataIniReader -from antares_xpansion.input_checker import check_candidates_file, check_options, str_to_bool +from antares_xpansion.input_checker import check_candidates_file, check_options from antares_xpansion.launcher_options_default_value import LauncherOptionsDefaultValues from antares_xpansion.launcher_options_keys import LauncherOptionsKeys from antares_xpansion.optimisation_keys import OptimisationKeys @@ -333,18 +333,6 @@ def get_batch_size(self): return int(batch_size_str) - def get_expert_logs(self): - """ - return the expert_log option from settings file - """ - expert_logs = self.options.get( - "expert_logs", - self._config.settings_default["expert_logs"], - ) - if (isinstance(expert_logs, bool)): - return expert_logs - else: - return str_to_bool(expert_logs)[1] def additional_constraints(self): """ @@ -498,8 +486,6 @@ def _set_options_for_benders_solver(self): options_values["LAST_MASTER_BASIS"] = self._config.LAST_MASTER_BASIS options_values[OptimisationKeys.batch_size_key() ] = self.get_batch_size() - options_values[OptimisationKeys.expert_logs_key() - ] = self.get_expert_logs() # generate options file for the solver with open(self.options_file_path(), "w") as options_file: json.dump(options_values, options_file, indent=4) diff --git a/src/python/antares_xpansion/input_checker.py b/src/python/antares_xpansion/input_checker.py index b5f5d89ee..a6eadf3be 100644 --- a/src/python/antares_xpansion/input_checker.py +++ b/src/python/antares_xpansion/input_checker.py @@ -377,7 +377,6 @@ def str_to_bool(my_str: str): "log_level": (type_int, ["0", "1", "2", "3"]), "separation_parameter": (type_float, None), "batch_size": (type_int, None), - "expert_logs": (type_bool, None), } @@ -588,10 +587,6 @@ def _check_setting_option_value(option, value): elif option == "batch_size": return _check_batch_size(value) - elif option == "expert_logs": - return str_to_bool(value)[0] - - logger.error( 'check_candidate_option_value: Illegal value %s for option %s' % (value, option)) sys.exit(1) diff --git a/src/python/antares_xpansion/optimisation_keys.py b/src/python/antares_xpansion/optimisation_keys.py index f79f14c5f..fcc6d3a6a 100644 --- a/src/python/antares_xpansion/optimisation_keys.py +++ b/src/python/antares_xpansion/optimisation_keys.py @@ -94,7 +94,3 @@ def separation_key(): @staticmethod def batch_size_key(): return "BATCH_SIZE" - - @staticmethod - def expert_logs_key(): - return "EXPERT_LOGS" diff --git a/src/python/antares_xpansion/xpansionConfig.py b/src/python/antares_xpansion/xpansionConfig.py index cf471641b..01d31e7b7 100644 --- a/src/python/antares_xpansion/xpansionConfig.py +++ b/src/python/antares_xpansion/xpansionConfig.py @@ -160,7 +160,6 @@ def _set_default_settings(self): "log_level": "0", "separation_parameter": "0.5", "batch_size": "0", - "expert_logs": False, } def _set_default_options(self): @@ -181,7 +180,6 @@ def _set_default_options(self): OptimisationKeys.bound_alpha_key(): self.bound_alpha_default_value(), OptimisationKeys.separation_key(): self.separation_default_value(), OptimisationKeys.batch_size_key(): self.batch_size_default_value(), - OptimisationKeys.expert_logs_key(): self.expert_logs_default_value(), } def bound_alpha_default_value(self): @@ -235,9 +233,6 @@ def separation_default_value(self): def batch_size_default_value(self): return "0" - def expert_logs_default_value(self): - return False - def _get_config_values(self): self.default_install_dir = self.config_parameters.default_install_dir From d77e49753d697435fad90d09bff3be534572d6d0 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Fri, 22 Dec 2023 16:12:58 +0100 Subject: [PATCH 50/68] control solver logs --- src/cpp/multisolver_interface/SolverCbc.cpp | 7 ++----- src/cpp/multisolver_interface/SolverClp.cpp | 7 ++----- src/cpp/multisolver_interface/SolverXpress.cpp | 2 +- 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/cpp/multisolver_interface/SolverCbc.cpp b/src/cpp/multisolver_interface/SolverCbc.cpp index 06d3de0fc..14278068b 100644 --- a/src/cpp/multisolver_interface/SolverCbc.cpp +++ b/src/cpp/multisolver_interface/SolverCbc.cpp @@ -10,10 +10,7 @@ int SolverCbc::_NumberOfProblems = 0; SolverCbc::SolverCbc(SolverLogManager &log_manager) : SolverCbc() { _fp = log_manager.log_file_ptr; - if (!_fp) { - std::cout << "Empty log file name, fallback to default behaviour" - << std::endl; - } else { + if (_fp) { _clp_inner_solver.messageHandler()->setFilePointer(_fp); _cbc.messageHandler()->setFilePointer(_fp); } @@ -622,7 +619,7 @@ void SolverCbc::set_output_log_level(int loglevel) { for (const auto message_handler : {_clp_inner_solver.messageHandler(), _cbc.messageHandler()}) { - if (loglevel > 0) { + if (loglevel > 1) { message_handler->setLogLevel(0, 1); // Coin messages message_handler->setLogLevel(1, 1); // Clp messages message_handler->setLogLevel(2, 1); // Presolve messages diff --git a/src/cpp/multisolver_interface/SolverClp.cpp b/src/cpp/multisolver_interface/SolverClp.cpp index e770a6e60..ebbdced70 100644 --- a/src/cpp/multisolver_interface/SolverClp.cpp +++ b/src/cpp/multisolver_interface/SolverClp.cpp @@ -10,10 +10,7 @@ int SolverClp::_NumberOfProblems = 0; SolverClp::SolverClp(SolverLogManager &log_manager) : SolverClp() { _fp = log_manager.log_file_ptr; - if (!_fp) { - std::cout << "Empty log file name, fallback to default behaviour" - << std::endl; - } else { + if (_fp) { _clp.messageHandler()->setFilePointer(_fp); } } @@ -490,7 +487,7 @@ void SolverClp::get_mip_sol(double *primals) { --------------------------- *************************************************************************************************/ void SolverClp::set_output_log_level(int loglevel) { - if (loglevel > 0) { + if (loglevel > 1) { _clp.messageHandler()->setLogLevel(1); } else { _clp.messageHandler()->setLogLevel(0); diff --git a/src/cpp/multisolver_interface/SolverXpress.cpp b/src/cpp/multisolver_interface/SolverXpress.cpp index bb3cc099f..6412d5de3 100644 --- a/src/cpp/multisolver_interface/SolverXpress.cpp +++ b/src/cpp/multisolver_interface/SolverXpress.cpp @@ -514,7 +514,7 @@ void SolverXpress::set_output_log_level(int loglevel) { int status = XPRSsetcbmessage(_xprs, optimizermsg, &get_stream()); zero_status_check(status, "set message stream to solver stream", LOGLOCATION); - if (loglevel > 0) { + if (loglevel > 1) { int status = XPRSsetintcontrol(_xprs, XPRS_OUTPUTLOG, XPRS_OUTPUTLOG_FULL_OUTPUT); zero_status_check(status, "set log level", LOGLOCATION); From e92292ad2250d1a412b617f1cb5ec1fc6a8992cb Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Fri, 22 Dec 2023 16:29:18 +0100 Subject: [PATCH 51/68] benders logs in file: only data --- src/cpp/benders/logger/MathLogger.cpp | 6 +++++- src/cpp/benders/logger/include/logger/MathLogger.h | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/cpp/benders/logger/MathLogger.cpp b/src/cpp/benders/logger/MathLogger.cpp index 4240fec5f..8880c61ee 100644 --- a/src/cpp/benders/logger/MathLogger.cpp +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -89,4 +89,8 @@ void MathLoggerBendersByBatch::Print(const CurrentIterationData& data) { MathLoggerFile::MathLoggerFile(const BENDERSMETHOD& method, const std::filesystem::path& filename, std::streamsize width) - : MathLoggerImplementation(method, filename, width, HEADERSTYPE::LONG) {} \ No newline at end of file + : MathLoggerImplementation(method, filename, width, HEADERSTYPE::LONG) {} + +void MathLoggerFile::display_message(const std::string& msg) { + // keep empty +} \ No newline at end of file diff --git a/src/cpp/benders/logger/include/logger/MathLogger.h b/src/cpp/benders/logger/include/logger/MathLogger.h index a2905d80e..ed9aa4e49 100644 --- a/src/cpp/benders/logger/include/logger/MathLogger.h +++ b/src/cpp/benders/logger/include/logger/MathLogger.h @@ -11,6 +11,8 @@ class MathLoggerFile : public MathLoggerImplementation { const std::filesystem::path& log_file, std::streamsize width = 45); + void display_message(const std::string& msg) override; + private: std::ofstream file_stream_; }; From 04a69fa1f5a95771a4a570dedf32e5b3b47f1075 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Fri, 22 Dec 2023 16:49:06 +0100 Subject: [PATCH 52/68] redirect console logs --- src/cpp/benders/factories/BendersFactory.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/cpp/benders/factories/BendersFactory.cpp b/src/cpp/benders/factories/BendersFactory.cpp index bb130045c..163bffbf2 100644 --- a/src/cpp/benders/factories/BendersFactory.cpp +++ b/src/cpp/benders/factories/BendersFactory.cpp @@ -89,10 +89,16 @@ int RunBenders(char** argv, const std::filesystem::path& options_file, writer->write_master_name(options.MASTER_NAME); writer->write_solver_name(options.SOLVER_NAME); benders->launch(); + std::stringstream str; - str << "Optimization results available in : " << options.JSON_FILE; + str << "Optimization results available in : " << options.JSON_FILE + << std::endl; + benders_loggers.display_message(str.str()); + + str.str(""); + str << "Benders ran in " << benders->execution_time() << " s" << std::endl; benders_loggers.display_message(str.str()); - logger->log_total_duration(benders->execution_time()); + } catch (std::exception& e) { std::ostringstream msg; msg << "error: " << e.what() << std::endl; From 41bbb647d6f9fb1bdef22fc7de9ccd3412c7aa7b Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Fri, 22 Dec 2023 16:55:02 +0100 Subject: [PATCH 53/68] COIN solver logs only if log_level >1 & log file provided --- src/cpp/multisolver_interface/SolverCbc.cpp | 2 +- src/cpp/multisolver_interface/SolverClp.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cpp/multisolver_interface/SolverCbc.cpp b/src/cpp/multisolver_interface/SolverCbc.cpp index 14278068b..6cb19eec9 100644 --- a/src/cpp/multisolver_interface/SolverCbc.cpp +++ b/src/cpp/multisolver_interface/SolverCbc.cpp @@ -619,7 +619,7 @@ void SolverCbc::set_output_log_level(int loglevel) { for (const auto message_handler : {_clp_inner_solver.messageHandler(), _cbc.messageHandler()}) { - if (loglevel > 1) { + if (loglevel > 1 && _fp) { message_handler->setLogLevel(0, 1); // Coin messages message_handler->setLogLevel(1, 1); // Clp messages message_handler->setLogLevel(2, 1); // Presolve messages diff --git a/src/cpp/multisolver_interface/SolverClp.cpp b/src/cpp/multisolver_interface/SolverClp.cpp index ebbdced70..8360103a7 100644 --- a/src/cpp/multisolver_interface/SolverClp.cpp +++ b/src/cpp/multisolver_interface/SolverClp.cpp @@ -487,7 +487,7 @@ void SolverClp::get_mip_sol(double *primals) { --------------------------- *************************************************************************************************/ void SolverClp::set_output_log_level(int loglevel) { - if (loglevel > 1) { + if (loglevel > 1 && _fp) { _clp.messageHandler()->setLogLevel(1); } else { _clp.messageHandler()->setLogLevel(0); From b6ee3224c48532468c0f793ee3032f9613c37d37 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Fri, 22 Dec 2023 17:09:12 +0100 Subject: [PATCH 54/68] shorten whitespaces --- src/cpp/benders/logger/include/logger/MathLogger.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cpp/benders/logger/include/logger/MathLogger.h b/src/cpp/benders/logger/include/logger/MathLogger.h index ed9aa4e49..c9e0a16d8 100644 --- a/src/cpp/benders/logger/include/logger/MathLogger.h +++ b/src/cpp/benders/logger/include/logger/MathLogger.h @@ -9,7 +9,7 @@ class MathLoggerFile : public MathLoggerImplementation { public: explicit MathLoggerFile(const BENDERSMETHOD& method, const std::filesystem::path& log_file, - std::streamsize width = 45); + std::streamsize width = 30); void display_message(const std::string& msg) override; From 84bcefcfa8abf5d16ac448531ba58cea6c24f1d7 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Fri, 22 Dec 2023 17:32:30 +0100 Subject: [PATCH 55/68] update doc --- docs/user-guide/get-started/settings-definition.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/docs/user-guide/get-started/settings-definition.md b/docs/user-guide/get-started/settings-definition.md index f7448926c..8fc44c2c7 100644 --- a/docs/user-guide/get-started/settings-definition.md +++ b/docs/user-guide/get-started/settings-definition.md @@ -16,12 +16,19 @@ The following section lists the configurable parameters. If the user does not sp |[`master`](#master) | `integer` | Resolution mode of the master problem | |[`yearly-weights`](#yearly-weights) | `None` | Path of the Monte-Carlo weights file | |[`solver`](#solver) | `Cbc` | Name of the solver | -|[`log_level`](#log_level) | `0` | Solver's log level | +|[`log_level`](#log_level) | `0` | logs severity see tab below | |[`additional-constraints`](#additional-constraints) | `None` | Path of the additional constraints file | |[`separation_parameter`](#separation_parameter) | `0.5` | Step size for the in-out separation | |[`relaxed_optimality_gap`](#relaxed_optimality_gap) | `1e-5` | Threshold to switch from relaxed to integer master | |[`batch_size`](#batch_size) | `0` | Number of subproblems per batch | -|[`expert_logs`](#expert_logs) | `false` | display pure optimization data (/!\ the corresponding option(`EXPERT_LOGS`) for benders is `true` by default ) | + +log display according to log_level + **** | **Operational** | **Benders** | **Solver** +-------------|------------------|-------------|------------ + **File** | always | always | 2 + **Console** | 0 | > 0 | never + + The format is a standard `.ini` and should follow this template: ```ini From 10e2a22e9dfbc925e19bc86e8d203f905ce17eaa Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Tue, 2 Jan 2024 10:44:04 +0100 Subject: [PATCH 56/68] delete useless if/else statement --- src/cpp/multisolver_interface/environment.cc | 10 +++------- .../include/multisolver_interface/environment.h | 2 +- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/cpp/multisolver_interface/environment.cc b/src/cpp/multisolver_interface/environment.cc index 53804daec..9aadf1180 100644 --- a/src/cpp/multisolver_interface/environment.cc +++ b/src/cpp/multisolver_interface/environment.cc @@ -202,15 +202,11 @@ bool LoadXpressFunctions(DynamicLibrary* xpress_dynamic_library) { return true; } -void printXpressBanner(bool error) { +void printXpressBanner() { char banner[XPRS_MAXBANNERLENGTH]; XPRSgetbanner(banner); - if (error) { - std::cout << "Xpress banner :\n" << banner << "\n"; - } else { - std::cout << "Xpress banner :\n" << banner << "\n"; - } + std::cout << "Xpress banner :\n" << banner << "\n"; } std::string GetXpressVarFromEnvironmentVariables(const char* XPRESS_var) { @@ -353,7 +349,7 @@ bool initXpressEnv(bool verbose, int xpress_oem_license_key) { if (!code) { // XPRSbanner informs about Xpress version, options and error messages if (verbose) { - printXpressBanner(false); + printXpressBanner(); char version[16]; XPRSgetversion(version); std::cout << "Warning: " diff --git a/src/cpp/multisolver_interface/include/multisolver_interface/environment.h b/src/cpp/multisolver_interface/include/multisolver_interface/environment.h index 003130ee9..e3c8b8452 100644 --- a/src/cpp/multisolver_interface/include/multisolver_interface/environment.h +++ b/src/cpp/multisolver_interface/include/multisolver_interface/environment.h @@ -23,7 +23,7 @@ typedef struct xo_prob_struct* XPRSprob; namespace LoadXpress { -void printXpressBanner(bool error); +void printXpressBanner(); bool initXpressEnv(bool verbose = true, int xpress_oem_license_key = 0); From 7524e72fa29db1d88c9b3fa95b61dcb1f03fe7e3 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Tue, 2 Jan 2024 10:58:20 +0100 Subject: [PATCH 57/68] remove code duplications --- src/cpp/benders/logger/MathLogger.cpp | 81 +++++++++++---------------- 1 file changed, 33 insertions(+), 48 deletions(-) diff --git a/src/cpp/benders/logger/MathLogger.cpp b/src/cpp/benders/logger/MathLogger.cpp index 8880c61ee..31f4a7513 100644 --- a/src/cpp/benders/logger/MathLogger.cpp +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -19,37 +19,44 @@ void MathLogger::setHeadersList(const std::vector& headers) { headers_ = headers; } -void MathLoggerBase::Print(const CurrentIterationData& data) { - auto type = HeadersType(); - - LogsDestination() << data.it; - LogsDestination() << std::scientific << std::setprecision(10) << data.lb; - LogsDestination() << std::scientific << std::setprecision(10) << data.ub; - LogsDestination() << std::scientific << std::setprecision(10) << data.best_ub; - LogsDestination() << std::scientific << std::setprecision(2) +void PrintData(LogDestination& log_destination, + const CurrentIterationData& data, const HEADERSTYPE& type, + const BENDERSMETHOD& method) { + log_destination << data.it; + log_destination << std::scientific << std::setprecision(10) << data.lb; + if (method == BENDERSMETHOD::BENDERS) { + log_destination << std::scientific << std::setprecision(10) << data.ub; + log_destination << std::scientific << std::setprecision(10) << data.best_ub; + log_destination << std::scientific << std::setprecision(2) << data.best_ub - data.lb; - LogsDestination() << std::scientific << std::setprecision(2) + log_destination << std::scientific << std::setprecision(2) << (data.best_ub - data.lb) / data.best_ub; - LogsDestination() << data.min_simplexiter; - LogsDestination() << data.max_simplexiter; + } + log_destination << data.min_simplexiter; + log_destination << data.max_simplexiter; + if (type == HEADERSTYPE::LONG || method == BENDERSMETHOD::BENDERSBYBATCH) { + log_destination << data.number_of_subproblem_solved; + } if (type == HEADERSTYPE::LONG) { - LogsDestination() << data.number_of_subproblem_solved; - LogsDestination() << data.cumulative_number_of_subproblem_solved; + log_destination << data.cumulative_number_of_subproblem_solved; } - // LogsDestination() << data.deletedcut; - LogsDestination() << std::setprecision(2) << data.elapsed_time; - LogsDestination() << std::setprecision(2) << data.timer_master; - LogsDestination() << std::setprecision(2) << data.subproblems_walltime; + log_destination << std::setprecision(2) << data.elapsed_time; + log_destination << std::setprecision(2) << data.timer_master; + log_destination << std::setprecision(2) << data.subproblems_walltime; + if (type == HEADERSTYPE::LONG) { - LogsDestination() << std::setprecision(2) - << data.subproblems_cumulative_cputime; - LogsDestination() << std::setprecision(2) - << getDurationNotSolving(data.elapsed_time, - data.timer_master, - data.subproblems_walltime); + log_destination << std::setprecision(2) + << data.subproblems_cumulative_cputime; + log_destination << std::setprecision(2) + << getDurationNotSolving(data.elapsed_time, + data.timer_master, + data.subproblems_walltime); } - LogsDestination() << std::endl; + log_destination << std::endl; +} +void MathLoggerBase::Print(const CurrentIterationData& data) { + PrintData(LogsDecorator(), data, HeadersType(), BENDERSMETHOD::BENDERS); } void MathLoggerBendersByBatch::setHeadersList() { @@ -60,30 +67,8 @@ void MathLoggerBendersByBatch::setHeadersList() { } void MathLoggerBendersByBatch::Print(const CurrentIterationData& data) { - auto type = HeadersType(); - - LogsDestination() << data.it; - LogsDestination() << std::scientific << std::setprecision(10) << data.lb; - LogsDestination() << data.min_simplexiter; - LogsDestination() << data.max_simplexiter; - LogsDestination() << data.number_of_subproblem_solved; - - if (type == HEADERSTYPE::LONG) { - LogsDestination() << data.cumulative_number_of_subproblem_solved; - } - LogsDestination() << std::setprecision(2) << data.elapsed_time; - LogsDestination() << std::setprecision(2) << data.timer_master; - LogsDestination() << std::setprecision(2) << data.subproblems_walltime; - if (type == HEADERSTYPE::LONG) { - LogsDestination() << std::setprecision(2) - << data.subproblems_cumulative_cputime; - LogsDestination() << std::setprecision(2) - << getDurationNotSolving(data.elapsed_time, - data.timer_master, - data.subproblems_walltime); - } - - LogsDestination() << std::endl; + PrintData(LogsDecorator(), data, HeadersType(), + BENDERSMETHOD::BENDERSBYBATCH); } MathLoggerFile::MathLoggerFile(const BENDERSMETHOD& method, From 62c811436e14e33db208ab2e3724437ac7dcdf70 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Tue, 2 Jan 2024 11:01:22 +0100 Subject: [PATCH 58/68] fix --- src/cpp/benders/logger/MathLogger.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cpp/benders/logger/MathLogger.cpp b/src/cpp/benders/logger/MathLogger.cpp index 31f4a7513..4e2da5ce2 100644 --- a/src/cpp/benders/logger/MathLogger.cpp +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -56,7 +56,7 @@ void PrintData(LogDestination& log_destination, log_destination << std::endl; } void MathLoggerBase::Print(const CurrentIterationData& data) { - PrintData(LogsDecorator(), data, HeadersType(), BENDERSMETHOD::BENDERS); + PrintData(LogsDestination(), data, HeadersType(), BENDERSMETHOD::BENDERS); } void MathLoggerBendersByBatch::setHeadersList() { @@ -67,7 +67,7 @@ void MathLoggerBendersByBatch::setHeadersList() { } void MathLoggerBendersByBatch::Print(const CurrentIterationData& data) { - PrintData(LogsDecorator(), data, HeadersType(), + PrintData(LogsDestination(), data, HeadersType(), BENDERSMETHOD::BENDERSBYBATCH); } From 136b8cb4c74a15dbb956edb16d6ee0016118a594 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Tue, 2 Jan 2024 11:35:50 +0100 Subject: [PATCH 59/68] Pr remarks --- .../benders_core/BendersMathLogger.cpp | 12 +++++------ tests/cpp/logger/logger_test.cpp | 20 +++++++++---------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/cpp/benders/benders_core/BendersMathLogger.cpp b/src/cpp/benders/benders_core/BendersMathLogger.cpp index d7d411b7e..8dbf980eb 100644 --- a/src/cpp/benders/benders_core/BendersMathLogger.cpp +++ b/src/cpp/benders/benders_core/BendersMathLogger.cpp @@ -4,13 +4,13 @@ #include "LoggerUtils.h" HeadersManager::HeadersManager(HEADERSTYPE type, const BENDERSMETHOD& method) { - headers_list.push_back("ITE"); - headers_list.push_back("LB"); + headers_list.push_back("Ite"); + headers_list.push_back("Lb"); if (method == BENDERSMETHOD::BENDERS) { - headers_list.push_back("UB"); - headers_list.push_back("BESTUB"); - headers_list.push_back("AGAP"); - headers_list.push_back("RGAP"); + headers_list.push_back("Ub"); + headers_list.push_back("BestUb"); + headers_list.push_back("AGap"); + headers_list.push_back("RGap"); } headers_list.push_back("MinSpx"); headers_list.push_back("MaxSpx"); diff --git a/tests/cpp/logger/logger_test.cpp b/tests/cpp/logger/logger_test.cpp index 9972e22e8..03106d253 100644 --- a/tests/cpp/logger/logger_test.cpp +++ b/tests/cpp/logger/logger_test.cpp @@ -797,12 +797,12 @@ TEST(MathLoggerHeadersManagerTest, LongBenders) { HEADERSTYPE headers_type = HEADERSTYPE::LONG; HeadersManager headers_manager(headers_type, BENDERSMETHOD::BENDERS); - std::vector expected_headers = {"ITE", - "LB", - "UB", - "BESTUB", - "AGAP", - "RGAP", + std::vector expected_headers = {"Ite", + "Lb", + "Ub", + "BestUb", + "AGap", + "RGap", "MinSpx", "MaxSpx", "NbSubPbSolv", @@ -820,7 +820,7 @@ TEST(MathLoggerHeadersManagerTest, ShortBenders) { HeadersManager headers_manager(headers_type, BENDERSMETHOD::BENDERS); std::vector expected_headers = { - "ITE", "LB", "UB", "BESTUB", "AGAP", "RGAP", + "Ite", "Lb", "Ub", "BestUb", "AGap", "RGap", "MinSpx", "MaxSpx", "IteTime", "MasterTime", "SPWallTime"}; ASSERT_EQ(expected_headers, headers_manager.headers_list); } @@ -829,8 +829,8 @@ TEST(MathLoggerHeadersManagerTest, LongBendersByBatch) { HEADERSTYPE headers_type = HEADERSTYPE::LONG; HeadersManager headers_manager(headers_type, BENDERSMETHOD::BENDERSBYBATCH); - std::vector expected_headers = {"ITE", - "LB", + std::vector expected_headers = {"Ite", + "Lb", "MinSpx", "MaxSpx", "NbSubPbSolv", @@ -848,7 +848,7 @@ TEST(MathLoggerHeadersManagerTest, ShortBendersByBatch) { HeadersManager headers_manager(headers_type, BENDERSMETHOD::BENDERSBYBATCH); std::vector expected_headers = { - "ITE", "LB", "MinSpx", "MaxSpx", + "Ite", "Lb", "MinSpx", "MaxSpx", "NbSubPbSolv", "IteTime", "MasterTime", "SPWallTime"}; ASSERT_EQ(expected_headers, headers_manager.headers_list); } From 2abbe7def011bba84efb1ac224fd22fdc9a191cf Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Tue, 2 Jan 2024 11:37:09 +0100 Subject: [PATCH 60/68] Pr remarks 2 --- src/cpp/benders/benders_core/BendersMathLogger.cpp | 4 ++-- tests/cpp/logger/logger_test.cpp | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/cpp/benders/benders_core/BendersMathLogger.cpp b/src/cpp/benders/benders_core/BendersMathLogger.cpp index 8dbf980eb..0a860aae4 100644 --- a/src/cpp/benders/benders_core/BendersMathLogger.cpp +++ b/src/cpp/benders/benders_core/BendersMathLogger.cpp @@ -9,8 +9,8 @@ HeadersManager::HeadersManager(HEADERSTYPE type, const BENDERSMETHOD& method) { if (method == BENDERSMETHOD::BENDERS) { headers_list.push_back("Ub"); headers_list.push_back("BestUb"); - headers_list.push_back("AGap"); - headers_list.push_back("RGap"); + headers_list.push_back("AbsGap"); + headers_list.push_back("RelGap"); } headers_list.push_back("MinSpx"); headers_list.push_back("MaxSpx"); diff --git a/tests/cpp/logger/logger_test.cpp b/tests/cpp/logger/logger_test.cpp index 03106d253..0e9ca004b 100644 --- a/tests/cpp/logger/logger_test.cpp +++ b/tests/cpp/logger/logger_test.cpp @@ -801,8 +801,8 @@ TEST(MathLoggerHeadersManagerTest, LongBenders) { "Lb", "Ub", "BestUb", - "AGap", - "RGap", + "AbsGap", + "RelGap", "MinSpx", "MaxSpx", "NbSubPbSolv", @@ -820,7 +820,7 @@ TEST(MathLoggerHeadersManagerTest, ShortBenders) { HeadersManager headers_manager(headers_type, BENDERSMETHOD::BENDERS); std::vector expected_headers = { - "Ite", "Lb", "Ub", "BestUb", "AGap", "RGap", + "Ite", "Lb", "Ub", "BestUb", "AbsGap", "RelGap", "MinSpx", "MaxSpx", "IteTime", "MasterTime", "SPWallTime"}; ASSERT_EQ(expected_headers, headers_manager.headers_list); } From 713d8997275cbb9da9fcfa543aefd755161806df Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Thu, 4 Jan 2024 10:09:43 +0100 Subject: [PATCH 61/68] units --- .../benders_core/BendersMathLogger.cpp | 10 +++---- tests/cpp/logger/logger_test.cpp | 26 +++++++++---------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/cpp/benders/benders_core/BendersMathLogger.cpp b/src/cpp/benders/benders_core/BendersMathLogger.cpp index 0a860aae4..4c75de659 100644 --- a/src/cpp/benders/benders_core/BendersMathLogger.cpp +++ b/src/cpp/benders/benders_core/BendersMathLogger.cpp @@ -22,12 +22,12 @@ HeadersManager::HeadersManager(HEADERSTYPE type, const BENDERSMETHOD& method) { if (type == HEADERSTYPE::LONG) { headers_list.push_back("CumulNbSubPbSolv"); } - headers_list.push_back("IteTime"); - headers_list.push_back("MasterTime"); - headers_list.push_back("SPWallTime"); + headers_list.push_back("IteTime (s)"); + headers_list.push_back("MasterTime (s)"); + headers_list.push_back("SPWallTime (s)"); if (type == HEADERSTYPE::LONG) { - headers_list.push_back("SPCpuTime"); - headers_list.push_back("NotSolvingWallTime"); + headers_list.push_back("SPCpuTime (s)"); + headers_list.push_back("NotSolvingWallTime (s)"); } } diff --git a/tests/cpp/logger/logger_test.cpp b/tests/cpp/logger/logger_test.cpp index 0e9ca004b..50fafc631 100644 --- a/tests/cpp/logger/logger_test.cpp +++ b/tests/cpp/logger/logger_test.cpp @@ -807,11 +807,11 @@ TEST(MathLoggerHeadersManagerTest, LongBenders) { "MaxSpx", "NbSubPbSolv", "CumulNbSubPbSolv", - "IteTime", - "MasterTime", - "SPWallTime", - "SPCpuTime", - "NotSolvingWallTime"}; + "IteTime (s)", + "MasterTime (s)", + "SPWallTime (s)", + "SPCpuTime (s)", + "NotSolvingWallTime (s)"}; ASSERT_EQ(expected_headers, headers_manager.headers_list); } @@ -821,7 +821,7 @@ TEST(MathLoggerHeadersManagerTest, ShortBenders) { std::vector expected_headers = { "Ite", "Lb", "Ub", "BestUb", "AbsGap", "RelGap", - "MinSpx", "MaxSpx", "IteTime", "MasterTime", "SPWallTime"}; + "MinSpx", "MaxSpx", "IteTime (s)", "MasterTime (s)", "SPWallTime (s)"}; ASSERT_EQ(expected_headers, headers_manager.headers_list); } @@ -835,11 +835,11 @@ TEST(MathLoggerHeadersManagerTest, LongBendersByBatch) { "MaxSpx", "NbSubPbSolv", "CumulNbSubPbSolv", - "IteTime", - "MasterTime", - "SPWallTime", - "SPCpuTime", - "NotSolvingWallTime"}; + "IteTime (s)", + "MasterTime (s)", + "SPWallTime (s)", + "SPCpuTime (s)", + "NotSolvingWallTime (s)"}; ASSERT_EQ(expected_headers, headers_manager.headers_list); } @@ -849,7 +849,7 @@ TEST(MathLoggerHeadersManagerTest, ShortBendersByBatch) { std::vector expected_headers = { "Ite", "Lb", "MinSpx", "MaxSpx", - "NbSubPbSolv", "IteTime", "MasterTime", "SPWallTime"}; + "NbSubPbSolv", "IteTime (s)", "MasterTime (s)", "SPWallTime (s)"}; ASSERT_EQ(expected_headers, headers_manager.headers_list); } @@ -1099,4 +1099,4 @@ TEST(MathLoggerBendersBaseTest, DataInStdOutShort) { std::cout.rdbuf(initialBufferCout); ASSERT_EQ(expected_msg.str(), redirectedStdout.str()); -} \ No newline at end of file +} From 2d92aff030391ea602baf4980d14f326c53e03c3 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Thu, 4 Jan 2024 10:46:39 +0100 Subject: [PATCH 62/68] rename vars --- src/cpp/benders/benders_by_batch/BendersByBatch.cpp | 4 ++-- src/cpp/benders/benders_core/BendersBase.cpp | 8 ++++---- .../benders/benders_core/include/BendersStructsDatas.h | 2 +- src/cpp/benders/benders_mpi/BendersMPI.cpp | 2 +- src/cpp/benders/benders_sequential/BendersSequential.cpp | 2 +- src/cpp/benders/logger/MathLogger.cpp | 8 ++++---- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/cpp/benders/benders_by_batch/BendersByBatch.cpp b/src/cpp/benders/benders_by_batch/BendersByBatch.cpp index 0c4c09a89..cb06060c0 100644 --- a/src/cpp/benders/benders_by_batch/BendersByBatch.cpp +++ b/src/cpp/benders/benders_by_batch/BendersByBatch.cpp @@ -113,7 +113,7 @@ void BendersByBatch::MasterLoop() { random_batch_permutation_.size(), rank_0); SeparationLoop(); if (Rank() == rank_0) { - _data.elapsed_time = GetBendersTime(); + _data.time_benders = GetBendersTime(); _data.stop = ShouldBendersStop(); } BroadCast(_data.stop, rank_0); @@ -310,7 +310,7 @@ double BendersByBatch::Gap() const { * */ void BendersByBatch::UpdateStoppingCriterion() { - if (_data.elapsed_time > Options().TIME_LIMIT) { + if (_data.time_benders > Options().TIME_LIMIT) { _data.stopping_criterion = StoppingCriterion::timelimit; } else if ((Options().MAX_ITERATIONS != -1) && (_data.it >= Options().MAX_ITERATIONS)) diff --git a/src/cpp/benders/benders_core/BendersBase.cpp b/src/cpp/benders/benders_core/BendersBase.cpp index 78896aa2f..12dd38ae4 100644 --- a/src/cpp/benders/benders_core/BendersBase.cpp +++ b/src/cpp/benders/benders_core/BendersBase.cpp @@ -179,7 +179,7 @@ bool BendersBase::ShouldRelaxationStop() const { * */ void BendersBase::UpdateStoppingCriterion() { - if (_data.elapsed_time > _options.TIME_LIMIT) + if (_data.time_benders > _options.TIME_LIMIT) _data.stopping_criterion = StoppingCriterion::timelimit; else if ((_options.MAX_ITERATIONS != -1) && (_data.it >= _options.MAX_ITERATIONS)) @@ -650,7 +650,7 @@ LogData BendersBase::bendersDataToLogData( optimal_gap, optimal_gap / data.best_ub, _options.MAX_ITERATIONS, - data.elapsed_time, + data.time_benders, data.timer_master, data.subproblems_walltime, data.cumulative_number_of_subproblem_solved + @@ -806,7 +806,7 @@ void BendersBase::UpdateMaxNumberIterationResumeMode( } } -double BendersBase::execution_time() const { return _data.elapsed_time; } +double BendersBase::execution_time() const { return _data.time_benders; } LogData BendersBase::GetBestIterationData() const { return best_iteration_data; } @@ -852,7 +852,7 @@ void BendersBase::ClearCurrentIterationCutTrace() const { } void BendersBase::EndWritingInOutputFile() const { _writer->updateEndTime(); - _writer->write_duration(_data.elapsed_time); + _writer->write_duration(_data.time_benders); SaveSolutionInOutputFile(); } double BendersBase::GetBendersTime() const { return benders_timer.elapsed(); } diff --git a/src/cpp/benders/benders_core/include/BendersStructsDatas.h b/src/cpp/benders/benders_core/include/BendersStructsDatas.h index 22d0f6de9..6afa32eba 100644 --- a/src/cpp/benders/benders_core/include/BendersStructsDatas.h +++ b/src/cpp/benders/benders_core/include/BendersStructsDatas.h @@ -28,7 +28,7 @@ struct CurrentIterationData { Point max_invest; int nsubproblem; int master_status; - double elapsed_time; + double time_benders; StoppingCriterion stopping_criterion; bool is_in_initial_relaxation; int number_of_subproblem_solved; diff --git a/src/cpp/benders/benders_mpi/BendersMPI.cpp b/src/cpp/benders/benders_mpi/BendersMPI.cpp index f02bfe495..568de652b 100644 --- a/src/cpp/benders/benders_mpi/BendersMPI.cpp +++ b/src/cpp/benders/benders_mpi/BendersMPI.cpp @@ -200,7 +200,7 @@ void BendersMpi::step_4_update_best_solution(int rank) { _logger->log_at_iteration_end(bendersDataToLogData(_data)); UpdateTrace(); - _data.elapsed_time = GetBendersTime(); + _data.time_benders = GetBendersTime(); _data.stop = ShouldBendersStop(); } } diff --git a/src/cpp/benders/benders_sequential/BendersSequential.cpp b/src/cpp/benders/benders_sequential/BendersSequential.cpp index e1be67864..5fd2aa1ab 100644 --- a/src/cpp/benders/benders_sequential/BendersSequential.cpp +++ b/src/cpp/benders/benders_sequential/BendersSequential.cpp @@ -117,7 +117,7 @@ void BendersSequential::Run() { UpdateTrace(); set_timer_master(timer_master.elapsed()); - _data.elapsed_time = GetBendersTime(); + _data.time_benders = GetBendersTime(); _data.stop = ShouldBendersStop(); SaveCurrentBendersData(); } diff --git a/src/cpp/benders/logger/MathLogger.cpp b/src/cpp/benders/logger/MathLogger.cpp index 4e2da5ce2..e4dfe5c53 100644 --- a/src/cpp/benders/logger/MathLogger.cpp +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -3,9 +3,9 @@ #include #include -double getDurationNotSolving(double interation, double master, +double getDurationNotSolving(double iteration, double master, double subproblems) { - return interation - master - subproblems; + return iteration - master - subproblems; } void MathLoggerBase::setHeadersList() { @@ -41,7 +41,7 @@ void PrintData(LogDestination& log_destination, log_destination << data.cumulative_number_of_subproblem_solved; } - log_destination << std::setprecision(2) << data.elapsed_time; + log_destination << std::setprecision(2) << data.time_benders; log_destination << std::setprecision(2) << data.timer_master; log_destination << std::setprecision(2) << data.subproblems_walltime; @@ -49,7 +49,7 @@ void PrintData(LogDestination& log_destination, log_destination << std::setprecision(2) << data.subproblems_cumulative_cputime; log_destination << std::setprecision(2) - << getDurationNotSolving(data.elapsed_time, + << getDurationNotSolving(data.time_benders, data.timer_master, data.subproblems_walltime); } From a59629830b040226d70d4fed122fc06a0f102122 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Thu, 4 Jan 2024 10:48:19 +0100 Subject: [PATCH 63/68] 2 rename vars --- src/cpp/benders/benders_by_batch/BendersByBatch.cpp | 4 ++-- src/cpp/benders/benders_core/BendersBase.cpp | 8 ++++---- .../benders/benders_core/include/BendersStructsDatas.h | 2 +- src/cpp/benders/benders_mpi/BendersMPI.cpp | 2 +- src/cpp/benders/benders_sequential/BendersSequential.cpp | 2 +- src/cpp/benders/logger/MathLogger.cpp | 4 ++-- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/cpp/benders/benders_by_batch/BendersByBatch.cpp b/src/cpp/benders/benders_by_batch/BendersByBatch.cpp index cb06060c0..55d297e46 100644 --- a/src/cpp/benders/benders_by_batch/BendersByBatch.cpp +++ b/src/cpp/benders/benders_by_batch/BendersByBatch.cpp @@ -113,7 +113,7 @@ void BendersByBatch::MasterLoop() { random_batch_permutation_.size(), rank_0); SeparationLoop(); if (Rank() == rank_0) { - _data.time_benders = GetBendersTime(); + _data.benders_time = GetBendersTime(); _data.stop = ShouldBendersStop(); } BroadCast(_data.stop, rank_0); @@ -310,7 +310,7 @@ double BendersByBatch::Gap() const { * */ void BendersByBatch::UpdateStoppingCriterion() { - if (_data.time_benders > Options().TIME_LIMIT) { + if (_data.benders_time > Options().TIME_LIMIT) { _data.stopping_criterion = StoppingCriterion::timelimit; } else if ((Options().MAX_ITERATIONS != -1) && (_data.it >= Options().MAX_ITERATIONS)) diff --git a/src/cpp/benders/benders_core/BendersBase.cpp b/src/cpp/benders/benders_core/BendersBase.cpp index 12dd38ae4..a9fd8a64f 100644 --- a/src/cpp/benders/benders_core/BendersBase.cpp +++ b/src/cpp/benders/benders_core/BendersBase.cpp @@ -179,7 +179,7 @@ bool BendersBase::ShouldRelaxationStop() const { * */ void BendersBase::UpdateStoppingCriterion() { - if (_data.time_benders > _options.TIME_LIMIT) + if (_data.benders_time > _options.TIME_LIMIT) _data.stopping_criterion = StoppingCriterion::timelimit; else if ((_options.MAX_ITERATIONS != -1) && (_data.it >= _options.MAX_ITERATIONS)) @@ -650,7 +650,7 @@ LogData BendersBase::bendersDataToLogData( optimal_gap, optimal_gap / data.best_ub, _options.MAX_ITERATIONS, - data.time_benders, + data.benders_time, data.timer_master, data.subproblems_walltime, data.cumulative_number_of_subproblem_solved + @@ -806,7 +806,7 @@ void BendersBase::UpdateMaxNumberIterationResumeMode( } } -double BendersBase::execution_time() const { return _data.time_benders; } +double BendersBase::execution_time() const { return _data.benders_time; } LogData BendersBase::GetBestIterationData() const { return best_iteration_data; } @@ -852,7 +852,7 @@ void BendersBase::ClearCurrentIterationCutTrace() const { } void BendersBase::EndWritingInOutputFile() const { _writer->updateEndTime(); - _writer->write_duration(_data.time_benders); + _writer->write_duration(_data.benders_time); SaveSolutionInOutputFile(); } double BendersBase::GetBendersTime() const { return benders_timer.elapsed(); } diff --git a/src/cpp/benders/benders_core/include/BendersStructsDatas.h b/src/cpp/benders/benders_core/include/BendersStructsDatas.h index 6afa32eba..96069dfd9 100644 --- a/src/cpp/benders/benders_core/include/BendersStructsDatas.h +++ b/src/cpp/benders/benders_core/include/BendersStructsDatas.h @@ -28,7 +28,7 @@ struct CurrentIterationData { Point max_invest; int nsubproblem; int master_status; - double time_benders; + double benders_time; StoppingCriterion stopping_criterion; bool is_in_initial_relaxation; int number_of_subproblem_solved; diff --git a/src/cpp/benders/benders_mpi/BendersMPI.cpp b/src/cpp/benders/benders_mpi/BendersMPI.cpp index 568de652b..b5ed14841 100644 --- a/src/cpp/benders/benders_mpi/BendersMPI.cpp +++ b/src/cpp/benders/benders_mpi/BendersMPI.cpp @@ -200,7 +200,7 @@ void BendersMpi::step_4_update_best_solution(int rank) { _logger->log_at_iteration_end(bendersDataToLogData(_data)); UpdateTrace(); - _data.time_benders = GetBendersTime(); + _data.benders_time = GetBendersTime(); _data.stop = ShouldBendersStop(); } } diff --git a/src/cpp/benders/benders_sequential/BendersSequential.cpp b/src/cpp/benders/benders_sequential/BendersSequential.cpp index 5fd2aa1ab..791f5ad85 100644 --- a/src/cpp/benders/benders_sequential/BendersSequential.cpp +++ b/src/cpp/benders/benders_sequential/BendersSequential.cpp @@ -117,7 +117,7 @@ void BendersSequential::Run() { UpdateTrace(); set_timer_master(timer_master.elapsed()); - _data.time_benders = GetBendersTime(); + _data.benders_time = GetBendersTime(); _data.stop = ShouldBendersStop(); SaveCurrentBendersData(); } diff --git a/src/cpp/benders/logger/MathLogger.cpp b/src/cpp/benders/logger/MathLogger.cpp index e4dfe5c53..087ef255a 100644 --- a/src/cpp/benders/logger/MathLogger.cpp +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -41,7 +41,7 @@ void PrintData(LogDestination& log_destination, log_destination << data.cumulative_number_of_subproblem_solved; } - log_destination << std::setprecision(2) << data.time_benders; + log_destination << std::setprecision(2) << data.benders_time; log_destination << std::setprecision(2) << data.timer_master; log_destination << std::setprecision(2) << data.subproblems_walltime; @@ -49,7 +49,7 @@ void PrintData(LogDestination& log_destination, log_destination << std::setprecision(2) << data.subproblems_cumulative_cputime; log_destination << std::setprecision(2) - << getDurationNotSolving(data.time_benders, + << getDurationNotSolving(data.benders_time, data.timer_master, data.subproblems_walltime); } From bb8505930bb8487f019a5e246abb93da9b75c181 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Thu, 4 Jan 2024 10:55:18 +0100 Subject: [PATCH 64/68] fix iteration time --- src/cpp/benders/benders_by_batch/BendersByBatch.cpp | 2 ++ src/cpp/benders/benders_core/include/BendersStructsDatas.h | 1 + src/cpp/benders/benders_mpi/BendersMPI.cpp | 2 ++ src/cpp/benders/benders_sequential/BendersSequential.cpp | 2 ++ src/cpp/benders/logger/MathLogger.cpp | 2 +- 5 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/cpp/benders/benders_by_batch/BendersByBatch.cpp b/src/cpp/benders/benders_by_batch/BendersByBatch.cpp index 55d297e46..df46c6b6c 100644 --- a/src/cpp/benders/benders_by_batch/BendersByBatch.cpp +++ b/src/cpp/benders/benders_by_batch/BendersByBatch.cpp @@ -113,7 +113,9 @@ void BendersByBatch::MasterLoop() { random_batch_permutation_.size(), rank_0); SeparationLoop(); if (Rank() == rank_0) { + _data.iteration_time = -_data.benders_time; _data.benders_time = GetBendersTime(); + _data.iteration_time += _data.benders_time; _data.stop = ShouldBendersStop(); } BroadCast(_data.stop, rank_0); diff --git a/src/cpp/benders/benders_core/include/BendersStructsDatas.h b/src/cpp/benders/benders_core/include/BendersStructsDatas.h index 96069dfd9..d37a57be8 100644 --- a/src/cpp/benders/benders_core/include/BendersStructsDatas.h +++ b/src/cpp/benders/benders_core/include/BendersStructsDatas.h @@ -15,6 +15,7 @@ struct CurrentIterationData { double best_ub; int deletedcut; int it; + double iteration_time; bool stop; double overall_subpb_cost_under_approx; std::vector single_subpb_costs_under_approx; diff --git a/src/cpp/benders/benders_mpi/BendersMPI.cpp b/src/cpp/benders/benders_mpi/BendersMPI.cpp index b5ed14841..56cd05a6c 100644 --- a/src/cpp/benders/benders_mpi/BendersMPI.cpp +++ b/src/cpp/benders/benders_mpi/BendersMPI.cpp @@ -200,7 +200,9 @@ void BendersMpi::step_4_update_best_solution(int rank) { _logger->log_at_iteration_end(bendersDataToLogData(_data)); UpdateTrace(); + _data.iteration_time = -_data.benders_time; _data.benders_time = GetBendersTime(); + _data.iteration_time += _data.benders_time; _data.stop = ShouldBendersStop(); } } diff --git a/src/cpp/benders/benders_sequential/BendersSequential.cpp b/src/cpp/benders/benders_sequential/BendersSequential.cpp index 791f5ad85..cdace96c3 100644 --- a/src/cpp/benders/benders_sequential/BendersSequential.cpp +++ b/src/cpp/benders/benders_sequential/BendersSequential.cpp @@ -117,7 +117,9 @@ void BendersSequential::Run() { UpdateTrace(); set_timer_master(timer_master.elapsed()); + _data.iteration_time = -_data.benders_time; _data.benders_time = GetBendersTime(); + _data.iteration_time += _data.benders_time; _data.stop = ShouldBendersStop(); SaveCurrentBendersData(); } diff --git a/src/cpp/benders/logger/MathLogger.cpp b/src/cpp/benders/logger/MathLogger.cpp index 087ef255a..4d57254b5 100644 --- a/src/cpp/benders/logger/MathLogger.cpp +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -49,7 +49,7 @@ void PrintData(LogDestination& log_destination, log_destination << std::setprecision(2) << data.subproblems_cumulative_cputime; log_destination << std::setprecision(2) - << getDurationNotSolving(data.benders_time, + << getDurationNotSolving(data.iteration_time, data.timer_master, data.subproblems_walltime); } From fc64ca2a9533e05bbaa4454b7062bcc90d046d3b Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Thu, 4 Jan 2024 10:58:37 +0100 Subject: [PATCH 65/68] fix --- tests/cpp/logger/logger_test.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/cpp/logger/logger_test.cpp b/tests/cpp/logger/logger_test.cpp index 50fafc631..7b8ad70ab 100644 --- a/tests/cpp/logger/logger_test.cpp +++ b/tests/cpp/logger/logger_test.cpp @@ -903,12 +903,12 @@ TEST(MathLoggerBendersByBatchTest, DataInFileLong) { data.max_simplexiter = 30; data.number_of_subproblem_solved = 657; data.cumulative_number_of_subproblem_solved = 1387; - data.elapsed_time = 1000; + data.benders_time = 1000; data.timer_master = 10; data.subproblems_walltime = 16; data.subproblems_cumulative_cputime = 160; auto time_not_solving = - data.elapsed_time - data.timer_master - data.subproblems_walltime; + data.benders_time - data.timer_master - data.subproblems_walltime; std::ostringstream expected_msg; expected_msg << std::left << std::setw(width) << data.it; @@ -922,7 +922,7 @@ TEST(MathLoggerBendersByBatchTest, DataInFileLong) { expected_msg << std::left << std::setw(width) << data.cumulative_number_of_subproblem_solved; expected_msg << std::left << std::setw(width) << std::setprecision(2) - << data.elapsed_time; + << data.benders_time; expected_msg << std::left << std::setw(width) << std::setprecision(2) << data.timer_master; expected_msg << std::left << std::setw(width) << std::setprecision(2) @@ -954,12 +954,12 @@ TEST(MathLoggerBendersByBatchTest, DataInStdOutShort) { data.max_simplexiter = 30; data.number_of_subproblem_solved = 657; data.cumulative_number_of_subproblem_solved = 1387; - data.elapsed_time = 1000; + data.benders_time = 1000; data.timer_master = 10; data.subproblems_walltime = 16; data.subproblems_cumulative_cputime = 160; auto time_not_solving = - data.elapsed_time - data.timer_master - data.subproblems_walltime; + data.benders_time - data.timer_master - data.subproblems_walltime; std::ostringstream expected_msg; expected_msg << std::left << std::setw(width) << data.it; @@ -971,7 +971,7 @@ TEST(MathLoggerBendersByBatchTest, DataInStdOutShort) { << data.number_of_subproblem_solved; expected_msg << std::left << std::setw(width) << std::setprecision(2) - << data.elapsed_time; + << data.benders_time; expected_msg << std::left << std::setw(width) << std::setprecision(2) << data.timer_master; expected_msg << std::left << std::setw(width) << std::setprecision(2) @@ -1000,12 +1000,12 @@ TEST(MathLoggerBendersBaseTest, DataInFileLong) { data.max_simplexiter = 30; data.number_of_subproblem_solved = 657; data.cumulative_number_of_subproblem_solved = 1387; - data.elapsed_time = 1000; + data.benders_time = 1000; data.timer_master = 10; data.subproblems_walltime = 16; data.subproblems_cumulative_cputime = 160; auto time_not_solving = - data.elapsed_time - data.timer_master - data.subproblems_walltime; + data.benders_time - data.timer_master - data.subproblems_walltime; std::ostringstream expected_msg; expected_msg << std::left << std::setw(width) << data.it; @@ -1028,7 +1028,7 @@ TEST(MathLoggerBendersBaseTest, DataInFileLong) { expected_msg << std::left << std::setw(width) << data.cumulative_number_of_subproblem_solved; expected_msg << std::left << std::setw(width) << std::setprecision(2) - << data.elapsed_time; + << data.benders_time; expected_msg << std::left << std::setw(width) << std::setprecision(2) << data.timer_master; expected_msg << std::left << std::setw(width) << std::setprecision(2) @@ -1060,12 +1060,12 @@ TEST(MathLoggerBendersBaseTest, DataInStdOutShort) { data.max_simplexiter = 30; data.number_of_subproblem_solved = 657; data.cumulative_number_of_subproblem_solved = 1387; - data.elapsed_time = 1000; + data.benders_time = 1000; data.timer_master = 10; data.subproblems_walltime = 16; data.subproblems_cumulative_cputime = 160; auto time_not_solving = - data.elapsed_time - data.timer_master - data.subproblems_walltime; + data.benders_time - data.timer_master - data.subproblems_walltime; std::ostringstream expected_msg; expected_msg << std::left << std::setw(width) << data.it; @@ -1085,7 +1085,7 @@ TEST(MathLoggerBendersBaseTest, DataInStdOutShort) { expected_msg << std::left << std::setw(width) << data.max_simplexiter; expected_msg << std::left << std::setw(width) << std::setprecision(2) - << data.elapsed_time; + << data.benders_time; expected_msg << std::left << std::setw(width) << std::setprecision(2) << data.timer_master; expected_msg << std::left << std::setw(width) << std::setprecision(2) From 434222f74cf4058b82dd4135e5c8c4790f62ea9d Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Thu, 4 Jan 2024 11:16:15 +0100 Subject: [PATCH 66/68] fix test --- tests/cpp/logger/logger_test.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/cpp/logger/logger_test.cpp b/tests/cpp/logger/logger_test.cpp index 7b8ad70ab..c97b7358a 100644 --- a/tests/cpp/logger/logger_test.cpp +++ b/tests/cpp/logger/logger_test.cpp @@ -903,12 +903,12 @@ TEST(MathLoggerBendersByBatchTest, DataInFileLong) { data.max_simplexiter = 30; data.number_of_subproblem_solved = 657; data.cumulative_number_of_subproblem_solved = 1387; - data.benders_time = 1000; + data.iteration_time = 1000; data.timer_master = 10; data.subproblems_walltime = 16; data.subproblems_cumulative_cputime = 160; auto time_not_solving = - data.benders_time - data.timer_master - data.subproblems_walltime; + data.iteration_time - data.timer_master - data.subproblems_walltime; std::ostringstream expected_msg; expected_msg << std::left << std::setw(width) << data.it; @@ -922,7 +922,7 @@ TEST(MathLoggerBendersByBatchTest, DataInFileLong) { expected_msg << std::left << std::setw(width) << data.cumulative_number_of_subproblem_solved; expected_msg << std::left << std::setw(width) << std::setprecision(2) - << data.benders_time; + << data.iteration_time; expected_msg << std::left << std::setw(width) << std::setprecision(2) << data.timer_master; expected_msg << std::left << std::setw(width) << std::setprecision(2) @@ -954,12 +954,12 @@ TEST(MathLoggerBendersByBatchTest, DataInStdOutShort) { data.max_simplexiter = 30; data.number_of_subproblem_solved = 657; data.cumulative_number_of_subproblem_solved = 1387; - data.benders_time = 1000; + data.iteration_time = 1000; data.timer_master = 10; data.subproblems_walltime = 16; data.subproblems_cumulative_cputime = 160; auto time_not_solving = - data.benders_time - data.timer_master - data.subproblems_walltime; + data.iteration_time - data.timer_master - data.subproblems_walltime; std::ostringstream expected_msg; expected_msg << std::left << std::setw(width) << data.it; @@ -971,7 +971,7 @@ TEST(MathLoggerBendersByBatchTest, DataInStdOutShort) { << data.number_of_subproblem_solved; expected_msg << std::left << std::setw(width) << std::setprecision(2) - << data.benders_time; + << data.iteration_time; expected_msg << std::left << std::setw(width) << std::setprecision(2) << data.timer_master; expected_msg << std::left << std::setw(width) << std::setprecision(2) @@ -1000,12 +1000,12 @@ TEST(MathLoggerBendersBaseTest, DataInFileLong) { data.max_simplexiter = 30; data.number_of_subproblem_solved = 657; data.cumulative_number_of_subproblem_solved = 1387; - data.benders_time = 1000; + data.iteration_time = 1000; data.timer_master = 10; data.subproblems_walltime = 16; data.subproblems_cumulative_cputime = 160; auto time_not_solving = - data.benders_time - data.timer_master - data.subproblems_walltime; + data.iteration_time - data.timer_master - data.subproblems_walltime; std::ostringstream expected_msg; expected_msg << std::left << std::setw(width) << data.it; @@ -1028,7 +1028,7 @@ TEST(MathLoggerBendersBaseTest, DataInFileLong) { expected_msg << std::left << std::setw(width) << data.cumulative_number_of_subproblem_solved; expected_msg << std::left << std::setw(width) << std::setprecision(2) - << data.benders_time; + << data.iteration_time; expected_msg << std::left << std::setw(width) << std::setprecision(2) << data.timer_master; expected_msg << std::left << std::setw(width) << std::setprecision(2) @@ -1060,12 +1060,12 @@ TEST(MathLoggerBendersBaseTest, DataInStdOutShort) { data.max_simplexiter = 30; data.number_of_subproblem_solved = 657; data.cumulative_number_of_subproblem_solved = 1387; - data.benders_time = 1000; + data.iteration_time = 1000; data.timer_master = 10; data.subproblems_walltime = 16; data.subproblems_cumulative_cputime = 160; auto time_not_solving = - data.benders_time - data.timer_master - data.subproblems_walltime; + data.iteration_time - data.timer_master - data.subproblems_walltime; std::ostringstream expected_msg; expected_msg << std::left << std::setw(width) << data.it; @@ -1085,7 +1085,7 @@ TEST(MathLoggerBendersBaseTest, DataInStdOutShort) { expected_msg << std::left << std::setw(width) << data.max_simplexiter; expected_msg << std::left << std::setw(width) << std::setprecision(2) - << data.benders_time; + << data.iteration_time; expected_msg << std::left << std::setw(width) << std::setprecision(2) << data.timer_master; expected_msg << std::left << std::setw(width) << std::setprecision(2) From fdbfaa2920e5547b2a352454c365fdc9aff435be Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Thu, 4 Jan 2024 11:17:09 +0100 Subject: [PATCH 67/68] fix iteration_time --- src/cpp/benders/logger/MathLogger.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cpp/benders/logger/MathLogger.cpp b/src/cpp/benders/logger/MathLogger.cpp index 4d57254b5..c5410cd3c 100644 --- a/src/cpp/benders/logger/MathLogger.cpp +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -41,7 +41,7 @@ void PrintData(LogDestination& log_destination, log_destination << data.cumulative_number_of_subproblem_solved; } - log_destination << std::setprecision(2) << data.benders_time; + log_destination << std::setprecision(2) << data.iteration_time; log_destination << std::setprecision(2) << data.timer_master; log_destination << std::setprecision(2) << data.subproblems_walltime; From c8ff8341db35b939aac5b541c717a1078b893e44 Mon Sep 17 00:00:00 2001 From: Thomas Bittar Date: Thu, 4 Jan 2024 12:59:27 +0100 Subject: [PATCH 68/68] Update docs --- .../get-started/settings-definition.md | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/docs/user-guide/get-started/settings-definition.md b/docs/user-guide/get-started/settings-definition.md index 8fc44c2c7..dd3de4aed 100644 --- a/docs/user-guide/get-started/settings-definition.md +++ b/docs/user-guide/get-started/settings-definition.md @@ -16,20 +16,12 @@ The following section lists the configurable parameters. If the user does not sp |[`master`](#master) | `integer` | Resolution mode of the master problem | |[`yearly-weights`](#yearly-weights) | `None` | Path of the Monte-Carlo weights file | |[`solver`](#solver) | `Cbc` | Name of the solver | -|[`log_level`](#log_level) | `0` | logs severity see tab below | +|[`log_level`](#log_level) | `0` | Logs severity | |[`additional-constraints`](#additional-constraints) | `None` | Path of the additional constraints file | |[`separation_parameter`](#separation_parameter) | `0.5` | Step size for the in-out separation | |[`relaxed_optimality_gap`](#relaxed_optimality_gap) | `1e-5` | Threshold to switch from relaxed to integer master | |[`batch_size`](#batch_size) | `0` | Number of subproblems per batch | -log display according to log_level - **** | **Operational** | **Benders** | **Solver** --------------|------------------|-------------|------------ - **File** | always | always | 2 - **Console** | 0 | > 0 | never - - - The format is a standard `.ini` and should follow this template: ```ini uc_type = expansion_fast @@ -213,13 +205,21 @@ To use another solver, you have to build the package with the chosen solver, ple ### `log_level` -Positive integer, specifying the `solver`'s log severity. Default value: `0`. +Possible values :`{0, 1, 2}`, specifying the `solver`'s log severity. Default value: `0`. + +Logs can be printed both in the console and in a file. There are 3 types of logs: + +- **Operational**: Displays progress information for the investment on candidates and costs, +- **Benders**: Displays information on the progress of the Benders algorithm, +- **Solver**: Logs of the solver called for the resolution of each master or subproblem. -For now two log levels are available: +The table below details the behavior depending on the `log_level`. - - If `log_level = 0`: basic solver logs are printed. +| | **Operational** | **Benders** | **Solver** +|-------------|------------------------------|-------------|------------ +| **File** | Always `(reportbenders.txt)` | Always `(benders_solver.log)` | 2 `(solver_log_proc_.txt)` +| **Console** | 0 | >= 1 | Never - - If `log_level > 0`: full logs are printed simultaneously in standard output and in `benders.log` file located in the `lp` folder. ### `additional-constraints`