OR-Tools  9.2
drat_checker.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_DRAT_CHECKER_H_
15#define OR_TOOLS_SAT_DRAT_CHECKER_H_
16
17#include <cstdint>
18#include <memory>
19#include <vector>
20
21#include "absl/container/flat_hash_set.h"
22#include "absl/types/span.h"
26
27namespace operations_research {
28namespace sat {
29
30// Index of a clause (>= 0).
31DEFINE_INT_TYPE(ClauseIndex, int);
32const ClauseIndex kNoClauseIndex(-1);
33
34// DRAT is a SAT proof format that allows a simple program to check that a
35// problem is really UNSAT. The description of the format and a checker are
36// available at http://www.cs.utexas.edu/~marijn/drat-trim/. This class checks
37// that a DRAT proof is valid.
38//
39// Note that DRAT proofs are often huge (can be GB), and can take about as much
40// time to check as it takes to find the proof in the first place!
42 public:
45
46 // Returns the number of Boolean variables used in the problem and infered
47 // clauses.
48 int num_variables() const { return num_variables_; }
49
50 // Adds a clause of the problem that must be checked. The problem clauses must
51 // be added first, before any infered clause. The given clause must not
52 // contain a literal and its negation. Must not be called after Check().
53 void AddProblemClause(absl::Span<const Literal> clause);
54
55 // Adds a clause which is infered from the problem clauses and the previously
56 // infered clauses (that are have not been deleted). Infered clauses must be
57 // added after the problem clauses. Clauses with the Reverse Asymetric
58 // Tautology (RAT) property for literal l must start with this literal. The
59 // given clause must not contain a literal and its negation. Must not be
60 // called after Check().
61 void AddInferedClause(absl::Span<const Literal> clause);
62
63 // Deletes a problem or infered clause. The order of the literals does not
64 // matter. In particular, it can be different from the order that was used
65 // when the clause was added. Must not be called after Check().
66 void DeleteClause(absl::Span<const Literal> clause);
67
68 // Checks that the infered clauses form a DRAT proof that the problem clauses
69 // are UNSAT. For this the last added infered clause must be the empty clause
70 // and each infered clause must have either the Reverse Unit Propagation (RUP)
71 // or the Reverse Asymetric Tautology (RAT) property with respect to the
72 // problem clauses and the previously infered clauses which are not deleted.
73 // Returns VALID if the proof is valid, INVALID if it is not, and UNKNOWN if
74 // the check timed out.
75 // WARNING: no new clause must be added or deleted after this method has been
76 // called.
77 enum Status {
81 };
82 Status Check(double max_time_in_seconds);
83
84 // Returns a subproblem of the original problem that is already UNSAT. The
85 // result is undefined if Check() was not previously called, or did not return
86 // true.
87 std::vector<std::vector<Literal>> GetUnsatSubProblem() const;
88
89 // Returns a DRAT proof that GetUnsatSubProblem() is UNSAT. The result is
90 // undefined if Check() was not previously called, or did not return true.
91 std::vector<std::vector<Literal>> GetOptimizedProof() const;
92
93 private:
94 // A problem or infered clause. The literals are specified as a subrange of
95 // 'literals_' (namely the subrange from 'first_literal_index' to
96 // 'first_literal_index' + 'num_literals' - 1), and are sorted in increasing
97 // order *before Check() is called*.
98 struct Clause {
99 // The index of the first literal of this clause in 'literals_'.
100 int first_literal_index;
101 // The number of literals of this clause.
102 int num_literals;
103
104 // The clause literal to use to check the RAT property, or kNoLiteralIndex
105 // for problem clauses and empty infered clauses.
106 LiteralIndex rat_literal_index = kNoLiteralIndex;
107
108 // The *current* number of copies of this clause. This number is incremented
109 // each time a copy of the clause is added, and decremented each time a copy
110 // is deleted. When this number reaches 0, the clause is actually marked as
111 // deleted (see 'deleted_index'). If other copies are added after this
112 // number reached 0, a new clause is added (because a Clause lifetime is a
113 // single interval of ClauseIndex values; therefore, in order to represent a
114 // lifetime made of several intervals, several Clause are used).
115 int num_copies = 1;
116
117 // The index in 'clauses_' from which this clause is deleted (inclusive).
118 // For instance, with AddProblemClause(c0), AddProblemClause(c1),
119 // DeleteClause(c0), AddProblemClause(c2), ... if c0's index is 0, then its
120 // deleted_index is 2. Meaning that when checking a clause whose index is
121 // larger than or equal to 2 (e.g. c2), c0 can be ignored.
122 ClauseIndex deleted_index = ClauseIndex(std::numeric_limits<int>::max());
123
124 // The indices of the clauses (with at least two literals) which are deleted
125 // just after this clause.
126 std::vector<ClauseIndex> deleted_clauses;
127
128 // Whether this clause is actually needed to check the DRAT proof.
129 bool is_needed_for_proof = false;
130 // Whether this clause is actually needed to check the current step (i.e. an
131 // infered clause) of the DRAT proof. This bool is always false, except in
132 // MarkAsNeededForProof() that uses it temporarily.
133 bool tmp_is_needed_for_proof_step = false;
134
135 Clause(int first_literal_index, int num_literals);
136
137 // Returns true if this clause is deleted before the given clause.
138 bool IsDeleted(ClauseIndex clause_index) const;
139 };
140
141 // A literal to assign to true during boolean constraint propagation. When a
142 // literal is assigned, new literals can be found that also need to be
143 // assigned to true (via unit clauses). In this case they are pushed on a
144 // stack of LiteralToAssign values, to be processed later on (the use of this
145 // stack avoids recursive calls to the boolean constraint propagation method
146 // AssignAndPropagate()).
147 struct LiteralToAssign {
148 // The literal that must be assigned to true.
149 Literal literal;
150 // The index of the clause from which this assignment was deduced. This is
151 // kNoClauseIndex for the clause we are currently checking (whose literals
152 // are all falsified to check if a conflict can be derived). Otherwise this
153 // is the index of a unit clause with unit literal 'literal' that was found
154 // during boolean constraint propagation.
155 ClauseIndex source_clause_index;
156 };
157
158 // Hash function for clauses.
159 struct ClauseHash {
160 DratChecker* checker;
161 explicit ClauseHash(DratChecker* checker) : checker(checker) {}
162 std::size_t operator()(const ClauseIndex clause_index) const;
163 };
164
165 // Equality function for clauses.
166 struct ClauseEquiv {
167 DratChecker* checker;
168 explicit ClauseEquiv(DratChecker* checker) : checker(checker) {}
169 bool operator()(const ClauseIndex clause_index1,
170 const ClauseIndex clause_index2) const;
171 };
172
173 // Adds a clause and returns its index.
174 ClauseIndex AddClause(absl::Span<const Literal> clause);
175
176 // Removes the last clause added to 'clauses_'.
177 void RemoveLastClause();
178
179 // Returns the literals of the given clause in increasing order.
180 absl::Span<const Literal> Literals(const Clause& clause) const;
181
182 // Initializes the data structures used to check the DRAT proof.
183 void Init();
184
185 // Adds 2 watch literals for the given clause.
186 void WatchClause(ClauseIndex clause_index);
187
188 // Returns true if, by assigning all the literals of 'clause' to false, a
189 // conflict can be found with boolean constraint propagation, using the non
190 // deleted clauses whose index is strictly less than 'num_clauses'. If so,
191 // marks the clauses actually used in this process as needed to check to DRAT
192 // proof.
193 bool HasRupProperty(ClauseIndex num_clauses,
194 absl::Span<const Literal> clause);
195
196 // Assigns 'literal' to true in 'assignment_' (and pushes it to 'assigned_'),
197 // records its source clause 'source_clause_index' in 'assignment_source_',
198 // and uses the watched literals to find all the clauses (whose index is less
199 // than 'num_clauses') that become unit due to this assignment. For each unit
200 // clause found, pushes its unit literal on top of
201 // 'high_priority_literals_to_assign_' or 'low_priority_literals_to_assign_'.
202 // Returns kNoClauseIndex if no falsified clause is found, or the index of the
203 // first found falsified clause otherwise.
204 ClauseIndex AssignAndPropagate(ClauseIndex num_clauses, Literal literal,
205 ClauseIndex source_clause_index);
206
207 // Marks the given clause as needed to check the DRAT proof, as well as the
208 // other clauses which were used to check this clause (these are found from
209 // 'unit_stack_', containing the clauses that became unit in
210 // AssignAndPropagate, and from 'assignment_source_', containing for each
211 // variable the clause that caused its assignment).
212 void MarkAsNeededForProof(Clause* clause);
213
214 // Returns the clauses whose index is in [begin,end) which are needed for the
215 // proof. The result is undefined if Check() was not previously called, or did
216 // not return true.
217 std::vector<std::vector<Literal>> GetClausesNeededForProof(
218 ClauseIndex begin, ClauseIndex end) const;
219
220 void LogStatistics(int64_t duration_nanos) const;
221
222 // The index of the first infered clause in 'clauses_', or kNoClauseIndex if
223 // there is no infered clause.
224 ClauseIndex first_infered_clause_index_;
225
226 // The problem clauses, followed by the infered clauses.
228
229 // A content addressable set of the non-deleted clauses in clauses_. After
230 // adding a clause to clauses_, this set can be used to find if the same
231 // clause was previously added (i.e if a find using the new clause index
232 // returns a previous index) and not yet deleted.
233 absl::flat_hash_set<ClauseIndex, ClauseHash, ClauseEquiv> clause_set_;
234
235 // All the literals used in 'clauses_'.
236 std::vector<Literal> literals_;
237
238 // The number of Boolean variables used in the clauses.
239 int num_variables_;
240
241 // ---------------------------------------------------------------------------
242 // Data initialized in Init() and used in Check() to check the DRAT proof.
243
244 // The literals that have been assigned so far (this is used to unassign them
245 // after a clause has been checked, before checking the next one).
246 std::vector<Literal> assigned_;
247
248 // The current assignment values of literals_.
249 VariablesAssignment assignment_;
250
251 // For each variable, the index of the unit clause that caused its assignment,
252 // or kNoClauseIndex if the variable is not assigned, or was assigned to
253 // falsify the clause that is currently being checked.
255
256 // The stack of literals that remain to be assigned to true during boolean
257 // constraint propagation, with high priority (unit clauses which are already
258 // marked as needed for the proof are given higher priority than the others
259 // during boolean constraint propagation. According to 'Trimming while
260 // Checking Clausal Proofs', this heuristics reduces the final number of
261 // clauses that are marked as needed for the proof, and therefore the
262 // verification time, in a majority of cases -- but not all).
263 std::vector<LiteralToAssign> high_priority_literals_to_assign_;
264
265 // The stack of literals that remain to be assigned to true during boolean
266 // constraint propagation, with low priority (see above).
267 std::vector<LiteralToAssign> low_priority_literals_to_assign_;
268
269 // For each literal, the list of clauses in which this literal is watched.
270 // Invariant 1: the literals with indices first_watched_literal_index and
271 // second_watched_literal_index of each clause with at least two literals are
272 // watched.
273 // Invariant 2: watched literals are non-falsified if the clause is not
274 // satisfied (in more details: if a clause c is contained in
275 // 'watched_literals_[l]' for literal l, then either c is satisfied with
276 // 'assignment_', or l is unassigned or assigned to true).
278
279 // The list of clauses with only one literal. This is needed for boolean
280 // constraint propagation, in addition to watched literals, because watched
281 // literals can only be used with clauses having at least two literals.
282 std::vector<ClauseIndex> single_literal_clauses_;
283
284 // The stack of clauses that have become unit during boolean constraint
285 // propagation, in HasRupProperty().
286 std::vector<ClauseIndex> unit_stack_;
287
288 // A temporary assignment, always fully unassigned except in Resolve().
289 VariablesAssignment tmp_assignment_;
290
291 // ---------------------------------------------------------------------------
292 // Statistics
293
294 // The number of infered clauses having the RAT property (but not the RUP
295 // property).
296 int num_rat_checks_;
297};
298
299// Returns true if the given clause contains the given literal. This works in
300// O(clause.size()).
301bool ContainsLiteral(absl::Span<const Literal> clause, Literal literal);
302
303// Returns true if 'complementary_literal' is the unique complementary literal
304// in the two given clauses. If so the resolvent of these clauses (i.e. their
305// union with 'complementary_literal' and its negation removed) is set in
306// 'resolvent'. 'clause' must contain 'complementary_literal', while
307// 'other_clause' must contain its negation. 'assignment' must have at least as
308// many variables as each clause, and they must all be unassigned. They are
309// still unassigned upon return.
310bool Resolve(absl::Span<const Literal> clause,
311 absl::Span<const Literal> other_clause,
312 Literal complementary_literal, VariablesAssignment* assignment,
313 std::vector<Literal>* resolvent);
314
315// Adds to the given drat checker the problem clauses from the file at the given
316// path, which must be in DIMACS format. Returns true iff the file was
317// successfully parsed.
318bool AddProblemClauses(const std::string& file_path, DratChecker* drat_checker);
319
320// Adds to the given drat checker the infered and deleted clauses from the file
321// at the given path, which must be in DRAT format. Returns true iff the file
322// was successfully parsed.
323bool AddInferedAndDeletedClauses(const std::string& file_path,
324 DratChecker* drat_checker);
325
326// The file formats that can be used to save a list of clauses.
330};
331
332// Prints the given clauses in the file at the given path, using the given file
333// format. Returns true iff the file was successfully written.
334bool PrintClauses(const std::string& file_path, SatFormat format,
335 const std::vector<std::vector<Literal>>& clauses,
336 int num_variables);
337
338} // namespace sat
339} // namespace operations_research
340
341#endif // OR_TOOLS_SAT_DRAT_CHECKER_H_
int64_t max
Definition: alldiff_cst.cc:140
void AddInferedClause(absl::Span< const Literal > clause)
Definition: drat_checker.cc:70
void DeleteClause(absl::Span< const Literal > clause)
Status Check(double max_time_in_seconds)
std::vector< std::vector< Literal > > GetUnsatSubProblem() const
void AddProblemClause(absl::Span< const Literal > clause)
Definition: drat_checker.cc:57
std::vector< std::vector< Literal > > GetOptimizedProof() const
const LiteralIndex kNoLiteralIndex(-1)
bool PrintClauses(const std::string &file_path, SatFormat format, const std::vector< std::vector< Literal > > &clauses, int num_variables)
bool Resolve(absl::Span< const Literal > clause, absl::Span< const Literal > other_clause, Literal complementary_literal, VariablesAssignment *assignment, std::vector< Literal > *resolvent)
DEFINE_INT_TYPE(ClauseIndex, int)
bool AddInferedAndDeletedClauses(const std::string &file_path, DratChecker *drat_checker)
bool ContainsLiteral(absl::Span< const Literal > clause, Literal literal)
bool AddProblemClauses(const std::string &file_path, DratChecker *drat_checker)
const ClauseIndex kNoClauseIndex(-1)
Collection of objects used to extend the Constraint Solver library.
Literal literal
Definition: optimization.cc:85
std::optional< int64_t > end