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

Write subproblems solution of last iteration #905

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions src/cpp/benders/benders_by_batch/BendersByBatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ void BendersByBatch::Run() {
CloseCsvFile();
EndWritingInOutputFile();
write_basis();
writeLastIterationSubproblemsSolution();
}
}

Expand Down
59 changes: 34 additions & 25 deletions src/cpp/benders/benders_core/BendersBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ BendersBase::BendersBase(const BendersBaseOptions &options, Logger logger,
(_options.CSV_NAME + ".csv")),
_logger(std::move(logger)),
_writer(std::move(writer)),
mathLoggerDriver_(std::move(mathLoggerDriver)) {
}
mathLoggerDriver_(std::move(mathLoggerDriver)) {}

std::filesystem::path BendersBase::OuterloopOptionsFile() const {
return std::filesystem::path(_options.INPUTROOT) /
Expand Down Expand Up @@ -61,7 +60,8 @@ void BendersBase::OpenCsvFile() {
<< std::endl;
} else {
using namespace std::string_literals;
_logger->display_message("Impossible to open the .csv file: "s + _csv_file_path.string());
_logger->display_message("Impossible to open the .csv file: "s +
_csv_file_path.string());
}
}
}
Expand Down Expand Up @@ -176,7 +176,8 @@ void BendersBase::update_best_ub() {
_data.outer_loop_current_iteration_data.max_criterion;
_data.outer_loop_current_iteration_data.max_criterion_area_best_it =
_data.outer_loop_current_iteration_data.max_criterion_area;
relevantIterationData_.best._cut_trace = relevantIterationData_.last._cut_trace;
relevantIterationData_.best._cut_trace =
relevantIterationData_.last._cut_trace;
best_iteration_data = bendersDataToLogData(_data);
}
}
Expand Down Expand Up @@ -775,8 +776,6 @@ void BendersBase::MatchProblemToId() {
}
}



void BendersBase::AddSubproblemName(const std::string &name) {
subproblems.push_back(name);
}
Expand Down Expand Up @@ -820,22 +819,24 @@ void BendersBase::SetSubproblemCost(const double &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;

* \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();
void BendersBase::ResetSimplexIterationsBounds() {
_data.max_simplexiter = 0;
_data.min_simplexiter = std::numeric_limits<int>::max();
}
bool BendersBase::IsResumeMode() const { return _options.RESUME; }

Expand Down Expand Up @@ -911,6 +912,16 @@ void BendersBase::write_basis() const {
_master->write_basis(filename);
}

void BendersBase::writeLastIterationSubproblemsSolution() const {
const auto dirname(std::filesystem::path(_options.OUTPUTROOT));
const auto &sub_pblm_map = GetSubProblemMap();

for (const auto &[name, worker] : sub_pblm_map) {
worker->_solver->write_sol(dirname /
std::filesystem::path(name).replace_extension());
}
}

WorkerMasterDataVect BendersBase::AllCuts() const {
return workerMasterDataVect_;
}
Expand All @@ -937,9 +948,9 @@ void BendersBase::MasterAddRows(
}
void BendersBase::ResetMasterFromLastIteration() {
reset_master<WorkerMaster>(master_variable_map_, LastMasterPath(),
get_solver_name(), get_log_level(),
_data.nsubproblem, solver_log_manager_,
IsResumeMode(), _logger);
get_solver_name(), get_log_level(),
_data.nsubproblem, solver_log_manager_,
IsResumeMode(), _logger);
}
bool BendersBase::MasterIsEmpty() const { return master_is_empty_; }

Expand Down Expand Up @@ -1017,8 +1028,6 @@ void BendersBase::init_data(double external_loop_lambda,
external_loop_lambda_max;
}



