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