Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/low level logs #723

Closed
Closed
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
3423dfc
start Math log
a-zakir Nov 20, 2023
f184ff7
add overloaded method
a-zakir Nov 20, 2023
4cfd573
fix platform specific standard
a-zakir Nov 20, 2023
d3527c4
add MathloggerDriver
a-zakir Nov 20, 2023
d341b1b
print basic data
a-zakir Nov 20, 2023
f28101c
add math logger in benders
a-zakir Nov 20, 2023
3710fbb
Benders take mathlooger
a-zakir Nov 20, 2023
e2e9a91
print maths log
a-zakir Nov 20, 2023
847b9db
add missing include
a-zakir Nov 20, 2023
b2680f0
fix
a-zakir Nov 20, 2023
ef37857
print more data
a-zakir Nov 22, 2023
896fdde
Reset Simplex Iterations Bounds
a-zakir Nov 24, 2023
3616fcf
introducing MathlogImplementation
a-zakir Nov 24, 2023
ff73b89
fix
a-zakir Nov 24, 2023
53adca3
fix
a-zakir Nov 24, 2023
7307b13
ok
a-zakir Nov 24, 2023
707b6e5
set log destination
a-zakir Nov 24, 2023
9402964
@tbittar remarks
a-zakir Dec 7, 2023
6871cd1
split behaviour and data holders
a-zakir Dec 7, 2023
bcc6c24
rename data header
a-zakir Dec 7, 2023
7edbb4b
add "other time"
a-zakir Dec 7, 2023
37baeb1
align columns
a-zakir Dec 7, 2023
59af269
align headers and data
a-zakir Dec 7, 2023
c2a13ec
fix master time
a-zakir Dec 7, 2023
d6810e6
add CUMULATIVE nb of sub-pblm
a-zakir Dec 7, 2023
00e909f
add num of subproblem solved / iteration
a-zakir Dec 8, 2023
5d29a74
add "expert" option for logs
a-zakir Dec 8, 2023
474380d
update
a-zakir Dec 8, 2023
8fb0d30
to fit terminal width
a-zakir Dec 8, 2023
c5df2ae
add expert_logs option in python code
a-zakir Dec 11, 2023
40b09f2
update doc
a-zakir Dec 11, 2023
e233d64
remove unused function
a-zakir Dec 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 9 additions & 4 deletions src/cpp/benders/benders_by_batch/BendersByBatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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> mathLoggerDriver)
: BendersMpi(options, logger, writer, env, world, mathLoggerDriver) {}

