OR-Tools  8.1
gscip_parameters.cc
Go to the documentation of this file.
1 // Copyright 2010-2018 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 "ortools/base/logging.h"
17 
18 namespace 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.
23 namespace {
24 constexpr absl::string_view kLimitsTime = "limits/time";
25 constexpr absl::string_view kParallelMaxNThreads = "parallel/maxnthreads";
26 constexpr absl::string_view kDisplayVerbLevel = "display/verblevel";
27 constexpr absl::string_view kRandomSeedParam = "randomization/randomseedshift";
28 } // namespace
29 
30 void SetTimeLimit(const absl::Duration time_limit,
31  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 
40 absl::Duration TimeLimit(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 
55 bool TimeLimitSet(const GScipParameters& parameters) {
56  return parameters.real_params().contains(std::string(kLimitsTime));
57 }
58 
59 void SetMaxNumThreads(int num_threads, GScipParameters* parameters) {
60  CHECK_GE(num_threads, 1);
61  (*parameters->mutable_int_params())[std::string(kParallelMaxNThreads)] =
62  num_threads;
63 }
64 
65 int MaxNumThreads(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 
72 bool MaxNumThreadsSet(const GScipParameters& parameters) {
73  return parameters.int_params().contains(std::string(kParallelMaxNThreads));
74 }
75 
76 void SetLogLevel(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 
83 int LogLevel(const GScipParameters& parameters) {
84  return parameters.int_params().contains(std::string(kDisplayVerbLevel))
85  ? parameters.int_params().at(std::string(kDisplayVerbLevel))
86  : 4;
87 }
88 bool LogLevelSet(const GScipParameters& parameters) {
89  return parameters.int_params().contains(std::string(kDisplayVerbLevel));
90 }
91 
92 void SetOutputEnabled(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 }
99 bool OutputEnabled(const GScipParameters& parameters) {
100  return !parameters.int_params().contains(std::string(kDisplayVerbLevel)) ||
101  (parameters.int_params().at(std::string(kDisplayVerbLevel)) > 0);
102 }
103 
104 bool OutputEnabledSet(const GScipParameters& parameters) {
105  return LogLevelSet(parameters);
106 }
107 
108 void SetRandomSeed(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 
114 int RandomSeed(const GScipParameters& parameters) {
115  if (RandomSeedSet(parameters)) {
116  return parameters.int_params().at(std::string(kRandomSeedParam));
117  }
118  return -1; // Unset value.
119 }
120 
121 bool RandomSeedSet(const GScipParameters& parameters) {
122  return parameters.int_params().contains(std::string(kRandomSeedParam));
123 }
124 } // namespace operations_research
operations_research::RandomSeed
int RandomSeed(const GScipParameters &parameters)
Definition: gscip_parameters.cc:114
max
int64 max
Definition: alldiff_cst.cc:139
operations_research::SetOutputEnabled
void SetOutputEnabled(GScipParameters *parameters, bool output_enabled)
Definition: gscip_parameters.cc:92
operations_research::MaxNumThreads
int MaxNumThreads(const GScipParameters &parameters)
Definition: gscip_parameters.cc:65
CHECK_GE
#define CHECK_GE(val1, val2)
Definition: base/logging.h:701
logging.h
operations_research::SetTimeLimit
void SetTimeLimit(const absl::Duration time_limit, GScipParameters *parameters)
Definition: gscip_parameters.cc:30
operations_research::RandomSeedSet
bool RandomSeedSet(const GScipParameters &parameters)
Definition: gscip_parameters.cc:121
operations_research::TimeLimitSet
bool TimeLimitSet(const GScipParameters &parameters)
Definition: gscip_parameters.cc:55
operations_research
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
Definition: dense_doubly_linked_list.h:21
operations_research::OutputEnabledSet
bool OutputEnabledSet(const GScipParameters &parameters)
Definition: gscip_parameters.cc:104
operations_research::TimeLimit
absl::Duration TimeLimit(const GScipParameters &parameters)
Definition: gscip_parameters.cc:40
operations_research::SetMaxNumThreads
void SetMaxNumThreads(int num_threads, GScipParameters *parameters)
Definition: gscip_parameters.cc:59
operations_research::SetRandomSeed
void SetRandomSeed(GScipParameters *parameters, int random_seed)
Definition: gscip_parameters.cc:108
time_limit
SharedTimeLimit * time_limit
Definition: cp_model_solver.cc:2083
operations_research::OutputEnabled
bool OutputEnabled(const GScipParameters &parameters)
Definition: gscip_parameters.cc:99
gscip_parameters.h
operations_research::LogLevel
int LogLevel(const GScipParameters &parameters)
Definition: gscip_parameters.cc:83
CHECK_LE
#define CHECK_LE(val1, val2)
Definition: base/logging.h:699
operations_research::MaxNumThreadsSet
bool MaxNumThreadsSet(const GScipParameters &parameters)
Definition: gscip_parameters.cc:72
operations_research::SetLogLevel
void SetLogLevel(GScipParameters *parameters, int log_level)
Definition: gscip_parameters.cc:76
operations_research::LogLevelSet
bool LogLevelSet(const GScipParameters &parameters)
Definition: gscip_parameters.cc:88
parameters
SatParameters parameters
Definition: cp_model_fz_solver.cc:107