Skip to content

Commit

Permalink
add method to rename rows
Browse files Browse the repository at this point in the history
  • Loading branch information
a-zakir committed Feb 8, 2024
1 parent 906caff commit 35035c5
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 18 deletions.
16 changes: 9 additions & 7 deletions src/cpp/helpers/solver_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,17 @@ void solver_addrows(SolverAbstract &solver_p, std::vector<char> const &qrtype_p,
std::vector<double> const &range_p,
std::vector<int> const &mstart_p,
std::vector<int> const &mclind_p,
std::vector<double> const &dmatval_p)
{
assert(qrtype_p.size() == rhs_p.size());
assert((range_p.size() == 0) || (range_p.size() == qrtype_p.size()));
assert(mclind_p.size() == dmatval_p.size());
std::vector<double> const &dmatval_p,
std::vector<std::string> const &names) {
assert(qrtype_p.size() == rhs_p.size());
assert((range_p.size() == 0) || (range_p.size() == qrtype_p.size()));
assert(mclind_p.size() == dmatval_p.size());

int nrows = rhs_p.size();
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(), names);
}

void solver_getlpsolution(SolverAbstract::Ptr const solver_p, std::vector<double> &x_p)
Expand Down
5 changes: 4 additions & 1 deletion src/cpp/helpers/solver_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ void solver_addcols(SolverAbstract &solver_p, std::vector<double> const &objx_p,
* for the elements in each row.
* @param dmatval_p : Double array of containing the (contiguous) element
* values.
* @param names : rows names
* values.
*
* @note ignores non-binding rows
*/
Expand All @@ -96,7 +98,8 @@ void solver_addrows(SolverAbstract &solver_p, std::vector<char> const &qrtype_p,
std::vector<double> const &range_p,
std::vector<int> const &mstart_p,
std::vector<int> const &mclind_p,
std::vector<double> const &dmatval_p);
std::vector<double> const &dmatval_p,
std::vector<std::string> const &names = {});