bool BendersBase::isExceptionRaised() const { return exception_raised_; }
/*
* after the 1st loop of the outer loop, we must re-build the objective
Expand Down
1 change: 1 addition & 0 deletions src/cpp/benders/benders_core/include/BendersBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ class BendersBase {
}
double GetBendersTime() const;
virtual void write_basis() const;
void writeLastIterationSubproblemsSolution() const;
// SubproblemsMapPtr GetSubProblemsMapPtr() { return subproblem_map; }
SubproblemsMapPtr GetSubProblemMap() const { return subproblem_map; }
StrVector GetSubProblemNames() const { return subproblems; }
Expand Down
1 change: 1 addition & 0 deletions src/cpp/benders/benders_mpi/BendersMPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ void BendersMpi::Run() {
CloseCsvFile();
EndWritingInOutputFile();
write_basis();
writeLastIterationSubproblemsSolution();
}
_world.barrier();
}
Expand Down
1 change: 1 addition & 0 deletions src/cpp/benders/benders_sequential/BendersSequential.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ void BendersSequential::Run() {
CloseCsvFile();
EndWritingInOutputFile();
write_basis();
writeLastIterationSubproblemsSolution();
}

void BendersSequential::launch() {
Expand Down
3 changes: 3 additions & 0 deletions src/cpp/lpnamer/model/Problem.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class Problem : public SolverAbstract {
void write_prob_mps(const std::filesystem::path &filename) override {
solver_abstract_->write_prob_mps(filename);
}
void write_sol(const std::filesystem::path &filename) override {
solver_abstract_->write_sol(filename);
}
void write_prob_lp(const std::filesystem::path &filename) override {
solver_abstract_->write_prob_lp(filename);
}
Expand Down
2 changes: 2 additions & 0 deletions src/cpp/multisolver_interface/SolverCbc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ void SolverCbc::write_basis(const std::filesystem::path &filename) {
zero_status_check(status, "write basis", LOGLOCATION);
}

void SolverCbc::write_sol(const std::filesystem::path &filename) {}

void SolverCbc::setClpSimplexColNamesFromInnerSolver(ClpSimplex *clps) const {
for (int col_id(0); col_id < get_ncols(); col_id++) {
std::string name = _clp_inner_solver.getColName(col_id);
Expand Down
1 change: 1 addition & 0 deletions src/cpp/multisolver_interface/SolverCbc.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class SolverCbc : public SolverAbstract {
virtual void write_prob_mps(const std::filesystem::path &filename) override;
virtual void write_prob_lp(const std::filesystem::path &filename) override;
virtual void write_basis(const std::filesystem::path &filename) override;
virtual void write_sol(const std::filesystem::path &filename) override;

virtual void read_prob_mps(const std::filesystem::path &filename) override;
virtual void read_prob_lp(const std::filesystem::path &filename) override;
Expand Down
2 changes: 2 additions & 0 deletions src/cpp/multisolver_interface/SolverClp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ void SolverClp::write_basis(const std::filesystem::path &filename) {
zero_status_check(status, "write basis", LOGLOCATION);
}

void SolverClp::write_sol(const std::filesystem::path &filename) {}

void SolverClp::read_prob_mps(const std::filesystem::path &filename) {
int status = _clp.readMps(filename.string().c_str(), true, false);
zero_status_check(status, " Clp readMps "s + filename.string(), LOGLOCATION);
Expand Down
1 change: 1 addition & 0 deletions src/cpp/multisolver_interface/SolverClp.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class SolverClp : public SolverAbstract {
virtual void write_prob_mps(const std::filesystem::path &filename) override;
virtual void write_prob_lp(const std::filesystem::path &filename) override;
virtual void write_basis(const std::filesystem::path &filename) override;
virtual void write_sol(const std::filesystem::path &filename) override;

virtual void read_prob_mps(const std::filesystem::path &filename) override;
virtual void read_prob_lp(const std::filesystem::path &filename) override;
Expand Down
5 changes: 5 additions & 0 deletions src/cpp/multisolver_interface/SolverXpress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ void SolverXpress::write_basis(const std::filesystem::path &filename) {
zero_status_check(status, "write basis", LOGLOCATION);
}

void SolverXpress::write_sol(const std::filesystem::path &filename) {
int status = XPRSwritesol(_xprs, filename.string().c_str(), "");
zero_status_check(status, "write solution", LOGLOCATION);
}

void SolverXpress::read_prob_mps(const std::filesystem::path &filename) {
std::string nFlags = "";
read_prob(filename.string().c_str(), nFlags.c_str());
Expand Down
1 change: 1 addition & 0 deletions src/cpp/multisolver_interface/SolverXpress.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class SolverXpress : public SolverAbstract {
virtual void write_prob_mps(const std::filesystem::path &filename) override;
virtual void write_prob_lp(const std::filesystem::path &filename) override;
virtual void write_basis(const std::filesystem::path &filename) override;
virtual void write_sol(const std::filesystem::path &filename) override;

virtual void read_prob_mps(const std::filesystem::path &filename) override;
virtual void read_prob_lp(const std::filesystem::path &filename) override;
Expand Down
3 changes: 3 additions & 0 deletions src/cpp/multisolver_interface/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ std::function<int(XPRSprob dest, XPRSprob src, const char* name)> XPRScopyprob =
nullptr;
std::function<int(XPRSprob prob, const char* filename, const char* flags)>
XPRSwritebasis = nullptr;
std::function<int(XPRSprob prob, const char* filename, const char* flags)>
XPRSwritesol = nullptr;
std::function<int(XPRSprob prob, const char* filename, const char* flags)>
XPRSreadprob = nullptr;
std::function<int(XPRSprob prob, const char* filename, const char* flags)>
Expand Down Expand Up @@ -149,6 +151,7 @@ bool XpressLoader::LoadXpressFunctions(DynamicLibrary* xpress_dynamic_library) {
xpress_dynamic_library->GetFunction(&XPRSinit, "XPRSinit");
xpress_dynamic_library->GetFunction(&XPRScopyprob, "XPRScopyprob");
xpress_dynamic_library->GetFunction(&XPRSwritebasis, "XPRSwritebasis");
xpress_dynamic_library->GetFunction(&XPRSwritesol, "XPRSwritesol");
xpress_dynamic_library->GetFunction(&XPRSreadprob, "XPRSreadprob");
xpress_dynamic_library->GetFunction(&XPRSreadbasis, "XPRSreadbasis");
xpress_dynamic_library->GetFunction(&XPRSgetrows, "XPRSgetrows");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,13 @@ class SolverAbstract {
*/
virtual void write_basis(const std::filesystem::path &filename) = 0;

