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 GurobiMemory : public ConicMemory {
50  // Gurobi environment
51  GRBenv *env;
52 
53  int return_status;
54 
55  int pool_sol_nr;
56  std::vector<double> pool_obj_vals;
57  std::vector<std::vector<double>> pool_solutions;
58 
59  // SOS structure
60  std::vector<double> sos_weights;
61  std::vector<int> sos_beg;
62  std::vector<int> sos_ind;
63  std::vector<int> sos_types;
64 
66  GurobiMemory();
67 
69  ~GurobiMemory();
70  };
71 
78  class GurobiInterface : public Conic {
79  public:
81  explicit GurobiInterface(const std::string& name,
82  const std::map<std::string, Sparsity>& st);
83 
85  static Conic* creator(const std::string& name,
86  const std::map<std::string, Sparsity>& st) {
87  return new GurobiInterface(name, st);
88  }
89 
91  ~GurobiInterface() override;
92 
93  // Get name of the plugin
94  const char* plugin_name() const override { return "gurobi";}
95 
96  // Get name of the class
97  std::string class_name() const override { return "GurobiInterface";}
98 
100 
101  static const Options options_;
102  const Options& get_options() const override { return options_;}
104 
106  void init(const Dict& opts) override;
107 
109  void* alloc_mem() const override { return new GurobiMemory();}
110 
112  int init_mem(void* mem) const override;
113 
115  void free_mem(void *mem) const override { delete static_cast<GurobiMemory*>(mem);}
116 
118  int solve(const double** arg, double** res,
119  casadi_int* iw, double* w, void* mem) const override;
120 
122  bool integer_support() const override { return true;}
123 
125  bool psd_support() const override { return true;}
126 
128  static const std::string meta_doc;
129 
131  Dict get_stats(void* mem) const override;
132 
133  // Variable types
134  std::vector<char> vtype_;
135 
136  // SOS structure
137  std::vector<double> sos_weights_;
138  std::vector<int> sos_beg_;
139  std::vector<int> sos_ind_;
140  std::vector<int> sos_types_;
141 
143  Dict opts_;
144 
146  SDPToSOCPMem sdp_to_socp_mem_;
147 
148  void serialize_body(SerializingStream &s) const override;
149 
151  static ProtoFunction* deserialize(DeserializingStream& s) { return new GurobiInterface(s); }
152 
153  protected:
155  explicit GurobiInterface(DeserializingStream& s);
156  };
157 
158 } // namespace casadi
159 
161 #endif // CASADI_GUROBI_INTERFACE_HPP
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.