OR-Tools  8.2
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, const char*)>
40  GRBaddrangeconstr = nullptr;
41 std::function<int(GRBmodel* model, int numnz, int* vind, double* vval,
42  double obj, double lb, double ub, char vtype,
43  const char* varname)>
44  GRBaddvar = nullptr;
45 std::function<int(GRBmodel*, int, int, int*, int*, double*, double*, double*,
46  double*, char*, char**)>
47  GRBaddvars = nullptr;
48 std::function<int(GRBmodel* model, int numchgs, int* cind, int* vind,
49  double* val)>
50  GRBchgcoeffs = nullptr;
51 std::function<void(GRBenv*)> GRBfreeenv = nullptr;
52 std::function<int(GRBmodel*)> GRBfreemodel = nullptr;
53 std::function<int(GRBmodel*, const char*, int, char*)> GRBgetcharattrelement =
54  nullptr;
55 std::function<int(GRBmodel*, const char*, double*)> GRBgetdblattr = nullptr;
56 std::function<int(GRBmodel*, const char*, int, int, double*)>
57  GRBgetdblattrarray = nullptr;
58 std::function<int(GRBmodel*, const char*, int, double*)> GRBgetdblattrelement =
59  nullptr;
60 std::function<int(GRBenv*, const char*, double*)> GRBgetdblparam = nullptr;
61 std::function<GRBenv*(GRBmodel*)> GRBgetenv = nullptr;
62 std::function<char*(GRBenv*)> GRBgeterrormsg = nullptr;
63 std::function<int(GRBmodel*, const char*, int*)> GRBgetintattr = nullptr;
64 std::function<int(GRBmodel*, const char*, int, int*)> GRBgetintattrelement =
65  nullptr;
66 std::function<int(GRBenv**, const char*)> GRBloadenv = nullptr;
67 std::function<int(GRBenv*, GRBmodel**, const char*, int numvars, double*,
68  double*, double*, char*, char**)>
69  GRBnewmodel = nullptr;
70 std::function<int(GRBmodel*)> GRBoptimize = nullptr;
71 std::function<int(GRBenv *dest, GRBenv *src)> GRBcopyparams = nullptr;
72 std::function<int(GRBenv*, const char*)> GRBreadparams = nullptr;
73 std::function<int(GRBenv*)> GRBresetparams = nullptr;
74 std::function<int(GRBmodel*, const char*, int, char)> GRBsetcharattrelement =
75  nullptr;
76 std::function<int(GRBmodel*, const char*, double)> GRBsetdblattr = nullptr;
77 std::function<int(GRBmodel*, const char*, int, double)> GRBsetdblattrelement =
78  nullptr;
79 std::function<int(GRBenv*, const char*, double)> GRBsetdblparam = nullptr;
80 std::function<int(GRBmodel*, const char*, int)> GRBsetintattr = nullptr;
81 std::function<int(GRBenv*, const char*, int)> GRBsetintparam = nullptr;
82 std::function<void(GRBmodel*)> GRBterminate = nullptr;
83 std::function<int(GRBmodel*)> GRBupdatemodel = nullptr;
84 std::function<void(int*, int*, int*)> GRBversion = nullptr;
85 std::function<int(GRBmodel*, const char*)> GRBwrite = nullptr;
86 std::function<int(void* cbdata, int where, int what, void* resultP)> GRBcbget =
87  nullptr;
88 std::function<int(void* cbdata, int cutlen, const int* cutind,
89  const double* cutval, char cutsense, double cutrhs)>
90  GRBcbcut = nullptr;
91 std::function<int(void* cbdata, int lazylen, const int* lazyind,
92  const double* lazyval, char lazysense, double lazyrhs)>
93  GRBcblazy = nullptr;
94 std::function<int(void* cbdata, const double* solution, double* objvalP)>
95  GRBcbsolution = nullptr;
96 std::function<int(GRBmodel* model, int numnz, int* cind, double* cval,
97  char sense, double rhs, const char* constrname)>
98  GRBaddconstr = nullptr;
99 std::function<int(GRBmodel* model, const char* name, int binvar, int binval,
100  int nvars, const int* vars, const double* vals, char sense,
101  double rhs)>
103 std::function<int(GRBmodel* model, const char* attrname, int element,
104  int newvalue)>
106 std::function<int(GRBmodel* model, int(STDCALL* cb)(CB_ARGS), void* usrdata)>
108 std::function<int(GRBenv* env, const char* paramname, const char* value)>
109  GRBsetparam = nullptr;
110 std::function<int(GRBmodel* model, int numsos, int nummembers, int* types,
111  int* beg, int* ind, double* weight)>
112  GRBaddsos = nullptr;
113 std::function<int(GRBmodel* model, int numlnz, int* lind, double* lval,
114  int numqnz, int* qrow, int* qcol, double* qval, char sense,
115  double rhs, const char* QCname)>
116  GRBaddqconstr = nullptr;
117 std::function<int(GRBmodel* model, const char* name, int resvar, int nvars,
118  const int* vars, double constant)>
120 std::function<int(GRBmodel* model, const char* name, int resvar, int nvars,
121  const int* vars, double constant)>
123 std::function<int(GRBmodel* model, const char* name, int resvar, int argvar)>
125 std::function<int(GRBmodel* model, const char* name, int resvar, int nvars,
126  const int* vars)>
128 std::function<int(GRBmodel* model, const char* name, int resvar, int nvars,
129  const int* vars)>
130  GRBaddgenconstrOr = nullptr;
131 std::function<int(GRBmodel* model, int numqnz, int* qrow, int* qcol,
132  double* qval)>
133  GRBaddqpterms = nullptr;
134 
135 std::unique_ptr<DynamicLibrary> gurobi_dynamic_library;
137 
202 }
203 
204 bool LoadSpecificGurobiLibrary(const std::string& full_library_path) {
205  CHECK(gurobi_dynamic_library.get() != nullptr);
206  VLOG(1) << "Try to load from " << full_library_path;
207  return gurobi_dynamic_library->TryToLoad(full_library_path);
208 }
209 
210 namespace {
211 const std::vector<std::vector<std::string>> GurobiVersionLib = {
212  {"911", "91"}, {"910", "91"}, {"903", "90"}, {"902", "90"}};
213 }
214 
216  if (!gurobi_library_path.empty() &&
218  return true;
219  }
220 
221  const char* gurobi_home_from_env = getenv("GUROBI_HOME");
222  for (const std::vector<std::string>& version_lib : GurobiVersionLib) {
223  const std::string& dir = version_lib[0];
224  const std::string& number = version_lib[1];
225 #if defined(_MSC_VER) // Windows
226  if (gurobi_home_from_env != nullptr &&
227  LoadSpecificGurobiLibrary(absl::StrCat(
228  gurobi_home_from_env, "\\bin\\gurobi", number, ".dll"))) {
229  return true;
230  }
232  absl::StrCat("C:\\Program Files\\gurobi", dir,
233  "\\win64\\bin\\gurobi", number, ".dll"))) {
234  return true;
235  }
236 #elif defined(__APPLE__) // OS X
237  if (gurobi_home_from_env != nullptr &&
238  LoadSpecificGurobiLibrary(absl::StrCat(
239  gurobi_home_from_env, "/lib/libgurobi", number, ".dylib"))) {
240  return true;
241  }
242  if (LoadSpecificGurobiLibrary(absl::StrCat(
243  "/Library/gurobi", dir, "/mac64/lib/libgurobi", number, ".dylib"))) {
244  return true;
245  }
246 #elif defined(__GNUC__) // Linux
247  if (gurobi_home_from_env != nullptr &&
248  LoadSpecificGurobiLibrary(absl::StrCat(
249  gurobi_home_from_env, "/lib/libgurobi", number, ".so"))) {
250  return true;
251  }
252  if (gurobi_home_from_env != nullptr &&
253  LoadSpecificGurobiLibrary(absl::StrCat(
254  gurobi_home_from_env, "/lib64/libgurobi", number, ".so"))) {
255  return true;
256  }
257 #endif
258  }
259 
260  return false;
261 }
262 
264  if (gurobi_dynamic_library.get() != nullptr) {
265  return gurobi_dynamic_library->LibraryIsLoaded();
266  }
267 
269 
272  return true;
273  }
274 
275  return false;
276 }
277 
278 void MPSolver::SetGurobiLibraryPath(const std::string& full_library_path) {
279  gurobi_library_path = full_library_path;
280 }
281 
282 bool MPSolver::GurobiIsCorrectlyInstalled() {
283  if (!LoadGurobiSharedLibrary()) return false;
284  GRBenv* env;
285  if (GRBloadenv(&env, nullptr) != 0 || env == nullptr) return false;
286 
287  GRBfreeenv(env);
288 
289  return true;
290 }
291 
292 } // namespace operations_research
#define CHECK(condition)
Definition: base/logging.h:495
#define VLOG(verboselevel)
Definition: base/logging.h:978
static void SetGurobiLibraryPath(const std::string &full_library_path)
const std::string name
int64 value
#define NAMEOF(x)
struct _GRBenv GRBenv
#define STDCALL
struct _GRBmodel GRBmodel
#define CB_ARGS
GRBmodel * model
int where
A C++ wrapper that provides a simple and unified interface to several linear programming and mixed in...
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
std::function< int(GRBenv *, GRBmodel **, const char *, int numvars, double *, double *, double *, char *, char **)> GRBnewmodel
std::function< void(int *, int *, int *)> GRBversion
std::function< int(GRBmodel *model, const char *name, int resvar, int nvars, const int *vars, double constant)> GRBaddgenconstrMin
std::function< int(GRBmodel *model, int numnz, int *cind, double *cval, char sense, double rhs, const char *constrname)> GRBaddconstr
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
std::function< int(GRBmodel *model, int numnz, int *vind, double *vval, double obj, double lb, double ub, char vtype, const char *varname)> GRBaddvar
std::function< void(GRBenv *)> GRBfreeenv
std::function< int(GRBmodel *, const char *, int, char)> GRBsetcharattrelement
std::function< char *(GRBenv *)> GRBgeterrormsg
std::function< int(GRBmodel *, const char *, int)> GRBsetintattr
std::function< int(GRBmodel *, const char *)> GRBwrite
std::function< int(GRBenv *env, const char *paramname, const char *value)> GRBsetparam
std::function< int(GRBmodel *model, int(STDCALL *cb)(CB_ARGS), void *usrdata)> GRBsetcallbackfunc
std::function< int(void *cbdata, int lazylen, const int *lazyind, const double *lazyval, char lazysense, double lazyrhs)> GRBcblazy
std::function< int(GRBenv *, const char *)> GRBreadparams
std::unique_ptr< DynamicLibrary > gurobi_dynamic_library
std::function< int(GRBmodel *model, const char *name, int resvar, int argvar)> GRBaddgenconstrAbs
std::function< int(void *cbdata, int where, int what, void *resultP)> GRBcbget
std::function< int(GRBmodel *, const char *, int *)> GRBgetintattr
std::function< int(GRBmodel *, const char *, int, int *)> GRBgetintattrelement
std::function< int(GRBenv *, const char *, double)> GRBsetdblparam
std::function< void(GRBmodel *)> GRBterminate
std::function< int(GRBenv *dest, GRBenv *src)> GRBcopyparams
std::function< int(GRBmodel *, const char *, double)> GRBsetdblattr
std::function< int(GRBmodel *)> GRBoptimize
std::function< int(GRBmodel *)> GRBupdatemodel
std::function< int(GRBmodel *model, int numqnz, int *qrow, int *qcol, double *qval)> GRBaddqpterms
std::function< int(GRBmodel *model, const char *name, int resvar, int nvars, const int *vars, double constant)> GRBaddgenconstrMax
std::function< int(GRBmodel *)> GRBfreemodel
std::function< int(GRBmodel *, const char *, int, int, double *)> GRBgetdblattrarray
std::function< int(GRBmodel *, int, int, int *, int *, double *, double *, double *, double *, char *, char **)> GRBaddvars
bool LoadSpecificGurobiLibrary(const std::string &full_library_path)
std::function< int(GRBmodel *, const char *, int, char *)> GRBgetcharattrelement
std::function< int(GRBenv *)> GRBresetparams
std::function< int(GRBmodel *model, const char *name, int resvar, int nvars, const int *vars)> GRBaddgenconstrAnd
std::function< int(GRBmodel *model, const char *attrname, int element, int newvalue)> GRBsetintattrelement
std::function< int(GRBmodel *model, const char *name, int resvar, int nvars, const int *vars)> GRBaddgenconstrOr
std::function< int(GRBmodel *model, int numchgs, int *cind, int *vind, double *val)> GRBchgcoeffs
std::function< int(void *cbdata, const double *solution, double *objvalP)> GRBcbsolution
std::function< int(GRBenv *, const char *, double *)> GRBgetdblparam
std::function< int(GRBmodel *, const char *, double *)> GRBgetdblattr
absl::Status LoadGurobiEnvironment(GRBenv **env)
std::function< int(GRBmodel *, const char *, int, double)> GRBsetdblattrelement
std::function< int(GRBmodel *, const char *, int, double *)> GRBgetdblattrelement
std::function< int(GRBmodel *model, int numsos, int nummembers, int *types, int *beg, int *ind, double *weight)> GRBaddsos
std::function< int(void *cbdata, int cutlen, const int *cutind, const double *cutval, char cutsense, double cutrhs)> GRBcbcut
std::function< GRBenv *(GRBmodel *)> GRBgetenv
std::function< int(GRBenv **, const char *)> GRBloadenv
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
std::function< int(GRBmodel *, int, int *, double *, double, double, const char *)> GRBaddrangeconstr
std::function< int(GRBenv *, const char *, int)> GRBsetintparam
int64 weight
Definition: pack.cc:509