Skip to content

Commit

Permalink
Merge pull request #3363 from WimVanRoy/add_gurobi_pool_output
Browse files Browse the repository at this point in the history
Add Gurobi pool output.
  • Loading branch information
jgillis committed Sep 6, 2023
2 parents 0292729 + ff62d28 commit 7f8f055
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
26 changes: 26 additions & 0 deletions casadi/interfaces/gurobi/gurobi_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ namespace casadi {
casadi_assert(!flag && m->env, "Failed to create GUROBI environment. Flag: " + str(flag)
+ ":" + GRBgeterrormsg(m->env));

m->pool_sol_nr = 0;

m->sos_weights = sos_weights_;
m->sos_beg = sos_beg_;
m->sos_ind = sos_ind_;
Expand Down Expand Up @@ -515,6 +517,27 @@ namespace casadi {
}
}

// Get solutions from solution pool
flag = GRBgetintattr(model, GRB_INT_ATTR_SOLCOUNT, &(m->pool_sol_nr));
if (!flag && m->pool_sol_nr > 0) {
m->pool_obj_vals = std::vector<double>(m->pool_sol_nr, casadi::nan);
for (int idx = 0; idx < m->pool_sol_nr; ++idx) {
double x_pool[nx_];

flag = GRBsetintparam(GRBgetenv(model), GRB_INT_PAR_SOLUTIONNUMBER, idx);
if (!flag) flag = GRBgetdblattr(model, GRB_DBL_ATTR_POOLOBJVAL, &(m->pool_obj_vals[idx]));
if (!flag) flag = GRBgetdblattrarray(model, GRB_DBL_ATTR_XN, 0, nx_, x_pool);

if (flag) {
m->pool_obj_vals[idx] = casadi::nan;
std::fill_n(x_pool, nx_, casadi::nan);
}

std::vector<double> x_pool_vec(x_pool, x_pool+nx_);
m->pool_solutions.push_back(x_pool_vec);
}
}

// Free memory
GRBfreemodel(model);
m->fstats.at("postprocessing").toc();
Expand All @@ -532,6 +555,9 @@ namespace casadi {
Dict stats = Conic::get_stats(mem);
auto m = static_cast<GurobiMemory*>(mem);
stats["return_status"] = return_status_string(m->return_status);
stats["pool_sol_nr"] = m->pool_sol_nr;
stats["pool_obj_val"] = m->pool_obj_vals;
stats["pool_solutions"] = m->pool_solutions;
return stats;
}

Expand Down
4 changes: 4 additions & 0 deletions casadi/interfaces/gurobi/gurobi_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ namespace casadi {

int return_status;

int pool_sol_nr;
std::vector<double> pool_obj_vals;
std::vector<std::vector<double>> pool_solutions;

// SOS structure
std::vector<double> sos_weights;
std::vector<int> sos_beg;
Expand Down

0 comments on commit 7f8f055

Please sign in to comment.