/**
* @brief returns the solution of a solved problem
Expand Down
9 changes: 7 additions & 2 deletions src/cpp/lpnamer/model/Problem.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ class Problem : public SolverAbstract {
}
void add_rows(int newrows, int newnz, const char *qrtype, const double *rhs,
const double *range, const int *mstart, const int *mclind,
const double *dmatval) override {
const double *dmatval,
const std::vector<std::string> &names = {}) override {
solver_abstract_->add_rows(newrows, newnz, qrtype, rhs, range, mstart,
mclind, dmatval);
mclind, dmatval, names);
}
void add_cols(int newcol, int newnz, const double *objx, const int *mstart,
const int *mrwind, const double *dmatval, const double *bdl,
Expand All @@ -121,6 +122,10 @@ class Problem : public SolverAbstract {
void add_name(int type, const char *cnames, int indice) override {
solver_abstract_->add_name(type, cnames, indice);
}
void add_names(int type, const std::vector<std::string> &cnames, int first,
int end) override {
solver_abstract_->add_names(type, cnames, first, end);
}
void chg_obj(const std::vector<int> &mindex,
const std::vector<double> &obj) override {
solver_abstract_->chg_obj(mindex, obj);
Expand Down
19 changes: 18 additions & 1 deletion src/cpp/multisolver_interface/SolverCbc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,13 +381,22 @@ void SolverCbc::del_rows(int first, int last) {
void SolverCbc::add_rows(int newrows, int newnz, const char *qrtype,
const double *rhs, const double *range,
const int *mstart, const int *mclind,
const double *dmatval) {
const double *dmatval,
const std::vector<std::string> &row_names) {
std::vector<double> rowLower(newrows);
std::vector<double> rowUpper(newrows);
int nrowInit = get_nrows();

coin_common::fill_row_bounds_from_new_rows_data(rowLower, rowUpper, newrows,
qrtype, rhs);
_clp_inner_solver.addRows(newrows, mstart, mclind, dmatval, rowLower.data(),
rowUpper.data());
if (row_names.size() > 0) {
int nrowFinal = get_nrows();
for (int i = nrowInit; i < nrowFinal; i++) {
chg_row_name(i, row_names[i - nrowInit]);
}
}
}

void SolverCbc::add_cols(int newcol, int newnz, const double *objx,
Expand All @@ -410,6 +419,14 @@ void SolverCbc::add_name(int type, const char *cnames, int indice) {
throw NotImplementedFeatureSolverException(error);
}

void SolverCbc::add_names(int type, const std::vector<std::string> &cnames,
int first, int end) {
// TODO
auto error =
LOGLOCATION + "ERROR : addnames not implemented in the CLP interface.";
throw NotImplementedFeatureSolverException(error);
}

void SolverCbc::chg_obj(const std::vector<int> &mindex,
const std::vector<double> &obj) {
assert(obj.size() == mindex.size());
Expand Down
5 changes: 4 additions & 1 deletion src/cpp/multisolver_interface/SolverCbc.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,15 @@ class SolverCbc : public SolverAbstract {
virtual void add_rows(int newrows, int newnz, const char *qrtype,
const double *rhs, const double *range,
const int *mstart, const int *mclind,
const double *dmatval) override;
const double *dmatval,
const std::vector<std::string> &names = {}) override;
virtual void add_cols(int newcol, int newnz, const double *objx,
const int *mstart, const int *mrwind,
const double *dmatval, const double *bdl,
const double *bdu) override;
virtual void add_name(int type, const char *cnames, int indice) override;
virtual void add_names(int type, const std::vector<std::string> &cnames,
int first, int end) override;
virtual void chg_obj(const std::vector<int> &mindex,
const std::vector<double> &obj) override;
virtual void chg_obj_direction(const bool minimize) override;
Expand Down
17 changes: 16 additions & 1 deletion src/cpp/multisolver_interface/SolverClp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,14 +256,22 @@ void SolverClp::del_rows(int first, int last) {
void SolverClp::add_rows(int newrows, int newnz, const char *qrtype,
const double *rhs, const double *range,
const int *mstart, const int *mclind,
const double *dmatval) {
const double *dmatval,
const std::vector<std::string> &row_names) {
std::vector<double> rowLower(newrows);
std::vector<double> rowUpper(newrows);
int nrowInit = get_nrows();
coin_common::fill_row_bounds_from_new_rows_data(rowLower, rowUpper, newrows,
qrtype, rhs);

_clp.addRows(newrows, rowLower.data(), rowUpper.data(), mstart, mclind,
dmatval);
if (row_names.size() > 0) {
int nrowFinal = get_nrows();
for (int i = nrowInit; i < nrowFinal; i++) {
chg_row_name(i, row_names[i - nrowInit]);
}
}
}

void SolverClp::add_cols(int newcol, int newnz, const double *objx,
Expand All @@ -284,6 +292,13 @@ void SolverClp::add_name(int type, const char *cnames, int indice) {
LOGLOCATION + "ERROR : addnames not implemented in the CLP interface.";
throw NotImplementedFeatureSolverException(error);
}
void SolverClp::add_names(int type, const std::vector<std::string> &cnames,
int first, int end) {
// TODO
auto error =
LOGLOCATION + "ERROR : addnames not implemented in the CLP interface.";
throw NotImplementedFeatureSolverException(error);
}

void SolverClp::chg_obj(const std::vector<int> &mindex,
const std::vector<double> &obj) {
Expand Down
5 changes: 4 additions & 1 deletion src/cpp/multisolver_interface/SolverClp.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,15 @@ class SolverClp : public SolverAbstract {
virtual void add_rows(int newrows, int newnz, const char *qrtype,
const double *rhs, const double *range,
const int *mstart, const int *mclind,
const double *dmatval) override;
const double *dmatval,
const std::vector<std::string> &names = {}) override;
virtual void add_cols(int newcol, int newnz, const double *objx,
const int *mstart, const int *mrwind,
const double *dmatval, const double *bdl,
const double *bdu) override;
virtual void add_name(int type, const char *cnames, int indice) override;
virtual void add_names(int type, const std::vector<std::string> &cnames,
int first, int end) override;
virtual void chg_obj(const std::vector<int> &mindex,
const std::vector<double> &obj) override;
virtual void chg_obj_direction(const bool minimize) override;
Expand Down
19 changes: 18 additions & 1 deletion src/cpp/multisolver_interface/SolverXpress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,16 @@ void SolverXpress::del_rows(int first, int last) {
void SolverXpress::add_rows(int newrows, int newnz, const char *qrtype,
const double *rhs, const double *range,
const int *mstart, const int *mclind,
const double *dmatval) {
const double *dmatval,
const std::vector<std::string> &row_names) {
int nrowInit = get_nrows();
int status = XPRSaddrows(_xprs, newrows, newnz, qrtype, rhs, range, mstart,
mclind, dmatval);
zero_status_check(status, "add rows", LOGLOCATION);
if (row_names.size() > 0) {
int nrowFinal = get_nrows();
add_names(1, row_names, nrowInit, nrowFinal);
}
}

void SolverXpress::add_cols(int newcol, int newnz, const double *objx,
Expand All @@ -359,6 +365,17 @@ void SolverXpress::add_name(int type, const char *cnames, int indice) {
zero_status_check(status, "add names", LOGLOCATION);
}

void SolverXpress::add_names(int type, const std::vector<std::string> &cnames,
int first, int end) {
std::vector<char> row_names_charp;
for (auto name : cnames) {
name += '\0';
row_names_charp.insert(row_names_charp.end(), name.begin(), name.end());
}
int status = XPRSaddnames(_xprs, type, row_names_charp.data(), first, end);
zero_status_check(status, "add names", LOGLOCATION);
}

void SolverXpress::chg_obj(const std::vector<int> &mindex,
const std::vector<double> &obj) {
assert(obj.size() == mindex.size());
Expand Down
5 changes: 4 additions & 1 deletion src/cpp/multisolver_interface/SolverXpress.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,15 @@ class SolverXpress : public SolverAbstract {
virtual void add_rows(int newrows, int newnz, const char *qrtype,
const double *rhs, const double *range,
const int *mstart, const int *mclind,
const double *dmatval) override;
const double *dmatval,
const std::vector<std::string> &names = {}) override;
virtual void add_cols(int newcol, int newnz, const double *objx,
const int *mstart, const int *mrwind,
const double *dmatval, const double *bdl,
const double *bdu) override;
virtual void add_name(int type, const char *cnames, int indice) override;
virtual void add_names(int type, const std::vector<std::string> &cnames,
int first, int end) override;
virtual void chg_obj(const std::vector<int> &mindex,
const std::vector<double> &obj) override;
virtual void chg_obj_direction(const bool minimize) override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,8 @@ class SolverAbstract {
virtual void add_rows(int newrows, int newnz, const char *qrtype,
const double *rhs, const double *range,
const int *mstart, const int *mclind,
const double *dmatval) = 0;
const double *dmatval,
const std::vector<std::string> &names = {}) = 0;

/**
* @brief Adds new columns to the problem
Expand Down Expand Up @@ -548,6 +549,8 @@ class SolverAbstract {
* @param indice : index of the row or of the column.
*/
virtual void add_name(int type, const char *cnames, int indice) = 0;
virtual void add_names(int type, const std::vector<std::string> &cnames,
int first, int end) = 0;

/**
* @brief Change coefficients in objective function
Expand Down
5 changes: 4 additions & 1 deletion tests/cpp/lp_namer/NOOPSolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,15 @@ class NOOPSolver: public SolverAbstract {
virtual void add_rows(int newrows, int newnz, const char *qrtype,
const double *rhs, const double *range,
const int *mstart, const int *mclind,
const double *dmatval) override {}
const double *dmatval,
const std::vector<std::string> &names = {}) override {}
virtual void add_cols(int newcol, int newnz, const double *objx,
const int *mstart, const int *mrwind,
const double *dmatval, const double *bdl,
const double *bdu) override {}
virtual void add_name(int type, const char *cnames, int indice) override {}
virtual void add_names(int type, const std::vector<std::string> &cnames,
int first, int end) override {}
virtual void chg_obj(const std::vector<int> &mindex,
const std::vector<double> &obj) override {}
virtual void chg_obj_direction(const bool minimize) override {}
Expand Down

0 comments on commit 35035c5

Please sign in to comment.