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