1 #ifndef LPINTERFACE_LPUTIL_GUROBI_H
2 #define LPINTERFACE_LPUTIL_GUROBI_H
5 #include "lpinterface/errors.hpp"
15 inline GRBenv* create_gurobi_env() {
17 int saved_stdout = dup(1);
19 int new_stdout = open(
"/dev/null", O_WRONLY);
20 if (new_stdout != 1) {
21 throw std::runtime_error(
"Failed to redirect stdout");
24 int err = GRBloadenv(&env,
"");
27 new_stdout = dup(saved_stdout);
28 if (new_stdout != 1) {
29 throw std::runtime_error(
"Failed to redirect stdout");
34 throw GurobiException(err, GRBgeterrormsg(env));
39 inline GRBmodel* create_gurobi_model(GRBenv* env) {
41 if (
int err = GRBnewmodel(env, &model,
nullptr, 0,
nullptr,
nullptr,
nullptr,
43 throw GurobiException(err, GRBgeterrormsg(env));
48 template <
class F,
class... Args>
49 inline void gurobi_function_checked(F f, GRBmodel* g, Args&&... args) {
50 if (
int error = f(g, std::forward<Args>(args)...)) {
51 throw GurobiException(error, GRBgeterrormsg(GRBgetenv(g)));
55 template <
class F,
class... Args>
56 inline void gurobi_function_checked(F f, GRBenv* g, Args&&... args) {
57 if (
int error = f(g, std::forward<Args>(args)...)) {
58 throw GurobiException(error, GRBgeterrormsg(g));
66 #endif // LPINTERFACE_LPUTIL_GUROBI_H