OR-Tools  8.0
gurobi_environment.cc
Go to the documentation of this file.
1 // Copyright 2010-2018 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
15 
16 #include <string>
17 
18 #include "absl/status/status.h"
19 #include "absl/strings/match.h"
20 #include "absl/strings/str_cat.h"
21 #include "absl/strings/str_format.h"
22 #include "ortools/base/logging.h"
24 
25 namespace operations_research {
26 absl::Status LoadGurobiEnvironment(GRBenv **env) {
27  constexpr int GRB_OK = 0;
28  const char kGurobiEnvErrorMsg[] =
29  "Could not load Gurobi environment. Is gurobi correctly installed and "
30  "licensed on this machine?";
31 
32  if (GRBloadenv(env, nullptr) != 0 || *env == nullptr) {
33  return absl::FailedPreconditionError(
34  absl::StrFormat("%s %s", kGurobiEnvErrorMsg, GRBgeterrormsg(*env)));
35  }
36  return absl::OkStatus();
37 }
38 
39 std::function<int(GRBmodel *, int, int *, double *, double, double,
40  const char *)>
41  GRBaddrangeconstr = nullptr;
42 std::function<int(GRBmodel *, int, int, int *, int *, double *, double *,
43  double *, double *, char *, char **)>
44  GRBaddvars = nullptr;
45 std::function<void(GRBenv *)> GRBfreeenv = nullptr;
46 std::function<int(GRBmodel *)> GRBfreemodel = nullptr;
47 std::function<int(GRBmodel *, const char *, int, char *)>
49 std::function<int(GRBmodel *, const char *, double *)> GRBgetdblattr = nullptr;
50 std::function<int(GRBmodel *, const char *, int, int, double *)>
51  GRBgetdblattrarray = nullptr;
52 std::function<int(GRBmodel *, const char *, int, double *)>
54 std::function<int(GRBenv *, const char *, double *)> GRBgetdblparam = nullptr;
55 std::function<GRBenv *(GRBmodel *)> GRBgetenv = nullptr;
56 std::function<char *(GRBenv *)> GRBgeterrormsg = nullptr;
57 std::function<int(GRBmodel *, const char *, int *)> GRBgetintattr = nullptr;
58 std::function<int(GRBmodel *, const char *, int, int *)> GRBgetintattrelement =
59  nullptr;
60 std::function<int(GRBenv **, const char *)> GRBloadenv = nullptr;
61 std::function<int(GRBenv *, GRBmodel **, const char *, int numvars, double *,
62  double *, double *, char *, char **)>
63  GRBnewmodel = nullptr;
64 std::function<int(GRBmodel *)> GRBoptimize = nullptr;
65 std::function<int(GRBenv *, const char *)> GRBreadparams = nullptr;
66 std::function<int(GRBenv *)> GRBresetparams = nullptr;
67 std::function<int(GRBmodel *, const char *, int, char)> GRBsetcharattrelement =
68  nullptr;
69 std::function<int(GRBmodel *, const char *, double)> GRBsetdblattr = nullptr;
70 std::function<int(GRBmodel *, const char *, int, double)> GRBsetdblattrelement =
71  nullptr;
72 std::function<int(GRBenv *, const char *, double)> GRBsetdblparam = nullptr;
73 std::function<int(GRBmodel *, const char *, int)> GRBsetintattr = nullptr;
74 std::function<int(GRBenv *, const char *, int)> GRBsetintparam = nullptr;
75 std::function<void(GRBmodel *)> GRBterminate = nullptr;
76 std::function<int(GRBmodel *)> GRBupdatemodel = nullptr;
77 std::function<void(int *, int *, int *)> GRBversion = nullptr;
78 std::function<int(GRBmodel *, const char *)> GRBwrite = nullptr;
79 std::function<int(void *cbdata, int where, int what, void *resultP)> GRBcbget =
80  nullptr;
81 std::function<int(void *cbdata, int cutlen, const int *cutind,
82  const double *cutval, char cutsense, double cutrhs)>
83  GRBcbcut = nullptr;
84 std::function<int(void *cbdata, int lazylen, const int *lazyind,
85  const double *lazyval, char lazysense, double lazyrhs)>
86  GRBcblazy = nullptr;
87 std::function<int(void *cbdata, const double *solution, double *objvalP)>
88  GRBcbsolution = nullptr;
89 std::function<int(GRBmodel *model, int numnz, int *cind, double *cval,
90  char sense, double rhs, const char *constrname)>
91  GRBaddconstr = nullptr;
92 std::function<int(GRBmodel *model, const char *name, int binvar, int binval,
93  int nvars, const int *vars, const double *vals, char sense,
94  double rhs)>
96 std::function<int(GRBmodel *model, const char *attrname, int element,
97  int newvalue)>
99 std::function<int(GRBmodel *model, int(STDCALL *cb)(CB_ARGS), void *usrdata)>
101 std::function<int(GRBenv *env, const char *paramname, const char *value)>
102  GRBsetparam = nullptr;
103 std::function<int(GRBmodel *model, int numsos, int nummembers, int *types,
104  int *beg, int *ind, double *weight)>
105  GRBaddsos = nullptr;
106 std::function<int(GRBmodel *model, int numlnz, int *lind, double *lval,
107  int numqnz, int *qrow, int *qcol, double *qval, char sense,
108  double rhs, const char *QCname)>
109  GRBaddqconstr = nullptr;
110 std::function<int(GRBmodel *model, const char *name, int resvar, int nvars,
111  const int *vars, double constant)>
113 std::function<int(GRBmodel *model, const char *name, int resvar, int nvars,
114  const int *vars, double constant)>
116 std::function<int(GRBmodel *model, const char *name, int resvar, int argvar)>
118 std::function<int(GRBmodel *model, const char *name, int resvar, int nvars,
119  const int *vars)>
121 std::function<int(GRBmodel *model, const char *name, int resvar, int nvars,
122  const int *vars)>
123  GRBaddgenconstrOr = nullptr;
124 std::function<int(GRBmodel *model, int numqnz, int *qrow, int *qcol,
125  double *qval)>
126  GRBaddqpterms = nullptr;
127 
128 std::unique_ptr<DynamicLibrary> gurobi_dynamic_library;
130 
192 }
193 
194 bool LoadSpecificGurobiLibrary(const std::string &full_library_path) {
195  CHECK(gurobi_dynamic_library.get() != nullptr);
196  VLOG(1) << "Try to load from " << full_library_path;
197  return gurobi_dynamic_library->TryToLoad(full_library_path);
198 }
199 
201  const char* gurobi_home_from_env = getenv("GUROBI_HOME");
202 #if defined(_MSC_VER) // Windows
203  if (!gurobi_library_path.empty() &&
205  return true;
206  }
207  if (gurobi_home_from_env != nullptr &&
209  absl::StrCat(gurobi_home_from_env, "\\bin\\gurobi90.dll"))) {
210  return true;
211  }
213  "C:\\Program Files\\gurobi902\\win64\\bin\\gurobi90.dll")) {
214  return true;
215  }
216 #elif defined(__APPLE__) // OS X
217  if (!gurobi_library_path.empty() &&
219  return true;
220  }
221  if (gurobi_home_from_env != nullptr &&
223  absl::StrCat(gurobi_home_from_env, "/lib/libgurobi90.dylib"))) {
224  return true;
225  }
227  "/Library/gurobi902/mac64/lib/libgurobi90.dylib")) {
228  return true;
229  }
230 #elif defined(__GNUC__) // Linux
231  if (gurobi_home_from_env != nullptr &&
233  absl::StrCat(gurobi_home_from_env, "/lib/libgurobi90.so"))) {
234  return true;
235  }
236  if (!gurobi_library_path.empty() &&
238  return true;
239  }
240 #endif
241 
242  return false;
243 }
244 
246  if (gurobi_dynamic_library.get() != nullptr) {
247  return gurobi_dynamic_library->LibraryIsLoaded();
248  }
249 
251 
254  return true;
255  }
256 
257  return false;
258 }
259 
260 void MPSolver::SetGurobiLibraryPath(const std::string &full_library_path) {
261  gurobi_library_path = full_library_path;
262 }
263 
264 bool MPSolver::GurobiIsCorrectlyInstalled() {
265  if (!LoadGurobiSharedLibrary()) return false;
266  GRBenv *env;
267  if (GRBloadenv(&env, nullptr) != 0 || env == nullptr) return false;
268 
269  return true;
270 }
271 
272 } // namespace operations_research
STDCALL
#define STDCALL
Definition: gurobi_environment.h:25
operations_research::GRBaddgenconstrIndicator
std::function< int(GRBmodel *model, const char *name, int binvar, int binval, int nvars, const int *vars, const double *vals, char sense, double rhs)> GRBaddgenconstrIndicator
Definition: gurobi_environment.cc:95
where
int where
Definition: gurobi_interface.cc:197
operations_research::GRBloadenv
std::function< int(GRBenv **, const char *)> GRBloadenv
Definition: gurobi_environment.cc:60
operations_research::GRBsetcallbackfunc
std::function< int(GRBmodel *model, int(STDCALL *cb)(CB_ARGS), void *usrdata)> GRBsetcallbackfunc
Definition: gurobi_environment.cc:100
NAMEOF
#define NAMEOF(x)
Definition: dynamic_library.h:30
operations_research::GRBsetintparam
std::function< int(GRBenv *, const char *, int)> GRBsetintparam
Definition: gurobi_environment.cc:74
logging.h
operations_research::GRBcbget
std::function< int(void *cbdata, int where, int what, void *resultP)> GRBcbget
Definition: gurobi_environment.cc:79
operations_research::GRBcblazy
std::function< int(void *cbdata, int lazylen, const int *lazyind, const double *lazyval, char lazysense, double lazyrhs)> GRBcblazy
Definition: gurobi_environment.cc:86
value
int64 value
Definition: demon_profiler.cc:43
weight
int64 weight
Definition: pack.cc:509
operations_research
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
Definition: dense_doubly_linked_list.h:21
operations_research::GRBgeterrormsg
std::function< char *(GRBenv *)> GRBgeterrormsg
Definition: gurobi_environment.cc:56
operations_research::LoadSpecificGurobiLibrary
bool LoadSpecificGurobiLibrary(const std::string &full_library_path)
Definition: gurobi_environment.cc:194
DynamicLibrary
Definition: dynamic_library.h:32
operations_research::LoadGurobiEnvironment
absl::Status LoadGurobiEnvironment(GRBenv **env)
Definition: gurobi_environment.cc:26
operations_research::GRBaddgenconstrAnd
std::function< int(GRBmodel *model, const char *name, int resvar, int nvars, const int *vars)> GRBaddgenconstrAnd
Definition: gurobi_environment.cc:120
operations_research::GRBaddgenconstrAbs
std::function< int(GRBmodel *model, const char *name, int resvar, int argvar)> GRBaddgenconstrAbs
Definition: gurobi_environment.cc:117
operations_research::GRBwrite
std::function< int(GRBmodel *, const char *)> GRBwrite
Definition: gurobi_environment.cc:78
operations_research::GRBreadparams
std::function< int(GRBenv *, const char *)> GRBreadparams
Definition: gurobi_environment.cc:65
operations_research::GRBgetdblparam
std::function< int(GRBenv *, const char *, double *)> GRBgetdblparam
Definition: gurobi_environment.cc:54
operations_research::MPSolver::SetGurobiLibraryPath
static void SetGurobiLibraryPath(const std::string &full_library_path)
Definition: gurobi_environment.cc:260
operations_research::GRBsetparam
std::function< int(GRBenv *env, const char *paramname, const char *value)> GRBsetparam
Definition: gurobi_environment.cc:102
operations_research::GRBgetdblattr
std::function< int(GRBmodel *, const char *, double *)> GRBgetdblattr
Definition: gurobi_environment.cc:49
operations_research::GRBfreemodel
std::function< int(GRBmodel *)> GRBfreemodel
Definition: gurobi_environment.cc:46
operations_research::GRBnewmodel
std::function< int(GRBenv *, GRBmodel **, const char *, int numvars, double *, double *, double *, char *, char **)> GRBnewmodel
Definition: gurobi_environment.cc:63
operations_research::GRBsetintattr
std::function< int(GRBmodel *, const char *, int)> GRBsetintattr
Definition: gurobi_environment.cc:73
operations_research::GRBresetparams
std::function< int(GRBenv *)> GRBresetparams
Definition: gurobi_environment.cc:66
operations_research::GRBgetdblattrarray
std::function< int(GRBmodel *, const char *, int, int, double *)> GRBgetdblattrarray
Definition: gurobi_environment.cc:51
operations_research::GRBcbcut
std::function< int(void *cbdata, int cutlen, const int *cutind, const double *cutval, char cutsense, double cutrhs)> GRBcbcut
Definition: gurobi_environment.cc:83
operations_research::GRBaddgenconstrMax
std::function< int(GRBmodel *model, const char *name, int resvar, int nvars, const int *vars, double constant)> GRBaddgenconstrMax
Definition: gurobi_environment.cc:112
operations_research::GRBsetintattrelement
std::function< int(GRBmodel *model, const char *attrname, int element, int newvalue)> GRBsetintattrelement
Definition: gurobi_environment.cc:98
operations_research::GRBsetcharattrelement
std::function< int(GRBmodel *, const char *, int, char)> GRBsetcharattrelement
Definition: gurobi_environment.cc:67
operations_research::SearchForGurobiDynamicLibrary
bool SearchForGurobiDynamicLibrary()
Definition: gurobi_environment.cc:200
operations_research::GRBsetdblattrelement
std::function< int(GRBmodel *, const char *, int, double)> GRBsetdblattrelement
Definition: gurobi_environment.cc:70
operations_research::MPSolver::LoadGurobiSharedLibrary
static bool LoadGurobiSharedLibrary()
Definition: gurobi_environment.cc:245
operations_research::GRBversion
std::function< void(int *, int *, int *)> GRBversion
Definition: gurobi_environment.cc:77
operations_research::GRBsetdblattr
std::function< int(GRBmodel *, const char *, double)> GRBsetdblattr
Definition: gurobi_environment.cc:69
operations_research::GRBaddsos
std::function< int(GRBmodel *model, int numsos, int nummembers, int *types, int *beg, int *ind, double *weight)> GRBaddsos
Definition: gurobi_environment.cc:105
operations_research::GRBaddrangeconstr
std::function< int(GRBmodel *, int, int *, double *, double, double, const char *)> GRBaddrangeconstr
Definition: gurobi_environment.cc:41
operations_research::GRBcbsolution
std::function< int(void *cbdata, const double *solution, double *objvalP)> GRBcbsolution
Definition: gurobi_environment.cc:88
operations_research::GRBgetintattr
std::function< int(GRBmodel *, const char *, int *)> GRBgetintattr
Definition: gurobi_environment.cc:57
operations_research::GRBaddgenconstrMin
std::function< int(GRBmodel *model, const char *name, int resvar, int nvars, const int *vars, double constant)> GRBaddgenconstrMin
Definition: gurobi_environment.cc:115
GRBenv
struct _GRBenv GRBenv
Definition: gurobi_environment.h:30
model
GRBmodel * model
Definition: gurobi_interface.cc:195
operations_research::LoadGurobiFunctions
void LoadGurobiFunctions()
Definition: gurobi_environment.cc:131
operations_research::GRBoptimize
std::function< int(GRBmodel *)> GRBoptimize
Definition: gurobi_environment.cc:64
operations_research::GRBaddqconstr
std::function< int(GRBmodel *model, int numlnz, int *lind, double *lval, int numqnz, int *qrow, int *qcol, double *qval, char sense, double rhs, const char *QCname)> GRBaddqconstr
Definition: gurobi_environment.cc:109
operations_research::GRBaddgenconstrOr
std::function< int(GRBmodel *model, const char *name, int resvar, int nvars, const int *vars)> GRBaddgenconstrOr
Definition: gurobi_environment.cc:123
gurobi_environment.h
operations_research::GRBaddqpterms
std::function< int(GRBmodel *model, int numqnz, int *qrow, int *qcol, double *qval)> GRBaddqpterms
Definition: gurobi_environment.cc:126
operations_research::GRBgetenv
std::function< GRBenv *(GRBmodel *)> GRBgetenv
Definition: gurobi_environment.cc:55
linear_solver.h
operations_research::GRBupdatemodel
std::function< int(GRBmodel *)> GRBupdatemodel
Definition: gurobi_environment.cc:76
operations_research::GRBgetcharattrelement
std::function< int(GRBmodel *, const char *, int, char *)> GRBgetcharattrelement
Definition: gurobi_environment.cc:48
GRBmodel
struct _GRBmodel GRBmodel
Definition: gurobi_environment.h:29
operations_research::GRBsetdblparam
std::function< int(GRBenv *, const char *, double)> GRBsetdblparam
Definition: gurobi_environment.cc:72
operations_research::GRBgetdblattrelement
std::function< int(GRBmodel *, const char *, int, double *)> GRBgetdblattrelement
Definition: gurobi_environment.cc:53
operations_research::GRBgetintattrelement
std::function< int(GRBmodel *, const char *, int, int *)> GRBgetintattrelement
Definition: gurobi_environment.cc:58
operations_research::GRBterminate
std::function< void(GRBmodel *)> GRBterminate
Definition: gurobi_environment.cc:75
operations_research::GRBfreeenv
std::function< void(GRBenv *)> GRBfreeenv
Definition: gurobi_environment.cc:45
operations_research::GRBaddconstr
std::function< int(GRBmodel *model, int numnz, int *cind, double *cval, char sense, double rhs, const char *constrname)> GRBaddconstr
Definition: gurobi_environment.cc:91
name
const std::string name
Definition: default_search.cc:807
operations_research::gurobi_dynamic_library
std::unique_ptr< DynamicLibrary > gurobi_dynamic_library
Definition: gurobi_environment.cc:128
operations_research::GRBaddvars
std::function< int(GRBmodel *, int, int, int *, int *, double *, double *, double *, double *, char *, char **)> GRBaddvars
Definition: gurobi_environment.cc:44
operations_research::gurobi_library_path
std::string gurobi_library_path
Definition: gurobi_environment.cc:129
CB_ARGS
#define CB_ARGS
Definition: gurobi_environment.h:36