Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
gscip_parameters.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 <algorithm>
17#include <string>
18#include <vector>
19
20#include "absl/strings/str_cat.h"
22
23namespace operations_research {
24
25// NOTE(user): the open source build for proto is less accepting of
26// absl::string_view than internally, so we do more conversions than would
27// appear necessary.
28namespace {
29constexpr absl::string_view kLimitsTime = "limits/time";
30constexpr absl::string_view kParallelMaxNThreads = "parallel/maxnthreads";
31constexpr absl::string_view kDisplayVerbLevel = "display/verblevel";
32constexpr absl::string_view kRandomSeedParam = "randomization/randomseedshift";
33constexpr absl::string_view kCatchCtrlCParam = "misc/catchctrlc";
34} // namespace
35
36void GScipSetTimeLimit(absl::Duration time_limit, GScipParameters* parameters) {
37 if (time_limit < absl::Seconds(1e20) && time_limit >= absl::ZeroDuration()) {
38 (*parameters->mutable_real_params())[std::string(kLimitsTime)] =
39 absl::ToDoubleSeconds(time_limit);
40 } else {
41 parameters->mutable_real_params()->erase(std::string(kLimitsTime));
42 }
43}
44
45absl::Duration GScipTimeLimit(const GScipParameters& parameters) {
46 if (parameters.real_params().contains(std::string(kLimitsTime))) {
47 const double scip_limit =
48 parameters.real_params().at(std::string(kLimitsTime));
49 if (scip_limit >= 1e20) {
50 return absl::InfiniteDuration();
51 } else if (scip_limit <= 0.0) {
52 return absl::ZeroDuration();
53 } else {
54 return absl::Seconds(scip_limit);
55 }
56 }
57 return absl::InfiniteDuration();
58}
59
60bool GScipTimeLimitSet(const GScipParameters& parameters) {
61 return parameters.real_params().contains(std::string(kLimitsTime));
62}
63
64void GScipSetMaxNumThreads(int num_threads, GScipParameters* parameters) {
65 CHECK_GE(num_threads, 1);
66 (*parameters->mutable_int_params())[std::string(kParallelMaxNThreads)] =
67 num_threads;
68}
69
70int GScipMaxNumThreads(const GScipParameters& parameters) {
71 if (parameters.int_params().contains(std::string(kParallelMaxNThreads))) {
72 return parameters.int_params().at(std::string(kParallelMaxNThreads));
73 }
74 return 1;
75}
76
77bool GScipMaxNumThreadsSet(const GScipParameters& parameters) {
78 return parameters.int_params().contains(std::string(kParallelMaxNThreads));
79}
80
81void GScipSetLogLevel(GScipParameters* parameters, int log_level) {
82 CHECK_GE(log_level, 0);
83 CHECK_LE(log_level, 5);
84 (*parameters->mutable_int_params())[std::string(kDisplayVerbLevel)] =
85 log_level;
86}
87
88int GScipLogLevel(const GScipParameters& parameters) {
89 return parameters.int_params().contains(std::string(kDisplayVerbLevel))
90 ? parameters.int_params().at(std::string(kDisplayVerbLevel))
91 : 4;
92}
93bool GScipLogLevelSet(const GScipParameters& parameters) {
94 return parameters.int_params().contains(std::string(kDisplayVerbLevel));
95}
96
97void GScipSetOutputEnabled(GScipParameters* parameters, bool output_enabled) {
98 if (output_enabled) {
99 parameters->mutable_int_params()->erase(std::string(kDisplayVerbLevel));
100 } else {
101 (*parameters->mutable_int_params())[std::string(kDisplayVerbLevel)] = 0;
102 }
103}
104bool GScipOutputEnabled(const GScipParameters& parameters) {
105 return !parameters.int_params().contains(std::string(kDisplayVerbLevel)) ||
106 (parameters.int_params().at(std::string(kDisplayVerbLevel)) > 0);
107}
108
109bool GScipOutputEnabledSet(const GScipParameters& parameters) {
111}
112
113void GScipSetRandomSeed(GScipParameters* parameters, int random_seed) {
114 random_seed = std::max(0, random_seed);
115 (*parameters->mutable_int_params())[std::string(kRandomSeedParam)] =
116 random_seed;
117}
118
119int GScipRandomSeed(const GScipParameters& parameters) {
121 return parameters.int_params().at(std::string(kRandomSeedParam));
122 }
123 return -1; // Unset value.
124}
125
126bool GScipRandomSeedSet(const GScipParameters& parameters) {
127 return parameters.int_params().contains(std::string(kRandomSeedParam));
128}
129
130void GScipSetCatchCtrlC(const bool catch_ctrl_c,
131 GScipParameters* const parameters) {
132 (*parameters->mutable_bool_params())[std::string(kCatchCtrlCParam)] =
133 catch_ctrl_c;
134}
135
136bool GScipCatchCtrlC(const GScipParameters& parameters) {
138 return parameters.bool_params().at(std::string(kCatchCtrlCParam));
139 }
140 return true;
141}
142
143bool GScipCatchCtrlCSet(const GScipParameters& parameters) {
144 return parameters.bool_params().contains(std::string(kCatchCtrlCParam));
145}
146
148 // The only way in SCIP's C API to disable all cuts except user-defined ones
149 // is to disable each individual cut. This may change if SCIP gets updated;
150 // the list below is for SCIP 7.0.1. See
151 // https://scip.zib.de/doc/html/group__SEPARATORS.php for a list of all cuts.
152 const std::vector<std::string> scip_separators = {
153 "cgmip", "clique", "closecuts", "flowcover", "cmir",
154 "aggregation", "convexproj", "disjunctive", "eccuts", "gauge",
155 "gomory", "impliedbounds", "intobj", "mcf", "oddcycle",
156 "rapidlearning", "strongcg", "zerohalf"};
157 for (const std::string& separator : scip_separators) {
158 const std::string separator_parameter =
159 absl::StrCat("separating/", separator, "/freq");
160 (*parameters->mutable_int_params())[separator_parameter] = -1;
161 }
162}
163
164} // namespace operations_research
SatParameters parameters
time_limit
Definition solve.cc:22
In SWIG mode, we don't want anything besides these top-level includes.
void GScipSetOutputEnabled(GScipParameters *parameters, bool output_enabled)
Sets the log level to 4 if enabled, 0 if disabled (see above).
void GScipSetRandomSeed(GScipParameters *parameters, int random_seed)
bool GScipCatchCtrlCSet(const GScipParameters &parameters)
Returns true when the misc/catchctrlc property is set.
void GScipSetCatchCtrlC(const bool catch_ctrl_c, GScipParameters *const parameters)
Sets the misc/catchctrlc property.
bool GScipTimeLimitSet(const GScipParameters &parameters)
bool GScipOutputEnabled(const GScipParameters &parameters)
Checks if the log level is equal to zero.
bool GScipLogLevelSet(const GScipParameters &parameters)
bool GScipRandomSeedSet(const GScipParameters &parameters)
bool GScipMaxNumThreadsSet(const GScipParameters &parameters)
bool GScipOutputEnabledSet(const GScipParameters &parameters)
void DisableAllCutsExceptUserDefined(GScipParameters *parameters)
Turns off all SCIP separators.
int GScipRandomSeed(const GScipParameters &parameters)
Returns -1 if unset.
int GScipMaxNumThreads(const GScipParameters &parameters)
Returns 1 if the number of threads it not specified.
bool GScipCatchCtrlC(const GScipParameters &parameters)
void GScipSetLogLevel(GScipParameters *parameters, int log_level)
absl::Duration GScipTimeLimit(const GScipParameters &parameters)
void GScipSetMaxNumThreads(int num_threads, GScipParameters *parameters)
CHECK fails if num_threads < 1.
void GScipSetTimeLimit(absl::Duration time_limit, GScipParameters *parameters)
int GScipLogLevel(const GScipParameters &parameters)