Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into feature/interface_…
Browse files Browse the repository at this point in the history
…probleme

# Conflicts:
#	src/cpp/exe/lpnamer/main.cpp
#	src/cpp/lpnamer/problem_modifier/LinkProblemsGenerator.cpp
#	src/cpp/lpnamer/problem_modifier/LinkProblemsGenerator.h
#	src/cpp/lpnamer/problem_modifier/MasterGeneration.cpp
  • Loading branch information
JasonMarechal25 committed Nov 16, 2022
2 parents f9f8c2c + b0a433c commit d7e7de1
Show file tree
Hide file tree
Showing 16 changed files with 178 additions and 85 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
_build
_install
antares-deps
include/google

# Cmake generated files
CMakeCache.txt
Expand All @@ -53,4 +54,8 @@ build_config.yaml
coverage-reports

# Doc pdf file
antaresxpansionuserguide.pdf
antaresxpansionuserguide.pdf
include/google/protobuf/message.h

# vscode
.vscode
34 changes: 34 additions & 0 deletions conception/Architecture_decision_records/Zip_Lib.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Choose Zip Library
===
This ADR aims to select the library that will be used to manage MPS files in ZIP archives.

Status
===
[minizip-ng](https://github.com/zlib-ng/minizip-ng): Accepted [08/2022]

Context
===
Mps files produced by both Antares Simulator and Xpansion can have a signicant weigths on disk space. It's has been proven that putting them in an archive does not alterate Antares and Xpansion algorithms and naturaly resulting zipped files has a less demand on disk space.

Decision
===

[libzip](https://libzip.org/) is a C library for reading, creating, and modifying zip archives. It's the most cited lib based on Google searchs. The first inconvenient of this lib is the repetitive manipulation of pointers and the risks that come with.
[libzippp](https://github.com/ctabin/libzippp) is a simple basic C++ wrapper around the [libzip](https://libzip.org/) library. It is meant to be a portable and easy-to-use library for ZIP handling.


libzip & libzippp were rejected because of the way they handle writes:

- Create a temporary copy of the archive cp archive.zip archive.zip.<random suffix>
- Write the new entry/entries to the temporary archive
- Replace the archive with the temporary archive mv archive.zip.<random suffix> archive.zip

This strategy ensures that a valid archive remains even in case the program is interrupted, mv being considered an atomic operation. However, it generates a lot of I/O, which is not suitable for us. This behavior cannot be disabled, meaning it is impossible to write entries straight to the archive.

[minizip-ng](https://github.com/zlib-ng/minizip-ng) on the other hand writes directly to the archive. Attention must be paid to call mz_zip_writer_close in case of user/system interruption. Signal handlers can be used, see e.g AntaresSimulatorTeam/Antares_Simulator#827

libzip and it's wrapper libzippp : rejected

Consequences
===
Add [minizip-ng](https://github.com/zlib-ng/minizip-ng) as dependency in [antares-deps](https://github.com/AntaresSimulatorTeam/antares-deps)
59 changes: 45 additions & 14 deletions src/cpp/lpnamer/helper/ProblemGenerationLogger.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include "ProblemGenerationLogger.h"

#include "Clock.h"

namespace ProblemGenerationLog {

std::string LogLevelToStr(const LOGLEVEL logLevel) {
switch (logLevel) {
std::string LogLevelToStr(const LOGLEVEL log_level) {
switch (log_level) {
case LOGLEVEL::TRACE:
return "<Trace> ";
case LOGLEVEL::DEBUG:
Expand All @@ -25,6 +26,7 @@ std::string LogLevelToStr(const LOGLEVEL logLevel) {
ProblemGenerationFileLogger::ProblemGenerationFileLogger(
const std::filesystem::path& logFilePath)
: logFilePath_(logFilePath) {
SetType(LOGGERTYPE::FILE);
logFile_.open(logFilePath_, std::ofstream::out | std::ofstream::app);
if (logFile_.fail()) {
std::cerr << "ProblemGenerationFileLogger: Invalid file ("
Expand All @@ -39,6 +41,7 @@ void ProblemGenerationFileLogger::DisplayMessage(const std::string& message) {
ProblemGenerationOstreamLogger::ProblemGenerationOstreamLogger(
std::ostream& stream)
: stream_(stream) {
SetType(LOGGERTYPE::CONSOLE);
if (stream_.fail()) {
std::cerr
<< "ProblemGenerationOstreamLogger: Invalid stream passed as parameter"
Expand All @@ -49,33 +52,61 @@ void ProblemGenerationOstreamLogger::DisplayMessage(
const std::string& message) {
stream_ << message << std::endl;
}

void ProblemGenerationLogger::DisplayMessage(const std::string& message)const {
for (const auto& logger : loggers_) {
void ProblemGenerationLogger::AddLogger(
const ProblemGenerationILoggerSharedPointer& logger) {
loggers_.push_back(logger);
try_to_add_logger_to_enabled_list(logger);
}
void ProblemGenerationLogger::DisplayMessage(const std::string& message) const {
for (const auto& logger : enabled_loggers_) {
logger->DisplayMessage(message);
}
}
void ProblemGenerationLogger::DisplayMessage(const std::string& message,
const LOGLEVEL logLevel)const {
for (const auto& logger : loggers_) {
logger->DisplayMessage(LogLevelToStr(logLevel));
const LOGLEVEL log_level) const {
for (const auto& logger : enabled_loggers_) {
logger->DisplayMessage(LogLevelToStr(log_level));
logger->DisplayMessage(message);
}
}
std::string ProblemGenerationLogger::PrefixMessage(const LOGLEVEL& logLevel) const {
return LogLevelToStr(logLevel) + clock_utils::timeToStr(std::time(nullptr))+ ": ";
void ProblemGenerationLogger::setLogLevel(const LOGLEVEL log_level) {
log_level_ = log_level;
prefix_ = LogLevelToStr(log_level_);
update_enabled_logger();
}
void ProblemGenerationLogger::update_enabled_logger() {
enabled_loggers_.clear();
for (const auto& logger : loggers_) {
try_to_add_logger_to_enabled_list(logger);
}
}
bool ProblemGenerationLogger::try_to_add_logger_to_enabled_list(
const ProblemGenerationILoggerSharedPointer& logger) {
if ((logger->Type() == LOGGERTYPE::CONSOLE && log_level_ != LOGLEVEL::DEBUG &&
log_level_ != LOGLEVEL::TRACE) ||
logger->Type() == LOGGERTYPE::FILE) {
enabled_loggers_.insert(logger);
return true;
}
return false;
}
std::string ProblemGenerationLogger::PrefixMessage(
const LOGLEVEL& log_level) const {
return LogLevelToStr(log_level) + clock_utils::timeToStr(std::time(nullptr)) +
": ";
}
ProblemGenerationLogger& ProblemGenerationLogger::operator<<(
const LOGLEVEL logLevel) {
for (const auto& subLogger : loggers_) {
subLogger->GetOstreamObject() << PrefixMessage(logLevel);
const LOGLEVEL log_level) {
setLogLevel(log_level);
for (const auto& subLogger : enabled_loggers_) {
subLogger->GetOstreamObject() << PrefixMessage(log_level);
}
return *this;
}

ProblemGenerationLogger& ProblemGenerationLogger::operator<<(
std::ostream& (*f)(std::ostream&)) {
for (const auto& subLogger : loggers_) {
for (const auto& subLogger : enabled_loggers_) {
subLogger->GetOstreamObject() << f;
}
return *this;
Expand Down
63 changes: 40 additions & 23 deletions src/cpp/lpnamer/helper/ProblemGenerationLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,31 @@
#include <list>
#include <memory>
#include <ostream>
#include <set>
#include <string>

inline std::string LogLocationToStr(int line, const char* file,
const char* func) {
return std::string("This is line ") + std::to_string(line) + " of file " +
file + " (function " + func + ")\n";
}
#define LOGLOCATION LogLocationToStr(__LINE__, __FILE__, __func__)
namespace ProblemGenerationLog {

enum class LOGLEVEL { NONE, TRACE, DEBUG, INFO, WARNING, ERROR, FATAL };
std::string LogLevelToStr(const LOGLEVEL logLevel);
enum class LOGGERTYPE { NONE, FILE, CONSOLE };
std::string LogLevelToStr(const LOGLEVEL log_level);
class ProblemGenerationILogger {
public:
virtual ~ProblemGenerationILogger() = default;
virtual void DisplayMessage(const std::string& message) = 0;
virtual std::ostream& GetOstreamObject() = 0;
LOGGERTYPE Type() const { return type_; }

protected:
void SetType(const LOGGERTYPE& type) { type_ = type; }

private:
LOGGERTYPE type_ = LOGGERTYPE::NONE;
};
using ProblemGenerationILoggerSharedPointer =
std::shared_ptr<ProblemGenerationILogger>;
Expand All @@ -28,7 +43,8 @@ class ProblemGenerationFileLogger : public ProblemGenerationILogger {

public:
~ProblemGenerationFileLogger() override { logFile_.close(); }
explicit ProblemGenerationFileLogger(const std::filesystem::path& logFilePath);
explicit ProblemGenerationFileLogger(
const std::filesystem::path& logFilePath);
void DisplayMessage(const std::string& message) override;
std::ostream& GetOstreamObject() override { return logFile_; }
};
Expand All @@ -38,10 +54,10 @@ class ProblemGenerationOstreamLogger : public ProblemGenerationILogger {
std::ostream& stream_;

public:
~ProblemGenerationOstreamLogger() override = default;
explicit ProblemGenerationOstreamLogger(std::ostream& stream) ;
~ProblemGenerationOstreamLogger() override = default;
explicit ProblemGenerationOstreamLogger(std::ostream& stream);
void DisplayMessage(const std::string& message) override;
std::ostream& GetOstreamObject() override{ return stream_; }
std::ostream& GetOstreamObject() override { return stream_; }
};

class ProblemGenerationLogger;
Expand All @@ -51,26 +67,23 @@ using ProblemGenerationLoggerSharedPointer =
class ProblemGenerationLogger {
private:
std::string prefix_;
LOGLEVEL logLevel_;
LOGLEVEL log_level_;

public:
explicit ProblemGenerationLogger(const LOGLEVEL logLevel)
: prefix_(LogLevelToStr(logLevel)), logLevel_(logLevel) {}
explicit ProblemGenerationLogger(const LOGLEVEL log_level)
: prefix_(LogLevelToStr(log_level)), log_level_(log_level) {}
~ProblemGenerationLogger() = default;

void AddLogger(const ProblemGenerationILoggerSharedPointer& logger) {
loggers_.push_back(logger);
}
void DisplayMessage(const std::string& message)const;
void DisplayMessage(const std::string& message, const LOGLEVEL logLevel)const;
void setLogLevel(const LOGLEVEL logLevel) {
logLevel_ = logLevel;
prefix_ = LogLevelToStr(logLevel_);
}
std::string PrefixMessage() const { return PrefixMessage(logLevel_); }
void AddLogger(const ProblemGenerationILoggerSharedPointer& logger);
void DisplayMessage(const std::string& message) const;
void DisplayMessage(const std::string& message,
const LOGLEVEL log_level) const;
void setLogLevel(const LOGLEVEL log_level);

std::string PrefixMessage() const { return PrefixMessage(log_level_); }
std::string PrefixMessage(const LOGLEVEL&) const;
ProblemGenerationLogger& operator()(const LOGLEVEL logLevel) {
return (*this) << logLevel;
ProblemGenerationLogger& operator()(const LOGLEVEL log_level) {
return (*this) << log_level;
}
ProblemGenerationLogger& operator()() { return (*this) << PrefixMessage(); }

Expand All @@ -79,17 +92,21 @@ class ProblemGenerationLogger {
const ProblemGenerationLoggerSharedPointer logger) {
return (*logger);
}
ProblemGenerationLogger& operator<<(const LOGLEVEL logLevel);
ProblemGenerationLogger& operator<<(const LOGLEVEL log_level);

template <typename T>
ProblemGenerationLogger& operator<<(T const& t);

private:
std::list<ProblemGenerationILoggerSharedPointer> loggers_;
std::set<ProblemGenerationILoggerSharedPointer> enabled_loggers_;
void update_enabled_logger();
bool try_to_add_logger_to_enabled_list(
const ProblemGenerationILoggerSharedPointer& logger);
};
template <typename T>
ProblemGenerationLogger& ProblemGenerationLogger::operator<<(T const& t) {
for (const auto& subLogger : loggers_) {
for (const auto& subLogger : enabled_loggers_) {
subLogger->GetOstreamObject() << t;
}
return *this;
Expand Down
9 changes: 5 additions & 4 deletions src/cpp/lpnamer/input_reader/LinkProfileReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ std::vector<LinkProfile> LinkProfileReader::ReadLinkProfile(
const std::filesystem::path &direct_filename,
const std::filesystem::path &indirect_file_name) {
(*logger_)(ProblemGenerationLog::LOGLEVEL::DEBUG)
<< "direct_filename : " << direct_filename << "\n"
<< LOGLOCATION << "direct_filename : " << direct_filename << "\n"
<< "indirect_file_name: " << indirect_file_name << "\n";
EnsureFileIsGood(direct_filename);
EnsureFileIsGood(indirect_file_name);
Expand All @@ -16,7 +16,7 @@ std::vector<LinkProfile> LinkProfileReader::ReadLinkProfile(
return result;
}
void LinkProfileReader::EnsureFileIsGood(
const std::filesystem::path &direct_filename)const {
const std::filesystem::path &direct_filename) const {
if (std::ifstream infile(direct_filename); !infile.good()) {
(*logger_)(ProblemGenerationLog::LOGLEVEL::FATAL)
<< "unable to open file" << direct_filename;
Expand Down Expand Up @@ -67,7 +67,8 @@ void LinkProfileReader::ReadLinkProfile(const std::filesystem::path &filename,
}
void LinkProfileReader::UpdateProfile(std::vector<LinkProfile> &result,
bool directProfile, double value,
int chronicle_id, size_t time_step)const {
int chronicle_id,
size_t time_step) const {
LinkProfile &profile = result.at(chronicle_id);
if (directProfile) {
profile.direct_link_profile.at(time_step) = value;
Expand All @@ -77,7 +78,7 @@ void LinkProfileReader::UpdateProfile(std::vector<LinkProfile> &result,
}

void LinkProfileReader::ConstructChronicle(std::vector<LinkProfile> &result,
int chronicle_id) const{
int chronicle_id) const {
if (result.size() <= chronicle_id) {
result.emplace_back();
}
Expand Down
6 changes: 4 additions & 2 deletions src/cpp/lpnamer/problem_modifier/MasterGeneration.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ class MasterGeneration {
const AdditionalConstraints &additionalConstraints_p,
const Couplings &couplings, std::string const &master_formulation,
std::string const &solver_name,
ProblemGenerationLog::ProblemGenerationLoggerSharedPointer logger);
ProblemGenerationLog::ProblemGenerationLoggerSharedPointer logger,
const std::filesystem::path &log_file_path);

private: /*methods*/
void add_candidates(const std::vector<ActiveLink> &links);
void write_master_mps(const std::filesystem::path &rootPath,
std::string const &master_formulation,
std::string const &solver_name,
const AdditionalConstraints &additionalConstraints_p) const;
const AdditionalConstraints &additionalConstraints_p,
const std::filesystem::path &log_file_path) const;
void write_structure_file(const std::filesystem::path &rootPath,
const Couplings &couplings) const;

Expand Down
5 changes: 3 additions & 2 deletions src/cpp/lpnamer/problem_modifier/MasterProblemBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ MasterProblemBuilder::MasterProblemBuilder(
: _master_formulation(master_formulation) {}

std::shared_ptr<SolverAbstract> MasterProblemBuilder::build(
const std::string& solverName, const std::vector<Candidate>& candidates) {
const std::string& solverName, const std::vector<Candidate>& candidates,
const std::filesystem::path& log_file_path) {
_indexOfNvar.clear();
_indexOfPmaxVar.clear();

SolverFactory factory;
auto master_l = factory.create_solver(solverName);
auto master_l = factory.create_solver(solverName, log_file_path);

addVariablesPmaxOnEachCandidate(candidates, master_l);

Expand Down
4 changes: 3 additions & 1 deletion src/cpp/lpnamer/problem_modifier/MasterProblemBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <multisolver_interface/SolverAbstract.h>

#include <filesystem>
#include <unordered_map>

#include "ActiveLinks.h"
Expand All @@ -17,7 +18,8 @@ class MasterProblemBuilder {
public:
explicit MasterProblemBuilder(const std::string& master_formulation);
std::shared_ptr<SolverAbstract> build(
const std::string& solverName, const std::vector<Candidate>& candidates);
const std::string& solverName, const std::vector<Candidate>& candidates,
const std::filesystem::path& log_file_path);

private:
void addNvarOnEachIntegerCandidate(
Expand Down
Loading

0 comments on commit d7e7de1

Please sign in to comment.