From 3423dfc4e50af0f0e48cfa510c784b4a0f204c65 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Mon, 20 Nov 2023 12:59:22 +0100 Subject: [PATCH 01/32] 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/32] 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/32] 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/32] 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/32] 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/32] 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/32] 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/32] 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/32] 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/32] 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/32] 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/32] 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/32] 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/32] 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/32] 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/32] 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/32] 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 94029640aaad83134f358bb24da8966a619e9e44 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Thu, 7 Dec 2023 12:24:56 +0100 Subject: [PATCH 18/32] @tbittar remarks --- src/cpp/benders/benders_mpi/BendersMPI.cpp | 1 - src/cpp/benders/factories/include/LoggerFactories.h | 10 +++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/cpp/benders/benders_mpi/BendersMPI.cpp b/src/cpp/benders/benders_mpi/BendersMPI.cpp index a2cd043ad..69bdf4222 100644 --- a/src/cpp/benders/benders_mpi/BendersMPI.cpp +++ b/src/cpp/benders/benders_mpi/BendersMPI.cpp @@ -228,7 +228,6 @@ 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 7cbaa10af..aa03b8d35 100644 --- a/src/cpp/benders/factories/include/LoggerFactories.h +++ b/src/cpp/benders/factories/include/LoggerFactories.h @@ -36,7 +36,7 @@ class FileAndStdoutLoggerFactory { class MathLoggerFactory { private: - MathLoggerDriver math_Logger_driver; + MathLoggerDriver math_logger_driver; public: explicit MathLoggerFactory( @@ -45,18 +45,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 From 6871cd132e6f7c128c789fadb7dc61b929a42e01 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Thu, 7 Dec 2023 13:02:51 +0100 Subject: [PATCH 19/32] split behaviour and data holders --- .../benders_core/include/BendersMathLogger.h | 33 +++++--- src/cpp/benders/factories/BendersFactory.cpp | 2 +- src/cpp/benders/logger/MathLogger.cpp | 81 +++++++++---------- 3 files changed, 57 insertions(+), 59 deletions(-) diff --git a/src/cpp/benders/benders_core/include/BendersMathLogger.h b/src/cpp/benders/benders_core/include/BendersMathLogger.h index 2674d90da..7bab3c0a6 100644 --- a/src/cpp/benders/benders_core/include/BendersMathLogger.h +++ b/src/cpp/benders/benders_core/include/BendersMathLogger.h @@ -35,12 +35,6 @@ 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); @@ -53,13 +47,26 @@ std::ostream& LogDestination::operator<<(const T& obj) { return (*stream_) << obj; } -struct MathLogger { +struct MathLoggerBehaviour { + void write_header() { + setHeadersList(); + for (const auto& header : Headers()) { + LogsDestination() << header; + } + LogsDestination() << std::endl; + } + virtual void Print(const CurrentIterationData& data) = 0; + virtual std::list Headers() const = 0; + virtual LogDestination& LogsDestination() = 0; + virtual void setHeadersList() = 0; +}; + +struct MathLogger : public MathLoggerBehaviour { 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_; } - virtual LogDestination& TheLogDestination() { return log_destination_; } + std::list Headers() const override { return headers_; } + virtual LogDestination& LogsDestination() { return log_destination_; } virtual void setHeadersList() = 0; protected: @@ -84,7 +91,7 @@ struct MathLoggerBendersByBatch : public MathLogger { void setHeadersList() override; }; -class MathLoggerImplementation : public MathLogger { +class MathLoggerImplementation : public MathLoggerBehaviour { public: explicit MathLoggerImplementation(const BENDERSMETHOD& method, std::ostream* stream) { @@ -111,8 +118,8 @@ class MathLoggerImplementation : public MathLogger { std::list Headers() const override { return implementation_->Headers(); } - virtual LogDestination& TheLogDestination() { - return implementation_->TheLogDestination(); + virtual LogDestination& LogsDestination() { + return implementation_->LogsDestination(); } private: diff --git a/src/cpp/benders/factories/BendersFactory.cpp b/src/cpp/benders/factories/BendersFactory.cpp index 2266be0b4..e6b21d655 100644 --- a/src/cpp/benders/factories/BendersFactory.cpp +++ b/src/cpp/benders/factories/BendersFactory.cpp @@ -36,7 +36,7 @@ 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; diff --git a/src/cpp/benders/logger/MathLogger.cpp b/src/cpp/benders/logger/MathLogger.cpp index 4fb2f2001..a88feb784 100644 --- a/src/cpp/benders/logger/MathLogger.cpp +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -14,48 +14,40 @@ void MathLogger::setHeadersList(const std::list& headers) { 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; + LogsDestination() << Indent(10) << data.it; if (data.lb == -1e20) - TheLogDestination() << Indent(20) << "-INF"; + LogsDestination() << Indent(20) << "-INF"; else - TheLogDestination() << Indent(20) << std::scientific - << std::setprecision(10) << data.lb; + LogsDestination() << Indent(20) << std::scientific << std::setprecision(10) + << data.lb; if (data.ub == +1e20) - TheLogDestination() << Indent(20) << "+INF"; + LogsDestination() << Indent(20) << "+INF"; else - TheLogDestination() << Indent(20) << std::scientific - << std::setprecision(10) << data.ub; + LogsDestination() << Indent(20) << std::scientific << std::setprecision(10) + << data.ub; if (data.best_ub == +1e20) - TheLogDestination() << Indent(20) << "+INF"; + LogsDestination() << 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; + LogsDestination() << Indent(20) << std::scientific << std::setprecision(10) + << data.best_ub; + LogsDestination() << Indent(15) << std::scientific << std::setprecision(2) + << data.best_ub - data.lb; - TheLogDestination() << Indent(15) << std::scientific << std::setprecision(2) - << (data.best_ub - data.lb) / data.best_ub; + LogsDestination() << 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; + LogsDestination() << Indent(15) << data.min_simplexiter; + LogsDestination() << 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() << Indent(15) << data.deletedcut; + LogsDestination() << Indent(15) << std::setprecision(2) << data.timer_master; + LogsDestination() << Indent(15) << std::setprecision(2) + << data.subproblems_cputime; + LogsDestination() << Indent(15) << std::setprecision(2) + << data.subproblems_walltime; - TheLogDestination() << std::endl; + LogsDestination() << std::endl; } void MathLoggerBendersByBatch::setHeadersList() { @@ -65,25 +57,24 @@ void MathLoggerBendersByBatch::setHeadersList() { } void MathLoggerBendersByBatch::Print(const CurrentIterationData& data) { - TheLogDestination() << Indent(10) << data.it; + LogsDestination() << Indent(10) << data.it; if (data.lb == -1e20) - TheLogDestination() << Indent(20) << "-INF"; + LogsDestination() << Indent(20) << "-INF"; else - TheLogDestination() << Indent(20) << std::scientific - << std::setprecision(10) << data.lb; + LogsDestination() << Indent(20) << std::scientific << std::setprecision(10) + << data.lb; - TheLogDestination() << Indent(15) << data.min_simplexiter; - TheLogDestination() << Indent(15) << data.max_simplexiter; + LogsDestination() << Indent(15) << data.min_simplexiter; + LogsDestination() << 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_cumulative_cputime; - TheLogDestination() << Indent(15) << std::setprecision(2) - << data.subproblems_walltime; + // LogsDestination() << Indent(15) << data.deletedcut; + LogsDestination() << Indent(15) << std::setprecision(2) << data.timer_master; + LogsDestination() << Indent(15) << std::setprecision(2) + << data.subproblems_cumulative_cputime; + LogsDestination() << Indent(15) << std::setprecision(2) + << data.subproblems_walltime; - TheLogDestination() << std::endl; + LogsDestination() << std::endl; } MathLoggerFile::MathLoggerFile(const BENDERSMETHOD& method, From bcc6c24653a23539c95c15e5a074296575c4e0bc Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Thu, 7 Dec 2023 13:08:41 +0100 Subject: [PATCH 20/32] rename data header --- src/cpp/benders/benders_core/include/BendersMathLogger.h | 2 +- src/cpp/benders/logger/MathLogger.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cpp/benders/benders_core/include/BendersMathLogger.h b/src/cpp/benders/benders_core/include/BendersMathLogger.h index 7bab3c0a6..c121bbf47 100644 --- a/src/cpp/benders/benders_core/include/BendersMathLogger.h +++ b/src/cpp/benders/benders_core/include/BendersMathLogger.h @@ -10,7 +10,7 @@ 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 ITERATION = Indent(10) + "ITERATION"; const std::string LB = Indent(20) + "LB"; const std::string UB = Indent(20) + "UB"; const std::string BESTUB = Indent(20) + "BESTUB"; diff --git a/src/cpp/benders/logger/MathLogger.cpp b/src/cpp/benders/logger/MathLogger.cpp index a88feb784..427a25d7a 100644 --- a/src/cpp/benders/logger/MathLogger.cpp +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -4,8 +4,8 @@ #include void MathLoggerBase::setHeadersList() { - MathLogger::setHeadersList({ITE, LB, UB, BESTUB, ABSOLUTE_GAP, RELATIVE_GAP, - MINSIMPLEX, MAXSIMPLEX, TIMEMASTER, + MathLogger::setHeadersList({ITERATION, LB, UB, BESTUB, ABSOLUTE_GAP, + RELATIVE_GAP, MINSIMPLEX, MAXSIMPLEX, TIMEMASTER, SUB_PROBLEMS_TIME_CPU, SUB_PROBLEMS_TIME_WALL}); } @@ -51,7 +51,7 @@ void MathLoggerBase::Print(const CurrentIterationData& data) { } void MathLoggerBendersByBatch::setHeadersList() { - MathLogger::setHeadersList({ITE, LB, MINSIMPLEX, MAXSIMPLEX, TIMEMASTER, + MathLogger::setHeadersList({ITERATION, LB, MINSIMPLEX, MAXSIMPLEX, TIMEMASTER, SUB_PROBLEMS_TIME_CPU, SUB_PROBLEMS_TIME_WALL, TIME_NOT_DOING_MASTER_OR_SUB_PROBLEMS_WALL}); } From 7edbb4ba51aaf1dfdabd7b15de2e13b220352d24 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Thu, 7 Dec 2023 15:22:11 +0100 Subject: [PATCH 21/32] add "other time" --- src/cpp/benders/logger/MathLogger.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/cpp/benders/logger/MathLogger.cpp b/src/cpp/benders/logger/MathLogger.cpp index 427a25d7a..b354b20e2 100644 --- a/src/cpp/benders/logger/MathLogger.cpp +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -3,10 +3,16 @@ #include #include +double getDurationNotDoingMasterOrSubproblems(double benders, double master, + double subproblems) { + return benders - master - subproblems; +} + void MathLoggerBase::setHeadersList() { MathLogger::setHeadersList({ITERATION, LB, UB, BESTUB, ABSOLUTE_GAP, RELATIVE_GAP, MINSIMPLEX, MAXSIMPLEX, TIMEMASTER, - SUB_PROBLEMS_TIME_CPU, SUB_PROBLEMS_TIME_WALL}); + SUB_PROBLEMS_TIME_CPU, SUB_PROBLEMS_TIME_WALL, + TIME_NOT_DOING_MASTER_OR_SUB_PROBLEMS_WALL}); } void MathLogger::setHeadersList(const std::list& headers) { @@ -46,6 +52,10 @@ void MathLoggerBase::Print(const CurrentIterationData& data) { << data.subproblems_cputime; LogsDestination() << Indent(15) << std::setprecision(2) << data.subproblems_walltime; + LogsDestination() << Indent(15) << std::setprecision(2) + << getDurationNotDoingMasterOrSubproblems( + data.elapsed_time, data.timer_master, + data.subproblems_walltime); LogsDestination() << std::endl; } @@ -73,6 +83,10 @@ void MathLoggerBendersByBatch::Print(const CurrentIterationData& data) { << data.subproblems_cumulative_cputime; LogsDestination() << Indent(15) << std::setprecision(2) << data.subproblems_walltime; + LogsDestination() << Indent(15) << std::setprecision(2) + << getDurationNotDoingMasterOrSubproblems( + data.elapsed_time, data.timer_master, + data.subproblems_walltime); LogsDestination() << std::endl; } From 37baeb13b5841b2770315ba88f27f4352ec0f11e Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Thu, 7 Dec 2023 15:42:53 +0100 Subject: [PATCH 22/32] align columns --- .../benders_core/include/BendersMathLogger.h | 2 +- src/cpp/benders/logger/MathLogger.cpp | 62 ++++++++++--------- 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/src/cpp/benders/benders_core/include/BendersMathLogger.h b/src/cpp/benders/benders_core/include/BendersMathLogger.h index c121bbf47..bef960a55 100644 --- a/src/cpp/benders/benders_core/include/BendersMathLogger.h +++ b/src/cpp/benders/benders_core/include/BendersMathLogger.h @@ -44,7 +44,7 @@ class LogDestination { template std::ostream& LogDestination::operator<<(const T& obj) { // write obj to stream - return (*stream_) << obj; + return (*stream_) << std::left << obj; } struct MathLoggerBehaviour { diff --git a/src/cpp/benders/logger/MathLogger.cpp b/src/cpp/benders/logger/MathLogger.cpp index b354b20e2..74033676f 100644 --- a/src/cpp/benders/logger/MathLogger.cpp +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -21,38 +21,39 @@ void MathLogger::setHeadersList(const std::list& headers) { } void MathLoggerBase::Print(const CurrentIterationData& data) { - LogsDestination() << Indent(10) << data.it; + LogsDestination() << std::setw(10) << data.it; if (data.lb == -1e20) - LogsDestination() << Indent(20) << "-INF"; + LogsDestination() << std::setw(20) << "-INF"; else - LogsDestination() << Indent(20) << std::scientific << std::setprecision(10) - << data.lb; + LogsDestination() << std::setw(20) << std::scientific + << std::setprecision(10) << data.lb; if (data.ub == +1e20) - LogsDestination() << Indent(20) << "+INF"; + LogsDestination() << std::setw(20) << "+INF"; else - LogsDestination() << Indent(20) << std::scientific << std::setprecision(10) - << data.ub; + LogsDestination() << std::setw(20) << std::scientific + << std::setprecision(10) << data.ub; if (data.best_ub == +1e20) - LogsDestination() << Indent(20) << "+INF"; + LogsDestination() << std::setw(20) << "+INF"; else - LogsDestination() << Indent(20) << std::scientific << std::setprecision(10) - << data.best_ub; - LogsDestination() << Indent(15) << std::scientific << std::setprecision(2) + LogsDestination() << std::setw(20) << std::scientific + << std::setprecision(10) << data.best_ub; + LogsDestination() << std::setw(15) << std::scientific << std::setprecision(2) << data.best_ub - data.lb; - LogsDestination() << Indent(15) << std::scientific << std::setprecision(2) + LogsDestination() << std::setw(15) << std::scientific << std::setprecision(2) << (data.best_ub - data.lb) / data.best_ub; - LogsDestination() << Indent(15) << data.min_simplexiter; - LogsDestination() << Indent(15) << data.max_simplexiter; + LogsDestination() << std::setw(15) << data.min_simplexiter; + LogsDestination() << std::setw(15) << data.max_simplexiter; - // LogsDestination() << Indent(15) << data.deletedcut; - LogsDestination() << Indent(15) << std::setprecision(2) << data.timer_master; - LogsDestination() << Indent(15) << std::setprecision(2) + // LogsDestination() << std::setw(15) << data.deletedcut; + LogsDestination() << std::setw(15) << std::setprecision(2) + << data.timer_master; + LogsDestination() << std::setw(15) << std::setprecision(2) << data.subproblems_cputime; - LogsDestination() << Indent(15) << std::setprecision(2) + LogsDestination() << std::setw(15) << std::setprecision(2) << data.subproblems_walltime; - LogsDestination() << Indent(15) << std::setprecision(2) + LogsDestination() << std::setw(15) << std::setprecision(2) << getDurationNotDoingMasterOrSubproblems( data.elapsed_time, data.timer_master, data.subproblems_walltime); @@ -67,23 +68,24 @@ void MathLoggerBendersByBatch::setHeadersList() { } void MathLoggerBendersByBatch::Print(const CurrentIterationData& data) { - LogsDestination() << Indent(10) << data.it; + LogsDestination() << std::setw(10) << data.it; if (data.lb == -1e20) - LogsDestination() << Indent(20) << "-INF"; + LogsDestination() << std::setw(20) << "-INF"; else - LogsDestination() << Indent(20) << std::scientific << std::setprecision(10) - << data.lb; + LogsDestination() << std::setw(20) << std::scientific + << std::setprecision(10) << data.lb; - LogsDestination() << Indent(15) << data.min_simplexiter; - LogsDestination() << Indent(15) << data.max_simplexiter; + LogsDestination() << std::setw(15) << data.min_simplexiter; + LogsDestination() << std::setw(15) << data.max_simplexiter; - // LogsDestination() << Indent(15) << data.deletedcut; - LogsDestination() << Indent(15) << std::setprecision(2) << data.timer_master; - LogsDestination() << Indent(15) << std::setprecision(2) + // LogsDestination() << std::setw(15) << data.deletedcut; + LogsDestination() << std::setw(15) << std::setprecision(2) + << data.timer_master; + LogsDestination() << std::setw(15) << std::setprecision(2) << data.subproblems_cumulative_cputime; - LogsDestination() << Indent(15) << std::setprecision(2) + LogsDestination() << std::setw(15) << std::setprecision(2) << data.subproblems_walltime; - LogsDestination() << Indent(15) << std::setprecision(2) + LogsDestination() << std::setw(15) << std::setprecision(2) << getDurationNotDoingMasterOrSubproblems( data.elapsed_time, data.timer_master, data.subproblems_walltime); From 59af269f4c323e42b6bc79f5afffd013fccf1333 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Thu, 7 Dec 2023 16:00:15 +0100 Subject: [PATCH 23/32] align headers and data --- .../benders_core/include/BendersMathLogger.h | 28 ++++----- src/cpp/benders/logger/MathLogger.cpp | 62 ++++++++----------- 2 files changed, 40 insertions(+), 50 deletions(-) diff --git a/src/cpp/benders/benders_core/include/BendersMathLogger.h b/src/cpp/benders/benders_core/include/BendersMathLogger.h index bef960a55..87b0cf388 100644 --- a/src/cpp/benders/benders_core/include/BendersMathLogger.h +++ b/src/cpp/benders/benders_core/include/BendersMathLogger.h @@ -10,21 +10,19 @@ const std::string MATHLOGGERCONTEXT = "Benders"; inline std::string Indent(int size) { return std::string(size, ' '); } -const std::string ITERATION = Indent(10) + "ITERATION"; -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 ITERATION = "ITERATION"; +const std::string LB = "LB"; +const std::string UB = "UB"; +const std::string BESTUB = "BESTUB"; +const std::string ABSOLUTE_GAP = "ABSOLUTE GAP"; +const std::string RELATIVE_GAP = "RELATIVE GAP"; +const std::string MINSIMPLEX = "MINSIMPLEX"; +const std::string MAXSIMPLEX = "MAXSIMPLEX"; +const std::string TIMEMASTER = "TIMEMASTER"; +const std::string SUB_PROBLEMS_TIME_CPU = "SUB-PROBLEMS TIME (CPU)"; +const std::string SUB_PROBLEMS_TIME_WALL = "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)"; + "TIME NOT DOING MASTER OR SUB-PROBLEMS (WALL)"; class LogDestination { public: explicit LogDestination(std::ostream* stream) : stream_(stream) {} @@ -44,7 +42,7 @@ class LogDestination { template std::ostream& LogDestination::operator<<(const T& obj) { // write obj to stream - return (*stream_) << std::left << obj; + return (*stream_) << std::left << std::setw(50) << obj; } struct MathLoggerBehaviour { diff --git a/src/cpp/benders/logger/MathLogger.cpp b/src/cpp/benders/logger/MathLogger.cpp index 74033676f..c24fc7cb4 100644 --- a/src/cpp/benders/logger/MathLogger.cpp +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -21,39 +21,34 @@ void MathLogger::setHeadersList(const std::list& headers) { } void MathLoggerBase::Print(const CurrentIterationData& data) { - LogsDestination() << std::setw(10) << data.it; + LogsDestination() << data.it; if (data.lb == -1e20) - LogsDestination() << std::setw(20) << "-INF"; + LogsDestination() << "-INF"; else - LogsDestination() << std::setw(20) << std::scientific - << std::setprecision(10) << data.lb; + LogsDestination() << std::scientific << std::setprecision(10) << data.lb; if (data.ub == +1e20) - LogsDestination() << std::setw(20) << "+INF"; + LogsDestination() << "+INF"; else - LogsDestination() << std::setw(20) << std::scientific - << std::setprecision(10) << data.ub; + LogsDestination() << std::scientific << std::setprecision(10) << data.ub; if (data.best_ub == +1e20) - LogsDestination() << std::setw(20) << "+INF"; + LogsDestination() << "+INF"; else - LogsDestination() << std::setw(20) << std::scientific - << std::setprecision(10) << data.best_ub; - LogsDestination() << std::setw(15) << std::scientific << std::setprecision(2) + LogsDestination() << std::scientific << std::setprecision(10) + << data.best_ub; + LogsDestination() << std::scientific << std::setprecision(2) << data.best_ub - data.lb; - LogsDestination() << std::setw(15) << std::scientific << std::setprecision(2) + LogsDestination() << std::scientific << std::setprecision(2) << (data.best_ub - data.lb) / data.best_ub; - LogsDestination() << std::setw(15) << data.min_simplexiter; - LogsDestination() << std::setw(15) << data.max_simplexiter; + LogsDestination() << data.min_simplexiter; + LogsDestination() << data.max_simplexiter; - // LogsDestination() << std::setw(15) << data.deletedcut; - LogsDestination() << std::setw(15) << std::setprecision(2) - << data.timer_master; - LogsDestination() << std::setw(15) << std::setprecision(2) - << data.subproblems_cputime; - LogsDestination() << std::setw(15) << std::setprecision(2) - << data.subproblems_walltime; - LogsDestination() << std::setw(15) << std::setprecision(2) + // LogsDestination() << data.deletedcut; + LogsDestination() << std::setprecision(2) << data.timer_master; + LogsDestination() << std::setprecision(2) << data.subproblems_cputime; + LogsDestination() << std::setprecision(2) << data.subproblems_walltime; + LogsDestination() << std::setprecision(2) << getDurationNotDoingMasterOrSubproblems( data.elapsed_time, data.timer_master, data.subproblems_walltime); @@ -68,24 +63,21 @@ void MathLoggerBendersByBatch::setHeadersList() { } void MathLoggerBendersByBatch::Print(const CurrentIterationData& data) { - LogsDestination() << std::setw(10) << data.it; + LogsDestination() << data.it; if (data.lb == -1e20) - LogsDestination() << std::setw(20) << "-INF"; + LogsDestination() << "-INF"; else - LogsDestination() << std::setw(20) << std::scientific - << std::setprecision(10) << data.lb; + LogsDestination() << std::scientific << std::setprecision(10) << data.lb; - LogsDestination() << std::setw(15) << data.min_simplexiter; - LogsDestination() << std::setw(15) << data.max_simplexiter; + LogsDestination() << data.min_simplexiter; + LogsDestination() << data.max_simplexiter; - // LogsDestination() << std::setw(15) << data.deletedcut; - LogsDestination() << std::setw(15) << std::setprecision(2) - << data.timer_master; - LogsDestination() << std::setw(15) << std::setprecision(2) + // LogsDestination() << data.deletedcut; + LogsDestination() << std::setprecision(2) << data.timer_master; + LogsDestination() << std::setprecision(2) << data.subproblems_cumulative_cputime; - LogsDestination() << std::setw(15) << std::setprecision(2) - << data.subproblems_walltime; - LogsDestination() << std::setw(15) << std::setprecision(2) + LogsDestination() << std::setprecision(2) << data.subproblems_walltime; + LogsDestination() << std::setprecision(2) << getDurationNotDoingMasterOrSubproblems( data.elapsed_time, data.timer_master, data.subproblems_walltime); From c2a13ec74703e796a1dc43a43db31b2b9acf7283 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Thu, 7 Dec 2023 17:49:59 +0100 Subject: [PATCH 24/32] fix master time --- .../benders/benders_by_batch/BendersByBatch.cpp | 4 ++-- .../benders_core/include/BendersMathLogger.h | 1 + src/cpp/benders/benders_mpi/BendersMPI.cpp | 17 ++++++----------- .../benders/benders_mpi/include/BendersMPI.h | 2 +- src/cpp/benders/logger/MathLogger.cpp | 15 +++++++++------ 5 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/cpp/benders/benders_by_batch/BendersByBatch.cpp b/src/cpp/benders/benders_by_batch/BendersByBatch.cpp index 64690e037..713d0969d 100644 --- a/src/cpp/benders/benders_by_batch/BendersByBatch.cpp +++ b/src/cpp/benders/benders_by_batch/BendersByBatch.cpp @@ -113,7 +113,6 @@ 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); @@ -125,10 +124,11 @@ void BendersByBatch::MasterLoop() { _logger->LogSubproblemsSolvingCumulativeCpuTime( GetSubproblemsCumulativeCpuTime()); _logger->LogSubproblemsSolvingWalltime(GetSubproblemsWalltime()); - mathLoggerDriver_->Print(_data); _logger->display_message( "\\________________________________________________________________" "________"); + _data.elapsed_time = GetBendersTime(); + mathLoggerDriver_->Print(_data); } } void BendersByBatch::SeparationLoop() { diff --git a/src/cpp/benders/benders_core/include/BendersMathLogger.h b/src/cpp/benders/benders_core/include/BendersMathLogger.h index 87b0cf388..cbb049bc7 100644 --- a/src/cpp/benders/benders_core/include/BendersMathLogger.h +++ b/src/cpp/benders/benders_core/include/BendersMathLogger.h @@ -18,6 +18,7 @@ const std::string ABSOLUTE_GAP = "ABSOLUTE GAP"; const std::string RELATIVE_GAP = "RELATIVE GAP"; const std::string MINSIMPLEX = "MINSIMPLEX"; const std::string MAXSIMPLEX = "MAXSIMPLEX"; +const std::string BENDERS_TIME = "BENDERS TIME"; const std::string TIMEMASTER = "TIMEMASTER"; const std::string SUB_PROBLEMS_TIME_CPU = "SUB-PROBLEMS TIME (CPU)"; const std::string SUB_PROBLEMS_TIME_WALL = "SUB-PROBLEMS TIME (WALL)"; diff --git a/src/cpp/benders/benders_mpi/BendersMPI.cpp b/src/cpp/benders/benders_mpi/BendersMPI.cpp index 69bdf4222..aaf8dd0e8 100644 --- a/src/cpp/benders/benders_mpi/BendersMPI.cpp +++ b/src/cpp/benders/benders_mpi/BendersMPI.cpp @@ -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(); } } @@ -229,7 +226,6 @@ void BendersMpi::free() { void BendersMpi::Run() { PreRunInitialization(); while (!_data.stop) { - Timer timer_master; ++_data.it; ResetSimplexIterationsBounds(); @@ -243,17 +239,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(); } } 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/logger/MathLogger.cpp b/src/cpp/benders/logger/MathLogger.cpp index c24fc7cb4..25dfea5d1 100644 --- a/src/cpp/benders/logger/MathLogger.cpp +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -9,10 +9,10 @@ double getDurationNotDoingMasterOrSubproblems(double benders, double master, } void MathLoggerBase::setHeadersList() { - MathLogger::setHeadersList({ITERATION, LB, UB, BESTUB, ABSOLUTE_GAP, - RELATIVE_GAP, MINSIMPLEX, MAXSIMPLEX, TIMEMASTER, - SUB_PROBLEMS_TIME_CPU, SUB_PROBLEMS_TIME_WALL, - TIME_NOT_DOING_MASTER_OR_SUB_PROBLEMS_WALL}); + MathLogger::setHeadersList( + {ITERATION, LB, UB, BESTUB, ABSOLUTE_GAP, RELATIVE_GAP, MINSIMPLEX, + MAXSIMPLEX, BENDERS_TIME, TIMEMASTER, SUB_PROBLEMS_TIME_CPU, + SUB_PROBLEMS_TIME_WALL, TIME_NOT_DOING_MASTER_OR_SUB_PROBLEMS_WALL}); } void MathLogger::setHeadersList(const std::list& headers) { @@ -45,6 +45,7 @@ void MathLoggerBase::Print(const CurrentIterationData& data) { LogsDestination() << data.max_simplexiter; // LogsDestination() << data.deletedcut; + LogsDestination() << std::setprecision(2) << data.elapsed_time; LogsDestination() << std::setprecision(2) << data.timer_master; LogsDestination() << std::setprecision(2) << data.subproblems_cputime; LogsDestination() << std::setprecision(2) << data.subproblems_walltime; @@ -57,8 +58,9 @@ void MathLoggerBase::Print(const CurrentIterationData& data) { } void MathLoggerBendersByBatch::setHeadersList() { - MathLogger::setHeadersList({ITERATION, LB, MINSIMPLEX, MAXSIMPLEX, TIMEMASTER, - SUB_PROBLEMS_TIME_CPU, SUB_PROBLEMS_TIME_WALL, + MathLogger::setHeadersList({ITERATION, LB, MINSIMPLEX, MAXSIMPLEX, + BENDERS_TIME, TIMEMASTER, SUB_PROBLEMS_TIME_CPU, + SUB_PROBLEMS_TIME_WALL, TIME_NOT_DOING_MASTER_OR_SUB_PROBLEMS_WALL}); } @@ -73,6 +75,7 @@ void MathLoggerBendersByBatch::Print(const CurrentIterationData& data) { LogsDestination() << data.max_simplexiter; // LogsDestination() << data.deletedcut; + LogsDestination() << std::setprecision(2) << data.elapsed_time; LogsDestination() << std::setprecision(2) << data.timer_master; LogsDestination() << std::setprecision(2) << data.subproblems_cumulative_cputime; From d6810e64d82a4a7690de73490cf188458b01a212 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Thu, 7 Dec 2023 18:36:43 +0100 Subject: [PATCH 25/32] add CUMULATIVE nb of sub-pblm --- src/cpp/benders/benders_by_batch/BendersByBatch.cpp | 10 +++++----- src/cpp/benders/benders_core/BendersBase.cpp | 8 ++++---- src/cpp/benders/benders_core/include/BendersBase.h | 2 +- .../benders/benders_core/include/BendersMathLogger.h | 2 ++ .../benders/benders_core/include/BendersStructsDatas.h | 2 +- src/cpp/benders/benders_mpi/BendersMPI.cpp | 8 ++++---- src/cpp/benders/logger/Master.cpp | 4 ++-- src/cpp/benders/logger/MathLogger.cpp | 2 ++ src/cpp/benders/logger/User.cpp | 2 +- src/cpp/benders/logger/UserFile.cpp | 2 +- src/cpp/benders/logger/include/logger/Master.h | 2 +- src/cpp/benders/logger/include/logger/User.h | 2 +- src/cpp/benders/logger/include/logger/UserFile.h | 2 +- src/cpp/xpansion_interfaces/ILogger.h | 2 +- tests/cpp/TestDoubles/LoggerStub.h | 2 +- tests/cpp/logger/logger_test.cpp | 6 +++--- 16 files changed, 31 insertions(+), 27 deletions(-) diff --git a/src/cpp/benders/benders_by_batch/BendersByBatch.cpp b/src/cpp/benders/benders_by_batch/BendersByBatch.cpp index 713d0969d..c61af59f2 100644 --- a/src/cpp/benders/benders_by_batch/BendersByBatch.cpp +++ b/src/cpp/benders/benders_by_batch/BendersByBatch.cpp @@ -77,7 +77,7 @@ void BendersByBatch::MasterLoop() { batch_counter_ = 0; current_batch_id_ = 0; - _data.number_of_subproblem_resolved = 0; + _data.number_of_subproblem_solved = 0; cumulative_subproblems_timer_per_iter_ = 0; first_unsolved_batch_ = 0; while (!_data.stop) { @@ -118,9 +118,9 @@ void BendersByBatch::MasterLoop() { 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.number_of_subproblem_solved + + GetNumOfSubProblemsSolvedBeforeResume()); _logger->LogSubproblemsSolvingCumulativeCpuTime( GetSubproblemsCumulativeCpuTime()); _logger->LogSubproblemsSolvingWalltime(GetSubproblemsWalltime()); @@ -199,7 +199,7 @@ 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(); 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..4473dbc18 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.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.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.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.number_of_subproblem_solved + cumulative_number_of_subproblem_resolved_before_resume}; } void BendersBase::set_log_file(const std::filesystem::path &log_file) { diff --git a/src/cpp/benders/benders_core/include/BendersBase.h b/src/cpp/benders/benders_core/include/BendersBase.h index 59855fdfb..1c59f2141 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; } diff --git a/src/cpp/benders/benders_core/include/BendersMathLogger.h b/src/cpp/benders/benders_core/include/BendersMathLogger.h index cbb049bc7..4aafd3b4f 100644 --- a/src/cpp/benders/benders_core/include/BendersMathLogger.h +++ b/src/cpp/benders/benders_core/include/BendersMathLogger.h @@ -18,6 +18,8 @@ const std::string ABSOLUTE_GAP = "ABSOLUTE GAP"; const std::string RELATIVE_GAP = "RELATIVE GAP"; const std::string MINSIMPLEX = "MINSIMPLEX"; const std::string MAXSIMPLEX = "MAXSIMPLEX"; +const std::string CUMULATIVE_NUMBER_OF_SUBPROBLEM_SOLVED = + "CUMULATIVE NUMBER OF SUBPROBLEMS SOLVED"; const std::string BENDERS_TIME = "BENDERS TIME"; const std::string TIMEMASTER = "TIMEMASTER"; const std::string SUB_PROBLEMS_TIME_CPU = "SUB-PROBLEMS TIME (CPU)"; diff --git a/src/cpp/benders/benders_core/include/BendersStructsDatas.h b/src/cpp/benders/benders_core/include/BendersStructsDatas.h index 05ec3deae..9ae90eed1 100644 --- a/src/cpp/benders/benders_core/include/BendersStructsDatas.h +++ b/src/cpp/benders/benders_core/include/BendersStructsDatas.h @@ -31,7 +31,7 @@ struct CurrentIterationData { double elapsed_time; StoppingCriterion stopping_criterion; bool is_in_initial_relaxation; - int number_of_subproblem_resolved; + int number_of_subproblem_solved; int min_simplexiter; int max_simplexiter; }; diff --git a/src/cpp/benders/benders_mpi/BendersMPI.cpp b/src/cpp/benders/benders_mpi/BendersMPI.cpp index aaf8dd0e8..ef68cfcc4 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.number_of_subproblem_solved += _data.nsubproblem; + _logger->cumulative_number_of_sub_problem_solved( + _data.number_of_subproblem_solved + + GetNumOfSubProblemsSolvedBeforeResume()); } } 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 25dfea5d1..9043f5845 100644 --- a/src/cpp/benders/logger/MathLogger.cpp +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -59,6 +59,7 @@ void MathLoggerBase::Print(const CurrentIterationData& data) { void MathLoggerBendersByBatch::setHeadersList() { MathLogger::setHeadersList({ITERATION, LB, MINSIMPLEX, MAXSIMPLEX, + CUMULATIVE_NUMBER_OF_SUBPROBLEM_SOLVED, BENDERS_TIME, TIMEMASTER, SUB_PROBLEMS_TIME_CPU, SUB_PROBLEMS_TIME_WALL, TIME_NOT_DOING_MASTER_OR_SUB_PROBLEMS_WALL}); @@ -75,6 +76,7 @@ void MathLoggerBendersByBatch::Print(const CurrentIterationData& data) { LogsDestination() << data.max_simplexiter; // LogsDestination() << data.deletedcut; + LogsDestination() << data.number_of_subproblem_solved; LogsDestination() << std::setprecision(2) << data.elapsed_time; LogsDestination() << std::setprecision(2) << data.timer_master; LogsDestination() << std::setprecision(2) 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/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..5ab7287a2 100644 --- a/src/cpp/xpansion_interfaces/ILogger.h +++ b/src/cpp/xpansion_interfaces/ILogger.h @@ -91,7 +91,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/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 00e909ffcb7adf5688258c9fcdc3fdd417d7cce6 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Fri, 8 Dec 2023 12:19:03 +0100 Subject: [PATCH 26/32] add num of subproblem solved / iteration --- .../benders_by_batch/BendersByBatch.cpp | 6 ++++-- src/cpp/benders/benders_core/BendersBase.cpp | 8 ++++---- .../benders_core/include/BendersMathLogger.h | 3 ++- .../include/BendersStructsDatas.h | 1 + src/cpp/benders/benders_mpi/BendersMPI.cpp | 7 +++++-- src/cpp/benders/logger/MathLogger.cpp | 19 ++++++++++++------- 6 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/cpp/benders/benders_by_batch/BendersByBatch.cpp b/src/cpp/benders/benders_by_batch/BendersByBatch.cpp index c61af59f2..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_solved = 0; + _data.cumulative_number_of_subproblem_solved = 0; cumulative_subproblems_timer_per_iter_ = 0; first_unsolved_batch_ = 0; while (!_data.stop) { @@ -119,7 +119,7 @@ void BendersByBatch::MasterLoop() { BroadCast(batch_counter_, rank_0); SetSubproblemsCumulativeCpuTime(cumulative_subproblems_timer_per_iter_); _logger->cumulative_number_of_sub_problem_solved( - _data.number_of_subproblem_solved + + _data.cumulative_number_of_subproblem_solved + GetNumOfSubProblemsSolvedBeforeResume()); _logger->LogSubproblemsSolvingCumulativeCpuTime( GetSubproblemsCumulativeCpuTime()); @@ -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) { @@ -200,6 +201,7 @@ void BendersByBatch::SolveBatches() { std::plus(), rank_0); if (Rank() == rank_0) { _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 4473dbc18..d20b068cd 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_solved = 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_solved + + _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_solved + + _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_solved + + data.cumulative_number_of_subproblem_solved + cumulative_number_of_subproblem_resolved_before_resume}; } void BendersBase::set_log_file(const std::filesystem::path &log_file) { diff --git a/src/cpp/benders/benders_core/include/BendersMathLogger.h b/src/cpp/benders/benders_core/include/BendersMathLogger.h index 4aafd3b4f..22f71c1b2 100644 --- a/src/cpp/benders/benders_core/include/BendersMathLogger.h +++ b/src/cpp/benders/benders_core/include/BendersMathLogger.h @@ -18,10 +18,11 @@ const std::string ABSOLUTE_GAP = "ABSOLUTE GAP"; const std::string RELATIVE_GAP = "RELATIVE GAP"; const std::string MINSIMPLEX = "MINSIMPLEX"; const std::string MAXSIMPLEX = "MAXSIMPLEX"; +const std::string NUMBER_OF_SUBPROBLEM_SOLVED = "NUMBER OF SUBPROBLEMS SOLVED"; const std::string CUMULATIVE_NUMBER_OF_SUBPROBLEM_SOLVED = "CUMULATIVE NUMBER OF SUBPROBLEMS SOLVED"; const std::string BENDERS_TIME = "BENDERS TIME"; -const std::string TIMEMASTER = "TIMEMASTER"; +const std::string TIMEMASTER = "MASTER TIME"; const std::string SUB_PROBLEMS_TIME_CPU = "SUB-PROBLEMS TIME (CPU)"; const std::string SUB_PROBLEMS_TIME_WALL = "SUB-PROBLEMS TIME (WALL)"; const std::string TIME_NOT_DOING_MASTER_OR_SUB_PROBLEMS_WALL = diff --git a/src/cpp/benders/benders_core/include/BendersStructsDatas.h b/src/cpp/benders/benders_core/include/BendersStructsDatas.h index 9ae90eed1..22d0f6de9 100644 --- a/src/cpp/benders/benders_core/include/BendersStructsDatas.h +++ b/src/cpp/benders/benders_core/include/BendersStructsDatas.h @@ -32,6 +32,7 @@ struct CurrentIterationData { StoppingCriterion stopping_criterion; bool is_in_initial_relaxation; int number_of_subproblem_solved; + int cumulative_number_of_subproblem_solved; int min_simplexiter; int max_simplexiter; }; diff --git a/src/cpp/benders/benders_mpi/BendersMPI.cpp b/src/cpp/benders/benders_mpi/BendersMPI.cpp index ef68cfcc4..6c546c80b 100644 --- a/src/cpp/benders/benders_mpi/BendersMPI.cpp +++ b/src/cpp/benders/benders_mpi/BendersMPI.cpp @@ -121,9 +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_solved += _data.nsubproblem; + _data.cumulative_number_of_subproblem_solved = _data.nsubproblem; + _data.cumulative_number_of_subproblem_solved += _data.nsubproblem; _logger->cumulative_number_of_sub_problem_solved( - _data.number_of_subproblem_solved + + _data.cumulative_number_of_subproblem_solved + GetNumOfSubProblemsSolvedBeforeResume()); } } @@ -225,6 +226,7 @@ void BendersMpi::free() { */ void BendersMpi::Run() { PreRunInitialization(); + _data.number_of_subproblem_solved = _data.nsubproblem; while (!_data.stop) { ++_data.it; ResetSimplexIterationsBounds(); @@ -280,6 +282,7 @@ void BendersMpi::PreRunInitialization() { } mathLoggerDriver_->write_header(); } + void BendersMpi::launch() { build_input_map(); _world.barrier(); diff --git a/src/cpp/benders/logger/MathLogger.cpp b/src/cpp/benders/logger/MathLogger.cpp index 9043f5845..b8f85810e 100644 --- a/src/cpp/benders/logger/MathLogger.cpp +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -11,8 +11,10 @@ double getDurationNotDoingMasterOrSubproblems(double benders, double master, void MathLoggerBase::setHeadersList() { MathLogger::setHeadersList( {ITERATION, LB, UB, BESTUB, ABSOLUTE_GAP, RELATIVE_GAP, MINSIMPLEX, - MAXSIMPLEX, BENDERS_TIME, TIMEMASTER, SUB_PROBLEMS_TIME_CPU, - SUB_PROBLEMS_TIME_WALL, TIME_NOT_DOING_MASTER_OR_SUB_PROBLEMS_WALL}); + MAXSIMPLEX, NUMBER_OF_SUBPROBLEM_SOLVED, + CUMULATIVE_NUMBER_OF_SUBPROBLEM_SOLVED, BENDERS_TIME, TIMEMASTER, + SUB_PROBLEMS_TIME_CPU, SUB_PROBLEMS_TIME_WALL, + TIME_NOT_DOING_MASTER_OR_SUB_PROBLEMS_WALL}); } void MathLogger::setHeadersList(const std::list& headers) { @@ -43,6 +45,8 @@ void MathLoggerBase::Print(const CurrentIterationData& data) { LogsDestination() << data.min_simplexiter; LogsDestination() << data.max_simplexiter; + LogsDestination() << data.number_of_subproblem_solved; + LogsDestination() << data.cumulative_number_of_subproblem_solved; // LogsDestination() << data.deletedcut; LogsDestination() << std::setprecision(2) << data.elapsed_time; @@ -58,11 +62,11 @@ void MathLoggerBase::Print(const CurrentIterationData& data) { } void MathLoggerBendersByBatch::setHeadersList() { - MathLogger::setHeadersList({ITERATION, LB, MINSIMPLEX, MAXSIMPLEX, - CUMULATIVE_NUMBER_OF_SUBPROBLEM_SOLVED, - BENDERS_TIME, TIMEMASTER, SUB_PROBLEMS_TIME_CPU, - SUB_PROBLEMS_TIME_WALL, - TIME_NOT_DOING_MASTER_OR_SUB_PROBLEMS_WALL}); + MathLogger::setHeadersList( + {ITERATION, LB, MINSIMPLEX, MAXSIMPLEX, NUMBER_OF_SUBPROBLEM_SOLVED, + CUMULATIVE_NUMBER_OF_SUBPROBLEM_SOLVED, BENDERS_TIME, TIMEMASTER, + SUB_PROBLEMS_TIME_CPU, SUB_PROBLEMS_TIME_WALL, + TIME_NOT_DOING_MASTER_OR_SUB_PROBLEMS_WALL}); } void MathLoggerBendersByBatch::Print(const CurrentIterationData& data) { @@ -77,6 +81,7 @@ void MathLoggerBendersByBatch::Print(const CurrentIterationData& data) { // LogsDestination() << data.deletedcut; LogsDestination() << data.number_of_subproblem_solved; + 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) From 5d29a742fe611ab273fa5ece725b48e304f8ccfe Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Fri, 8 Dec 2023 16:22:31 +0100 Subject: [PATCH 27/32] add "expert" option for logs --- src/cpp/benders/benders_core/SimulationOptions.cpp | 1 + .../benders_core/include/SimulationOptions.hxx | 5 ++++- src/cpp/benders/benders_core/include/common.h | 1 + src/cpp/benders/factories/BendersFactory.cpp | 6 ++++-- src/cpp/benders/factories/include/LoggerFactories.h | 12 ++++++++---- 5 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/cpp/benders/benders_core/SimulationOptions.cpp b/src/cpp/benders/benders_core/SimulationOptions.cpp index d88d0cc9a..00c1c33f5 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_LOG_AT_CONSOLE = EXPERT_LOG_AT_CONSOLE; 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 3b216a185..1e08d3383 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_LOG_AT_CONSOLE, 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..578dd9419 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_LOG_AT_CONSOLE = true; }; void usage(int argc); diff --git a/src/cpp/benders/factories/BendersFactory.cpp b/src/cpp/benders/factories/BendersFactory.cpp index e6b21d655..d5aad2a4d 100644 --- a/src/cpp/benders/factories/BendersFactory.cpp +++ b/src/cpp/benders/factories/BendersFactory.cpp @@ -41,8 +41,10 @@ int RunBenders(char** argv, const std::filesystem::path& options_file, 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_LOG_AT_CONSOLE); + auto math_log_factory = MathLoggerFactory( + method, benders_options.EXPERT_LOG_AT_CONSOLE, math_logs_file); logger = logger_factory.get_logger(); math_log_driver = math_log_factory.get_logger(); diff --git a/src/cpp/benders/factories/include/LoggerFactories.h b/src/cpp/benders/factories/include/LoggerFactories.h index aa03b8d35..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; } From 474380d24b8b532c330bc941f212c267fdedf1b1 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Fri, 8 Dec 2023 16:46:38 +0100 Subject: [PATCH 28/32] update --- .../benders_core/BendersMathLogger.cpp | 54 +++++++++++++ .../benders_core/include/BendersMathLogger.h | 76 ++++++++++++------- src/cpp/benders/benders_mpi/BendersMPI.cpp | 1 - src/cpp/benders/logger/MathLogger.cpp | 30 +++++--- .../logger/include/logger/MathLogger.h | 9 ++- 5 files changed, 127 insertions(+), 43 deletions(-) diff --git a/src/cpp/benders/benders_core/BendersMathLogger.cpp b/src/cpp/benders/benders_core/BendersMathLogger.cpp index a39d04962..2e3a74bbc 100644 --- a/src/cpp/benders/benders_core/BendersMathLogger.cpp +++ b/src/cpp/benders/benders_core/BendersMathLogger.cpp @@ -1,5 +1,59 @@ #include "BendersMathLogger.h" +// #ifdef _WIN32 +// #include +// #endif + +HeadersManager::HeadersManager(HEADERSTYPE type) { + if (type == HEADERSTYPE::SHORT) { + ITERATION = "ITE"; + LB = "LB"; + UB = "UB"; + BESTUB = "BESTUB"; + ABSOLUTE_GAP = "AGAP"; + RELATIVE_GAP = "RGAP"; + MINSIMPLEX = "miSpx"; + MAXSIMPLEX = "MaSpx"; + NUMBER_OF_SUBPROBLEM_SOLVED = "NBSUBSOLVD"; + CUMULATIVE_NUMBER_OF_SUBPROBLEM_SOLVED = "CNBSBSOLVD"; + BENDERS_TIME = "BENDTime"; + TIMEMASTER = "MASTTime"; + SUB_PROBLEMS_TIME_CPU = "SUBCPUTIME"; + SUB_PROBLEMS_TIME_WALL = "SUBWATIME"; + TIME_NOT_DOING_MASTER_OR_SUB_PROBLEMS_WALL = "TNotMAoSUB"; + } else { + ITERATION = "ITERATION"; + LB = "LB"; + UB = "UB"; + BESTUB = "BESTUB"; + ABSOLUTE_GAP = "ABSOLUTE GAP"; + RELATIVE_GAP = "RELATIVE GAP"; + MINSIMPLEX = "MINSIMPLEX"; + MAXSIMPLEX = "MAXSIMPLEX"; + NUMBER_OF_SUBPROBLEM_SOLVED = "NUMBER OF SUBPROBLEMS SOLVED"; + CUMULATIVE_NUMBER_OF_SUBPROBLEM_SOLVED = + "CUMULATIVE NUMBER OF SUBPROBLEMS SOLVED"; + BENDERS_TIME = "BENDERS TIME"; + TIMEMASTER = "MASTER TIME"; + SUB_PROBLEMS_TIME_CPU = "SUB-PROBLEMS TIME (CPU)"; + SUB_PROBLEMS_TIME_WALL = "SUB-PROBLEMS TIME (WALL)"; + TIME_NOT_DOING_MASTER_OR_SUB_PROBLEMS_WALL = + "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) { diff --git a/src/cpp/benders/benders_core/include/BendersMathLogger.h b/src/cpp/benders/benders_core/include/BendersMathLogger.h index 22f71c1b2..a62d5afbb 100644 --- a/src/cpp/benders/benders_core/include/BendersMathLogger.h +++ b/src/cpp/benders/benders_core/include/BendersMathLogger.h @@ -10,26 +10,34 @@ const std::string MATHLOGGERCONTEXT = "Benders"; inline std::string Indent(int size) { return std::string(size, ' '); } -const std::string ITERATION = "ITERATION"; -const std::string LB = "LB"; -const std::string UB = "UB"; -const std::string BESTUB = "BESTUB"; -const std::string ABSOLUTE_GAP = "ABSOLUTE GAP"; -const std::string RELATIVE_GAP = "RELATIVE GAP"; -const std::string MINSIMPLEX = "MINSIMPLEX"; -const std::string MAXSIMPLEX = "MAXSIMPLEX"; -const std::string NUMBER_OF_SUBPROBLEM_SOLVED = "NUMBER OF SUBPROBLEMS SOLVED"; -const std::string CUMULATIVE_NUMBER_OF_SUBPROBLEM_SOLVED = - "CUMULATIVE NUMBER OF SUBPROBLEMS SOLVED"; -const std::string BENDERS_TIME = "BENDERS TIME"; -const std::string TIMEMASTER = "MASTER TIME"; -const std::string SUB_PROBLEMS_TIME_CPU = "SUB-PROBLEMS TIME (CPU)"; -const std::string SUB_PROBLEMS_TIME_WALL = "SUB-PROBLEMS TIME (WALL)"; -const std::string TIME_NOT_DOING_MASTER_OR_SUB_PROBLEMS_WALL = - "TIME NOT DOING MASTER OR SUB-PROBLEMS (WALL)"; + +enum class HEADERSTYPE { SHORT, LONG }; +struct HeadersManager { + explicit HeadersManager(HEADERSTYPE type); + + std::string ITERATION; + std::string LB = "LB"; + std::string UB = "UB"; + std::string BESTUB = "BESTUB"; + std::string ABSOLUTE_GAP = "ABSOLUTE GAP"; + std::string RELATIVE_GAP = "RELATIVE GAP"; + std::string MINSIMPLEX = "MINSIMPLEX"; + std::string MAXSIMPLEX = "MAXSIMPLEX"; + std::string NUMBER_OF_SUBPROBLEM_SOLVED = "NUMBER OF SUBPROBLEMS SOLVED"; + std::string CUMULATIVE_NUMBER_OF_SUBPROBLEM_SOLVED = + "CUMULATIVE NUMBER OF SUBPROBLEMS SOLVED"; + std::string BENDERS_TIME = "BENDERS TIME"; + std::string TIMEMASTER = "MASTER TIME"; + std::string SUB_PROBLEMS_TIME_CPU = "SUB-PROBLEMS TIME (CPU)"; + std::string SUB_PROBLEMS_TIME_WALL = "SUB-PROBLEMS TIME (WALL)"; + std::string TIME_NOT_DOING_MASTER_OR_SUB_PROBLEMS_WALL = + "TIME NOT DOING MASTER OR SUB-PROBLEMS (WALL)"; + // const std::string BATCH_SIZE = "BATCH SIZE"; + // const std::string SEPARATION_PARAMETER = "SEPARATION PARAMETER"; +}; 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&)) { @@ -42,11 +50,12 @@ class LogDestination { private: std::ostream* stream_; + std::streamsize width_ = 40; }; template std::ostream& LogDestination::operator<<(const T& obj) { // write obj to stream - return (*stream_) << std::left << std::setw(50) << obj; + return (*stream_) << std::left << std::setw(width_) << obj; } struct MathLoggerBehaviour { @@ -64,12 +73,18 @@ struct MathLoggerBehaviour { }; struct MathLogger : public MathLoggerBehaviour { - explicit MathLogger(std::ostream* stream) : log_destination_(stream) {} - explicit MathLogger() : log_destination_(&std::cout) {} + 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::list 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); @@ -77,6 +92,7 @@ struct MathLogger : public MathLoggerBehaviour { private: std::list headers_; LogDestination log_destination_; + HEADERSTYPE type_; }; struct MathLoggerBase : public MathLogger { @@ -89,26 +105,30 @@ struct MathLoggerBase : public MathLogger { struct MathLoggerBendersByBatch : public MathLogger { using MathLogger::MathLogger; void Print(const CurrentIterationData& data) override; - void setHeadersList() override; }; 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 } } diff --git a/src/cpp/benders/benders_mpi/BendersMPI.cpp b/src/cpp/benders/benders_mpi/BendersMPI.cpp index 6c546c80b..49ba18e9c 100644 --- a/src/cpp/benders/benders_mpi/BendersMPI.cpp +++ b/src/cpp/benders/benders_mpi/BendersMPI.cpp @@ -121,7 +121,6 @@ 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.cumulative_number_of_subproblem_solved = _data.nsubproblem; _data.cumulative_number_of_subproblem_solved += _data.nsubproblem; _logger->cumulative_number_of_sub_problem_solved( _data.cumulative_number_of_subproblem_solved + diff --git a/src/cpp/benders/logger/MathLogger.cpp b/src/cpp/benders/logger/MathLogger.cpp index b8f85810e..41d8096b4 100644 --- a/src/cpp/benders/logger/MathLogger.cpp +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -9,12 +9,15 @@ double getDurationNotDoingMasterOrSubproblems(double benders, double master, } void MathLoggerBase::setHeadersList() { + HeadersManager headers(HeadersType()); MathLogger::setHeadersList( - {ITERATION, LB, UB, BESTUB, ABSOLUTE_GAP, RELATIVE_GAP, MINSIMPLEX, - MAXSIMPLEX, NUMBER_OF_SUBPROBLEM_SOLVED, - CUMULATIVE_NUMBER_OF_SUBPROBLEM_SOLVED, BENDERS_TIME, TIMEMASTER, - SUB_PROBLEMS_TIME_CPU, SUB_PROBLEMS_TIME_WALL, - TIME_NOT_DOING_MASTER_OR_SUB_PROBLEMS_WALL}); + {headers.ITERATION, headers.LB, headers.UB, headers.BESTUB, + headers.ABSOLUTE_GAP, headers.RELATIVE_GAP, headers.MINSIMPLEX, + headers.MAXSIMPLEX, headers.NUMBER_OF_SUBPROBLEM_SOLVED, + headers.CUMULATIVE_NUMBER_OF_SUBPROBLEM_SOLVED, headers.BENDERS_TIME, + headers.TIMEMASTER, headers.SUB_PROBLEMS_TIME_CPU, + headers.SUB_PROBLEMS_TIME_WALL, + headers.TIME_NOT_DOING_MASTER_OR_SUB_PROBLEMS_WALL}); } void MathLogger::setHeadersList(const std::list& headers) { @@ -62,11 +65,14 @@ void MathLoggerBase::Print(const CurrentIterationData& data) { } void MathLoggerBendersByBatch::setHeadersList() { + HeadersManager headers(HeadersType()); MathLogger::setHeadersList( - {ITERATION, LB, MINSIMPLEX, MAXSIMPLEX, NUMBER_OF_SUBPROBLEM_SOLVED, - CUMULATIVE_NUMBER_OF_SUBPROBLEM_SOLVED, BENDERS_TIME, TIMEMASTER, - SUB_PROBLEMS_TIME_CPU, SUB_PROBLEMS_TIME_WALL, - TIME_NOT_DOING_MASTER_OR_SUB_PROBLEMS_WALL}); + {headers.ITERATION, headers.LB, headers.MINSIMPLEX, headers.MAXSIMPLEX, + headers.NUMBER_OF_SUBPROBLEM_SOLVED, + headers.CUMULATIVE_NUMBER_OF_SUBPROBLEM_SOLVED, headers.BENDERS_TIME, + headers.TIMEMASTER, headers.SUB_PROBLEMS_TIME_CPU, + headers.SUB_PROBLEMS_TIME_WALL, + headers.TIME_NOT_DOING_MASTER_OR_SUB_PROBLEMS_WALL}); } void MathLoggerBendersByBatch::Print(const CurrentIterationData& data) { @@ -96,8 +102,10 @@ void MathLoggerBendersByBatch::Print(const CurrentIterationData& data) { } 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/include/logger/MathLogger.h b/src/cpp/benders/logger/include/logger/MathLogger.h index 2564d3d5b..74e32c9f3 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 = 40); 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) {} }; From 8fb0d30b3558d2a4525505e526c8032698ddfaff Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Fri, 8 Dec 2023 19:46:21 +0100 Subject: [PATCH 29/32] to fit terminal width --- src/cpp/benders/benders_core/BendersBase.cpp | 6 +- .../benders_core/BendersMathLogger.cpp | 68 ++++++------ .../benders_core/include/BendersBase.h | 2 +- .../benders_core/include/BendersMathLogger.h | 55 +++++----- src/cpp/benders/logger/MathLogger.cpp | 103 ++++++++---------- .../logger/include/logger/MathLogger.h | 2 +- 6 files changed, 116 insertions(+), 120 deletions(-) diff --git a/src/cpp/benders/benders_core/BendersBase.cpp b/src/cpp/benders/benders_core/BendersBase.cpp index d20b068cd..ad461521b 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() { - 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 2e3a74bbc..93e41005e 100644 --- a/src/cpp/benders/benders_core/BendersMathLogger.cpp +++ b/src/cpp/benders/benders_core/BendersMathLogger.cpp @@ -4,41 +4,43 @@ // #include // #endif -HeadersManager::HeadersManager(HEADERSTYPE type) { +HeadersManager::HeadersManager(HEADERSTYPE type, const BENDERSMETHOD& method) { if (type == HEADERSTYPE::SHORT) { - ITERATION = "ITE"; - LB = "LB"; - UB = "UB"; - BESTUB = "BESTUB"; - ABSOLUTE_GAP = "AGAP"; - RELATIVE_GAP = "RGAP"; - MINSIMPLEX = "miSpx"; - MAXSIMPLEX = "MaSpx"; - NUMBER_OF_SUBPROBLEM_SOLVED = "NBSUBSOLVD"; - CUMULATIVE_NUMBER_OF_SUBPROBLEM_SOLVED = "CNBSBSOLVD"; - BENDERS_TIME = "BENDTime"; - TIMEMASTER = "MASTTime"; - SUB_PROBLEMS_TIME_CPU = "SUBCPUTIME"; - SUB_PROBLEMS_TIME_WALL = "SUBWATIME"; - TIME_NOT_DOING_MASTER_OR_SUB_PROBLEMS_WALL = "TNotMAoSUB"; + 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 { - ITERATION = "ITERATION"; - LB = "LB"; - UB = "UB"; - BESTUB = "BESTUB"; - ABSOLUTE_GAP = "ABSOLUTE GAP"; - RELATIVE_GAP = "RELATIVE GAP"; - MINSIMPLEX = "MINSIMPLEX"; - MAXSIMPLEX = "MAXSIMPLEX"; - NUMBER_OF_SUBPROBLEM_SOLVED = "NUMBER OF SUBPROBLEMS SOLVED"; - CUMULATIVE_NUMBER_OF_SUBPROBLEM_SOLVED = - "CUMULATIVE NUMBER OF SUBPROBLEMS SOLVED"; - BENDERS_TIME = "BENDERS TIME"; - TIMEMASTER = "MASTER TIME"; - SUB_PROBLEMS_TIME_CPU = "SUB-PROBLEMS TIME (CPU)"; - SUB_PROBLEMS_TIME_WALL = "SUB-PROBLEMS TIME (WALL)"; - TIME_NOT_DOING_MASTER_OR_SUB_PROBLEMS_WALL = - "TIME NOT DOING MASTER OR SUB-PROBLEMS (WALL)"; + 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)"); } } diff --git a/src/cpp/benders/benders_core/include/BendersBase.h b/src/cpp/benders/benders_core/include/BendersBase.h index 1c59f2141..c50866fb6 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 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 a62d5afbb..1c6bc84e2 100644 --- a/src/cpp/benders/benders_core/include/BendersMathLogger.h +++ b/src/cpp/benders/benders_core/include/BendersMathLogger.h @@ -10,30 +10,35 @@ const std::string MATHLOGGERCONTEXT = "Benders"; inline std::string Indent(int size) { return std::string(size, ' '); } +struct Header { + std::string label; + int pos; +}; enum class HEADERSTYPE { SHORT, LONG }; struct HeadersManager { - explicit HeadersManager(HEADERSTYPE type); - - std::string ITERATION; - std::string LB = "LB"; - std::string UB = "UB"; - std::string BESTUB = "BESTUB"; - std::string ABSOLUTE_GAP = "ABSOLUTE GAP"; - std::string RELATIVE_GAP = "RELATIVE GAP"; - std::string MINSIMPLEX = "MINSIMPLEX"; - std::string MAXSIMPLEX = "MAXSIMPLEX"; - std::string NUMBER_OF_SUBPROBLEM_SOLVED = "NUMBER OF SUBPROBLEMS SOLVED"; - std::string CUMULATIVE_NUMBER_OF_SUBPROBLEM_SOLVED = - "CUMULATIVE NUMBER OF SUBPROBLEMS SOLVED"; - std::string BENDERS_TIME = "BENDERS TIME"; - std::string TIMEMASTER = "MASTER TIME"; - std::string SUB_PROBLEMS_TIME_CPU = "SUB-PROBLEMS TIME (CPU)"; - std::string SUB_PROBLEMS_TIME_WALL = "SUB-PROBLEMS TIME (WALL)"; - std::string TIME_NOT_DOING_MASTER_OR_SUB_PROBLEMS_WALL = - "TIME NOT DOING MASTER OR SUB-PROBLEMS (WALL)"; + explicit HeadersManager(HEADERSTYPE type, const BENDERSMETHOD& method); + std::vector headers_list; + + // Header ITERATION; + // Header LB; + // Header UB; + // Header BESTUB; + // Header ABSOLUTE_GAP; + // Header RELATIVE_GAP; + // Header MINSIMPLEX; + // Header MAXSIMPLEX; + // Header NUMBER_OF_SUBPROBLEM_SOLVED; + // Header CUMULATIVE_NUMBER_OF_SUBPROBLEM_SOLVED; + // Header ITE_TIME; + // Header TIMEMASTER; + // Header SUB_PROBLEMS_TIME_CPU; + // Header SUB_PROBLEMS_TIME_WALL; + // Header TIME_NOT_DOING_MASTER_OR_SUB_PROBLEMS_WALL; // const std::string BATCH_SIZE = "BATCH SIZE"; // const std::string SEPARATION_PARAMETER = "SEPARATION PARAMETER"; + // private: + // void SetHeader(Header& header, const std::string& label); }; class LogDestination { public: @@ -67,7 +72,7 @@ struct MathLoggerBehaviour { LogsDestination() << std::endl; } virtual void Print(const CurrentIterationData& data) = 0; - virtual std::list Headers() const = 0; + virtual std::vector Headers() const = 0; virtual LogDestination& LogsDestination() = 0; virtual void setHeadersList() = 0; }; @@ -81,16 +86,16 @@ struct MathLogger : public MathLoggerBehaviour { HEADERSTYPE type = HEADERSTYPE::LONG) : log_destination_(&std::cout, width), type_(type) {} virtual void Print(const CurrentIterationData& data) = 0; - std::list Headers() const override { return headers_; } + 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_; }; @@ -137,7 +142,7 @@ class MathLoggerImplementation : public MathLoggerBehaviour { protected: void setHeadersList() override { implementation_->setHeadersList(); } - std::list Headers() const override { + std::vector Headers() const override { return implementation_->Headers(); } virtual LogDestination& LogsDestination() { @@ -157,5 +162,5 @@ class MathLoggerDriver { void Print(const CurrentIterationData& data); private: - std::list> math_loggers_; + std::vector> math_loggers_; }; diff --git a/src/cpp/benders/logger/MathLogger.cpp b/src/cpp/benders/logger/MathLogger.cpp index 41d8096b4..919529a6c 100644 --- a/src/cpp/benders/logger/MathLogger.cpp +++ b/src/cpp/benders/logger/MathLogger.cpp @@ -3,100 +3,89 @@ #include #include -double getDurationNotDoingMasterOrSubproblems(double benders, double master, +double getDurationNotDoingMasterOrSubproblems(double interation, double master, double subproblems) { - return benders - master - subproblems; + return interation - master - subproblems; } void MathLoggerBase::setHeadersList() { - HeadersManager headers(HeadersType()); - MathLogger::setHeadersList( - {headers.ITERATION, headers.LB, headers.UB, headers.BESTUB, - headers.ABSOLUTE_GAP, headers.RELATIVE_GAP, headers.MINSIMPLEX, - headers.MAXSIMPLEX, headers.NUMBER_OF_SUBPROBLEM_SOLVED, - headers.CUMULATIVE_NUMBER_OF_SUBPROBLEM_SOLVED, headers.BENDERS_TIME, - headers.TIMEMASTER, headers.SUB_PROBLEMS_TIME_CPU, - headers.SUB_PROBLEMS_TIME_WALL, - headers.TIME_NOT_DOING_MASTER_OR_SUB_PROBLEMS_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 MathLoggerBase::Print(const CurrentIterationData& data) { + auto type = HeadersType(); + LogsDestination() << data.it; - if (data.lb == -1e20) - LogsDestination() << "-INF"; - else - LogsDestination() << std::scientific << std::setprecision(10) << data.lb; - if (data.ub == +1e20) - LogsDestination() << "+INF"; - else - LogsDestination() << std::scientific << std::setprecision(10) << data.ub; - if (data.best_ub == +1e20) - LogsDestination() << "+INF"; - else - LogsDestination() << std::scientific << std::setprecision(10) - << data.best_ub; + 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; - LogsDestination() << data.number_of_subproblem_solved; - LogsDestination() << data.cumulative_number_of_subproblem_solved; + if (type == HEADERSTYPE::LONG) { + LogsDestination() << data.number_of_subproblem_solved; + LogsDestination() << 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_cputime; + if (type == HEADERSTYPE::LONG) { + LogsDestination() << std::setprecision(2) + << data.subproblems_cumulative_cputime; + } LogsDestination() << std::setprecision(2) << data.subproblems_walltime; - LogsDestination() << std::setprecision(2) - << getDurationNotDoingMasterOrSubproblems( - data.elapsed_time, data.timer_master, - 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() { - HeadersManager headers(HeadersType()); - MathLogger::setHeadersList( - {headers.ITERATION, headers.LB, headers.MINSIMPLEX, headers.MAXSIMPLEX, - headers.NUMBER_OF_SUBPROBLEM_SOLVED, - headers.CUMULATIVE_NUMBER_OF_SUBPROBLEM_SOLVED, headers.BENDERS_TIME, - headers.TIMEMASTER, headers.SUB_PROBLEMS_TIME_CPU, - headers.SUB_PROBLEMS_TIME_WALL, - headers.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) { - LogsDestination() << data.it; - if (data.lb == -1e20) - LogsDestination() << "-INF"; - else - LogsDestination() << std::scientific << std::setprecision(10) << data.lb; + auto type = HeadersType(); + LogsDestination() << data.it; + LogsDestination() << std::scientific << std::setprecision(10) << data.lb; LogsDestination() << data.min_simplexiter; LogsDestination() << data.max_simplexiter; - - // LogsDestination() << data.deletedcut; LogsDestination() << data.number_of_subproblem_solved; - LogsDestination() << data.cumulative_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_cumulative_cputime; + if (type == HEADERSTYPE::LONG) { + LogsDestination() << std::setprecision(2) + << data.subproblems_cumulative_cputime; + } LogsDestination() << std::setprecision(2) << data.subproblems_walltime; - LogsDestination() << std::setprecision(2) - << getDurationNotDoingMasterOrSubproblems( - data.elapsed_time, data.timer_master, - data.subproblems_walltime); + if (type == HEADERSTYPE::LONG) { + LogsDestination() << std::setprecision(2) + << getDurationNotDoingMasterOrSubproblems( + data.elapsed_time, data.timer_master, + data.subproblems_walltime); + } LogsDestination() << std::endl; } diff --git a/src/cpp/benders/logger/include/logger/MathLogger.h b/src/cpp/benders/logger/include/logger/MathLogger.h index 74e32c9f3..0b9b8bcd9 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 = 40); + std::streamsize width = 45); private: std::ofstream file_stream_; From c5df2ae44aec2f071958aed10e2e727fac658842 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Mon, 11 Dec 2023 12:41:50 +0100 Subject: [PATCH 30/32] add expert_logs option in python code --- .../benders_core/BendersMathLogger.cpp | 6 ++++ .../benders_core/SimulationOptions.cpp | 2 +- .../benders_core/include/BendersMathLogger.h | 13 ++++++-- .../include/SimulationOptions.hxx | 2 +- src/cpp/benders/benders_core/include/common.h | 2 +- src/cpp/benders/factories/BendersFactory.cpp | 20 ++++++------ src/cpp/xpansion_interfaces/ILogger.h | 19 +++++++++++- 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 +++ 11 files changed, 104 insertions(+), 17 deletions(-) diff --git a/src/cpp/benders/benders_core/BendersMathLogger.cpp b/src/cpp/benders/benders_core/BendersMathLogger.cpp index 93e41005e..bca9d460e 100644 --- a/src/cpp/benders/benders_core/BendersMathLogger.cpp +++ b/src/cpp/benders/benders_core/BendersMathLogger.cpp @@ -74,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 00c1c33f5..d59440231 100644 --- a/src/cpp/benders/benders_core/SimulationOptions.cpp +++ b/src/cpp/benders/benders_core/SimulationOptions.cpp @@ -167,7 +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_LOG_AT_CONSOLE = EXPERT_LOG_AT_CONSOLE; + result.EXPERT_LOGS = EXPERT_LOGS; return result; } \ 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 1c6bc84e2..ed74608f9 100644 --- a/src/cpp/benders/benders_core/include/BendersMathLogger.h +++ b/src/cpp/benders/benders_core/include/BendersMathLogger.h @@ -6,6 +6,7 @@ #include #include "BendersStructsDatas.h" +#include "ILogger.h" #include "common.h" const std::string MATHLOGGERCONTEXT = "Benders"; @@ -63,7 +64,7 @@ std::ostream& LogDestination::operator<<(const T& obj) { return (*stream_) << std::left << std::setw(width_) << obj; } -struct MathLoggerBehaviour { +struct MathLoggerBehaviour : public ILoggerBenders { void write_header() { setHeadersList(); for (const auto& header : Headers()) { @@ -71,6 +72,11 @@ struct MathLoggerBehaviour { } LogsDestination() << std::endl; } + + virtual void display_message(const std::string& str) { + LogsDestination() << str; + } + virtual void Print(const CurrentIterationData& data) = 0; virtual std::vector Headers() const = 0; virtual LogDestination& LogsDestination() = 0; @@ -85,6 +91,7 @@ struct MathLogger : public MathLoggerBehaviour { 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_; } @@ -153,11 +160,11 @@ class MathLoggerImplementation : public MathLoggerBehaviour { 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); diff --git a/src/cpp/benders/benders_core/include/SimulationOptions.hxx b/src/cpp/benders/benders_core/include/SimulationOptions.hxx index 1e08d3383..e49a90009 100644 --- a/src/cpp/benders/benders_core/include/SimulationOptions.hxx +++ b/src/cpp/benders/benders_core/include/SimulationOptions.hxx @@ -74,4 +74,4 @@ BENDERS_OPTIONS_MACRO(LAST_MASTER_BASIS, std::string, "master_last_basis", BENDERS_OPTIONS_MACRO(BATCH_SIZE, size_t, 0, asUInt()) // which logs must be printed in the console -BENDERS_OPTIONS_MACRO(EXPERT_LOG_AT_CONSOLE, bool, true, asBool()) +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 578dd9419..51f956eb7 100644 --- a/src/cpp/benders/benders_core/include/common.h +++ b/src/cpp/benders/benders_core/include/common.h @@ -159,7 +159,7 @@ struct BendersBaseOptions : public BaseOptions { std::string LAST_MASTER_BASIS; size_t BATCH_SIZE; - bool EXPERT_LOG_AT_CONSOLE = true; + 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 d5aad2a4d..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; @@ -42,9 +43,9 @@ int RunBenders(char** argv, const std::filesystem::path& options_file, if (world.rank() == 0) { auto logger_factory = FileAndStdoutLoggerFactory( - log_reports_name, benders_options.EXPERT_LOG_AT_CONSOLE); + log_reports_name, benders_options.EXPERT_LOGS); auto math_log_factory = MathLoggerFactory( - method, benders_options.EXPERT_LOG_AT_CONSOLE, math_logs_file); + method, benders_options.EXPERT_LOGS, math_logs_file); logger = logger_factory.get_logger(); math_log_driver = math_log_factory.get_logger(); @@ -58,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, @@ -68,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); @@ -83,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/xpansion_interfaces/ILogger.h b/src/cpp/xpansion_interfaces/ILogger.h index 5ab7287a2..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; 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 From 40b09f280e30493383975589754c5217b751fe64 Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Mon, 11 Dec 2023 14:41:21 +0100 Subject: [PATCH 31/32] update doc --- docs/user-guide/get-started/settings-definition.md | 1 + 1 file changed, 1 insertion(+) 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 From e233d6444dda6acb8892473da599e0dffc25176c Mon Sep 17 00:00:00 2001 From: Abdoulbari ZAKIR Date: Mon, 11 Dec 2023 14:44:02 +0100 Subject: [PATCH 32/32] remove unused function --- .../benders_core/include/BendersMathLogger.h | 21 ------------------- 1 file changed, 21 deletions(-) diff --git a/src/cpp/benders/benders_core/include/BendersMathLogger.h b/src/cpp/benders/benders_core/include/BendersMathLogger.h index ed74608f9..4844b7580 100644 --- a/src/cpp/benders/benders_core/include/BendersMathLogger.h +++ b/src/cpp/benders/benders_core/include/BendersMathLogger.h @@ -10,7 +10,6 @@ #include "common.h" const std::string MATHLOGGERCONTEXT = "Benders"; -inline std::string Indent(int size) { return std::string(size, ' '); } struct Header { std::string label; int pos; @@ -20,26 +19,6 @@ enum class HEADERSTYPE { SHORT, LONG }; struct HeadersManager { explicit HeadersManager(HEADERSTYPE type, const BENDERSMETHOD& method); std::vector headers_list; - - // Header ITERATION; - // Header LB; - // Header UB; - // Header BESTUB; - // Header ABSOLUTE_GAP; - // Header RELATIVE_GAP; - // Header MINSIMPLEX; - // Header MAXSIMPLEX; - // Header NUMBER_OF_SUBPROBLEM_SOLVED; - // Header CUMULATIVE_NUMBER_OF_SUBPROBLEM_SOLVED; - // Header ITE_TIME; - // Header TIMEMASTER; - // Header SUB_PROBLEMS_TIME_CPU; - // Header SUB_PROBLEMS_TIME_WALL; - // Header TIME_NOT_DOING_MASTER_OR_SUB_PROBLEMS_WALL; - // const std::string BATCH_SIZE = "BATCH SIZE"; - // const std::string SEPARATION_PARAMETER = "SEPARATION PARAMETER"; - // private: - // void SetHeader(Header& header, const std::string& label); }; class LogDestination { public: