Skip to content

Commit

Permalink
introducing MathlogImplementation
Browse files Browse the repository at this point in the history
  • Loading branch information
a-zakir committed Nov 24, 2023
1 parent 896fdde commit 3616fcf
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 53 deletions.
2 changes: 1 addition & 1 deletion cmake/dependencies/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 3 additions & 0 deletions src/cpp/benders/benders_by_batch/BendersByBatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ void BendersByBatch::MasterLoop() {
_logger->LogSubproblemsSolvingCumulativeCpuTime(
GetSubproblemsCumulativeCpuTime());
_logger->LogSubproblemsSolvingWalltime(GetSubproblemsWalltime());
mathLoggerDriver_->Print(_data);
_logger->display_message(
"\\________________________________________________________________"
"________");
Expand All @@ -136,13 +137,15 @@ void BendersByBatch::SeparationLoop() {
batch_counter_ = 0;
while (misprice_ && batch_counter_ < number_of_batch_) {
_data.it++;
ResetSimplexIterationsBounds();

_logger->log_at_initialization(_data.it + GetNumIterationsBeforeRestart());
ComputeXCut();
_logger->log_iteration_candidates(bendersDataToLogData(_data));
BroadcastXCut();
UpdateRemainingEpsilon();
SolveBatches();

if (Rank() == rank_0) {
UpdateTrace();
SaveCurrentBendersData();
Expand Down
3 changes: 2 additions & 1 deletion src/cpp/benders/benders_core/BendersMathLogger.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "BendersMathLogger.h"

void MathLoggerDriver::add_logger(std::shared_ptr<MathLogger> logger) {
void MathLoggerDriver::add_logger(
std::shared_ptr<MathLoggerImplementation> logger) {
if (logger) {
math_loggers_.push_back(logger);
}
Expand Down
2 changes: 1 addition & 1 deletion src/cpp/benders/benders_core/SimulationOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)<<std::boolalpha << name__ << std::endl;
#include "SimulationOptions.hxx"
#undef BENDERS_OPTIONS_MACRO
stream << std::endl;
Expand Down
77 changes: 71 additions & 6 deletions src/cpp/benders/benders_core/include/BendersMathLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,27 @@
#include <iostream>
#include <list>
#include <memory>

#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) {}
Expand Down Expand Up @@ -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<std::string> 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<MathLoggerBase>(stream);
} else if (method == BENDERSMETHOD::BENDERSBYBATCH) {
implementation_ = std::make_shared<MathLoggerBendersByBatch>(stream);
}
// else
}
explicit MathLoggerImplementation(const BENDERSMETHOD& method) {
if (method == BENDERSMETHOD::BENDERS) {
implementation_ = std::make_shared<MathLoggerBase>();
} else if (method == BENDERSMETHOD::BENDERSBYBATCH) {
implementation_ = std::make_shared<MathLoggerBendersByBatch>();
}
// else }
}

virtual void Print(const CurrentIterationData& data) {
implementation_->Print(data);
}
virtual void setHeadersList() { implementation_->setHeadersList(); }

private:
std::shared_ptr<MathLogger> implementation_;
};

class MathLoggerDriver {
public:
MathLoggerDriver() = default;
void write_header();
void add_logger(std::shared_ptr<MathLogger> logger);

void add_logger(std::shared_ptr<MathLoggerImplementation> logger);
void Print(const CurrentIterationData& data);

private:
std::list<std::shared_ptr<MathLogger>> math_loggers_;
std::list<std::shared_ptr<MathLoggerImplementation>> math_loggers_;
};
2 changes: 2 additions & 0 deletions src/cpp/benders/benders_core/include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ typedef std::vector<ActiveCut> ActiveCutStorage;
typedef std::pair<std::string, std::string> mps_coupling;
typedef std::list<mps_coupling> mps_coupling_list;

enum class BENDERSMETHOD { BENDERS, BENDERSBYBATCH, MERGEMPS };

struct Predicate {
bool operator()(PointPtr const &lhs, PointPtr const &rhs) const {
return *lhs < *rhs;
Expand Down
10 changes: 7 additions & 3 deletions src/cpp/benders/benders_mpi/BendersMPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ void BendersMpi::gather_subproblems_cut_package_and_build_cuts(
Reduce(GetSubproblemsCpuTime(), cumulative_subproblems_timer_per_iter,
std::plus<double>(), rank_0);
SetSubproblemsCumulativeCpuTime(cumulative_subproblems_timer_per_iter);
// only rank_0 receive non-emtpy gathered_subproblem_map
master_build_cuts(gathered_subproblem_map);
}
}
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -281,6 +284,7 @@ void BendersMpi::PreRunInitialization() {
OpenCsvFile();
}
}
mathLoggerDriver_->write_header();
}
void BendersMpi::launch() {
build_input_map();
Expand Down
2 changes: 1 addition & 1 deletion src/cpp/benders/factories/BendersFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/cpp/benders/factories/include/BendersFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
23 changes: 13 additions & 10 deletions src/cpp/benders/factories/include/LoggerFactories.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <filesystem>

#include "BendersFactory.h"
#include "ILogger.h"
#include "SimulationOptions.h"
#include "logger/Master.h"
Expand Down Expand Up @@ -36,23 +37,25 @@ class FileAndStdoutLoggerFactory {
class MathLoggerFactory {
private:
MathLoggerDriver math_Logger_driver;
std::shared_ptr<MathLoggerFile> math_logger_file_;
std::shared_ptr<MathLoggerOstream> 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<MathLoggerFile>(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<MathLoggerFile>(method, math_logs_file_path);
math_Logger_driver.add_logger(math_logger_file);

if (console_log) {
auto math_Logger_ostream = std::make_shared<MathLoggerOstream>(method);

math_Logger_driver.add_logger(math_Logger_ostream);
}
}
explicit MathLoggerFactory() = default;
std::shared_ptr<MathLoggerDriver> get_logger() {
return std::make_shared<MathLoggerDriver>(math_Logger_driver);
}
};
};
#endif // ANTARESXPANSION_LOGGERFACTORIES_H
83 changes: 60 additions & 23 deletions src/cpp/benders/logger/MathLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,28 @@
#include <iomanip>
#include <sstream>

#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";
Expand All @@ -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;
}
}
}
Loading

0 comments on commit 3616fcf

Please sign in to comment.