Google OR-Tools v9.12
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
probing.h
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
14#ifndef OR_TOOLS_SAT_PROBING_H_
15#define OR_TOOLS_SAT_PROBING_H_
16
17#include <functional>
18#include <string>
19#include <utility>
20#include <vector>
21
22#include "absl/container/btree_map.h"
23#include "absl/container/btree_set.h"
24#include "absl/strings/str_cat.h"
25#include "absl/strings/string_view.h"
26#include "absl/types/span.h"
27#include "ortools/sat/clause.h"
29#include "ortools/sat/integer.h"
31#include "ortools/sat/model.h"
34#include "ortools/util/bitset.h"
37
38namespace operations_research {
39namespace sat {
40
41class Prober {
42 public:
43 explicit Prober(Model* model);
44
45 // Fixes Booleans variables to true/false and see what is propagated. This
46 // can:
47 //
48 // - Fix some Boolean variables (if we reach a conflict while probing).
49 //
50 // - Infer new direct implications. We add them directly to the
51 // BinaryImplicationGraph and they can later be used to detect equivalent
52 // literals, expand at most ones clique, etc...
53 //
54 // - Tighten the bounds of integer variables. If we probe the two possible
55 // values of a Boolean (b=0 and b=1), we get for each integer variables two
56 // propagated domain D_0 and D_1. The level zero domain can then be
57 // intersected with D_0 U D_1. This can restrict the lower/upper bounds of a
58 // variable, but it can also create holes in the domain! This will detect
59 // common cases like an integer variable in [0, 10] that actually only take
60 // two values [0] or [10] depending on one Boolean.
61 //
62 // Returns false if the problem was proved INFEASIBLE during probing.
63 //
64 // TODO(user): For now we process the Boolean in their natural order, this is
65 // not the most efficient.
66 //
67 // TODO(user): This might generate a lot of new direct implications. We might
68 // not want to add them directly to the BinaryImplicationGraph and could
69 // instead use them directly to detect equivalent literal like in
70 // ProbeAndFindEquivalentLiteral(). The situation is not clear.
71 //
72 // TODO(user): More generally, we might want to register any literal => bound
73 // in the IntegerEncoder. This would allow to remember them and use them in
74 // other part of the solver (cuts, lifting, ...).
75 //
76 // TODO(user): Rename to include Integer in the name and distinguish better
77 // from FailedLiteralProbing() below.
78 bool ProbeBooleanVariables(double deterministic_time_limit);
79
80 // Same as above method except it probes only on the variables given in
81 // 'bool_vars'.
82 bool ProbeBooleanVariables(double deterministic_time_limit,
83 absl::Span<const BooleanVariable> bool_vars);
84
85 bool ProbeOneVariable(BooleanVariable b);
86
87 // Probes the given problem DNF (disjunction of conjunctions). Since one of
88 // the conjunction must be true, we might be able to fix literal or improve
89 // integer bounds if all conjunction propagate the same thing.
90 bool ProbeDnf(absl::string_view name,
91 absl::Span<const std::vector<Literal>> dnf);
92
93 // Statistics.
94 // They are reset each time ProbleBooleanVariables() is called.
95 // Note however that we do not reset them on a call to ProbeOneVariable().
96 int num_decisions() const { return num_decisions_; }
97 int num_new_literals_fixed() const { return num_new_literals_fixed_; }
98 int num_new_binary_clauses() const { return num_new_binary_; }
99
100 // Register a callback that will be called on each "propagation".
101 // One can inspect the VariablesAssignment to see what are the inferred
102 // literals.
103 void SetPropagationCallback(std::function<void(Literal decision)> f) {
104 callback_ = f;
105 }
106
107 private:
108 bool ProbeOneVariableInternal(BooleanVariable b);
109
110 // Model owned classes.
111 const Trail& trail_;
112 const VariablesAssignment& assignment_;
113 IntegerTrail* integer_trail_;
114 ImpliedBounds* implied_bounds_;
115 ProductDetector* product_detector_;
116 SatSolver* sat_solver_;
117 TimeLimit* time_limit_;
118 BinaryImplicationGraph* implication_graph_;
119
120 // To detect literal x that must be true because b => x and not(b) => x.
121 // When probing b, we add all propagated literal to propagated, and when
122 // probing not(b) we check if any are already there.
123 SparseBitset<LiteralIndex> propagated_;
124
125 // Modifications found during probing.
126 std::vector<Literal> to_fix_at_true_;
127 std::vector<IntegerLiteral> new_integer_bounds_;
128 std::vector<std::pair<Literal, Literal>> new_binary_clauses_;
129 absl::btree_set<LiteralIndex> new_propagated_literals_;
130 absl::btree_set<LiteralIndex> always_propagated_literals_;
131 absl::btree_map<IntegerVariable, IntegerValue> new_propagated_bounds_;
132 absl::btree_map<IntegerVariable, IntegerValue> always_propagated_bounds_;
133
134 // Probing statistics.
135 int num_decisions_ = 0;
136 int num_new_holes_ = 0;
137 int num_new_binary_ = 0;
138 int num_new_integer_bounds_ = 0;
139 int num_new_literals_fixed_ = 0;
140
141 std::function<void(Literal decision)> callback_ = nullptr;
142
143 // Logger.
144 SolverLogger* logger_;
145};
146
147// Try to randomly tweak the search and stop at the first conflict each time.
148// This can sometimes find feasible solution, but more importantly, it is a form
149// of probing that can sometimes find small and interesting conflicts or fix
150// variables. This seems to work well on the SAT14/app/rook-* problems and
151// do fix more variables if run before probing.
152//
153// If a feasible SAT solution is found (i.e. all Boolean assigned), then this
154// abort and leave the solver with the full solution assigned.
155//
156// Returns false iff the problem is UNSAT.
157bool LookForTrivialSatSolution(double deterministic_time_limit, Model* model,
158 SolverLogger* logger);
159
160// Options for the FailedLiteralProbing() code below.
161//
162// A good reference for the algorithms involved here is the paper "Revisiting
163// Hyper Binary Resolution" Marijn J. H. Heule, Matti Jarvisalo, Armin Biere,
164// http://www.cs.utexas.edu/~marijn/cpaior2013.pdf
166 // The probing will consume all this deterministic time or stop if nothing
167 // else can be deduced and everything has been probed until fix-point. The
168 // fix point depend on the extract_binay_clauses option:
169 // - If false, we will just stop when no more failed literal can be found.
170 // - If true, we will do more work and stop when all failed literal have been
171 // found and all hyper binary resolution have been performed.
172 //
173 // TODO(user): We can also provide a middle ground and probe all failed
174 // literal but do not extract all binary clauses.
175 //
176 // Note that the fix-point is unique, modulo the equivalent literal detection
177 // we do. And if we add binary clauses, modulo the transitive reduction of the
178 // binary implication graph.
179 //
180 // To be fast, we only use the binary clauses in the binary implication graph
181 // for the equivalence detection. So the power of the equivalence detection
182 // changes if the extract_binay_clauses option is true or not.
183 //
184 // TODO(user): The fix point is not yet reached since we don't currently
185 // simplify non-binary clauses with these equivalence, but we will.
187
188 // This is also called hyper binary resolution. Basically, we make sure that
189 // the binary implication graph is augmented with all the implication of the
190 // form a => b that can be derived by fixing 'a' at level zero and doing a
191 // propagation using all constraints. Note that we only add clauses that
192 // cannot be derived by the current implication graph.
193 //
194 // With these extra clause the power of the equivalence literal detection
195 // using only the binary implication graph with increase. Note that it is
196 // possible to do exactly the same thing without adding these binary clause
197 // first. This is what is done by yet another probing algorithm (currently in
198 // simplification.cc).
199 //
200 // TODO(user): Note that adding binary clause before/during the SAT presolve
201 // is currently not always a good idea. This is because we don't simplify the
202 // other clause as much as we could. Also, there can be up to a quadratic
203 // number of clauses added this way, which might slow down things a lot. But
204 // then because of the deterministic limit, we usually cannot add too much
205 // clauses, even for huge problems, since we will reach the limit before that.
207
208 // Use a version of the "Tree look" algorithm as explained in the paper above.
209 // This is usually faster and more efficient. Note that when extracting binary
210 // clauses it might currently produce more "redundant" one in the sense that a
211 // transitive reduction of the binary implication graph after all hyper binary
212 // resolution have been performed may need to do more work.
213 bool use_tree_look = true;
214
215 // There is two slightly different implementation of the tree-look algo.
216 //
217 // TODO(user): Decide which one is better, currently the difference seems
218 // small but the queue seems slightly faster.
219 bool use_queue = true;
220
221 // If we detect as we probe that a new binary clause subsumes one of the
222 // non-binary clause, we will replace the long clause by the binary one. This
223 // is orthogonal to the extract_binary_clauses parameters which will add all
224 // binary clauses but not necessarily check for subsumption.
226
227 // We assume this is also true if --v 1 is activated.
228 bool log_info = false;
229
230 std::string ToString() const {
231 return absl::StrCat("deterministic_limit: ", deterministic_limit,
232 " extract_binary_clauses: ", extract_binary_clauses,
233 " use_tree_look: ", use_tree_look,
234 " use_queue: ", use_queue);
235 }
236};
237
238// Similar to ProbeBooleanVariables() but different :-)
239//
240// First, this do not consider integer variable. It doesn't do any disjunctive
241// reasoning (i.e. changing the domain of an integer variable by intersecting
242// it with the union of what happen when x is fixed and not(x) is fixed).
243//
244// However this should be more efficient and just work better for pure Boolean
245// problems. On integer problems, we might also want to run this one first,
246// and then do just one quick pass of ProbeBooleanVariables().
247//
248// Note that this by itself just do one "round", look at the code in the
249// Inprocessing class that call this interleaved with other reductions until a
250// fix point is reached.
251//
252// This can fix a lot of literals via failed literal detection, that is when
253// we detect that x => not(x) via propagation after taking x as a decision. It
254// also use the strongly connected component algorithm to detect equivalent
255// literals.
256//
257// It will add any detected binary clause (via hyper binary resolution) to
258// the implication graph. See the option comments for more details.
259bool FailedLiteralProbingRound(ProbingOptions options, Model* model);
260
261} // namespace sat
262} // namespace operations_research
263
264#endif // OR_TOOLS_SAT_PROBING_H_
Definition model.h:341
bool ProbeBooleanVariables(double deterministic_time_limit)
Definition probing.cc:60
bool ProbeDnf(absl::string_view name, absl::Span< const std::vector< Literal > > dnf)
Definition probing.cc:304
bool ProbeOneVariable(BooleanVariable b)
Definition probing.cc:205
void SetPropagationCallback(std::function< void(Literal decision)> f)
Definition probing.h:103
bool LookForTrivialSatSolution(double deterministic_time_limit, Model *model, SolverLogger *logger)
Definition probing.cc:420
bool FailedLiteralProbingRound(ProbingOptions options, Model *model)
Definition probing.cc:500
In SWIG mode, we don't want anything besides these top-level includes.
bool log_info
We assume this is also true if –v 1 is activated.
Definition probing.h:228