Skip to content

Commit

Permalink
Merge pull request #684 from AntaresSimulatorTeam/feature/refactoring
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
JasonMarechal25 authored Sep 14, 2023
2 parents d1ef5f1 + 34cb3f0 commit c67070f
Show file tree
Hide file tree
Showing 23 changed files with 217 additions and 212 deletions.
2 changes: 1 addition & 1 deletion src/cpp/benders/benders_core/SubproblemWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ SubproblemWorker::SubproblemWorker(
for (int i = 0; i < mps_ncols; ++i) {
sequence[i] = i;
}
solver_get_obj_func_coeffs(_solver, obj_func_coeffs, 0, mps_ncols - 1);
solver_get_obj_func_coeffs(*_solver, obj_func_coeffs, 0, mps_ncols - 1);
for (auto &c : obj_func_coeffs) {
c *= slave_weight;
}
Expand Down
14 changes: 7 additions & 7 deletions src/cpp/benders/benders_core/WorkerMaster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ void WorkerMaster::add_cut(Point const &s, Point const &x_cut,
DefineRhsWithMasterVariable(s, x_cut, rhs, rowrhs);
define_matval_mclind(s, matval, mclind);

solver_addrows(_solver, rowtype, rowrhs, {}, mstart, mclind, matval);
solver_addrows(*_solver, rowtype, rowrhs, {}, mstart, mclind, matval);
}

void WorkerMaster::DefineRhsWithMasterVariable(
Expand Down Expand Up @@ -154,7 +154,7 @@ void WorkerMaster::add_dynamic_cut(Point const &s, double const &sx0,

define_rhs_from_sx0(sx0, rhs, rowrhs);
define_matval_mclind(s, matval, mclind);
solver_addrows(_solver, rowtype, rowrhs, {}, mstart, mclind, matval);
solver_addrows(*_solver, rowtype, rowrhs, {}, mstart, mclind, matval);
}

void WorkerMaster::define_rhs_from_sx0(const double &sx0, const double &rhs,
Expand Down Expand Up @@ -184,7 +184,7 @@ void WorkerMaster::add_cut_by_iter(int const i, Point const &s,
define_rhs_from_sx0(sx0, rhs, rowrhs);
define_matval_mclind_for_index(i, s, matval, mclind);

solver_addrows(_solver, rowtype, rowrhs, {}, mstart, mclind, matval);
solver_addrows(*_solver, rowtype, rowrhs, {}, mstart, mclind, matval);
}

void WorkerMaster::define_matval_mclind_for_index(
Expand Down Expand Up @@ -224,7 +224,7 @@ void WorkerMaster::addSubproblemCut(int i, Point const &s, Point const &x_cut,
DefineRhsWithMasterVariable(s, x_cut, rhs, rowrhs);
define_matval_mclind_for_index(i, s, matval, mclind);

solver_addrows(_solver, rowtype, rowrhs, {}, mstart, mclind, matval);
solver_addrows(*_solver, rowtype, rowrhs, {}, mstart, mclind, matval);
}

void WorkerMaster::_set_upper_bounds() const {
Expand Down Expand Up @@ -265,7 +265,7 @@ void WorkerMaster::_set_alpha_var() {
_solver->get_ncols(); /* Set the number of columns in _id_alpha */

solver_addcols(
_solver, DblVector(1, obj), IntVector(1, 0), IntVector(0, 0),
*_solver, DblVector(1, obj), IntVector(1, 0), IntVector(0, 0),
DblVector(0, 0.0), DblVector(1, lb), DblVector(1, ub),
CharVector(1, 'C'),
StrVector(1,
Expand All @@ -277,7 +277,7 @@ void WorkerMaster::_set_alpha_var() {
buffer << "alpha_" << i;
id_single_subpb_costs_under_approx_[i] = _solver->get_ncols();
solver_addcols(
_solver, DblVector(1, 0.0), IntVector(1, 0), IntVector(0, 0),
*_solver, DblVector(1, 0.0), IntVector(1, 0), IntVector(0, 0),
DblVector(0, 0.0), DblVector(1, lb), DblVector(1, ub),
CharVector(1, 'C'),
StrVector(
Expand All @@ -298,7 +298,7 @@ void WorkerMaster::_set_alpha_var() {
matval[i + 1] = -1;
}

solver_addrows(_solver, rowtype, rowrhs, {}, mstart, mclind, matval);
solver_addrows(*_solver, rowtype, rowrhs, {}, mstart, mclind, matval);
}
} else {
LOG(INFO)
Expand Down
6 changes: 3 additions & 3 deletions src/cpp/benders/merge_mps/MergeMPS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void MergeMPS::launch() {
for (int i(0); i < mps_ncols; ++i) {
sequence[i] = i;
}
solver_get_obj_func_coeffs(solver_l, o, 0, mps_ncols - 1);
solver_get_obj_func_coeffs(*solver_l, o, 0, mps_ncols - 1);
double const weigth = slave_weight(nslaves, kvp.first);
for (auto &c : o) {
c *= weigth;
Expand All @@ -53,7 +53,7 @@ void MergeMPS::launch() {
} else {
solver_l->read_prob_mps(problem_name);
}
StandardLp lpData(solver_l);
StandardLp lpData(*solver_l);
std::string varPrefix_l = "prob" + std::to_string(cntProblems_l) + "_";

lpData.append_in(mergedSolver_l, varPrefix_l);
Expand Down Expand Up @@ -125,7 +125,7 @@ void MergeMPS::launch() {

DblVector rhs(nrows, 0);
CharVector sense(nrows, 'E');
solver_addrows(mergedSolver_l, sense, rhs, {}, mstart, cindex, values);
solver_addrows(*mergedSolver_l, sense, rhs, {}, mstart, cindex, values);

LOG(INFO) << "Problems merged." << std::endl;
LOG(INFO) << "Writting mps file" << std::endl;
Expand Down
14 changes: 7 additions & 7 deletions src/cpp/benders/merge_mps/include/MergeMPS.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,18 @@ class StandardLp {
initialise_dbl_vectors();
}

explicit StandardLp(SolverAbstract::Ptr solver_p) {
explicit StandardLp(SolverAbstract &solver_p) {
init();

int ncols = solver_p->get_ncols();
int nrows = solver_p->get_nrows();
int nelems = solver_p->get_nelems();
int ncols = solver_p.get_ncols();
int nrows = solver_p.get_nrows();
int nelems = solver_p.get_nelems();

std::get<Attribute::INT_VALUE>(_data)[IntAttribute::NCOLS] = ncols;
std::get<Attribute::INT_VALUE>(_data)[IntAttribute::NROWS] = nrows;
std::get<Attribute::INT_VALUE>(_data)[IntAttribute::NELES] = nelems;

_colNames = solver_p->get_col_names(0, ncols - 1);
_colNames = solver_p.get_col_names(0, ncols - 1);

std::get<Attribute::INT_VECTOR>(_data)[IntVectorAttribute::MSTART].clear();
std::get<Attribute::INT_VECTOR>(_data)[IntVectorAttribute::MSTART].resize(
Expand Down Expand Up @@ -189,7 +189,7 @@ class StandardLp {
std::vector<int> mstart(
std::get<Attribute::INT_VALUE>(_data)[IntAttribute::NCOLS], 0);
solver_addcols(
containingSolver_p,
*containingSolver_p,
std::get<Attribute::DBL_VECTOR>(_data)[DblVectorAttribute::OBJ], mstart,
IntVector(0, 0), DblVector(0, 0.0),
std::get<Attribute::DBL_VECTOR>(_data)[DblVectorAttribute::LB],
Expand All @@ -198,7 +198,7 @@ class StandardLp {
newNames);

solver_addrows(
containingSolver_p,
*containingSolver_p,
std::get<Attribute::CHAR_VECTOR>(_data)[CharVectorAttribute::ROWTYPE],
std::get<Attribute::DBL_VECTOR>(_data)[DblVectorAttribute::RHS], {},
std::get<Attribute::INT_VECTOR>(_data)[IntVectorAttribute::MSTART],
Expand Down
56 changes: 26 additions & 30 deletions src/cpp/helpers/solver_utils.cc
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
#include "solver_utils.h"

void solver_getrows(SolverAbstract::Ptr const solver_p,
std::vector<int> &mstart_p,
void solver_getrows(const SolverAbstract &solver_p, std::vector<int> &mstart_p,
std::vector<int> &mclind_p,
std::vector<double> &dmatval_p,
int first_p, int last_p)
{

int nelems_returned = 0;
solver_p->get_rows(mstart_p.data(), mclind_p.data(), dmatval_p.data(),
solver_p->get_nelems(), &nelems_returned, first_p, last_p);
solver_p.get_rows(mstart_p.data(), mclind_p.data(), dmatval_p.data(),
solver_p.get_nelems(), &nelems_returned, first_p, last_p);
}

void solver_get_obj_func_coeffs(const std::shared_ptr<const SolverAbstract> solver_p, std::vector<double> &obj_p,
int first_p, int last_p)
void solver_get_obj_func_coeffs(const SolverAbstract &solver_p,
std::vector<double> &obj_p, int first_p, int last_p)
{
solver_p->get_obj(obj_p.data(), first_p, last_p);
solver_p.get_obj(obj_p.data(), first_p, last_p);
}

void solver_addcols(SolverAbstract::Ptr solver_p,
std::vector<double> const &objx_p,
void solver_addcols(SolverAbstract &solver_p, std::vector<double> const &objx_p,
std::vector<int> const &mstart_p,
std::vector<int> const &mrwind_p,
std::vector<double> const &dmatval_p,
Expand All @@ -32,32 +30,31 @@ void solver_addcols(SolverAbstract::Ptr solver_p,
assert(mrwind_p.size() == dmatval_p.size());

int newCols = colTypes_p.size();
int ncolInit = solver_p->get_ncols();
int ncolInit = solver_p.get_ncols();
int newnnz = dmatval_p.size();

solver_p->add_cols(newCols, newnnz, objx_p.data(), mstart_p.data(),
mrwind_p.data(), dmatval_p.data(), bdl_p.data(), bdu_p.data());
solver_p.add_cols(newCols, newnnz, objx_p.data(), mstart_p.data(),
mrwind_p.data(), dmatval_p.data(), bdl_p.data(), bdu_p.data());

std::vector<int> newIndex(newCols);
for (int i = 0; i < newCols; i++)
{
newIndex[i] = ncolInit + i;
}

solver_p->chg_col_type(newIndex, colTypes_p);
solver_p.chg_col_type(newIndex, colTypes_p);

if (colNames_p.size() > 0)
{
int ncolFinal = solver_p->get_ncols();
int ncolFinal = solver_p.get_ncols();
for (int i = ncolInit; i < ncolFinal; i++)
{
solver_p->chg_col_name(i, colNames_p[i - ncolInit]);
solver_p.chg_col_name(i, colNames_p[i - ncolInit]);
}
}
}

void solver_addrows(SolverAbstract::Ptr solver_p,
std::vector<char> const &qrtype_p,
void solver_addrows(SolverAbstract &solver_p, std::vector<char> const &qrtype_p,
std::vector<double> const &rhs_p,
std::vector<double> const &range_p,
std::vector<int> const &mstart_p,
Expand All @@ -70,8 +67,7 @@ void solver_addrows(SolverAbstract::Ptr solver_p,

int nrows = rhs_p.size();

solver_p->add_rows(nrows, dmatval_p.size(), qrtype_p.data(),
rhs_p.data(), range_p.data(), mstart_p.data(), mclind_p.data(), dmatval_p.data());
solver_p.add_rows(nrows, dmatval_p.size(), qrtype_p.data(), rhs_p.data(), range_p.data(), mstart_p.data(), mclind_p.data(), dmatval_p.data());
}

void solver_getlpsolution(SolverAbstract::Ptr const solver_p, std::vector<double> &x_p)
Expand All @@ -89,21 +85,21 @@ void solver_getlpreducedcost(SolverAbstract::Ptr const solver_p, std::vector<dou
solver_p->get_lp_sol(NULL, NULL, dj_p.data());
}

void solver_getrowtype(SolverAbstract::Ptr const solver_p, std::vector<char> &qrtype_p,
int first_p, int last_p)
void solver_getrowtype(const SolverAbstract &solver_p,
std::vector<char> &qrtype_p, int first_p, int last_p)
{
if (last_p >= first_p)
{
solver_p->get_row_type(qrtype_p.data(), first_p, last_p);
solver_p.get_row_type(qrtype_p.data(), first_p, last_p);
}
}

void solver_getrhs(SolverAbstract::Ptr const solver_p, std::vector<double> &rhs_p,
void solver_getrhs(const SolverAbstract &solver_p, std::vector<double> &rhs_p,
int first_p, int last_p)
{
if (last_p >= first_p)
{
solver_p->get_rhs(rhs_p.data(), first_p, last_p);
solver_p.get_rhs(rhs_p.data(), first_p, last_p);
}
}

Expand All @@ -116,14 +112,14 @@ void solver_getrhsrange(SolverAbstract::Ptr const solver_p, std::vector<double>
}
}

void solver_getcolinfo(SolverAbstract::Ptr const solver_p,
void solver_getcolinfo(const SolverAbstract &solver_p,
std::vector<char> &coltype_p,
std::vector<double> &bdl_p, std::vector<double> &bdu_p,
int first_p, int last_p)
{
solver_p->get_lb(bdl_p.data(), first_p, last_p);
solver_p->get_ub(bdu_p.data(), first_p, last_p);
solver_p->get_col_type(coltype_p.data(), first_p, last_p);
solver_p.get_lb(bdl_p.data(), first_p, last_p);
solver_p.get_ub(bdu_p.data(), first_p, last_p);
solver_p.get_col_type(coltype_p.data(), first_p, last_p);
}

void solver_deactivaterows(SolverAbstract::Ptr solver_p, std::vector<int> const &mindex)
Expand Down Expand Up @@ -152,8 +148,8 @@ void solver_chgbounds(SolverAbstract::Ptr solver_p,
solver_p->chg_bounds(mindex_p, qbtype_p, bnd_p);
}

void solver_rename_vars(SolverAbstract::Ptr outSolver_p, const std::vector<std::string> &names_p)
{
void solver_rename_vars(SolverAbstract *outSolver_p,
const std::vector<std::string> &names_p) {

for (int i = 0; i < outSolver_p->get_ncols(); i++)
{
Expand Down
25 changes: 10 additions & 15 deletions src/cpp/helpers/solver_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ class InvalidSolverStatusException
* @param first_p : First row in the range.
* @param last_p : Last row in the range.
*/
void solver_getrows(SolverAbstract::Ptr const solver_p,
std::vector<int> &mstart_p, std::vector<int> &mclind_p,
void solver_getrows(const SolverAbstract &solver_p, std::vector<int> &mstart_p, std::vector<int> &mclind_p,
std::vector<double> &dmatval_p, int first_p, int last_p);

/**
Expand All @@ -39,9 +38,8 @@ void solver_getrows(SolverAbstract::Ptr const solver_p,
* @param first_p : first column to consider.
* @param last_p : last column to consider.
*/
void solver_get_obj_func_coeffs(
const std::shared_ptr<const SolverAbstract> solver_p,
std::vector<double> &obj_p, int first_p, int last_p);
void solver_get_obj_func_coeffs(const SolverAbstract &solver_p,
std::vector<double> &obj_p, int first_p, int last_p);

/**
* @brief Adds variables to an existent solver
Expand All @@ -63,9 +61,8 @@ void solver_get_obj_func_coeffs(
* @param colNames_p : optional parameter. array containing the names of the new
* columns to add.
*/
void solver_addcols(
SolverAbstract::Ptr solver_p, std::vector<double> const &objx_p,
std::vector<int> const &mstart_p, std::vector<int> const &mrwind_p,
void solver_addcols(SolverAbstract &solver_p, std::vector<double> const &objx_p,
std::vector<int> const &mstart_p, std::vector<int> const &mrwind_p,
std::vector<double> const &dmatval_p, std::vector<double> const &bdl_p,
std::vector<double> const &bdu_p, std::vector<char> const &colTypes_p,
std::vector<std::string> const &colNames_p);
Expand Down Expand Up @@ -94,8 +91,7 @@ void solver_addcols(
*
* @note ignores non-binding rows
*/
void solver_addrows(SolverAbstract::Ptr solver_p,
std::vector<char> const &qrtype_p,
void solver_addrows(SolverAbstract &solver_p, std::vector<char> const &qrtype_p,
std::vector<double> const &rhs_p,
std::vector<double> const &range_p,
std::vector<int> const &mstart_p,
Expand Down Expand Up @@ -143,7 +139,7 @@ void solver_getlpreducedcost(SolverAbstract::Ptr const solver_p,
* @param first_p : First row in the range.
* @param last_p : Last row in the range
*/
void solver_getrowtype(SolverAbstract::Ptr const solver_p,
void solver_getrowtype(const SolverAbstract &solver_p,
std::vector<char> &qrtype_p, int first_p, int last_p);

/**
Expand All @@ -158,8 +154,7 @@ void solver_getrowtype(SolverAbstract::Ptr const solver_p,
* @note if the constraint is a range, returns the ub.
* @note if the constraint is unbound, still returns the ub i.e. solver's +inf.
*/
void solver_getrhs(SolverAbstract::Ptr const solver_p,
std::vector<double> &rhs_p, int first_p, int last_p);
void solver_getrhs(const SolverAbstract &solver_p, std::vector<double> &rhs_p, int first_p, int last_p);

/**
* @brief Returns the right hand side range values for the rows in a given
Expand Down Expand Up @@ -190,7 +185,7 @@ void solver_getrhsrange(SolverAbstract::Ptr const solver_p,
*
* @note return 'I' if the variable is binary or integer and it was fixed
*/
void solver_getcolinfo(SolverAbstract::Ptr const solver_p,
void solver_getcolinfo(const SolverAbstract &solver_p,
std::vector<char> &coltype_p, std::vector<double> &bdl_p,
std::vector<double> &bdu_p, int first_p, int last_p);

Expand Down Expand Up @@ -264,5 +259,5 @@ void solver_chgbounds(SolverAbstract::Ptr solver_p,
* verifications are done (eg. infinity values correspondance)
* @note duplicate/empty names will be named automatically by ortools
*/
void solver_rename_vars(SolverAbstract::Ptr outSolver_p,
void solver_rename_vars(SolverAbstract *outSolver_p,
const std::vector<std::string> &names_p);
Loading

0 comments on commit c67070f

Please sign in to comment.