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