Skip to content

Commit

Permalink
fix mc-years bug --windows (#697)
Browse files Browse the repository at this point in the history
* implement SolverLogManager

* check log filepath emptiness

* move buffer initialization

* check FILE*

* logical

* add check on file

* define cbc model before it's file handler

* deal with shared_ptr

* Antares Simulator pre-release test

* test version

* update

* test with simulator v8.7.1

* revert workflows

* share solver log manager

* update tests

* run with simulator 8.8.0-rc2

* get rid of shared_ptr

* add missings file

* Avoid multiple procs to one file

* cerr >>cout

* Rollback

* Test

* Update SolverXpress.cpp

* Update SolverXpress.cpp

* Update antares_driver.py

* Update SolverXpress.cpp

* Update test_antares_driver.py

* Update antares_driver.py

* close log file

* add test

* Benders Step set Xpansion directory

* fix test

* fix test 2

* update

* disable test

* fix multiple call to fclose

* restore

* remove dev files

* delete  file

* fix dangling ref
  • Loading branch information
a-zakir authored Oct 19, 2023
1 parent fa5a99f commit 0643037
Show file tree
Hide file tree
Showing 49 changed files with 237 additions and 154 deletions.
4 changes: 2 additions & 2 deletions antares-version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"antares_version": "8.7.0",
"antares_version_executable": "8.7",
"antares_version": "8.8.0-rc2",
"antares_version_executable": "8.8",
"antares_xpansion_version": "1.1.0",
"antares_deps_version": "2.0.7",
"minizip_ng_version": "3.0.6"
Expand Down
8 changes: 5 additions & 3 deletions src/cpp/benders/benders_core/BendersBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,8 +654,10 @@ LogData BendersBase::bendersDataToLogData(
data.number_of_subproblem_resolved +
cumulative_number_of_subproblem_resolved_before_resume};
}
void BendersBase::set_log_file(const std::filesystem::path &log_name) {
_log_name = log_name;
void BendersBase::set_log_file(const std::filesystem::path &log_file) {
_log_name = log_file;

solver_log_manager_ = SolverLogManager(log_name());
}

/*!
Expand Down Expand Up @@ -715,7 +717,7 @@ void BendersBase::AddSubproblem(
subproblem_map[kvp.first] = std::make_shared<SubproblemWorker>(
kvp.second, GetSubproblemPath(kvp.first),
SubproblemWeight(_data.nsubproblem, kvp.first), _options.SOLVER_NAME,
_options.LOG_LEVEL, log_name(), _logger);
_options.LOG_LEVEL, solver_log_manager_, _logger);
}

void BendersBase::free_subproblems() {
Expand Down
5 changes: 3 additions & 2 deletions src/cpp/benders/benders_core/SubproblemWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
SubproblemWorker::SubproblemWorker(
VariableMap const &variable_map, const std::filesystem::path &path_to_mps,
double const &slave_weight, const std::string &solver_name,
const int log_level, const std::filesystem::path &log_name, Logger logger)
const int log_level, SolverLogManager&solver_log_manager,
Logger logger)
: Worker(logger) {
init(variable_map, path_to_mps, solver_name, log_level, log_name);
init(variable_map, path_to_mps, solver_name, log_level, solver_log_manager);

int mps_ncols(_solver->get_ncols());
DblVector obj_func_coeffs(mps_ncols);
Expand Down
11 changes: 6 additions & 5 deletions src/cpp/benders/benders_core/Worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@ void Worker::get_value(double &lb) const {
void Worker::init(VariableMap const &variable_map,
const std::filesystem::path &path_to_mps,
std::string const &solver_name, int log_level,
const std::filesystem::path &log_name) {
SolverLogManager&solver_log_manager) {
_path_to_mps = path_to_mps;
SolverFactory factory;

if (_is_master) {
_solver =
factory.create_solver(solver_name, SOLVER_TYPE::INTEGER, log_name);
_solver = factory.create_solver(solver_name, SOLVER_TYPE::INTEGER,
solver_log_manager);
} else {
_solver =
factory.create_solver(solver_name, SOLVER_TYPE::CONTINUOUS, log_name);
_solver = factory.create_solver(solver_name, SOLVER_TYPE::CONTINUOUS,
solver_log_manager);
}

_solver->init();
Expand Down
13 changes: 6 additions & 7 deletions src/cpp/benders/benders_core/WorkerMaster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@ WorkerMaster::WorkerMaster(Logger logger) : Worker(logger) {
* \param log_level : solver log level
* \param subproblems_count : number of subproblems
*/
WorkerMaster::WorkerMaster(VariableMap const &variable_map,
const std::filesystem::path &path_to_mps,
const std::string &solver_name, const int log_level,
int subproblems_count,
const std::filesystem::path &log_name,
const bool mps_has_alpha, Logger logger)
WorkerMaster::WorkerMaster(
VariableMap const &variable_map, const std::filesystem::path &path_to_mps,
const std::string &solver_name, const int log_level, int subproblems_count,
SolverLogManager&solver_log_manager,
const bool mps_has_alpha, Logger logger)
: Worker(logger),
subproblems_count(subproblems_count),
_mps_has_alpha(mps_has_alpha) {
_is_master = true;

init(variable_map, path_to_mps, solver_name, log_level, log_name);
init(variable_map, path_to_mps, solver_name, log_level, solver_log_manager);
if (!_mps_has_alpha) {
_set_upper_bounds();
}
Expand Down
4 changes: 3 additions & 1 deletion src/cpp/benders/benders_core/include/BendersBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ class BendersBase {
return cumulative_number_of_subproblem_resolved_before_resume;
}

SolverLogManager solver_log_manager_;

private:
void print_master_and_cut(std::ostream &file, int ite,
WorkerMasterDataPtr &trace, Point const &xopt);
Expand All @@ -157,7 +159,7 @@ class BendersBase {
private:
BendersBaseOptions _options;
unsigned int _totalNbProblems = 0;
std::filesystem::path _log_name;
std::filesystem::path _log_name = "";
BendersRelevantIterationsData relevantIterationData_ = {
std::make_shared<WorkerMasterData>(), nullptr};
WorkerMasterPtr _master;
Expand Down
3 changes: 2 additions & 1 deletion src/cpp/benders/benders_core/include/SubproblemWorker.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class SubproblemWorker : public Worker {
SubproblemWorker(VariableMap const &variable_map,
const std::filesystem::path &path_to_mps,
double const &slave_weight, const std::string &solver_name,
const int log_level, const std::filesystem::path &log_name,
const int log_level,
SolverLogManager&solver_log_manager,
Logger logger);
virtual ~SubproblemWorker() = default;

Expand Down
2 changes: 1 addition & 1 deletion src/cpp/benders/benders_core/include/Worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Worker {
void init(VariableMap const &variable_map,
const std::filesystem::path &path_to_mps,
std::string const &solver_name, int log_level,
const std::filesystem::path &log_name);
SolverLogManager&solver_log_manager);
virtual ~Worker() = default;

void get_value(double &lb) const;
Expand Down
3 changes: 2 additions & 1 deletion src/cpp/benders/benders_core/include/WorkerMaster.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class WorkerMaster : public Worker {
WorkerMaster(VariableMap const &variable_map,
const std::filesystem::path &path_to_mps,
const std::string &solver_name, const int log_level,
int subproblems_count, const std::filesystem::path &log_name,
int subproblems_count,
SolverLogManager&solver_log_manager,
bool mps_has_alpha, Logger logger);
virtual ~WorkerMaster() = default;

Expand Down
4 changes: 2 additions & 2 deletions src/cpp/benders/benders_mpi/BendersMPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ void BendersMpi::BuildMasterProblem() {
if (_world.rank() == rank_0) {
reset_master(new WorkerMaster(master_variable_map, get_master_path(),
get_solver_name(), get_log_level(),
_data.nsubproblem, log_name(), IsResumeMode(),
_logger));
_data.nsubproblem, solver_log_manager_,
IsResumeMode(), _logger));
}
}
/*!
Expand Down
7 changes: 4 additions & 3 deletions src/cpp/benders/benders_sequential/BendersSequential.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ BendersSequential::BendersSequential(BendersBaseOptions const &options,
void BendersSequential::InitializeProblems() {
MatchProblemToId();

reset_master(new WorkerMaster(
master_variable_map, get_master_path(), get_solver_name(),
get_log_level(), _data.nsubproblem, log_name(), IsResumeMode(), _logger));
reset_master(new WorkerMaster(master_variable_map, get_master_path(),
get_solver_name(), get_log_level(),
_data.nsubproblem, solver_log_manager_,
IsResumeMode(), _logger));
for (const auto &problem : coupling_map) {
const auto subProblemFilePath = GetSubproblemPath(problem.first);

Expand Down
3 changes: 2 additions & 1 deletion src/cpp/benders/factories/BendersFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ int RunBenders(char** argv, const std::filesystem::path& options_file,
std::ostringstream oss_l = start_message(options, benders->BendersName());
oss_l << std::endl;
logger->display_message(oss_l.str());

benders->set_log_file(log_reports_name);

writer->write_log_level(options.LOG_LEVEL);
Expand Down Expand Up @@ -118,4 +119,4 @@ BendersMainFactory::BendersMainFactory(
}
int BendersMainFactory::Run() const {
return RunBenders(argv_, options_file_, *penv_, *pworld_, method_);
}
}
6 changes: 5 additions & 1 deletion src/cpp/benders/logger/UserFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ UserFile::UserFile(const std::filesystem::path &filename) {
}
}

UserFile::~UserFile() { _file.close(); }
UserFile::~UserFile() {
if (_file.is_open()) {
_file.close();
}
}

void UserFile::display_message(const std::string &str) {
_file << PrefixMessage(LogUtils::LOGLEVEL::INFO, CONTEXT) << str << std::endl;
Expand Down
16 changes: 9 additions & 7 deletions src/cpp/lpnamer/main/RunProblemGeneration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void validateMasterFormulation(
}

std::vector<std::shared_ptr<Problem>> getXpansionProblems(
const std::filesystem::path& log_file_path, const std::string& solver_name,
SolverLogManager& solver_log_manager, const std::string& solver_name,
const std::vector<ProblemData>& mpsList, std::filesystem::path& lpDir_,
std::shared_ptr<ArchiveReader>& reader) {
std::vector<std::string> problem_names;
Expand All @@ -126,7 +126,7 @@ std::vector<std::shared_ptr<Problem>> getXpansionProblems(
[](ProblemData const& data) { return data._problem_mps; });
auto adapter = std::make_shared<ZipProblemsProviderAdapter>(lpDir_, reader,
problem_names);
return adapter->provideProblems(solver_name, log_file_path);
return adapter->provideProblems(solver_name, solver_log_manager);
}
void RunProblemGeneration(
const std::filesystem::path& xpansion_output_dir,
Expand Down Expand Up @@ -171,16 +171,18 @@ void RunProblemGeneration(

bool use_zip_implementation = true;
bool use_file_implementation = false;

auto solver_log_manager = SolverLogManager(log_file_path);
Couplings couplings;
LinkProblemsGenerator linkProblemsGenerator(lpDir_, links, solver_name,
logger, log_file_path, rename_problems);
LinkProblemsGenerator linkProblemsGenerator(
lpDir_, links, solver_name, logger, solver_log_manager, rename_problems);
if (use_zip_implementation) {
std::shared_ptr<ArchiveReader> reader =
InstantiateZipReader(antares_archive_path);

/* Main stuff */
std::vector<std::shared_ptr<Problem>> xpansion_problems =
getXpansionProblems(log_file_path, solver_name, mpsList, lpDir_,
getXpansionProblems(solver_log_manager, solver_name, mpsList, lpDir_,
reader);

std::vector<std::pair<std::shared_ptr<Problem>, ProblemData>>
Expand Down Expand Up @@ -228,7 +230,7 @@ void RunProblemGeneration(

XpansionProblemsFromAntaresProvider adapter(lps);
auto xpansion_problems =
adapter.provideProblems(solver_name, log_file_path);
adapter.provideProblems(solver_name, solver_log_manager);
std::vector<std::pair<std::shared_ptr<Problem>, ProblemData>>
problems_and_data;
for (int i = 0; i < xpansion_problems.size(); ++i) {
Expand Down Expand Up @@ -259,7 +261,7 @@ void RunProblemGeneration(

MasterGeneration master_generation(
xpansion_output_dir, links, additionalConstraints, couplings,
master_formulation, solver_name, logger, log_file_path);
master_formulation, solver_name, logger, solver_log_manager);
(*logger)(LogUtils::LOGLEVEL::INFO)
<< "Problem Generation ran in: "
<< format_time_str(problem_generation_timer.elapsed()) << std::endl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
std::shared_ptr<Problem>
AntaresProblemToXpansionProblemTranslator::translateToXpansionProblem(
const LpsFromAntares& lps, unsigned int year, unsigned int week,
const std::string& solver_name,
const std::filesystem::path& log_file_path) {
const std::string& solver_name, SolverLogManager& solver_log_manager) {
SolverFactory factory;
auto problem = std::make_shared<Problem>(
factory.create_solver(solver_name, log_file_path));
factory.create_solver(solver_name, solver_log_manager));
problem->init();
const auto& constant = lps._constant;
const auto& hebdo = lps._hebdo.at({year, week});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class AntaresProblemToXpansionProblemTranslator {
public:
[[nodiscard]] static std::shared_ptr<Problem> translateToXpansionProblem(
const LpsFromAntares& lps, unsigned int year, unsigned int week,
const std::string& solver_name,
const std::filesystem::path& log_file_path);
const std::string& solver_name, SolverLogManager& solver_log_manager);
static std::vector<char> convertSignToLEG(char* data);
};
2 changes: 1 addition & 1 deletion src/cpp/lpnamer/problem_modifier/IProblemProviderPort.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ class IProblemProviderPort {
virtual ~IProblemProviderPort() = default;
[[nodiscard]] virtual std::shared_ptr<Problem> provide_problem(
const std::string& solver_name,
const std::filesystem::path& log_file_path) const = 0;
SolverLogManager& solver_log_manager) const = 0;
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ class IXpansionProblemsProvider {
virtual ~IXpansionProblemsProvider() = default;
[[nodiscard]] virtual std::vector<std::shared_ptr<Problem>> provideProblems(
const std::string& solver_name,
const std::filesystem::path& log_file_path) const = 0;
SolverLogManager& solver_log_manager) const = 0;
};
11 changes: 5 additions & 6 deletions src/cpp/lpnamer/problem_modifier/LinkProblemsGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,16 @@
void LinkProblemsGenerator::treat(
const std::string &problem_name, Couplings &couplings,
IProblemProviderPort *problem_provider,
IProblemVariablesProviderPort *variable_provider,
IProblemWriter *writer) const {
IProblemVariablesProviderPort *variable_provider, IProblemWriter *writer) {
auto in_prblm =
problem_provider->provide_problem(_solver_name, log_file_path_);
problem_provider->provide_problem(_solver_name, solver_log_manager_);

treat(problem_name, couplings, in_prblm.get(), variable_provider, writer);
}

void LinkProblemsGenerator::treat(
const std::string &problem_name, Couplings &couplings, Problem *problem,
IProblemVariablesProviderPort *variable_provider,
IProblemWriter *writer) const {
IProblemVariablesProviderPort *variable_provider, IProblemWriter *writer) {
ProblemVariables problem_variables = variable_provider->Provide();

if (rename_problems_) {
Expand Down Expand Up @@ -74,7 +72,8 @@ void LinkProblemsGenerator::treatloop(const std::filesystem::path &root,
[&](const auto &mps) {
auto adapter = std::make_unique<MPSFileProblemProviderAdapter>(
root, mps._problem_mps);
auto problem = adapter->provide_problem(_solver_name, log_file_path_);
auto problem =
adapter->provide_problem(_solver_name, solver_log_manager_);
std::unique_ptr<IProblemVariablesProviderPort> variables_provider;
if (rename_problems_) {
variables_provider = std::make_unique<ProblemVariablesFileAdapter>(
Expand Down
11 changes: 5 additions & 6 deletions src/cpp/lpnamer/problem_modifier/LinkProblemsGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,31 @@ class LinkProblemsGenerator {
std::filesystem::path& lpDir, const std::vector<ActiveLink>& links,
std::string solver_name,
ProblemGenerationLog::ProblemGenerationLoggerSharedPointer logger,
std::filesystem::path log_file_path,
bool rename_problems)
SolverLogManager& solver_log_manager, bool rename_problems)
: _links(links),
_solver_name(std::move(solver_name)),
lpDir_(lpDir),
logger_(std::move(logger)),
log_file_path_(std::move(log_file_path)),
solver_log_manager_(solver_log_manager),
rename_problems_(rename_problems) {}

void treatloop(const std::filesystem::path& root, Couplings& couplings,
const std::vector<ProblemData>& mps_list,
IProblemWriter* writer);
void treat(const std::string& problem_name, Couplings& couplings,
Problem* problem, IProblemVariablesProviderPort* variable_provider,
IProblemWriter* writer) const;
IProblemWriter* writer);
void treat(const std::string& problem_name, Couplings& couplings,
IProblemProviderPort* problem_provider,
IProblemVariablesProviderPort* variable_provider,
IProblemWriter* writer) const;
IProblemWriter* writer);

private:
const std::vector<ActiveLink>& _links;
std::string _solver_name;
std::filesystem::path lpDir_ = "";
ProblemGenerationLog::ProblemGenerationLoggerSharedPointer logger_;
mutable std::mutex coupling_mutex_;
std::filesystem::path log_file_path_;
bool rename_problems_ = false;
SolverLogManager& solver_log_manager_;
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
#include "multisolver_interface/SolverFactory.h"
std::shared_ptr<Problem> MPSFileProblemProviderAdapter::provide_problem(
const std::string& solver_name,
const std::filesystem::path& log_file_path) const {
SolverLogManager& solver_log_manager) const {
SolverFactory factory;
auto const lp_mps_name = lp_dir_ / problem_name_;
auto in_prblm = std::make_shared<Problem>(
factory.create_solver(solver_name, log_file_path));
factory.create_solver(solver_name, solver_log_manager));

in_prblm->read_prob_mps(lp_mps_name);
return in_prblm;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class MPSFileProblemProviderAdapter : public IProblemProviderPort {
const std::string& problem_name);
[[nodiscard]] std::shared_ptr<Problem> provide_problem(
const std::string& solver_name,
const std::filesystem::path& log_file_path) const override;
SolverLogManager& solver_log_manager) const override;
const std::filesystem::path lp_dir_;
const std::string& problem_name_;
};
Loading

0 comments on commit 0643037

Please sign in to comment.