OR-Tools  9.1
simplification.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// Implementation of a pure SAT presolver. This roughly follows the paper:
15//
16// "Effective Preprocessing in SAT through Variable and Clause Elimination",
17// Niklas Een and Armin Biere, published in the SAT 2005 proceedings.
18
19#ifndef OR_TOOLS_SAT_SIMPLIFICATION_H_
20#define OR_TOOLS_SAT_SIMPLIFICATION_H_
21
22#include <cstdint>
23#include <deque>
24#include <memory>
25#include <set>
26#include <vector>
27
28#include "absl/types/span.h"
32#include "ortools/base/macros.h"
40
41namespace operations_research {
42namespace sat {
43
44// A simple sat postsolver.
45//
46// The idea is that any presolve algorithm can just update this class, and at
47// the end, this class will recover a solution of the initial problem from a
48// solution of the presolved problem.
50 public:
51 explicit SatPostsolver(int num_variables);
52
53 // The postsolver will process the Add() calls in reverse order. If the given
54 // clause has all its literals at false, it simply sets the literal x to true.
55 // Note that x must be a literal of the given clause.
56 void Add(Literal x, const absl::Span<const Literal> clause);
57
58 // Tells the postsolver that the given literal must be true in any solution.
59 // We currently check that the variable is not already fixed.
60 //
61 // TODO(user): this as almost the same effect as adding an unit clause, and we
62 // should probably remove this to simplify the code.
63 void FixVariable(Literal x);
64
65 // This assumes that the given variable mapping has been applied to the
66 // problem. All the subsequent Add() and FixVariable() will refer to the new
67 // problem. During postsolve, the initial solution must also correspond to
68 // this new problem. Note that if mapping[v] == -1, then the literal v is
69 // assumed to be deleted.
70 //
71 // This can be called more than once. But each call must refer to the current
72 // variables set (after all the previous mapping have been applied).
73 void ApplyMapping(
75
76 // Extracts the current assignment of the given solver and postsolve it.
77 //
78 // Node(fdid): This can currently be called only once (but this is easy to
79 // change since only some CHECK will fail).
80 std::vector<bool> ExtractAndPostsolveSolution(const SatSolver& solver);
81 std::vector<bool> PostsolveSolution(const std::vector<bool>& solution);
82
83 // Getters to the clauses managed by this class.
84 // Important: This will always put the associated literal first.
85 int NumClauses() const { return clauses_start_.size(); }
86 std::vector<Literal> Clause(int i) const {
87 // TODO(user): we could avoid the copy here, but because clauses_literals_
88 // is a deque, we do need a special return class and cannot juste use
89 // absl::Span<Literal> for instance.
90 const int begin = clauses_start_[i];
91 const int end = i + 1 < clauses_start_.size() ? clauses_start_[i + 1]
92 : clauses_literals_.size();
93 std::vector<Literal> result(clauses_literals_.begin() + begin,
94 clauses_literals_.begin() + end);
95 for (int j = 0; j < result.size(); ++j) {
96 if (result[j] == associated_literal_[i]) {
97 std::swap(result[0], result[j]);
98 break;
99 }
100 }
101 return result;
102 }
103
104 private:
105 Literal ApplyReverseMapping(Literal l);
106 void Postsolve(VariablesAssignment* assignment) const;
107
108 // The presolve can add new variables, so we need to store the number of
109 // original variables in order to return a solution with the correct number
110 // of variables.
111 const int initial_num_variables_;
112 int num_variables_;
113
114 // Stores the arguments of the Add() calls: clauses_start_[i] is the index of
115 // the first literal of the clause #i in the clauses_literals_ deque.
116 std::vector<int> clauses_start_;
117 std::deque<Literal> clauses_literals_;
118 std::vector<Literal> associated_literal_;
119
120 // All the added clauses will be mapped back to the initial variables using
121 // this reverse mapping. This way, clauses_ and associated_literal_ are only
122 // in term of the initial problem.
124
125 // This will stores the fixed variables value and later the postsolved
126 // assignment.
127 VariablesAssignment assignment_;
128
129 DISALLOW_COPY_AND_ASSIGN(SatPostsolver);
130};
131
132// This class holds a SAT problem (i.e. a set of clauses) and the logic to
133// presolve it by a series of subsumption, self-subsuming resolution, and
134// variable elimination by clause distribution.
135//
136// Note that this does propagate unit-clauses, but probably much
137// less efficiently than the propagation code in the SAT solver. So it is better
138// to use a SAT solver to fix variables before using this class.
139//
140// TODO(user): Interact more with a SAT solver to reuse its propagation logic.
141//
142// TODO(user): Forbid the removal of some variables. This way we can presolve
143// only the clause part of a general Boolean problem by not removing variables
144// appearing in pseudo-Boolean constraints.
146 public:
147 // TODO(user): use IntType!
148 typedef int32_t ClauseIndex;
149
150 explicit SatPresolver(SatPostsolver* postsolver, SolverLogger* logger)
151 : postsolver_(postsolver),
152 num_trivial_clauses_(0),
153 drat_proof_handler_(nullptr),
154 logger_(logger) {}
155
156 void SetParameters(const SatParameters& params) { parameters_ = params; }
157 void SetTimeLimit(TimeLimit* time_limit) { time_limit_ = time_limit; }
158
159 // Registers a mapping to encode equivalent literals.
160 // See ProbeAndFindEquivalentLiteral().
163 equiv_mapping_ = mapping;
164 }
165
166 // Adds new clause to the SatPresolver.
167 void SetNumVariables(int num_variables);
169 void AddClause(absl::Span<const Literal> clause);
170
171 // Presolves the problem currently loaded. Returns false if the model is
172 // proven to be UNSAT during the presolving.
173 //
174 // TODO(user): Add support for a time limit and some kind of iterations limit
175 // so that this can never take too much time.
176 bool Presolve();
177
178 // Same as Presolve() but only allow to remove BooleanVariable whose index
179 // is set to true in the given vector.
180 bool Presolve(const std::vector<bool>& var_that_can_be_removed);
181
182 // All the clauses managed by this class.
183 // Note that deleted clauses keep their indices (they are just empty).
184 int NumClauses() const { return clauses_.size(); }
185 const std::vector<Literal>& Clause(ClauseIndex ci) const {
186 return clauses_[ci];
187 }
188
189 // The number of variables. This is computed automatically from the clauses
190 // added to the SatPresolver.
191 int NumVariables() const { return literal_to_clause_sizes_.size() / 2; }
192
193 // After presolving, Some variables in [0, NumVariables()) have no longer any
194 // clause pointing to them. This return a mapping that maps this interval to
195 // [0, new_size) such that now all variables are used. The unused variable
196 // will be mapped to BooleanVariable(-1).
198
199 // Loads the current presolved problem in to the given sat solver.
200 // Note that the variables will be re-indexed according to the mapping given
201 // by GetMapping() so that they form a dense set.
202 //
203 // IMPORTANT: This is not const because it deletes the presolver clauses as
204 // they are added to the SatSolver in order to save memory. After this is
205 // called, only VariableMapping() will still works.
207
208 // Visible for Testing. Takes a given clause index and looks for clause that
209 // can be subsumed or strengthened using this clause. Returns false if the
210 // model is proven to be unsat.
212
213 // Visible for testing. Tries to eliminate x by clause distribution.
214 // This is also known as bounded variable elimination.
215 //
216 // It is always possible to remove x by resolving each clause containing x
217 // with all the clauses containing not(x). Hence the cross-product name. Note
218 // that this function only do that if the number of clauses is reduced.
219 bool CrossProduct(Literal x);
220
221 // Visible for testing. Just applies the BVA step of the presolve.
222 void PresolveWithBva();
223
224 void SetDratProofHandler(DratProofHandler* drat_proof_handler) {
225 drat_proof_handler_ = drat_proof_handler;
226 }
227
228 private:
229 // Internal function used by ProcessClauseToSimplifyOthers().
230 bool ProcessClauseToSimplifyOthersUsingLiteral(ClauseIndex clause_index,
231 Literal lit);
232
233 // Internal function to add clauses generated during the presolve. The clause
234 // must already be sorted with the default Literal order and will be cleared
235 // after this call.
236 void AddClauseInternal(std::vector<Literal>* clause);
237
238 // Clause removal function.
239 void Remove(ClauseIndex ci);
240 void RemoveAndRegisterForPostsolve(ClauseIndex ci, Literal x);
241 void RemoveAndRegisterForPostsolveAllClauseContaining(Literal x);
242
243 // Call ProcessClauseToSimplifyOthers() on all the clauses in
244 // clause_to_process_ and empty the list afterwards. Note that while some
245 // clauses are processed, new ones may be added to the list. Returns false if
246 // the problem is shown to be UNSAT.
247 bool ProcessAllClauses();
248
249 // Finds the literal from the clause that occur the less in the clause
250 // database.
251 Literal FindLiteralWithShortestOccurrenceList(
252 const std::vector<Literal>& clause);
253 LiteralIndex FindLiteralWithShortestOccurrenceListExcluding(
254 const std::vector<Literal>& clause, Literal to_exclude);
255
256 // Tests and maybe perform a Simple Bounded Variable addition starting from
257 // the given literal as described in the paper: "Automated Reencoding of
258 // Boolean Formulas", Norbert Manthey, Marijn J. H. Heule, and Armin Biere,
259 // Volume 7857 of the series Lecture Notes in Computer Science pp 102-117,
260 // 2013.
261 // https://www.research.ibm.com/haifa/conferences/hvc2012/papers/paper16.pdf
262 //
263 // This seems to have a mostly positive effect, except on the crafted problem
264 // familly mugrauer_balint--GI.crafted_nxx_d6_cx_numxx where the reduction
265 // is big, but apparently the problem is harder to prove UNSAT for the solver.
266 void SimpleBva(LiteralIndex l);
267
268 // Display some statistics on the current clause database.
269 void DisplayStats(double elapsed_seconds);
270
271 // Returns a hash of the given clause variables (not literal) in such a way
272 // that hash1 & not(hash2) == 0 iff the set of variable of clause 1 is a
273 // subset of the one of clause2.
274 uint64_t ComputeSignatureOfClauseVariables(ClauseIndex ci);
275
276 // The "active" variables on which we want to call CrossProduct() are kept
277 // in a priority queue so that we process first the ones that occur the least
278 // often in the clause database.
279 void InitializePriorityQueue();
280 void UpdatePriorityQueue(BooleanVariable var);
281 struct PQElement {
282 PQElement() : heap_index(-1), variable(-1), weight(0.0) {}
283
284 // Interface for the AdjustablePriorityQueue.
285 void SetHeapIndex(int h) { heap_index = h; }
286 int GetHeapIndex() const { return heap_index; }
287
288 // Priority order. The AdjustablePriorityQueue returns the largest element
289 // first, but our weight goes this other way around (smaller is better).
290 bool operator<(const PQElement& other) const {
291 return weight > other.weight;
292 }
293
294 int heap_index;
295 BooleanVariable variable;
296 double weight;
297 };
300
301 // Literal priority queue for BVA. The literals are ordered by descending
302 // number of occurrences in clauses.
303 void InitializeBvaPriorityQueue();
304 void UpdateBvaPriorityQueue(LiteralIndex lit);
305 void AddToBvaPriorityQueue(LiteralIndex lit);
306 struct BvaPqElement {
307 BvaPqElement() : heap_index(-1), literal(-1), weight(0.0) {}
308
309 // Interface for the AdjustablePriorityQueue.
310 void SetHeapIndex(int h) { heap_index = h; }
311 int GetHeapIndex() const { return heap_index; }
312
313 // Priority order.
314 // The AdjustablePriorityQueue returns the largest element first.
315 bool operator<(const BvaPqElement& other) const {
316 return weight < other.weight;
317 }
318
319 int heap_index;
320 LiteralIndex literal;
321 double weight;
322 };
323 std::deque<BvaPqElement> bva_pq_elements_; // deque because we add variables.
325
326 // Temporary data for SimpleBva().
327 std::set<LiteralIndex> m_lit_;
328 std::vector<ClauseIndex> m_cls_;
329 absl::StrongVector<LiteralIndex, int> literal_to_p_size_;
330 std::vector<std::pair<LiteralIndex, ClauseIndex>> flattened_p_;
331 std::vector<Literal> tmp_new_clause_;
332
333 // List of clauses on which we need to call ProcessClauseToSimplifyOthers().
334 // See ProcessAllClauses().
335 std::vector<bool> in_clause_to_process_;
336 std::deque<ClauseIndex> clause_to_process_;
337
338 // The set of all clauses.
339 // An empty clause means that it has been removed.
340 std::vector<std::vector<Literal>> clauses_; // Indexed by ClauseIndex
341
342 // The cached value of ComputeSignatureOfClauseVariables() for each clause.
343 std::vector<uint64_t> signatures_; // Indexed by ClauseIndex
344 int64_t num_inspected_signatures_ = 0;
345 int64_t num_inspected_literals_ = 0;
346
347 // Occurrence list. For each literal, contains the ClauseIndex of the clause
348 // that contains it (ordered by clause index).
350 literal_to_clauses_;
351
352 // Because we only lazily clean the occurrence list after clause deletions,
353 // we keep the size of the occurrence list (without the deleted clause) here.
354 absl::StrongVector<LiteralIndex, int> literal_to_clause_sizes_;
355
356 // Used for postsolve.
357 SatPostsolver* postsolver_;
358
359 // Equivalent literal mapping.
361
362 int num_trivial_clauses_;
363 SatParameters parameters_;
364 DratProofHandler* drat_proof_handler_;
365 TimeLimit* time_limit_ = nullptr;
366 SolverLogger* logger_;
367
368 DISALLOW_COPY_AND_ASSIGN(SatPresolver);
369};
370
371// Visible for testing. Returns true iff:
372// - a subsume b (subsumption): the clause a is a subset of b, in which case
373// opposite_literal is set to -1.
374// - b is strengthened by self-subsumption using a (self-subsuming resolution):
375// the clause a with one of its literal negated is a subset of b, in which
376// case opposite_literal is set to this negated literal index. Moreover, this
377// opposite_literal is then removed from b.
378//
379// If num_inspected_literals_ is not nullptr, the "complexity" of this function
380// will be added to it in order to track the amount of work done.
381//
382// TODO(user): when a.size() << b.size(), we should use binary search instead
383// of scanning b linearly.
384bool SimplifyClause(const std::vector<Literal>& a, std::vector<Literal>* b,
385 LiteralIndex* opposite_literal,
386 int64_t* num_inspected_literals = nullptr);
387
388// Visible for testing. Returns kNoLiteralIndex except if:
389// - a and b differ in only one literal.
390// - For a it is the given literal l.
391// In which case, returns the LiteralIndex of the literal in b that is not in a.
392LiteralIndex DifferAtGivenLiteral(const std::vector<Literal>& a,
393 const std::vector<Literal>& b, Literal l);
394
395// Visible for testing. Computes the resolvant of 'a' and 'b' obtained by
396// performing the resolution on 'x'. If the resolvant is trivially true this
397// returns false, otherwise it returns true and fill 'out' with the resolvant.
398//
399// Note that the resolvant is just 'a' union 'b' with the literals 'x' and
400// not(x) removed. The two clauses are assumed to be sorted, and the computed
401// resolvant will also be sorted.
402//
403// This is the basic operation when a variable is eliminated by clause
404// distribution.
405bool ComputeResolvant(Literal x, const std::vector<Literal>& a,
406 const std::vector<Literal>& b, std::vector<Literal>* out);
407
408// Same as ComputeResolvant() but just returns the resolvant size.
409// Returns -1 when ComputeResolvant() returns false.
410int ComputeResolvantSize(Literal x, const std::vector<Literal>& a,
411 const std::vector<Literal>& b);
412
413// Presolver that does literals probing and finds equivalent literals by
414// computing the strongly connected components of the graph:
415// literal l -> literals propagated by l.
416//
417// Clears the mapping if there are no equivalent literals. Otherwise, mapping[l]
418// is the representative of the equivalent class of l. Note that mapping[l] may
419// be equal to l.
420//
421// The postsolver will be updated so it can recover a solution of the mapped
422// problem. Note that this works on any problem the SatSolver can handle, not
423// only pure SAT problem, but the returned mapping do need to be applied to all
424// constraints.
426 SatSolver* solver, SatPostsolver* postsolver,
427 DratProofHandler* drat_proof_handler,
429
430// Given a 'solver' with a problem already loaded, this will try to simplify the
431// problem (i.e. presolve it) before calling solver->Solve(). In the process,
432// because of the way the presolve is implemented, the underlying SatSolver may
433// change (it is why we use this unique_ptr interface). In particular, the final
434// variables and 'solver' state may have nothing to do with the problem
435// originaly present in the solver. That said, if the problem is shown to be
436// SAT, then the returned solution will be in term of the original variables.
437//
438// Note that the full presolve is only executed if the problem is a pure SAT
439// problem with only clauses.
441 std::unique_ptr<SatSolver>* solver, TimeLimit* time_limit,
442 std::vector<bool>* solution /* only filled if SAT */,
443 DratProofHandler* drat_proof_handler /* can be nullptr */,
444 SolverLogger* logger);
445
446} // namespace sat
447} // namespace operations_research
448
449#endif // OR_TOOLS_SAT_SIMPLIFICATION_H_
A simple class to enforce both an elapsed time limit and a deterministic time limit in the same threa...
Definition: time_limit.h:105
void Add(Literal x, const absl::Span< const Literal > clause)
void ApplyMapping(const absl::StrongVector< BooleanVariable, BooleanVariable > &mapping)
std::vector< bool > PostsolveSolution(const std::vector< bool > &solution)
std::vector< Literal > Clause(int i) const
std::vector< bool > ExtractAndPostsolveSolution(const SatSolver &solver)
void SetNumVariables(int num_variables)
const std::vector< Literal > & Clause(ClauseIndex ci) const
void LoadProblemIntoSatSolver(SatSolver *solver)
void AddBinaryClause(Literal a, Literal b)
SatPresolver(SatPostsolver *postsolver, SolverLogger *logger)
void SetEquivalentLiteralMapping(const absl::StrongVector< LiteralIndex, LiteralIndex > &mapping)
void SetParameters(const SatParameters &params)
absl::StrongVector< BooleanVariable, BooleanVariable > VariableMapping() const
void SetDratProofHandler(DratProofHandler *drat_proof_handler)
void AddClause(absl::Span< const Literal > clause)
bool ProcessClauseToSimplifyOthers(ClauseIndex clause_index)
void SetTimeLimit(TimeLimit *time_limit)
int64_t b
int64_t a
ModelSharedTimeLimit * time_limit
IntVar * var
Definition: expr_array.cc:1874
void swap(IdMap< K, V > &a, IdMap< K, V > &b)
Definition: id_map.h:263
int ComputeResolvantSize(Literal x, const std::vector< Literal > &a, const std::vector< Literal > &b)
LiteralIndex DifferAtGivenLiteral(const std::vector< Literal > &a, const std::vector< Literal > &b, Literal l)
bool SimplifyClause(const std::vector< Literal > &a, std::vector< Literal > *b, LiteralIndex *opposite_literal, int64_t *num_inspected_literals)
bool ComputeResolvant(Literal x, const std::vector< Literal > &a, const std::vector< Literal > &b, std::vector< Literal > *out)
SatSolver::Status SolveWithPresolve(std::unique_ptr< SatSolver > *solver, TimeLimit *time_limit, std::vector< bool > *solution, DratProofHandler *drat_proof_handler, SolverLogger *logger)
void ProbeAndFindEquivalentLiteral(SatSolver *solver, SatPostsolver *postsolver, DratProofHandler *drat_proof_handler, absl::StrongVector< LiteralIndex, LiteralIndex > *mapping)
Collection of objects used to extend the Constraint Solver library.
Literal literal
Definition: optimization.cc:85
int64_t weight
Definition: pack.cc:510