From 963b71b49e606a1232d70a0e0e7e4f0ce1a293f2 Mon Sep 17 00:00:00 2001 From: Henri Lefebvre Date: Thu, 31 Oct 2024 10:25:46 +0100 Subject: [PATCH] use push_back to create model in Gurobi --- lib/include/idol/general/utils/SparseVector.h | 4 ++-- .../wrappers/Gurobi/Optimizers_Gurobi.cpp | 15 ++++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/include/idol/general/utils/SparseVector.h b/lib/include/idol/general/utils/SparseVector.h index 17b28390..96aa967b 100644 --- a/lib/include/idol/general/utils/SparseVector.h +++ b/lib/include/idol/general/utils/SparseVector.h @@ -496,11 +496,11 @@ idol::SparseVector 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; diff --git a/lib/src/mixed-integer/optimizers/wrappers/Gurobi/Optimizers_Gurobi.cpp b/lib/src/mixed-integer/optimizers/wrappers/Gurobi/Optimizers_Gurobi.cpp index 5aae20b2..ae5f2a4a 100644 --- a/lib/src/mixed-integer/optimizers/wrappers/Gurobi/Optimizers_Gurobi.cpp +++ b/lib/src/mixed-integer/optimizers/wrappers/Gurobi/Optimizers_Gurobi.cpp @@ -486,12 +486,11 @@ idol::Model idol::Optimizers::Gurobi::read_from_file(idol::Env &t_env, const std std::unique_ptr model; GUROBI_CATCH(model = std::make_unique(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); @@ -499,16 +498,21 @@ idol::Model idol::Optimizers::Gurobi::read_from_file(idol::Env &t_env, const std 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_; @@ -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);