Google OR-Tools v9.15
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 TEST_NOT_NAN(max_drat_time_in_seconds);
110
111 // Parallelism.
112 const int kMaxReasonableParallelism = 10'000;
113 TEST_IN_RANGE(num_workers, 0, kMaxReasonableParallelism);
114 TEST_IN_RANGE(num_search_workers, 0, kMaxReasonableParallelism);
115 TEST_IN_RANGE(shared_tree_num_workers, -1, kMaxReasonableParallelism);
116 TEST_IN_RANGE(interleave_batch_size, 0, kMaxReasonableParallelism);
117 TEST_IN_RANGE(shared_tree_open_leaves_per_worker, 1,
118 kMaxReasonableParallelism);
119 TEST_IN_RANGE(shared_tree_balance_tolerance, 0,
120 log2(kMaxReasonableParallelism));
121
122 // TODO(user): Consider using annotations directly in the proto for these
123 // validation. It is however not open sourced.
124 TEST_IN_RANGE(mip_max_activity_exponent, 1, 62);
125 TEST_IN_RANGE(mip_max_bound, 0, 1e17);
126 TEST_IN_RANGE(solution_pool_size, 1, std::numeric_limits<int32_t>::max());
127
128 // Feasibility jump.
129 TEST_NOT_NAN(feasibility_jump_decay);
130 TEST_NOT_NAN(feasibility_jump_var_randomization_probability);
131 TEST_NOT_NAN(feasibility_jump_var_perburbation_range_ratio);
132 TEST_IN_RANGE(feasibility_jump_decay, 0.0, 1.0);
133 TEST_IN_RANGE(feasibility_jump_var_randomization_probability, 0.0, 1.0);
134 TEST_IN_RANGE(feasibility_jump_var_perburbation_range_ratio, 0.0, 1.0);
135
136 // Violation ls.
137 TEST_NOT_NAN(violation_ls_compound_move_probability);
138 TEST_IN_RANGE(num_violation_ls, 0, kMaxReasonableParallelism);
139 TEST_IN_RANGE(violation_ls_perturbation_period, 1, 1'000'000'000);
140 TEST_IN_RANGE(violation_ls_compound_move_probability, 0.0, 1.0);
141
142 TEST_POSITIVE(glucose_decay_increment_period);
143 TEST_POSITIVE(shared_tree_max_nodes_per_worker);
144 TEST_POSITIVE(shared_tree_open_leaves_per_worker);
145 TEST_NON_NEGATIVE(shared_tree_split_min_dtime);
146 TEST_IS_FINITE(shared_tree_split_min_dtime);
147 TEST_POSITIVE(mip_var_scaling);
148
149 // Test LP tolerances.
150 TEST_IS_FINITE(lp_primal_tolerance);
151 TEST_IS_FINITE(lp_dual_tolerance);
152 TEST_NON_NEGATIVE(lp_primal_tolerance);
153 TEST_NON_NEGATIVE(lp_dual_tolerance);
154
155 TEST_NON_NEGATIVE(linearization_level);
156 TEST_NON_NEGATIVE(max_deterministic_time);
157 TEST_NON_NEGATIVE(max_time_in_seconds);
158 TEST_NON_NEGATIVE(mip_wanted_precision);
159 TEST_NON_NEGATIVE(new_constraints_batch_size);
160 TEST_NON_NEGATIVE(presolve_probing_deterministic_time_limit);
161 TEST_NON_NEGATIVE(probing_deterministic_time_limit);
162 TEST_NON_NEGATIVE(symmetry_detection_deterministic_time_limit);
163 TEST_POSITIVE(share_glue_clauses_dtime);
164
165 if (params.enumerate_all_solutions() &&
166 (!params.subsolvers().empty() || !params.extra_subsolvers().empty() ||
167 !params.ignore_subsolvers().empty())) {
168 return "Enumerating all solutions does not work with custom subsolvers";
169 }
170
171 if (params.num_search_workers() >= 1 && params.num_workers() >= 1) {
172 return "Do not specify both num_search_workers and num_workers";
173 }
174
175 if (params.use_shared_tree_search()) {
176 return "use_shared_tree_search must only be set on workers' parameters";
177 }
178
179 if (params.enumerate_all_solutions() && params.interleave_search()) {
180 return "Enumerating all solutions does not work with interleaved search";
181 }
182
183 for (const SatParameters& new_subsolver : params.subsolver_params()) {
184 if (new_subsolver.name().empty()) {
185 return "New subsolver parameter defined without a name";
186 }
187 }
188
189 if (!params.subsolvers().empty() || !params.extra_subsolvers().empty()) {
190 const auto strategies = GetNamedParameters(params);
191 for (const std::string& subsolver : params.subsolvers()) {
192 if (subsolver == "core_or_no_lp") continue; // Used by fz free search.
193 if (!strategies.contains(subsolver)) {
194 return absl::StrCat("subsolver \'", subsolver, "\' is not valid");
195 }
196 }
197
198 for (const std::string& subsolver : params.extra_subsolvers()) {
199 if (!strategies.contains(subsolver)) {
200 return absl::StrCat("subsolver \'", subsolver, "\' is not valid");
201 }
202 }
203 }
204
205 return "";
206}
207
208#undef TEST_IN_RANGE
209#undef TEST_POSITIVE
210#undef TEST_NON_NEGATIVE
211#undef TEST_NOT_NAN
212#undef TEST_IS_FINITE
213
214} // namespace sat
215} // 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)
OR-Tools root namespace.
#define TEST_IN_RANGE(name, min, max)
#define TEST_POSITIVE(name)
#define TEST_IS_FINITE(name)