Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
gurobi_util.cc
Go to the documentation of this file.
1// Copyright 2010-2025 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 <cstring>
17#include <string>
18#include <vector>
19
20#include "absl/log/log.h"
21#include "absl/status/status.h"
22#include "absl/status/statusor.h"
23#include "absl/strings/str_cat.h"
24#include "absl/strings/str_format.h"
25#include "absl/strings/str_join.h"
28
29namespace operations_research {
30
32 absl::StatusOr<GRBenv*> status = GetGurobiEnv();
33 if (!status.ok() || status.value() == nullptr) {
34 LOG(WARNING) << status.status();
35 return false;
36 }
37
38 GRBfreeenv(status.value());
39
40 return true;
41}
42
43absl::StatusOr<GRBenv*> GetGurobiEnv() {
44 GRBenv* env = nullptr;
45
47
48 if (GRBloadenv(&env, nullptr) != 0 || env == nullptr) {
49 return absl::FailedPreconditionError(
50 absl::StrCat("Found the Gurobi shared library, but could not create "
51 "Gurobi environment: is Gurobi licensed on this machine?",
52 GRBgeterrormsg(env)));
53 }
54
55 return env;
56}
57
58std::string GurobiParamInfoForLogging(GRBenv* grb, bool one_liner_output) {
59 const absl::ParsedFormat<'s', 's', 's'> kExtendedFormat(
60 " Parameter: '%s' value: %s default: %s");
61 const absl::ParsedFormat<'s', 's', 's'> kOneLinerFormat("'%s':%s (%s)");
62 const absl::ParsedFormat<'s', 's', 's'>& format =
63 one_liner_output ? kOneLinerFormat : kExtendedFormat;
64 std::vector<std::string> changed_parameters;
65 const int num_parameters = GRBgetnumparams(grb);
66 for (int i = 0; i < num_parameters; ++i) {
67 char* param_name = nullptr;
68 GRBgetparamname(grb, i, &param_name);
69 const int param_type = GRBgetparamtype(grb, param_name);
70 switch (param_type) {
71 case 1: // integer parameters.
72 {
73 int default_value;
74 int min_value;
75 int max_value;
76 int current_value;
77 GRBgetintparaminfo(grb, param_name, &current_value, &min_value,
78 &max_value, &default_value);
79 if (current_value != default_value) {
80 changed_parameters.push_back(
81 absl::StrFormat(format, param_name, absl::StrCat(current_value),
82 absl::StrCat(default_value)));
83 }
84 break;
85 }
86 case 2: // double parameters.
87 {
88 double default_value;
89 double min_value;
90 double max_value;
91 double current_value;
92 GRBgetdblparaminfo(grb, param_name, &current_value, &min_value,
93 &max_value, &default_value);
94 if (current_value != default_value) {
95 changed_parameters.push_back(
96 absl::StrFormat(format, param_name, absl::StrCat(current_value),
97 absl::StrCat(default_value)));
98 }
99 break;
100 }
101 case 3: // string parameters.
102 {
103 char current_value[GRB_MAX_STRLEN + 1];
104 char default_value[GRB_MAX_STRLEN + 1];
105 GRBgetstrparaminfo(grb, param_name, current_value, default_value);
106 // This ensure that strcmp does not go beyond the end of the char
107 // array.
108 current_value[GRB_MAX_STRLEN] = '\0';
109 default_value[GRB_MAX_STRLEN] = '\0';
110 if (std::strcmp(current_value, default_value) != 0) {
111 changed_parameters.push_back(absl::StrFormat(
112 format, param_name, current_value, default_value));
113 }
114 break;
115 }
116 default: // unknown parameter types
117 changed_parameters.push_back(absl::StrFormat(
118 "Parameter '%s' of unknown type %d", param_name, param_type));
119 }
120 }
121 if (changed_parameters.empty()) return "";
122 if (one_liner_output) {
123 return absl::StrCat("GurobiParams{",
124 absl::StrJoin(changed_parameters, ", "), "}");
125 }
126 return absl::StrJoin(changed_parameters, "\n");
127}
128
129} // namespace operations_research
#define RETURN_IF_ERROR(expr)
struct _GRBenv GRBenv
#define GRB_MAX_STRLEN
OR-Tools root namespace.
std::function< void(GRBenv *env)> GRBfreeenv
std::string GurobiParamInfoForLogging(GRBenv *grb, bool one_liner_output)
bool GurobiIsCorrectlyInstalled()
std::function< int(GRBenv *env, const char *paramname, char *valueP, char *defP)> GRBgetstrparaminfo
absl::Status LoadGurobiDynamicLibrary(std::vector< absl::string_view > potential_paths)
std::function< int(GRBenv **envP, const char *logfilename)> GRBloadenv
std::function< int(GRBenv *env, const char *paramname)> GRBgetparamtype
std::function< int(GRBenv *env, int i, char **paramnameP)> GRBgetparamname
std::function< const char *(GRBenv *env)> GRBgeterrormsg
absl::StatusOr< GRBenv * > GetGurobiEnv()
std::function< int(GRBenv *env, const char *paramname, double *valueP, double *minP, double *maxP, double *defP)> GRBgetdblparaminfo
std::function< int(GRBenv *env, const char *paramname, int *valueP, int *minP, int *maxP, int *defP)> GRBgetintparaminfo
std::function< int(GRBenv *env)> GRBgetnumparams