Google OR-Tools v9.11
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-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 <cstring>
17#include <string>
18#include <vector>
19
20#include "absl/strings/str_cat.h"
21#include "absl/strings/str_format.h"
22#include "absl/strings/str_join.h"
24
25namespace operations_research {
26
27std::string GurobiParamInfoForLogging(GRBenv* grb, bool one_liner_output) {
28 const absl::ParsedFormat<'s', 's', 's'> kExtendedFormat(
29 " Parameter: '%s' value: %s default: %s");
30 const absl::ParsedFormat<'s', 's', 's'> kOneLinerFormat("'%s':%s (%s)");
31 const absl::ParsedFormat<'s', 's', 's'>& format =
32 one_liner_output ? kOneLinerFormat : kExtendedFormat;
33 std::vector<std::string> changed_parameters;
34 const int num_parameters = GRBgetnumparams(grb);
35 for (int i = 0; i < num_parameters; ++i) {
36 char* param_name = nullptr;
37 GRBgetparamname(grb, i, &param_name);
38 const int param_type = GRBgetparamtype(grb, param_name);
39 switch (param_type) {
40 case 1: // integer parameters.
41 {
42 int default_value;
43 int min_value;
44 int max_value;
45 int current_value;
46 GRBgetintparaminfo(grb, param_name, &current_value, &min_value,
47 &max_value, &default_value);
48 if (current_value != default_value) {
49 changed_parameters.push_back(
50 absl::StrFormat(format, param_name, absl::StrCat(current_value),
51 absl::StrCat(default_value)));
52 }
53 break;
54 }
55 case 2: // double parameters.
56 {
57 double default_value;
58 double min_value;
59 double max_value;
60 double current_value;
61 GRBgetdblparaminfo(grb, param_name, &current_value, &min_value,
62 &max_value, &default_value);
63 if (current_value != default_value) {
64 changed_parameters.push_back(
65 absl::StrFormat(format, param_name, absl::StrCat(current_value),
66 absl::StrCat(default_value)));
67 }
68 break;
69 }
70 case 3: // string parameters.
71 {
72 char current_value[GRB_MAX_STRLEN + 1];
73 char default_value[GRB_MAX_STRLEN + 1];
74 GRBgetstrparaminfo(grb, param_name, current_value, default_value);
75 // This ensure that strcmp does not go beyond the end of the char
76 // array.
77 current_value[GRB_MAX_STRLEN] = '\0';
78 default_value[GRB_MAX_STRLEN] = '\0';
79 if (std::strcmp(current_value, default_value) != 0) {
80 changed_parameters.push_back(absl::StrFormat(
81 format, param_name, current_value, default_value));
82 }
83 break;
84 }
85 default: // unknown parameter types
86 changed_parameters.push_back(absl::StrFormat(
87 "Parameter '%s' of unknown type %d", param_name, param_type));
88 }
89 }
90 if (changed_parameters.empty()) return "";
91 if (one_liner_output) {
92 return absl::StrCat("GurobiParams{",
93 absl::StrJoin(changed_parameters, ", "), "}");
94 }
95 return absl::StrJoin(changed_parameters, "\n");
96}
97
98} // namespace operations_research
struct _GRBenv GRBenv
Definition environment.h:32
#define GRB_MAX_STRLEN
In SWIG mode, we don't want anything besides these top-level includes.
std::string GurobiParamInfoForLogging(GRBenv *grb, bool one_liner_output)
std::function< int(GRBenv *env, const char *paramname, char *valueP, char *defP)> GRBgetstrparaminfo
std::function< int(GRBenv *env, const char *paramname)> GRBgetparamtype
std::function< int(GRBenv *env, int i, char **paramnameP)> GRBgetparamname
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