OR-Tools  9.3
gscip_parameters.cc
Go to the documentation of this file.
1// Copyright 2010-2021 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
17
18namespace operations_research {
19
20// NOTE(user): the open source build for proto is less accepting of
21// absl::string_view than internally, so we do more conversions than would
22// appear necessary.
23namespace {
24constexpr absl::string_view kLimitsTime = "limits/time";
25constexpr absl::string_view kParallelMaxNThreads = "parallel/maxnthreads";
26constexpr absl::string_view kDisplayVerbLevel = "display/verblevel";
27constexpr absl::string_view kRandomSeedParam = "randomization/randomseedshift";
28constexpr absl::string_view kCatchCtrlCParam = "misc/catchctrlc";
29} // namespace
30
31void GScipSetTimeLimit(absl::Duration time_limit, GScipParameters* parameters) {
32 if (time_limit < absl::Seconds(1e20) && time_limit > absl::Duration()) {
33 (*parameters->mutable_real_params())[std::string(kLimitsTime)] =
34 absl::ToDoubleSeconds(time_limit);
35 } else {
36 parameters->mutable_real_params()->erase(std::string(kLimitsTime));
37 }
38}
39
40absl::Duration GScipTimeLimit(const GScipParameters& parameters) {
41 if (parameters.real_params().contains(std::string(kLimitsTime))) {
42 const double scip_limit =
43 parameters.real_params().at(std::string(kLimitsTime));
44 if (scip_limit >= 1e20) {
45 return absl::InfiniteDuration();
46 } else if (scip_limit <= 0.0) {
47 return absl::Duration();
48 } else {
49 return absl::Seconds(scip_limit);
50 }
51 }
52 return absl::InfiniteDuration();
53}
54
55bool GScipTimeLimitSet(const GScipParameters& parameters) {
56 return parameters.real_params().contains(std::string(kLimitsTime));
57}
58
59void GScipSetMaxNumThreads(int num_threads, GScipParameters* parameters) {
60 CHECK_GE(num_threads, 1);
61 (*parameters->mutable_int_params())[std::string(kParallelMaxNThreads)] =
62 num_threads;
63}
64
65int GScipMaxNumThreads(const GScipParameters& parameters) {
66 if (parameters.int_params().contains(std::string(kParallelMaxNThreads))) {
67 return parameters.int_params().at(std::string(kParallelMaxNThreads));
68 }
69 return 1;
70}
71
72bool GScipMaxNumThreadsSet(const GScipParameters& parameters) {
73 return parameters.int_params().contains(std::string(kParallelMaxNThreads));
74}
75
76void GScipSetLogLevel(GScipParameters* parameters, int log_level) {
77 CHECK_GE(log_level, 0);
78 CHECK_LE(log_level, 5);
79 (*parameters->mutable_int_params())[std::string(kDisplayVerbLevel)] =
80 log_level;
81}
82
83int GScipLogLevel(const GScipParameters& parameters) {
84 return parameters.int_params().contains(std::string(kDisplayVerbLevel))
85 ? parameters.int_params().at(std::string(kDisplayVerbLevel))
86 : 4;
87}
88bool GScipLogLevelSet(const GScipParameters& parameters) {
89 return parameters.int_params().contains(std::string(kDisplayVerbLevel));
90}
91
92void GScipSetOutputEnabled(GScipParameters* parameters, bool output_enabled) {
93 if (output_enabled) {
94 parameters->mutable_int_params()->erase(std::string(kDisplayVerbLevel));
95 } else {
96 (*parameters->mutable_int_params())[std::string(kDisplayVerbLevel)] = 0;
97 }
98}
99bool GScipOutputEnabled(const GScipParameters& parameters) {
100 return !parameters.int_params().contains(std::string(kDisplayVerbLevel)) ||
101 (parameters.int_params().at(std::string(kDisplayVerbLevel)) > 0);
102}
103
104bool GScipOutputEnabledSet(const GScipParameters& parameters) {
106}
107
108void GScipSetRandomSeed(GScipParameters* parameters, int random_seed) {
109 random_seed = std::max(0, random_seed);
110 (*parameters->mutable_int_params())[std::string(kRandomSeedParam)] =
111 random_seed;
112}
113
114int GScipRandomSeed(const GScipParameters& parameters) {
116 return parameters.int_params().at(std::string(kRandomSeedParam));
117 }
118 return -1; // Unset value.
119}
120
121bool GScipRandomSeedSet(const GScipParameters& parameters) {
122 return parameters.int_params().contains(std::string(kRandomSeedParam));
123}
124
125void GScipSetCatchCtrlC(const bool catch_ctrl_c,
126 GScipParameters* const parameters) {
127 (*parameters->mutable_bool_params())[std::string(kCatchCtrlCParam)] =
128 catch_ctrl_c;
129}
130
131bool GScipCatchCtrlC(const GScipParameters& parameters) {
133 return parameters.bool_params().at(std::string(kCatchCtrlCParam));
134 }
135 return true;
136}
137
138bool GScipCatchCtrlCSet(const GScipParameters& parameters) {
139 return parameters.bool_params().contains(std::string(kCatchCtrlCParam));
140}
141
142} // namespace operations_research
int64_t max
Definition: alldiff_cst.cc:140
#define CHECK_GE(val1, val2)
Definition: base/logging.h:707
#define CHECK_LE(val1, val2)
Definition: base/logging.h:705
SatParameters parameters
ModelSharedTimeLimit * time_limit
Collection of objects used to extend the Constraint Solver library.
void GScipSetCatchCtrlC(const bool catch_ctrl_c, GScipParameters *const parameters)
int GScipLogLevel(const GScipParameters &parameters)
void GScipSetTimeLimit(absl::Duration time_limit, GScipParameters *parameters)
bool GScipLogLevelSet(const GScipParameters &parameters)
int GScipMaxNumThreads(const GScipParameters &parameters)
void GScipSetRandomSeed(GScipParameters *parameters, int random_seed)
bool GScipCatchCtrlC(const GScipParameters &parameters)
bool GScipOutputEnabledSet(const GScipParameters &parameters)
void GScipSetOutputEnabled(GScipParameters *parameters, bool output_enabled)
bool GScipCatchCtrlCSet(const GScipParameters &parameters)
bool GScipMaxNumThreadsSet(const GScipParameters &parameters)
bool GScipOutputEnabled(const GScipParameters &parameters)
void GScipSetMaxNumThreads(int num_threads, GScipParameters *parameters)
int GScipRandomSeed(const GScipParameters &parameters)
void GScipSetLogLevel(GScipParameters *parameters, int log_level)
bool GScipRandomSeedSet(const GScipParameters &parameters)
bool GScipTimeLimitSet(const GScipParameters &parameters)
absl::Duration GScipTimeLimit(const GScipParameters &parameters)