/**
* @brief Writes the current solution to a CSV format ASCII file, problem_name.asc (and .hdr).
*
* @param filename : file name where the solution is written
*/
virtual void write_sol(const std::filesystem::path &filename) = 0;

/**
* @brief reads an optimization problem contained in a MPS file
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ extern std::function<int(const char* path)> XPRSinit;
extern std::function<int(XPRSprob* p_prob)> XPRScreateprob;
extern std::function<int(XPRSprob dest, XPRSprob src, const char* name)> XPRScopyprob;
extern std::function<int(XPRSprob prob, const char* filename, const char* flags)> XPRSwritebasis;
extern std::function<int(XPRSprob prob, const char* filename, const char* flags)> XPRSwritesol;
extern std::function<int(XPRSprob prob, const char* filename, const char* flags)> XPRSreadprob;
extern std::function<int(XPRSprob prob, const char* filename, const char* flags)> XPRSreadbasis;
extern std::function<int(XPRSprob prob, int start[], int colind[], double colcoef[], int maxcoefs, int* p_ncoefs, int first, int last)> XPRSgetrows;
Expand Down
1 change: 1 addition & 0 deletions tests/cpp/lp_namer/NOOPSolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class NOOPSolver: public SolverAbstract {
virtual void free() override {}
virtual void write_prob_mps(const std::filesystem::path &filename) override {}
virtual void write_prob_lp(const std::filesystem::path &filename) override {}
virtual void write_sol(const std::filesystem::path &filename) override {}
virtual void read_prob_mps(const std::filesystem::path &filename) override {}
virtual void read_prob_lp(const std::filesystem::path &filename) override {}
virtual void copy_prob(Ptr fictif_solv) override {}
Expand Down
Loading