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 GScipSetTimeLimit(absl::Duration time_limit, GScipParameters* parameters) {
31  if (time_limit < absl::Seconds(1e20) && time_limit > absl::Duration()) {
32  (*parameters->mutable_real_params())[std::string(kLimitsTime)] =
33  absl::ToDoubleSeconds(time_limit);
34  } else {
35  parameters->mutable_real_params()->erase(std::string(kLimitsTime));
36  }
37 }
38 
39 absl::Duration GScipTimeLimit(const GScipParameters& parameters) {
40  if (parameters.real_params().contains(std::string(kLimitsTime))) {
41  const double scip_limit =
42  parameters.real_params().at(std::string(kLimitsTime));
43  if (scip_limit >= 1e20) {
44  return absl::InfiniteDuration();
45  } else if (scip_limit <= 0.0) {
46  return absl::Duration();
47  } else {
48  return absl::Seconds(scip_limit);
49  }
50  }
51  return absl::InfiniteDuration();
52 }
53 
54 bool GScipTimeLimitSet(const GScipParameters& parameters) {
55  return parameters.real_params().contains(std::string(kLimitsTime));
56 }
57 
58 void GScipSetMaxNumThreads(int num_threads, GScipParameters* parameters) {
59  CHECK_GE(num_threads, 1);
60  (*parameters->mutable_int_params())[std::string(kParallelMaxNThreads)] =
61  num_threads;
62 }
63 
64 int GScipMaxNumThreads(const GScipParameters& parameters) {
65  if (parameters.int_params().contains(std::string(kParallelMaxNThreads))) {
66  return parameters.int_params().at(std::string(kParallelMaxNThreads));
67  }
68  return 1;
69 }
70 
71 bool GScipMaxNumThreadsSet(const GScipParameters& parameters) {
72  return parameters.int_params().contains(std::string(kParallelMaxNThreads));
73 }
74 
75 void GScipSetLogLevel(GScipParameters* parameters, int log_level) {
76  CHECK_GE(log_level, 0);
77  CHECK_LE(log_level, 5);
78  (*parameters->mutable_int_params())[std::string(kDisplayVerbLevel)] =
79  log_level;
80 }
81 
82 int GScipLogLevel(const GScipParameters& parameters) {
83  return parameters.int_params().contains(std::string(kDisplayVerbLevel))
84  ? parameters.int_params().at(std::string(kDisplayVerbLevel))
85  : 4;
86 }
87 bool GScipLogLevelSet(const GScipParameters& parameters) {
88  return parameters.int_params().contains(std::string(kDisplayVerbLevel));
89 }
90 
91 void GScipSetOutputEnabled(GScipParameters* parameters, bool output_enabled) {
92  if (output_enabled) {
93  parameters->mutable_int_params()->erase(std::string(kDisplayVerbLevel));
94  } else {
95  (*parameters->mutable_int_params())[std::string(kDisplayVerbLevel)] = 0;
96  }
97 }
98 bool GScipOutputEnabled(const GScipParameters& parameters) {
99  return !parameters.int_params().contains(std::string(kDisplayVerbLevel)) ||
100  (parameters.int_params().at(std::string(kDisplayVerbLevel)) > 0);
101 }
102 
103 bool GScipOutputEnabledSet(const GScipParameters& parameters) {
105 }
106 
107 void GScipSetRandomSeed(GScipParameters* parameters, int random_seed) {
108  random_seed = std::max(0, random_seed);
109  (*parameters->mutable_int_params())[std::string(kRandomSeedParam)] =
110  random_seed;
111 }
112 
113 int GScipRandomSeed(const GScipParameters& parameters) {
115  return parameters.int_params().at(std::string(kRandomSeedParam));
116  }
117  return -1; // Unset value.
118 }
119 
120 bool GScipRandomSeedSet(const GScipParameters& parameters) {
121  return parameters.int_params().contains(std::string(kRandomSeedParam));
122 }
123 } // namespace operations_research
operations_research::GScipRandomSeedSet
bool GScipRandomSeedSet(const GScipParameters &parameters)
Definition: gscip_parameters.cc:120
max
int64 max
Definition: alldiff_cst.cc:139
operations_research::GScipLogLevel
int GScipLogLevel(const GScipParameters &parameters)
Definition: gscip_parameters.cc:82
CHECK_GE
#define CHECK_GE(val1, val2)
Definition: base/logging.h:701
logging.h
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::GScipTimeLimitSet
bool GScipTimeLimitSet(const GScipParameters &parameters)
Definition: gscip_parameters.cc:54
operations_research::GScipTimeLimit
absl::Duration GScipTimeLimit(const GScipParameters &parameters)
Definition: gscip_parameters.cc:39
operations_research::GScipRandomSeed
int GScipRandomSeed(const GScipParameters &parameters)
Definition: gscip_parameters.cc:113
time_limit
SharedTimeLimit * time_limit
Definition: cp_model_solver.cc:2103
gscip_parameters.h
operations_research::GScipOutputEnabled
bool GScipOutputEnabled(const GScipParameters &parameters)
Definition: gscip_parameters.cc:98
operations_research::GScipSetLogLevel
void GScipSetLogLevel(GScipParameters *parameters, int log_level)
Definition: gscip_parameters.cc:75
operations_research::GScipLogLevelSet
bool GScipLogLevelSet(const GScipParameters &parameters)
Definition: gscip_parameters.cc:87
operations_research::GScipMaxNumThreadsSet
bool GScipMaxNumThreadsSet(const GScipParameters &parameters)
Definition: gscip_parameters.cc:71
CHECK_LE
#define CHECK_LE(val1, val2)
Definition: base/logging.h:699
operations_research::GScipSetOutputEnabled
void GScipSetOutputEnabled(GScipParameters *parameters, bool output_enabled)
Definition: gscip_parameters.cc:91
operations_research::GScipMaxNumThreads
int GScipMaxNumThreads(const GScipParameters &parameters)
Definition: gscip_parameters.cc:64
operations_research::GScipSetRandomSeed
void GScipSetRandomSeed(GScipParameters *parameters, int random_seed)
Definition: gscip_parameters.cc:107
operations_research::GScipSetTimeLimit
void GScipSetTimeLimit(absl::Duration time_limit, GScipParameters *parameters)
Definition: gscip_parameters.cc:30
operations_research::GScipOutputEnabledSet
bool GScipOutputEnabledSet(const GScipParameters &parameters)
Definition: gscip_parameters.cc:103
operations_research::GScipSetMaxNumThreads
void GScipSetMaxNumThreads(int num_threads, GScipParameters *parameters)
Definition: gscip_parameters.cc:58
parameters
SatParameters parameters
Definition: cp_model_fz_solver.cc:108