Skip to content

Commit

Permalink
set log destination
Browse files Browse the repository at this point in the history
  • Loading branch information
a-zakir committed Nov 24, 2023
1 parent 7307b13 commit 707b6e5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 41 deletions.
10 changes: 7 additions & 3 deletions src/cpp/benders/benders_core/include/BendersMathLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,20 @@ 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<std::string> Headers() const { return headers_; }
LogDestination log_destination;
virtual LogDestination& TheLogDestination() { return log_destination_; }
virtual void setHeadersList() = 0;

protected:
void setHeadersList(const std::list<std::string>& headers);

private:
std::list<std::string> headers_;
LogDestination log_destination_;
};

struct MathLoggerBase : public MathLogger {
Expand Down Expand Up @@ -110,6 +111,9 @@ class MathLoggerImplementation : public MathLogger {
std::list<std::string> Headers() const override {
return implementation_->Headers();
}
virtual LogDestination& TheLogDestination() {
return implementation_->TheLogDestination();
}

private:
std::shared_ptr<MathLogger> implementation_;
Expand Down
78 changes: 40 additions & 38 deletions src/cpp/benders/logger/MathLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,45 @@ void MathLogger::setHeadersList(const std::list<std::string>& 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() {
Expand All @@ -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,
Expand Down

0 comments on commit 707b6e5

Please sign in to comment.