Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
gurobi_isv.cc
Go to the documentation of this file.
1// Copyright 2010-2024 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#include <utility>
18
19#include "absl/cleanup/cleanup.h"
20#include "absl/log/check.h"
21#include "absl/status/status.h"
22#include "absl/status/statusor.h"
23#include "absl/strings/string_view.h"
26#include "ortools/math_opt/solvers/gurobi.pb.h"
27
29
30absl::StatusOr<GRBenv*> NewPrimaryEnvFromISVKey(const GurobiIsvKey& isv_key) {
31 GRBenv* primary_env = nullptr;
32 // Surprisingly, even when Gurobi fails to load the environment, it still
33 // creates one. Here we make sure to free it properly. If initialization
34 // succeeds, we Cancel() this below.
35 absl::Cleanup primary_env_cleanup = [primary_env] {
36 GRBfreeenv(primary_env);
37 };
38 const auto handle_failure =
39 [primary_env](const int err_code,
40 const absl::string_view operation_name) -> absl::Status {
41 if (err_code == 0) {
42 return absl::OkStatus();
43 }
44 // We use GRBgeterrormsg() to get the associated error message that goes
45 // with the error and the contains additional data like the user, the host,
46 // and the hostid.
48 << "failed to create Gurobi primary environment with ISV key, "
49 << operation_name << " returned the error (" << err_code
50 << "): " << GRBgeterrormsg(primary_env);
51 };
52 RETURN_IF_ERROR(handle_failure(GRBemptyenv(&primary_env), "GRBemptyenv()"));
53 // We want to turn off logging before setting the ISV key so that it doesn't
54 // leak. We store the original logging state, and reset it at the end.
55 int original_output_flag;
57 handle_failure(GRBgetintparam(primary_env, GRB_INT_PAR_OUTPUTFLAG,
58 &original_output_flag),
59 "getting original GRB_INT_PAR_OUTPUTFLAG value"));
61 handle_failure(GRBsetintparam(primary_env, GRB_INT_PAR_OUTPUTFLAG, 0),
62 "turning off GRB_INT_PAR_OUTPUTFLAG"));
63 RETURN_IF_ERROR(handle_failure(
64 GRBsetstrparam(primary_env, "GURO_PAR_ISVNAME", isv_key.name.c_str()),
65 "setting GURO_PAR_ISVNAME"));
67 handle_failure(GRBsetstrparam(primary_env, "GURO_PAR_ISVAPPNAME",
68 isv_key.application_name.c_str()),
69 "setting GURO_PAR_ISVAPPNAME"));
70 if (isv_key.expiration != 0) {
72 handle_failure(GRBsetintparam(primary_env, "GURO_PAR_ISVEXPIRATION",
73 isv_key.expiration),
74 "setting GURO_PAR_ISVEXPIRATION"));
75 }
76 RETURN_IF_ERROR(handle_failure(
77 GRBsetstrparam(primary_env, "GURO_PAR_ISVKEY", isv_key.key.c_str()),
78 "setting GURO_PAR_ISVKEY"));
79 RETURN_IF_ERROR(handle_failure(GRBstartenv(primary_env), "GRBstartenv()"));
80 // Reset output flag to its original value.
81 RETURN_IF_ERROR(handle_failure(
82 GRBsetintparam(primary_env, GRB_INT_PAR_OUTPUTFLAG, original_output_flag),
83 "resetting GRB_INT_PAR_OUTPUTFLAG"));
84 // Environment initialization succeeded, we don't want to free it upon exiting
85 // this function.
86 std::move(primary_env_cleanup).Cancel();
87 return primary_env;
88}
89
90} // namespace operations_research::math_opt
#define RETURN_IF_ERROR(expr)
struct _GRBenv GRBenv
Definition environment.h:32
#define GRB_INT_PAR_OUTPUTFLAG
An object oriented wrapper for quadratic constraints in ModelStorage.
Definition gurobi_isv.cc:28
absl::StatusOr< GRBenv * > NewPrimaryEnvFromISVKey(const GurobiIsvKey &isv_key)
Definition gurobi_isv.cc:30
std::function< void(GRBenv *env)> GRBfreeenv
std::function< int(GRBenv *env, const char *paramname, int value)> GRBsetintparam
std::function< int(GRBenv *env, const char *paramname, const char *value)> GRBsetstrparam
std::function< int(GRBenv **envP)> GRBemptyenv
std::function< const char *(GRBenv *env)> GRBgeterrormsg
std::function< int(GRBenv *env, const char *paramname, int *valueP)> GRBgetintparam
std::function< int(GRBenv *env)> GRBstartenv
StatusBuilder InvalidArgumentErrorBuilder()
int32_t expiration
Zero means no expiration.
Definition gurobi_isv.h:32