void BendersByBatch::InitializeProblems() {
MatchProblemToId();
Expand Down Expand Up @@ -124,6 +125,7 @@ void BendersByBatch::MasterLoop() {
_logger->LogSubproblemsSolvingCumulativeCpuTime(
GetSubproblemsCumulativeCpuTime());
_logger->LogSubproblemsSolvingWalltime(GetSubproblemsWalltime());
mathLoggerDriver_->Print(_data);
_logger->display_message(
"\\________________________________________________________________"
"________");
Expand All @@ -135,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 Expand Up @@ -229,6 +233,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) {
Expand Down
4 changes: 2 additions & 2 deletions src/cpp/benders/benders_by_batch/include/BendersByBatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -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> mathLoggerDriver);
~BendersByBatch() override = default;
void Run() override;
void BuildCut(const std::vector<std::string> &batch_sub_problems,
Expand Down
24 changes: 22 additions & 2 deletions src/cpp/benders/benders_core/BendersBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
#include "solver_utils.h"

BendersBase::BendersBase(BendersBaseOptions options, Logger logger,
Writer writer)
Writer writer,
std::shared_ptr<MathLoggerDriver> 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
Expand Down Expand Up @@ -773,6 +775,24 @@ 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;

}

void BendersBase::ResetSimplexIterationsBounds()
{
_data.max_simplexiter = 0;
_data.min_simplexiter = std::numeric_limits<int>::max();
}
bool BendersBase::IsResumeMode() const { return _options.RESUME; }

void BendersBase::UpdateMaxNumberIterationResumeMode(
Expand Down
20 changes: 20 additions & 0 deletions src/cpp/benders/benders_core/BendersMathLogger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "BendersMathLogger.h"

void MathLoggerDriver::add_logger(
std::shared_ptr<MathLoggerImplementation> logger) {
if (logger) {
math_loggers_.push_back(logger);
}
}

void MathLoggerDriver::Print(const CurrentIterationData& data) {
for (auto logger : math_loggers_) {
logger->Print(data);
}
}

void MathLoggerDriver::write_header() {
for (auto logger : math_loggers_) {
logger->write_header();
}
}
1 change: 1 addition & 0 deletions src/cpp/benders/benders_core/BendersStructsDatas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ 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; }

4 changes: 3 additions & 1 deletion src/cpp/benders/benders_core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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
8 changes: 7 additions & 1 deletion src/cpp/benders/benders_core/include/BendersBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <execution>
#include <filesystem>

#include "BendersMathLogger.h"
#include "BendersStructsDatas.h"
#include "ILogger.h"
#include "OutputWriter.h"
Expand All @@ -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> 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; }
Expand All @@ -40,6 +42,7 @@ class BendersBase {
CurrentIterationData _data;
VariableMap master_variable_map;
CouplingMap coupling_map;
std::shared_ptr<MathLoggerDriver> mathLoggerDriver_;

protected:
virtual void free() = 0;
Expand Down Expand Up @@ -136,6 +139,9 @@ class BendersBase {
return cumulative_number_of_subproblem_resolved_before_resume;
}

void BoundSimplexIterations(int subproblem_iteration);
void ResetSimplexIterationsBounds();

SolverLogManager solver_log_manager_;

private:
Expand Down
139 changes: 139 additions & 0 deletions src/cpp/benders/benders_core/include/BendersMathLogger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#pragma once

#include <fstream>
#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 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 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) {}

// for std::endl
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented code if useless

std::ostream& operator<<(std::ostream& (*function)(std::ostream&)) {
// write obj to stream
return function(*stream_);
}

template <class T>
std::ostream& operator<<(const T& obj);

private:
std::ostream* stream_;
};
template <class T>
std::ostream& LogDestination::operator<<(const T& obj) {
// write obj to stream
return (*stream_) << std::left << obj;
}

struct MathLoggerBehaviour {
void write_header() {
setHeadersList();
for (const auto& header : Headers()) {
LogsDestination() << header;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR contains console logs. Please review or remove them.

LogsDestination() << std::endl;
}
virtual void Print(const CurrentIterationData& data) = 0;
virtual std::list<std::string> 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) {}
virtual void Print(const CurrentIterationData& data) = 0;
std::list<std::string> Headers() const override { return headers_; }
virtual LogDestination& LogsDestination() { 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 {
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 MathLoggerBehaviour {
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 }
}

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

protected:
void setHeadersList() override { implementation_->setHeadersList(); }
std::list<std::string> Headers() const override {
return implementation_->Headers();
}
virtual LogDestination& LogsDestination() {
return implementation_->LogsDestination();
}

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

class MathLoggerDriver {
public:
MathLoggerDriver() = default;
void write_header();

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

private:
std::list<std::shared_ptr<MathLoggerImplementation>> math_loggers_;
};
4 changes: 3 additions & 1 deletion src/cpp/benders/benders_core/include/BendersStructsDatas.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once

#include "ILogger.h"
#include "SubproblemCut.h"
#include "Worker.h"
#include "common.h"
#include "ILogger.h"

struct CurrentIterationData {
double subproblems_walltime;
Expand Down Expand Up @@ -32,6 +32,8 @@ struct CurrentIterationData {
StoppingCriterion stopping_criterion;
bool is_in_initial_relaxation;
int number_of_subproblem_resolved;
int min_simplexiter;
int max_simplexiter;
};
/*!
* \class WorkerMasterData
Expand Down
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
Loading
Loading