gurobi_interface.cpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 
26 #include "gurobi_interface.hpp"
27 #include "casadi/core/casadi_misc.hpp"
28 #include "casadi/core/nlp_tools.hpp"
29 
30 namespace casadi {
31 
32  extern "C"
33  int CASADI_CONIC_GUROBI_EXPORT
34  casadi_register_conic_gurobi(Conic::Plugin* plugin) {
35  plugin->creator = GurobiInterface::creator;
36  plugin->name = "gurobi";
37  plugin->doc = GurobiInterface::meta_doc.c_str();
38  plugin->version = CASADI_VERSION;
39  plugin->options = &GurobiInterface::options_;
40  plugin->deserialize = &GurobiInterface::deserialize;
41  #ifdef GUROBI_ADAPTOR
42  char buffer[400];
43  int ret = gurobi_adaptor_load(buffer, sizeof(buffer));
44  if (ret!=0) {
45  casadi_warning("Failed to load Gurobi adaptor: " + std::string(buffer) + ".");
46  return 1;
47  }
48  #endif
49  return 0;
50  }
51 
52  extern "C"
53  void CASADI_CONIC_GUROBI_EXPORT casadi_load_conic_gurobi() {
55  }
56 
57  GurobiInterface::GurobiInterface(const std::string& name,
58  const std::map<std::string, Sparsity>& st)
59  : Conic(name, st) {
60  }
61 
63  clear_mem();
64  }
65 
67  = {{&Conic::options_},
68  {{"vtype",
70  "Type of variables: [CONTINUOUS|binary|integer|semicont|semiint]"}},
71  {"gurobi",
72  {OT_DICT,
73  "Options to be passed to gurobi."}},
74  {"sos_groups",
76  "Definition of SOS groups by indices."}},
77  {"sos_weights",
79  "Weights corresponding to SOS entries."}},
80  {"sos_types",
81  {OT_INTVECTOR,
82  "Specify 1 or 2 for each SOS group."}}
83  }
84  };
85 
86  void GurobiInterface::init(const Dict& opts) {
87  // Initialize the base classes
88  Conic::init(opts);
89 
90  // Default options
91  std::vector<std::string> vtype;
92 
93  std::vector< std::vector<casadi_int> > sos_groups;
94  std::vector< std::vector<double> > sos_weights;
95  std::vector<casadi_int> sos_types;
96 
97  // Read options
98  for (auto&& op : opts) {
99  if (op.first=="vtype") {
100  vtype = op.second;
101  } else if (op.first=="gurobi") {
102  opts_ = op.second;
103  } else if (op.first=="sos_groups") {
104  sos_groups = op.second.to_int_vector_vector();
105  } else if (op.first=="sos_weights") {
106  sos_weights = op.second.to_double_vector_vector();
107  } else if (op.first=="sos_types") {
108  sos_types = op.second.to_int_vector();
109  }
110  }
111 
112  // Validaty SOS constraints
113  check_sos(nx_, sos_groups, sos_weights, sos_types);
114 
115  // Populate SOS structures
117  if (!sos_weights.empty())
118  flatten_nested_vector(sos_weights, sos_weights_);
119 
120  sos_types_ = to_int(sos_types);
121 
122  // Variable types
123  if (!vtype.empty()) {
124  casadi_assert(vtype.size()==nx_, "Option 'vtype' has wrong length");
125  vtype_.resize(nx_);
126  for (casadi_int i=0; i<nx_; ++i) {
127  if (vtype[i]=="continuous") {
128  vtype_[i] = GRB_CONTINUOUS;
129  } else if (vtype[i]=="binary") {
130  vtype_[i] = GRB_BINARY;
131  } else if (vtype[i]=="integer") {
132  vtype_[i] = GRB_INTEGER;
133  } else if (vtype[i]=="semicont") {
134  vtype_[i] = GRB_SEMICONT;
135  } else if (vtype[i]=="semiint") {
136  vtype_[i] = GRB_SEMIINT;
137  } else {
138  casadi_error("No such variable type: " + vtype[i]);
139  }
140  }
141  }
142 
143  // Initialize SDP to SOCP memory
145 
146  // Temporary memory
147  alloc_w(sdp_to_socp_mem_.indval_size, true); // val
148  alloc_iw(sdp_to_socp_mem_.indval_size, true); // ind
149  alloc_iw(nx_, true); // ind2
150  alloc_iw(nx_, true); // vtypes
151  }
152 
153  int GurobiInterface::init_mem(void* mem) const {
154  if (Conic::init_mem(mem)) return 1;
155  auto m = static_cast<GurobiMemory*>(mem);
156 
157  // Load environment
158  casadi_int flag = GRBloadenv(&m->env, nullptr); // no log file
159  casadi_assert(!flag && m->env, "Failed to create GUROBI environment. Flag: " + str(flag)
160  + ":" + GRBgeterrormsg(m->env));
161 
162  m->pool_sol_nr = 0;
163 
164  m->sos_weights = sos_weights_;
165  m->sos_beg = sos_beg_;
166  m->sos_ind = sos_ind_;
167  m->sos_types = sos_types_;
168 
169  m->add_stat("preprocessing");
170  m->add_stat("solver");
171  m->add_stat("postprocessing");
172  return 0;
173  }
174 
175  inline const char* return_status_string(casadi_int status) {
176  switch (status) {
177  case GRB_LOADED:
178  return "LOADED";
179  case GRB_OPTIMAL:
180  return "OPTIMAL";
181  case GRB_INFEASIBLE:
182  return "INFEASIBLE";
183  case GRB_INF_OR_UNBD:
184  return "INF_OR_UNBD";
185  case GRB_UNBOUNDED:
186  return "UNBOUNDED";
187  case GRB_CUTOFF:
188  return "CUTOFF";
189  case GRB_ITERATION_LIMIT:
190  return "ITERATION_LIMIT";
191  case GRB_NODE_LIMIT:
192  return "NODE_LIMIT";
193  case GRB_TIME_LIMIT:
194  return "TIME_LIMIT";
195  case GRB_SOLUTION_LIMIT:
196  return "SOLUTION_LIMIT";
197  case GRB_INTERRUPTED:
198  return "INTERRUPTED";
199  case GRB_NUMERIC:
200  return "NUMERIC";
201  case GRB_SUBOPTIMAL:
202  return "SUBOPTIMAL";
203  case GRB_INPROGRESS:
204  return "INPROGRESS";
205  }
206  return "Unknown";
207  }
208 
210  solve(const double** arg, double** res, casadi_int* iw, double* w, void* mem) const {
211  auto m = static_cast<GurobiMemory*>(mem);
212  const SDPToSOCPMem& sm = sdp_to_socp_mem_;
213 
214  // Statistics
215  m->fstats.at("preprocessing").tic();
216 
217  // Problem has not been solved at this point
218  m->return_status = -1;
219 
220  if (inputs_check_) {
221  check_inputs(arg[CONIC_LBX], arg[CONIC_UBX], arg[CONIC_LBA], arg[CONIC_UBA]);
222  }
223 
224  // Inputs
225  const double *h=arg[CONIC_H],
226  *g=arg[CONIC_G],
227  *a=arg[CONIC_A],
228  *lba=arg[CONIC_LBA],
229  *uba=arg[CONIC_UBA],
230  *lbx=arg[CONIC_LBX],
231  *ubx=arg[CONIC_UBX],
232  *p=arg[CONIC_P],
233  *q=arg[CONIC_Q],
234  *x0=arg[CONIC_X0];
235  //*lam_x0=arg[CONIC_LAM_X0];
236 
237  // Outputs
238  double *x=res[CONIC_X],
239  *cost=res[CONIC_COST],
240  *lam_a=res[CONIC_LAM_A],
241  *lam_x=res[CONIC_LAM_X];
242 
243  // Temporary memory
244  double *val=w; w+=sm.indval_size;
245  int *ind=reinterpret_cast<int*>(iw); iw+=sm.indval_size;
246  int *ind2=reinterpret_cast<int*>(iw); iw+=nx_;
247  char *vtypes=reinterpret_cast<char*>(iw); iw+=nx_;
248 
249  // Greate an empty model
250  GRBmodel *model = nullptr;
251  try {
252  casadi_int flag = GRBnewmodel(m->env, &model, name_.c_str(), 0,
253  nullptr, nullptr, nullptr, nullptr, nullptr);
254  casadi_assert(!flag, GRBgeterrormsg(m->env));
255 
256  // Add variables
257  for (casadi_int i=0; i<nx_; ++i) {
258  // Get bounds
259  double lb = lbx ? lbx[i] : 0., ub = ubx ? ubx[i] : 0.;
260  if (isinf(lb)) lb = -GRB_INFINITY;
261  if (isinf(ub)) ub = GRB_INFINITY;
262 
263  // Get variable type
264  char vtype;
265  if (!vtype_.empty()) {
266  // Explicitly set 'vtype' takes precedence
267  vtype = vtype_.at(i);
268  } else if (!discrete_.empty() && discrete_.at(i)) {
269  // Variable marked as discrete (integer or binary)
270  vtype = lb==0 && ub==1 ? GRB_BINARY : GRB_INTEGER;
271  } else {
272  // Continious variable
273  vtype = GRB_CONTINUOUS;
274  }
275  vtypes[i] = vtype;
276 
277  // Pass to model
278  flag = GRBaddvar(model, 0, nullptr, nullptr, g ? g[i] : 0., lb, ub, vtype, nullptr);
279  casadi_assert(!flag, GRBgeterrormsg(m->env));
280  }
281 
282  GRBupdatemodel(model);
283  for (casadi_int i=0; i<nx_; ++i) {
284  // If it is a discrete variable, we can pass the start guess
285  if (vtypes[i] != GRB_CONTINUOUS) {
286  flag = GRBsetdblattrelement(model, "Start", i, x0[i]);
287  casadi_assert(!flag, GRBgeterrormsg(m->env));
288  }
289  }
290 
291 
292  /* Treat SOCP constraints */
293 
294  // Add helper variables for SOCP
295  for (casadi_int i=0;i<sm.r.size()-1;++i) {
296  for (casadi_int k=0;k<sm.r[i+1]-sm.r[i]-1;++k) {
297  flag = GRBaddvar(model, 0, nullptr, nullptr, 0, -GRB_INFINITY, GRB_INFINITY,
298  GRB_CONTINUOUS, nullptr);
299  casadi_assert(!flag, GRBgeterrormsg(m->env));
300  }
301  flag = GRBaddvar(model, 0, nullptr, nullptr, 0, 0, GRB_INFINITY, GRB_CONTINUOUS, nullptr);
302  casadi_assert(!flag, GRBgeterrormsg(m->env));
303  }
304 
305  flag = GRBupdatemodel(model);
306  casadi_assert(!flag, GRBgeterrormsg(m->env));
307 
308  // Add quadratic terms
309  const casadi_int *H_colind=H_.colind(), *H_row=H_.row();
310  for (int i=0; i<nx_; ++i) {
311 
312  // Quadratic term nonzero indices
313  casadi_int numqnz = H_colind[1]-H_colind[0];
314  for (casadi_int k=0;k<numqnz;++k) ind[k]=H_row[k];
315  H_colind++;
316  H_row += numqnz;
317 
318  // Corresponding column
319  casadi_fill(ind2, numqnz, i);
320 
321  // Quadratic term nonzeros
322  if (h) {
323  casadi_copy(h, numqnz, val);
324  casadi_scal(numqnz, 0.5, val);
325  h += numqnz;
326  } else {
327  casadi_clear(val, numqnz);
328  }
329 
330  // Pass to model
331  flag = GRBaddqpterms(model, numqnz, ind, ind2, val);
332  casadi_assert(!flag, GRBgeterrormsg(m->env));
333  }
334 
335  std::vector<char> constraint_type(na_); // For each a entry: 0 absent, 1 linear
336  casadi_int npi = 0;
337 
338  // Add constraints
339  const casadi_int *AT_colind=sm.AT.colind(), *AT_row=sm.AT.row();
340  for (casadi_int i=0; i<na_; ++i) {
341  // Get bounds
342  double lb = lba ? lba[i] : 0., ub = uba ? uba[i] : 0.;
343 
344  casadi_int numnz = 0;
345  // Loop over rows
346  for (casadi_int k=AT_colind[i]; k<AT_colind[i+1]; ++k) {
347  casadi_int j = AT_row[k];
348 
349  ind[numnz] = j;
350  val[numnz] = a ? a[sm.A_mapping[k]] : 0;
351 
352  numnz++;
353  }
354 
355  constraint_type[i] = 1;
356  // Pass to model
357  if (isinf(lb)) {
358  if (isinf(ub)) {
359  constraint_type[i] = 0;
360  // Neither upper or lower bounds, skip
361  } else {
362  // Only upper bound
363  flag = GRBaddconstr(model, numnz, ind, val, GRB_LESS_EQUAL, ub, nullptr);
364  casadi_assert(!flag, GRBgeterrormsg(m->env));
365  npi++;
366  }
367  } else {
368  if (isinf(ub)) {
369  // Only lower bound
370  flag = GRBaddconstr(model, numnz, ind, val, GRB_GREATER_EQUAL, lb, nullptr);
371  casadi_assert(!flag, GRBgeterrormsg(m->env));
372  npi++;
373  } else if (lb==ub) {
374  // Upper and lower bounds equal
375  flag = GRBaddconstr(model, numnz, ind, val, GRB_EQUAL, lb, nullptr);
376  casadi_assert(!flag, GRBgeterrormsg(m->env));
377  npi++;
378  } else {
379  // Both upper and lower bounds
380  flag = GRBaddrangeconstr(model, numnz, ind, val, lb, ub, nullptr);
381  casadi_assert(!flag, GRBgeterrormsg(m->env));
382  npi++;
383  }
384  }
385  }
386  std::vector<double> pi(npi);
387 
388  // Add SOS constraints when applicable
389  if (!m->sos_ind.empty()) {
390  flag = GRBaddsos(model, m->sos_beg.size()-1, m->sos_ind.size(),
391  get_ptr(m->sos_types), get_ptr(m->sos_beg), get_ptr(m->sos_ind),
392  get_ptr(m->sos_weights));
393  casadi_assert(!flag, GRBgeterrormsg(m->env));
394  }
395 
396  // SOCP helper constraints
397  const Sparsity& sp = sm.map_Q.sparsity();
398  const casadi_int* colind = sp.colind();
399  const casadi_int* row = sp.row();
400  const casadi_int* data = sm.map_Q.ptr();
401 
402  // Loop over columns
403  for (casadi_int i=0; i<sp.size2(); ++i) {
404 
405  casadi_int numnz = 0;
406  // Loop over rows
407  for (casadi_int k=colind[i]; k<colind[i+1]; ++k) {
408  casadi_int j = row[k];
409 
410  ind[numnz] = j;
411  val[numnz] = (q && j<nx_) ? q[data[k]] : -1;
412 
413  numnz++;
414  }
415 
416  // Get bound
417  double bound = sm.map_P[i]==-1 ? 0 : -p[sm.map_P[i]];
418 
419  flag = GRBaddconstr(model, numnz, ind, val, GRB_EQUAL, bound, nullptr);
420  casadi_assert(!flag, GRBgeterrormsg(m->env));
421  }
422 
423  // Loop over blocks
424  for (casadi_int i=0; i<sm.r.size()-1; ++i) {
425  casadi_int block_size = sm.r[i+1]-sm.r[i];
426 
427  // Indicate x'x - y^2 <= 0
428  for (casadi_int j=0;j<block_size;++j) {
429  ind[j] = nx_ + sm.r[i] + j;
430  val[j] = j<block_size-1 ? 1 : -1;
431  }
432 
433  flag = GRBaddqconstr(model, 0, nullptr, nullptr,
434  block_size, ind, ind, val,
435  GRB_LESS_EQUAL, 0, nullptr);
436  casadi_assert(!flag, GRBgeterrormsg(m->env));
437  }
438 
439  flag = 0;
440  for (auto && op : opts_) {
441  int ret = GRBgetparamtype(m->env, op.first.c_str());
442  switch (ret) {
443  case -1:
444  casadi_error("Parameter '" + op.first + "' unknown to Gurobi.");
445  case 1:
446  {
447  flag = GRBsetintparam(GRBgetenv(model), op.first.c_str(), op.second);
448  break;
449  }
450  case 2:
451  flag = GRBsetdblparam(GRBgetenv(model), op.first.c_str(), op.second);
452  break;
453  case 3:
454  {
455  std::string s = op.second;
456  flag = GRBsetstrparam(GRBgetenv(model), op.first.c_str(), s.c_str());
457  break;
458  }
459  default:
460  casadi_error("Not implememented : " + str(ret));
461  }
462  casadi_assert(!flag, GRBgeterrormsg(m->env));
463  }
464 
465  m->fstats.at("preprocessing").toc();
466  m->fstats.at("solver").tic();
467 
468  // Solve the optimization problem
469  flag = GRBoptimize(model);
470  casadi_assert(!flag, GRBgeterrormsg(m->env));
471 
472  m->fstats.at("solver").toc();
473  m->fstats.at("postprocessing").tic();
474 
475  int optimstatus;
476  flag = GRBgetintattr(model, "Status", &optimstatus);
477  casadi_assert(!flag, GRBgeterrormsg(m->env));
478 
479  if (verbose_) uout() << "return status: " << return_status_string(optimstatus) <<
480  " (" << optimstatus <<")" << std::endl;
481 
482  m->return_status = optimstatus;
483  m->d_qp.success = optimstatus==GRB_OPTIMAL;
484  if (optimstatus==GRB_ITERATION_LIMIT || optimstatus==GRB_TIME_LIMIT
485  || optimstatus==GRB_NODE_LIMIT || optimstatus==GRB_SOLUTION_LIMIT)
486  m->d_qp.unified_return_status = SOLVER_RET_LIMITED;
487 
488  // Get the objective value, if requested
489  if (cost) {
490  flag = GRBgetdblattr(model, "ObjVal", cost);
491  if (flag) cost[0] = casadi::nan;
492  }
493 
494  // Get the optimal solution, if requested
495  if (x) {
496  flag = GRBgetdblattrarray(model, "X", 0, nx_, x);
497  if (flag) std::fill_n(x, nx_, casadi::nan);
498  }
499  if (lam_x) {
500  flag = GRBgetdblattrarray(model, "RC", 0, nx_, lam_x);
501  if (!flag) casadi_scal(nx_, -1.0, lam_x);
502  if (flag) std::fill_n(lam_x, nx_, casadi::nan);
503  }
504  if (lam_a) {
505  flag = GRBgetdblattrarray(model, "Pi", 0, npi, get_ptr(pi));
506  if (flag) {
507  std::fill_n(lam_a, na_, casadi::nan);
508  } else {
509  const double * p = get_ptr(pi);
510  for (casadi_int i=0;i<na_;++i) {
511  if (constraint_type[i]==0) {
512  lam_a[i] = 0;
513  } else if (constraint_type[i]==1) {
514  lam_a[i] = -(*p++);
515  }
516  }
517  }
518  }
519 
520  // Get solutions from solution pool
521  flag = GRBgetintattr(model, GRB_INT_ATTR_SOLCOUNT, &(m->pool_sol_nr));
522  if (!flag && m->pool_sol_nr > 0) {
523  m->pool_obj_vals = std::vector<double>(m->pool_sol_nr, casadi::nan);
524  for (int idx = 0; idx < m->pool_sol_nr; ++idx) {
525  std::vector<double> x_pool(nx_);
526 
527  flag = GRBsetintparam(GRBgetenv(model), GRB_INT_PAR_SOLUTIONNUMBER, idx);
528  if (!flag) flag = GRBgetdblattr(model, GRB_DBL_ATTR_POOLOBJVAL, &(m->pool_obj_vals[idx]));
529  if (!flag) flag = GRBgetdblattrarray(model, GRB_DBL_ATTR_XN, 0, nx_, get_ptr(x_pool));
530 
531  if (flag) {
532  m->pool_obj_vals[idx] = casadi::nan;
533  std::fill(x_pool.begin(), x_pool.end(), casadi::nan);
534  }
535 
536  m->pool_solutions.push_back(x_pool);
537  }
538  }
539 
540  // Free memory
541  GRBfreemodel(model);
542  m->fstats.at("postprocessing").toc();
543 
544  } catch (...) {
545  // Free memory
546  if (model) GRBfreemodel(model);
547  throw;
548  }
549 
550  return 0;
551  }
552 
553  Dict GurobiInterface::get_stats(void* mem) const {
554  Dict stats = Conic::get_stats(mem);
555  auto m = static_cast<GurobiMemory*>(mem);
556  stats["return_status"] = return_status_string(m->return_status);
557  stats["pool_sol_nr"] = m->pool_sol_nr;
558  stats["pool_obj_val"] = m->pool_obj_vals;
559  stats["pool_solutions"] = m->pool_solutions;
560  return stats;
561  }
562 
564  this->env = nullptr;
565  }
566 
568  if (this->env) GRBfreeenv(this->env);
569  }
570 
572  s.version("GurobiInterface", 1);
573  s.unpack("GurobiInterface::vtype", vtype_);
574  s.unpack("GurobiInterface::opts", opts_);
575  s.unpack("GurobiInterface::sos_weights", sos_weights_);
576  s.unpack("GurobiInterface::sos_beg", sos_beg_);
577  s.unpack("GurobiInterface::sos_ind", sos_ind_);
578  s.unpack("GurobiInterface::sos_types", sos_types_);
580  }
581 
584  s.version("GurobiInterface", 1);
585  s.pack("GurobiInterface::vtype", vtype_);
586  s.pack("GurobiInterface::opts", opts_);
587  s.pack("GurobiInterface::sos_weights", sos_weights_);
588  s.pack("GurobiInterface::sos_beg", sos_beg_);
589  s.pack("GurobiInterface::sos_ind", sos_ind_);
590  s.pack("GurobiInterface::sos_types", sos_types_);
592  }
593 
594 } // namespace casadi
Internal class.
Definition: conic_impl.hpp:44
static const Options options_
Options.
Definition: conic_impl.hpp:83
casadi_int nx_
Number of decision variables.
Definition: conic_impl.hpp:169
int init_mem(void *mem) const override
Initalize memory block.
Definition: conic.cpp:451
casadi_int na_
The number of constraints (counting both equality and inequality) == A.size1()
Definition: conic_impl.hpp:172
virtual void check_inputs(const double *lbx, const double *ubx, const double *lba, const double *uba) const
Check if the numerical values of the supplied bounds make sense.
Definition: conic.cpp:488
Sparsity H_
Problem structure.
Definition: conic_impl.hpp:166
void init(const Dict &opts) override
Initialize.
Definition: conic.cpp:412
void deserialize(DeserializingStream &s, SDPToSOCPMem &m)
Definition: conic.cpp:729
std::vector< bool > discrete_
Options.
Definition: conic_impl.hpp:161
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
Definition: conic.cpp:738
Dict get_stats(void *mem) const override
Get all statistics.
Definition: conic.cpp:711
void sdp_to_socp_init(SDPToSOCPMem &mem) const
SDP to SOCP conversion initialization.
Definition: conic.cpp:580
void serialize(SerializingStream &s, const SDPToSOCPMem &m) const
Definition: conic.cpp:721
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
void version(const std::string &name, int v)
void alloc_iw(size_t sz_iw, bool persistent=false)
Ensure required length of iw field.
bool inputs_check_
Errors are thrown if numerical values of inputs look bad.
void alloc_w(size_t sz_w, bool persistent=false)
Ensure required length of w field.
std::vector< int > sos_ind_
Dict get_stats(void *mem) const override
Get all statistics.
GurobiInterface(const std::string &name, const std::map< std::string, Sparsity > &st)
Create a new Solver.
static Conic * creator(const std::string &name, const std::map< std::string, Sparsity > &st)
Create a new QP Solver.
std::vector< int > sos_types_
~GurobiInterface() override
Destructor.
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
std::vector< int > sos_beg_
std::vector< char > vtype_
void init(const Dict &opts) override
Initialize.
Dict opts_
Gurobi options.
static ProtoFunction * deserialize(DeserializingStream &s)
Deserialize with type disambiguation.
SDPToSOCPMem sdp_to_socp_mem_
SDP to SOCP conversion memory.
int solve(const double **arg, double **res, casadi_int *iw, double *w, void *mem) const override
Solve the QP.
static const std::string meta_doc
A documentation string.
static const Options options_
Options.
int init_mem(void *mem) const override
Initalize memory block.
std::vector< double > sos_weights_
const Sparsity & sparsity() const
Const access the sparsity - reference to data member.
Scalar * ptr()
static void registerPlugin(const Plugin &plugin, bool needs_lock=true)
Register an integrator in the factory.
bool verbose_
Verbose printout.
void clear_mem()
Clear all memory (called from destructor)
Helper class for Serialization.
void version(const std::string &name, int v)
void pack(const Sparsity &e)
Serializes an object to the output stream.
General sparsity class.
Definition: sparsity.hpp:106
casadi_int size2() const
Get the number of columns.
Definition: sparsity.cpp:128
const casadi_int * row() const
Get a reference to row-vector,.
Definition: sparsity.cpp:164
const casadi_int * colind() const
Get a reference to the colindex of all column element (see class description)
Definition: sparsity.cpp:168
The casadi namespace.
Definition: archiver.cpp:28
int CASADI_CONIC_GUROBI_EXPORT casadi_register_conic_gurobi(Conic::Plugin *plugin)
@ CONIC_UBA
dense, (nc x 1)
Definition: conic.hpp:181
@ CONIC_X0
dense, (n x 1)
Definition: conic.hpp:187
@ CONIC_A
The matrix A: sparse, (nc x n) - product with x must be dense.
Definition: conic.hpp:177
@ CONIC_G
The vector g: dense, (n x 1)
Definition: conic.hpp:175
@ CONIC_Q
The matrix Q: sparse symmetric, (np^2 x n)
Definition: conic.hpp:193
@ CONIC_LBA
dense, (nc x 1)
Definition: conic.hpp:179
@ CONIC_UBX
dense, (n x 1)
Definition: conic.hpp:185
@ CONIC_H
Definition: conic.hpp:173
@ CONIC_LBX
dense, (n x 1)
Definition: conic.hpp:183
@ CONIC_P
The matrix P: sparse symmetric, (np x np)
Definition: conic.hpp:195
int to_int(casadi_int rhs)
Definition: casadi_misc.cpp:56
void flatten_nested_vector(const std::vector< std::vector< T > > &nested, std::vector< S > &flat)
Flatten a nested std::vector tot a single flattened vector.
void casadi_copy(const T1 *x, casadi_int n, T1 *y)
COPY: y <-x.
void casadi_fill(T1 *x, casadi_int n, T1 alpha)
FILL: x <- alpha.
const char * return_status_string(Bonmin::TMINLP::SolverReturn status)
@ OT_STRINGVECTOR
@ OT_INTVECTOR
@ OT_DOUBLEVECTORVECTOR
@ OT_INTVECTORVECTOR
std::string str(const T &v)
String representation, any type.
void check_sos(casadi_int nx, const std::vector< std::vector< T > > &groups, std::vector< std::vector< double > > &weights, std::vector< casadi_int > &types)
Check sos structure and generate defaults.
Definition: nlp_tools.hpp:79
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
const double nan
Not a number.
Definition: calculus.hpp:53
void casadi_scal(casadi_int n, T1 alpha, T1 *x)
SCAL: x <- alpha*x.
T * get_ptr(std::vector< T > &v)
Get a pointer to the data contained in the vector.
void casadi_clear(T1 *x, casadi_int n)
CLEAR: x <- 0.
std::ostream & uout()
void CASADI_CONIC_GUROBI_EXPORT casadi_load_conic_gurobi()
@ SOLVER_RET_LIMITED
const double pi
Define pi.
Definition: calculus.hpp:46
@ CONIC_X
The primal solution.
Definition: conic.hpp:201
@ CONIC_LAM_A
The dual solution corresponding to linear bounds.
Definition: conic.hpp:205
@ CONIC_COST
The optimal cost.
Definition: conic.hpp:203
@ CONIC_LAM_X
The dual solution corresponding to simple bounds.
Definition: conic.hpp:207
SDP to SOCP conversion memory.
Definition: conic_impl.hpp:178
std::vector< casadi_int > r
Definition: conic_impl.hpp:180
std::vector< casadi_int > A_mapping
Definition: conic_impl.hpp:184
std::vector< casadi_int > map_P
Definition: conic_impl.hpp:190
Options metadata for a class.
Definition: options.hpp:40