gurobi_interface.hpp
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 #ifndef CASADI_GUROBI_INTERFACE_HPP
27 #define CASADI_GUROBI_INTERFACE_HPP
28 
29 #include "casadi/core/conic_impl.hpp"
30 #include <casadi/interfaces/gurobi/casadi_conic_gurobi_export.h>
31 
32 // GUROBI header
33 extern "C" {
34 #include "gurobi_c.h" // NOLINT(build/include)
35 }
36 
47 namespace casadi {
48 
49  struct LazyCallbackMemory {
50  // sizes
51  casadi_int nx;
52  casadi_int sz_arg, sz_res, sz_iw, sz_w;
53 
54  // solution and inputs
55  std::vector<double> x_vals;
56  double obj_val;
57  double obj_best;
58  double obj_bound;
59  double sol_count;
60  std::vector<double> input_data;
61 
62  // outputs
63  double flag;
64  std::vector<double> a_vec;
65  double b_val;
66 
67  // casadi workspaces
68  std::vector<casadi_int> iw;
69  std::vector<double> w;
70  std::vector<const double*> arg;
71  std::vector<double*> res;
72  };
73 
74  class GurobiInterface;
75 
76  struct GurobiMemory : public ConicMemory {
77  // Gurobi environment
78  GRBenv *env;
79 
80  const GurobiInterface* interface;
81 
82  int return_status;
83 
84  int pool_sol_nr;
85  std::vector<double> pool_obj_vals;
86  std::vector<std::vector<double>> pool_solutions;
87 
88  // SOS structure
89  std::vector<double> sos_weights;
90  std::vector<int> sos_beg;
91  std::vector<int> sos_ind;
92  std::vector<int> sos_types;
93 
94  LazyCallbackMemory lazy_cb_mem;
95 
97  GurobiMemory();
98 
100  ~GurobiMemory();
101  };
102 
109  class GurobiInterface : public Conic {
110  public:
112  explicit GurobiInterface(const std::string& name,
113  const std::map<std::string, Sparsity>& st);
114 
116  static Conic* creator(const std::string& name,
117  const std::map<std::string, Sparsity>& st) {
118  return new GurobiInterface(name, st);
119  }
120 
122  ~GurobiInterface() override;
123 
124  // Get name of the plugin
125  const char* plugin_name() const override { return "gurobi";}
126 
127  // Get name of the class
128  std::string class_name() const override { return "GurobiInterface";}
129 
131 
132  static const Options options_;
133  const Options& get_options() const override { return options_;}
135 
137  void init(const Dict& opts) override;
138 
140  void* alloc_mem() const override { return new GurobiMemory();}
141 
143  int init_mem(void* mem) const override;
144 
146  void free_mem(void *mem) const override { delete static_cast<GurobiMemory*>(mem);}
147 
149  int solve(const double** arg, double** res,
150  casadi_int* iw, double* w, void* mem) const override;
151 
153  bool integer_support() const override { return true;}
154 
156  bool psd_support() const override { return true;}
157 
159  static const std::string meta_doc;
160 
162  Dict get_stats(void* mem) const override;
163 
164  // Variable types
165  std::vector<char> vtype_;
166 
167  // SOS structure
168  std::vector<double> sos_weights_;
169  std::vector<int> sos_beg_;
170  std::vector<int> sos_ind_;
171  std::vector<int> sos_types_;
172 
174  Dict opts_;
175 
177  SDPToSOCPMem sdp_to_socp_mem_;
178 
179  void serialize_body(SerializingStream &s) const override;
180 
182  static ProtoFunction* deserialize(DeserializingStream& s) { return new GurobiInterface(s); }
183 
185  Function lazy_constraints_callback_;
186 
188  void handle_lazy_constraints_callback(GurobiMemory* mem, GRBmodel *model,
189  void *cbdata, int where) const;
190 
192  void process_lazy_constraints(GRBmodel *model, void *cbdata,
193  std::vector<double>* a_vec,
194  double* b_val) const;
195 
196  protected:
198  explicit GurobiInterface(DeserializingStream& s);
199  };
200 
201 } // namespace casadi
202 
204 #endif // CASADI_GUROBI_INTERFACE_HPP
The casadi namespace.
Definition: archiver.hpp:32
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.