Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
parameters_validation.cc
Go to the documentation of this file.
1// Copyright 2010-2025 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 <stdint.h>
17
18#include <cmath>
19#include <limits>
20#include <string>
21
22#include "absl/strings/str_cat.h"
25
26namespace operations_research {
27namespace sat {
28
29#define TEST_IN_RANGE(name, min, max) \
30 if (params.name() < min || params.name() > max) { \
31 return absl::StrCat("parameter '", #name, "' should be in [", min, ",", \
32 max, "]. Current value is ", params.name()); \
33 }
34
35#define TEST_NON_NEGATIVE(name) \
36 if (params.name() < 0) { \
37 return absl::StrCat("Parameters ", #name, " must be non-negative"); \
38 }
39
40#define TEST_POSITIVE(name) \
41 if (params.name() <= 0) { \
42 return absl::StrCat("Parameters ", #name, " must be positive"); \
43 }
44
45#define TEST_NOT_NAN(name) \
46 if (std::isnan(params.name())) { \
47 return absl::StrCat("parameter '", #name, "' is NaN"); \
48 }
49
50#define TEST_IS_FINITE(name) \
51 if (!std::isfinite(params.name())) { \
52 return absl::StrCat("parameter '", #name, "' is NaN or not finite"); \
53 }
54
55std::string ValidateParameters(const SatParameters& params) {
56 // Test that all floating point parameters are not NaN or +/- infinity.
57 TEST_IS_FINITE(absolute_gap_limit);
58 TEST_IS_FINITE(blocking_restart_multiplier);
59 TEST_IS_FINITE(clause_activity_decay);
60 TEST_IS_FINITE(clause_cleanup_ratio);
61 TEST_IS_FINITE(cut_active_count_decay);
62 TEST_IS_FINITE(cut_max_active_count_value);
63 TEST_IS_FINITE(feasibility_jump_batch_dtime);
64 TEST_IS_FINITE(glucose_decay_increment);
65 TEST_IS_FINITE(glucose_max_decay);
66 TEST_IS_FINITE(initial_variables_activity);
67 TEST_IS_FINITE(inprocessing_dtime_ratio);
68 TEST_IS_FINITE(inprocessing_minimization_dtime);
69 TEST_IS_FINITE(inprocessing_probing_dtime);
70 TEST_IS_FINITE(max_clause_activity_value);
71 TEST_IS_FINITE(max_variable_activity_value);
72 TEST_IS_FINITE(merge_at_most_one_work_limit);
73 TEST_IS_FINITE(merge_no_overlap_work_limit);
74 TEST_IS_FINITE(min_orthogonality_for_lp_constraints);
75 TEST_IS_FINITE(mip_check_precision);
76 TEST_IS_FINITE(mip_drop_tolerance);
77 TEST_IS_FINITE(mip_max_bound);
78 TEST_IS_FINITE(mip_max_valid_magnitude);
79 TEST_IS_FINITE(mip_var_scaling);
80 TEST_IS_FINITE(mip_wanted_precision);
81 TEST_IS_FINITE(pb_cleanup_ratio);
82 TEST_IS_FINITE(presolve_probing_deterministic_time_limit);
83 TEST_IS_FINITE(probing_deterministic_time_limit);
84 TEST_IS_FINITE(propagation_loop_detection_factor);
85 TEST_IS_FINITE(random_branches_ratio);
86 TEST_IS_FINITE(random_polarity_ratio);
87 TEST_IS_FINITE(relative_gap_limit);
88 TEST_IS_FINITE(restart_dl_average_ratio);
89 TEST_IS_FINITE(restart_lbd_average_ratio);
90 TEST_IS_FINITE(share_glue_clauses_dtime);
91 TEST_IS_FINITE(shared_tree_open_leaves_per_worker);
92 TEST_IS_FINITE(shaving_deterministic_time_in_probing_search);
93 TEST_IS_FINITE(shaving_search_deterministic_time);
94 TEST_IS_FINITE(strategy_change_increase_ratio);
95 TEST_IS_FINITE(symmetry_detection_deterministic_time_limit);
96 TEST_IS_FINITE(variable_activity_decay);
97 TEST_IN_RANGE(routing_cut_subset_size_for_tight_binary_relation_bound, 0, 32);
98 TEST_IS_FINITE(routing_cut_dp_effort);
99
100 TEST_IS_FINITE(lns_initial_difficulty);
101 TEST_IS_FINITE(lns_initial_deterministic_limit);
102 TEST_IN_RANGE(lns_initial_difficulty, 0.0, 1.0);
103
104 TEST_POSITIVE(at_most_one_max_expansion_size);
105 TEST_POSITIVE(max_alldiff_domain_size);
106
107 TEST_NOT_NAN(max_time_in_seconds);
108 TEST_NOT_NAN(max_deterministic_time);
109
110 // Parallelism.
111 const int kMaxReasonableParallelism = 10'000;
112 TEST_IN_RANGE(num_workers, 0, kMaxReasonableParallelism);
113 TEST_IN_RANGE(num_search_workers, 0, kMaxReasonableParallelism);
114 TEST_IN_RANGE(shared_tree_num_workers, -1, kMaxReasonableParallelism);
115 TEST_IN_RANGE(interleave_batch_size, 0, kMaxReasonableParallelism);
116 TEST_IN_RANGE(shared_tree_open_leaves_per_worker, 1,
117 kMaxReasonableParallelism);
118 TEST_IN_RANGE(shared_tree_balance_tolerance, 0,
119 log2(kMaxReasonableParallelism));
120
121 // TODO(user): Consider using annotations directly in the proto for these
122 // validation. It is however not open sourced.
123 TEST_IN_RANGE(mip_max_activity_exponent, 1, 62);
124 TEST_IN_RANGE(mip_max_bound, 0, 1e17);
125 TEST_IN_RANGE(solution_pool_size, 1, std::numeric_limits<int32_t>::max());
126
127 // Feasibility jump.
128 TEST_NOT_NAN(feasibility_jump_decay);
129 TEST_NOT_NAN(feasibility_jump_var_randomization_probability);
130 TEST_NOT_NAN(feasibility_jump_var_perburbation_range_ratio);
131 TEST_IN_RANGE(feasibility_jump_decay, 0.0, 1.0);
132 TEST_IN_RANGE(feasibility_jump_var_randomization_probability, 0.0, 1.0);
133 TEST_IN_RANGE(feasibility_jump_var_perburbation_range_ratio, 0.0, 1.0);
134
135 // Violation ls.
136 TEST_NOT_NAN(violation_ls_compound_move_probability);
137 TEST_IN_RANGE(num_violation_ls, 0, kMaxReasonableParallelism);
138 TEST_IN_RANGE(violation_ls_perturbation_period, 1, 1'000'000'000);
139 TEST_IN_RANGE(violation_ls_compound_move_probability, 0.0, 1.0);
140
141 TEST_POSITIVE(glucose_decay_increment_period);
142 TEST_POSITIVE(shared_tree_max_nodes_per_worker);
143 TEST_POSITIVE(shared_tree_open_leaves_per_worker);
144 TEST_POSITIVE(mip_var_scaling);
145
146 // Test LP tolerances.
147 TEST_IS_FINITE(lp_primal_tolerance);
148 TEST_IS_FINITE(lp_dual_tolerance);
149 TEST_NON_NEGATIVE(lp_primal_tolerance);
150 TEST_NON_NEGATIVE(lp_dual_tolerance);
151
152 TEST_NON_NEGATIVE(linearization_level);
153 TEST_NON_NEGATIVE(max_deterministic_time);
154 TEST_NON_NEGATIVE(max_time_in_seconds);
155 TEST_NON_NEGATIVE(mip_wanted_precision);
156 TEST_NON_NEGATIVE(new_constraints_batch_size);
157 TEST_NON_NEGATIVE(presolve_probing_deterministic_time_limit);
158 TEST_NON_NEGATIVE(probing_deterministic_time_limit);
159 TEST_NON_NEGATIVE(symmetry_detection_deterministic_time_limit);
160 TEST_POSITIVE(share_glue_clauses_dtime);
161
162 if (params.enumerate_all_solutions() &&
163 (params.num_search_workers() > 1 || params.num_workers() > 1)) {
164 return "Enumerating all solutions does not work in parallel";
165 }
166
167 if (params.enumerate_all_solutions() &&
168 (!params.subsolvers().empty() || !params.extra_subsolvers().empty() ||
169 !params.ignore_subsolvers().empty())) {
170 return "Enumerating all solutions does not work with custom subsolvers";
171 }
172
173 if (params.num_search_workers() >= 1 && params.num_workers() >= 1) {
174 return "Do not specify both num_search_workers and num_workers";
175 }
176
177 if (params.use_shared_tree_search()) {
178 return "use_shared_tree_search must only be set on workers' parameters";
179 }
180
181 if (params.enumerate_all_solutions() && params.interleave_search()) {
182 return "Enumerating all solutions does not work with interleaved search";
183 }
184
185 for (const SatParameters& new_subsolver : params.subsolver_params()) {
186 if (new_subsolver.name().empty()) {
187 return "New subsolver parameter defined without a name";
188 }
189 }
190
191 if (!params.subsolvers().empty() || !params.extra_subsolvers().empty()) {
192 const auto strategies = GetNamedParameters(params);
193 for (const std::string& subsolver : params.subsolvers()) {
194 if (subsolver == "core_or_no_lp") continue; // Used by fz free search.
195 if (!strategies.contains(subsolver)) {
196 return absl::StrCat("subsolver \'", subsolver, "\' is not valid");
197 }
198 }
199
200 for (const std::string& subsolver : params.extra_subsolvers()) {
201 if (!strategies.contains(subsolver)) {
202 return absl::StrCat("subsolver \'", subsolver, "\' is not valid");
203 }
204 }
205 }
206
207 return "";
208}
209
210#undef TEST_IN_RANGE
211#undef TEST_POSITIVE
212#undef TEST_NON_NEGATIVE
213#undef TEST_NOT_NAN
214#undef TEST_IS_FINITE
215
216} // namespace sat
217} // namespace operations_research
const ::std::string & ignore_subsolvers(int index) const
const ::operations_research::sat::SatParameters & subsolver_params(int index) const
const ::std::string & extra_subsolvers(int index) const
const ::std::string & subsolvers(int index) const
#define TEST_NON_NEGATIVE(name)
#define TEST_NOT_NAN(name)
std::string ValidateParameters(const SatParameters &params)
absl::flat_hash_map< std::string, SatParameters > GetNamedParameters(SatParameters base_params)
In SWIG mode, we don't want anything besides these top-level includes.
#define TEST_IN_RANGE(name, min, max)
#define TEST_POSITIVE(name)
#define TEST_IS_FINITE(name)