Skip to content

Commit

Permalink
use push_back to create model in Gurobi
Browse files Browse the repository at this point in the history
  • Loading branch information
hlefebvr committed Oct 31, 2024
1 parent c9fa7d9 commit 963b71b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/include/idol/general/utils/SparseVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -496,11 +496,11 @@ idol::SparseVector<IndexT, ValueT, IndexExtractorT>
if (this == &t_vector) {
return *this *= 2;
}

/*
if (m_sorting_criteria == SortingCriteria::Index && t_vector.m_sorting_criteria == SortingCriteria::Index) {
return binary_operation_on_sorted_vectors(t_vector, [](const ValueT& t_value1, const ValueT& t_value2) { return t_value1 + t_value2; });;
}

*/
push_back(t_vector.m_indices, t_vector.m_values);

return *this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,29 +486,33 @@ idol::Model idol::Optimizers::Gurobi::read_from_file(idol::Env &t_env, const std
std::unique_ptr<GRBModel> model;
GUROBI_CATCH(model = std::make_unique<GRBModel>(get_global_env(), t_filename);)

model->update();

const unsigned int n_vars = model->get(GRB_IntAttr_NumVars);
const unsigned int n_ctrs = model->get(GRB_IntAttr_NumConstrs);
const unsigned int n_quad_ctrs = model->get(GRB_IntAttr_NumQConstrs);

result.reserve_vars(n_vars);
for (unsigned int j = 0 ; j < n_vars ; ++j) {

const auto& var = model->getVar(j);
const double lb = var.get(GRB_DoubleAttr_LB);
const double ub = var.get(GRB_DoubleAttr_UB);
const double obj = var.get(GRB_DoubleAttr_Obj);
VarType type = idol_var_type(var.get(GRB_CharAttr_VType));
const auto name = var.get(GRB_StringAttr_VarName);

result.add_var(lb, ub, type, obj, var.get(GRB_StringAttr_VarName));
result.add_var(lb, ub, type, obj, name);
}

const auto parse_linear = [&](const GRBLinExpr& t_lin_expr) {

Expr result_ = t_lin_expr.getConstant();
auto& linear_ = result_.linear();
const auto n_terms = t_lin_expr.size();
linear_.reserve(n_terms);

for (unsigned int j = 0, n = t_lin_expr.size() ; j < n ; ++j) {
for (unsigned int j = 0 ; j < n_terms ; ++j) {
auto var = t_lin_expr.getVar(j);
result_ += t_lin_expr.getCoeff(j) * result.get_var_by_index(var.index());
linear_.push_back(result.get_var_by_index(var.index()), t_lin_expr.getCoeff(j));
}

return result_;
Expand Down Expand Up @@ -543,6 +547,7 @@ idol::Model idol::Optimizers::Gurobi::read_from_file(idol::Env &t_env, const std

};

result.reserve_ctrs(n_ctrs);
for (unsigned int i = 0 ; i < n_ctrs ; ++i) {

const auto& ctr = model->getConstr(i);
Expand Down

0 comments on commit 963b71b

Please sign in to comment.