forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 3
/
drat_checker.cc
610 lines (563 loc) · 22.2 KB
/
drat_checker.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
// Copyright 2010-2018 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ortools/sat/drat_checker.h"
#include <algorithm>
#include <fstream>
#include "absl/strings/numbers.h"
#include "absl/strings/str_split.h"
#include "absl/time/clock.h"
#include "ortools/base/hash.h"
#include "ortools/base/stl_util.h"
#include "ortools/util/time_limit.h"
namespace operations_research {
namespace sat {
DratChecker::Clause::Clause(int first_literal_index, int num_literals)
: first_literal_index(first_literal_index), num_literals(num_literals) {}
std::size_t DratChecker::ClauseHash::operator()(
const ClauseIndex clause_index) const {
size_t hash = 0;
for (Literal literal : checker->Literals(checker->clauses_[clause_index])) {
hash = util_hash::Hash(literal.Index().value(), hash);
}
return hash;
}
bool DratChecker::ClauseEquiv::operator()(
const ClauseIndex clause_index1, const ClauseIndex clause_index2) const {
return checker->Literals(checker->clauses_[clause_index1]) ==
checker->Literals(checker->clauses_[clause_index2]);
}
DratChecker::DratChecker()
: first_infered_clause_index_(kNoClauseIndex),
clause_set_(0, ClauseHash(this), ClauseEquiv(this)),
num_variables_(0) {}
bool DratChecker::Clause::IsDeleted(ClauseIndex clause_index) const {
return deleted_index <= clause_index;
}
void DratChecker::AddProblemClause(absl::Span<const Literal> clause) {
DCHECK_EQ(first_infered_clause_index_, kNoClauseIndex);
const ClauseIndex clause_index = AddClause(clause);
const auto it = clause_set_.find(clause_index);
if (it != clause_set_.end()) {
clauses_[*it].num_copies += 1;
RemoveLastClause();
} else {
clause_set_.insert(clause_index);
}
}
void DratChecker::AddInferedClause(absl::Span<const Literal> clause) {
const ClauseIndex infered_clause_index = AddClause(clause);
if (first_infered_clause_index_ == kNoClauseIndex) {
first_infered_clause_index_ = infered_clause_index;
}
const auto it = clause_set_.find(infered_clause_index);
if (it != clause_set_.end()) {
clauses_[*it].num_copies += 1;
if (*it >= first_infered_clause_index_ && !clause.empty()) {
CHECK_EQ(clauses_[*it].rat_literal_index, clause[0].Index());
}
RemoveLastClause();
} else {
clauses_[infered_clause_index].rat_literal_index =
clause.empty() ? kNoLiteralIndex : clause[0].Index();
clause_set_.insert(infered_clause_index);
}
}
ClauseIndex DratChecker::AddClause(absl::Span<const Literal> clause) {
const int first_literal_index = literals_.size();
literals_.insert(literals_.end(), clause.begin(), clause.end());
// Sort the input clause in strictly increasing order (by sorting and then
// removing the duplicate literals).
std::sort(literals_.begin() + first_literal_index, literals_.end());
literals_.erase(
std::unique(literals_.begin() + first_literal_index, literals_.end()),
literals_.end());
for (int i = first_literal_index + 1; i < literals_.size(); ++i) {
CHECK(literals_[i] != literals_[i - 1].Negated());
}
clauses_.push_back(
Clause(first_literal_index, literals_.size() - first_literal_index));
if (!clause.empty()) {
num_variables_ =
std::max(num_variables_, literals_.back().Variable().value() + 1);
}
return ClauseIndex(clauses_.size() - 1);
}
void DratChecker::DeleteClause(absl::Span<const Literal> clause) {
// Temporarily add 'clause' to find if it has been previously added.
const auto it = clause_set_.find(AddClause(clause));
if (it != clause_set_.end()) {
Clause& existing_clause = clauses_[*it];
existing_clause.num_copies -= 1;
if (existing_clause.num_copies == 0) {
DCHECK(existing_clause.deleted_index == std::numeric_limits<int>::max());
existing_clause.deleted_index = clauses_.size() - 1;
if (clauses_.back().num_literals >= 2) {
clauses_[ClauseIndex(clauses_.size() - 2)].deleted_clauses.push_back(
*it);
}
clause_set_.erase(it);
}
} else {
LOG(WARNING) << "Couldn't find deleted clause";
}
// Delete 'clause' and its literals.
RemoveLastClause();
}
void DratChecker::RemoveLastClause() {
literals_.resize(clauses_.back().first_literal_index);
clauses_.pop_back();
}
// See Algorithm of Fig. 8 in 'Trimming while Checking Clausal Proofs'.
DratChecker::Status DratChecker::Check(double max_time_in_seconds) {
// First check that the last infered clause is empty (this implies there
// should be at least one infered clause), and mark it as needed for the
// proof.
if (clauses_.empty() || first_infered_clause_index_ == kNoClauseIndex ||
clauses_.back().num_literals != 0) {
return Status::INVALID;
}
clauses_.back().is_needed_for_proof = true;
// Checks the infered clauses in reversed order. The advantage of this order
// is that when checking a clause, one can mark all the clauses that are used
// to check it. In turn, only these marked clauses need to be checked (and so
// on recursively). By contrast, a forward iteration needs to check all the
// clauses.
const int64 start_time_nanos = absl::GetCurrentTimeNanos();
TimeLimit time_limit(max_time_in_seconds);
Init();
for (ClauseIndex i(clauses_.size() - 1); i >= first_infered_clause_index_;
--i) {
if (time_limit.LimitReached()) {
return Status::UNKNOWN;
}
const Clause& clause = clauses_[i];
// Start watching the literals of the clauses that were deleted just after
// this one, and which are now no longer deleted.
for (const ClauseIndex j : clause.deleted_clauses) {
WatchClause(j);
}
if (!clause.is_needed_for_proof) {
continue;
}
// 'clause' must have either the Reverse Unit Propagation (RUP) property:
if (HasRupProperty(i, Literals(clause))) {
continue;
}
// or the Reverse Asymetric Tautology (RAT) property. This property is
// defined by the fact that all clauses which contain the negation of
// the RAT literal of 'clause', after resolution with 'clause', must have
// the RUP property.
// Note from 'DRAT-trim: Efficient Checking and Trimming Using Expressive
// Clausal Proofs': "[in order] to access to all clauses containing the
// negation of the resolution literal, one could build a literal-to-clause
// lookup table of the original formula and update it after each lemma
// addition and deletion step. However, these updates can be expensive and
// the lookup table potentially doubles the memory usage of the tool.
// Since most lemmas emitted by state-of-the-art SAT solvers can be
// validated using the RUP check, such a lookup table has been omitted."
if (clause.rat_literal_index == kNoLiteralIndex) return Status::INVALID;
++num_rat_checks_;
std::vector<Literal> resolvent;
for (ClauseIndex j(0); j < i; ++j) {
if (!clauses_[j].IsDeleted(i) &&
ContainsLiteral(Literals(clauses_[j]),
Literal(clause.rat_literal_index).Negated())) {
// Check that the resolvent has the RUP property.
if (!Resolve(Literals(clause), Literals(clauses_[j]),
Literal(clause.rat_literal_index), &tmp_assignment_,
&resolvent) ||
!HasRupProperty(i, resolvent)) {
return Status::INVALID;
}
}
}
}
LogStatistics(absl::GetCurrentTimeNanos() - start_time_nanos);
return Status::VALID;
}
std::vector<std::vector<Literal>> DratChecker::GetUnsatSubProblem() const {
return GetClausesNeededForProof(ClauseIndex(0), first_infered_clause_index_);
}
std::vector<std::vector<Literal>> DratChecker::GetOptimizedProof() const {
return GetClausesNeededForProof(first_infered_clause_index_,
ClauseIndex(clauses_.size()));
}
std::vector<std::vector<Literal>> DratChecker::GetClausesNeededForProof(
ClauseIndex begin, ClauseIndex end) const {
std::vector<std::vector<Literal>> result;
for (ClauseIndex i = begin; i < end; ++i) {
const Clause& clause = clauses_[i];
if (clause.is_needed_for_proof) {
const absl::Span<const Literal>& literals = Literals(clause);
result.emplace_back(literals.begin(), literals.end());
if (clause.rat_literal_index != kNoLiteralIndex) {
const int rat_literal_clause_index =
std::find(literals.begin(), literals.end(),
Literal(clause.rat_literal_index)) -
literals.begin();
std::swap(result.back()[0], result.back()[rat_literal_clause_index]);
}
}
}
return result;
}
absl::Span<const Literal> DratChecker::Literals(const Clause& clause) const {
return absl::Span<const Literal>(
literals_.data() + clause.first_literal_index, clause.num_literals);
}
void DratChecker::Init() {
assigned_.clear();
assignment_.Resize(num_variables_);
assignment_source_.resize(num_variables_, kNoClauseIndex);
high_priority_literals_to_assign_.clear();
low_priority_literals_to_assign_.clear();
watched_literals_.clear();
watched_literals_.resize(2 * num_variables_);
single_literal_clauses_.clear();
unit_stack_.clear();
tmp_assignment_.Resize(num_variables_);
num_rat_checks_ = 0;
for (ClauseIndex clause_index(0); clause_index < clauses_.size();
++clause_index) {
Clause& clause = clauses_[clause_index];
if (clause.num_literals >= 2) {
// Don't watch the literals of the deleted clauses right away, instead
// watch them when these clauses become 'undeleted' in backward checking.
if (clause.deleted_index == std::numeric_limits<int>::max()) {
WatchClause(clause_index);
}
} else if (clause.num_literals == 1) {
single_literal_clauses_.push_back(clause_index);
}
}
}
void DratChecker::WatchClause(ClauseIndex clause_index) {
const Literal* clause_literals =
literals_.data() + clauses_[clause_index].first_literal_index;
watched_literals_[clause_literals[0].Index()].push_back(clause_index);
watched_literals_[clause_literals[1].Index()].push_back(clause_index);
}
bool DratChecker::HasRupProperty(ClauseIndex num_clauses,
absl::Span<const Literal> clause) {
ClauseIndex conflict = kNoClauseIndex;
for (const Literal literal : clause) {
conflict =
AssignAndPropagate(num_clauses, literal.Negated(), kNoClauseIndex);
if (conflict != kNoClauseIndex) {
break;
}
}
for (const ClauseIndex clause_index : single_literal_clauses_) {
const Clause& clause = clauses_[clause_index];
// TODO(user): consider ignoring the deletion of single literal clauses
// as done in drat-trim.
if (clause_index < num_clauses && !clause.IsDeleted(num_clauses)) {
if (clause.is_needed_for_proof) {
high_priority_literals_to_assign_.push_back(
{literals_[clause.first_literal_index], clause_index});
} else {
low_priority_literals_to_assign_.push_back(
{literals_[clause.first_literal_index], clause_index});
}
}
}
while (!(high_priority_literals_to_assign_.empty() &&
low_priority_literals_to_assign_.empty()) &&
conflict == kNoClauseIndex) {
std::vector<LiteralToAssign>& stack =
high_priority_literals_to_assign_.empty()
? low_priority_literals_to_assign_
: high_priority_literals_to_assign_;
const LiteralToAssign literal_to_assign = stack.back();
stack.pop_back();
if (assignment_.LiteralIsAssigned(literal_to_assign.literal)) {
// If the literal to assign to true is already assigned to false, we found
// a conflict, with the source clause of this previous assignment.
if (assignment_.LiteralIsFalse(literal_to_assign.literal)) {
conflict = literal_to_assign.source_clause_index;
break;
} else {
continue;
}
}
DCHECK(literal_to_assign.source_clause_index != kNoClauseIndex);
unit_stack_.push_back(literal_to_assign.source_clause_index);
conflict = AssignAndPropagate(num_clauses, literal_to_assign.literal,
literal_to_assign.source_clause_index);
}
if (conflict != kNoClauseIndex) {
MarkAsNeededForProof(&clauses_[conflict]);
}
for (const Literal literal : assigned_) {
assignment_.UnassignLiteral(literal);
}
assigned_.clear();
high_priority_literals_to_assign_.clear();
low_priority_literals_to_assign_.clear();
unit_stack_.clear();
return conflict != kNoClauseIndex;
}
ClauseIndex DratChecker::AssignAndPropagate(ClauseIndex num_clauses,
Literal literal,
ClauseIndex source_clause_index) {
assigned_.push_back(literal);
assignment_.AssignFromTrueLiteral(literal);
assignment_source_[literal.Variable()] = source_clause_index;
const Literal false_literal = literal.Negated();
std::vector<ClauseIndex>& watched = watched_literals_[false_literal.Index()];
int new_watched_size = 0;
ClauseIndex conflict_index = kNoClauseIndex;
for (const ClauseIndex clause_index : watched) {
if (clause_index >= num_clauses) {
// Stop watching the literals of clauses which cannot possibly be
// necessary to check the rest of the proof.
continue;
}
Clause& clause = clauses_[clause_index];
DCHECK(!clause.IsDeleted(num_clauses));
if (conflict_index != kNoClauseIndex) {
watched[new_watched_size++] = clause_index;
continue;
}
Literal* clause_literals = literals_.data() + clause.first_literal_index;
const Literal other_watched_literal(LiteralIndex(
clause_literals[0].Index().value() ^
clause_literals[1].Index().value() ^ false_literal.Index().value()));
if (assignment_.LiteralIsTrue(other_watched_literal)) {
watched[new_watched_size++] = clause_index;
continue;
}
bool new_watched_literal_found = false;
for (int i = 2; i < clause.num_literals; ++i) {
if (!assignment_.LiteralIsFalse(clause_literals[i])) {
clause_literals[0] = other_watched_literal;
clause_literals[1] = clause_literals[i];
clause_literals[i] = false_literal;
watched_literals_[clause_literals[1].Index()].push_back(clause_index);
new_watched_literal_found = true;
break;
}
}
if (!new_watched_literal_found) {
if (assignment_.LiteralIsFalse(other_watched_literal)) {
// 'clause' is falsified with 'assignment_', we found a conflict.
// TODO(user): test moving the rest of the vector here and
// returning right away.
conflict_index = clause_index;
} else {
DCHECK(!assignment_.LiteralIsAssigned(other_watched_literal));
// 'clause' is unit, push its unit literal on
// 'literals_to_assign_high_priority' or
// 'literals_to_assign_low_priority' to assign it to true and propagate
// it in a later call to AssignAndPropagate().
if (clause.is_needed_for_proof) {
high_priority_literals_to_assign_.push_back(
{other_watched_literal, clause_index});
} else {
low_priority_literals_to_assign_.push_back(
{other_watched_literal, clause_index});
}
}
watched[new_watched_size++] = clause_index;
}
}
watched.resize(new_watched_size);
return conflict_index;
}
void DratChecker::MarkAsNeededForProof(Clause* clause) {
const auto mark_clause_and_sources = [&](Clause* clause) {
clause->is_needed_for_proof = true;
for (const Literal literal : Literals(*clause)) {
const ClauseIndex source_clause_index =
assignment_source_[literal.Variable()];
if (source_clause_index != kNoClauseIndex) {
clauses_[source_clause_index].tmp_is_needed_for_proof_step = true;
}
}
};
mark_clause_and_sources(clause);
for (int i = unit_stack_.size() - 1; i >= 0; --i) {
Clause& unit_clause = clauses_[unit_stack_[i]];
if (unit_clause.tmp_is_needed_for_proof_step) {
mark_clause_and_sources(&unit_clause);
// We can clean this flag here without risking missing clauses needed for
// the proof, because the clauses needed for a clause C are always lower
// than C in the stack.
unit_clause.tmp_is_needed_for_proof_step = false;
}
}
}
void DratChecker::LogStatistics(int64 duration_nanos) const {
int problem_clauses_needed_for_proof = 0;
int infered_clauses_needed_for_proof = 0;
for (ClauseIndex i(0); i < clauses_.size(); ++i) {
if (clauses_[i].is_needed_for_proof) {
if (i < first_infered_clause_index_) {
++problem_clauses_needed_for_proof;
} else {
++infered_clauses_needed_for_proof;
}
}
}
LOG(INFO) << problem_clauses_needed_for_proof
<< " problem clauses needed for proof, out of "
<< first_infered_clause_index_;
LOG(INFO) << infered_clauses_needed_for_proof
<< " infered clauses needed for proof, out of "
<< clauses_.size() - first_infered_clause_index_;
LOG(INFO) << num_rat_checks_ << " RAT infered clauses";
LOG(INFO) << "verification time: " << 1e-9 * duration_nanos << " s";
}
bool ContainsLiteral(absl::Span<const Literal> clause, Literal literal) {
return std::find(clause.begin(), clause.end(), literal) != clause.end();
}
bool Resolve(absl::Span<const Literal> clause,
absl::Span<const Literal> other_clause,
Literal complementary_literal, VariablesAssignment* assignment,
std::vector<Literal>* resolvent) {
DCHECK(ContainsLiteral(clause, complementary_literal));
DCHECK(ContainsLiteral(other_clause, complementary_literal.Negated()));
resolvent->clear();
for (const Literal literal : clause) {
if (literal != complementary_literal) {
// Temporary assignment used to do the checks below in linear time.
assignment->AssignFromTrueLiteral(literal);
resolvent->push_back(literal);
}
}
bool result = true;
for (const Literal other_literal : other_clause) {
if (other_literal != complementary_literal.Negated()) {
if (assignment->LiteralIsFalse(other_literal)) {
result = false;
break;
} else if (!assignment->LiteralIsAssigned(other_literal)) {
resolvent->push_back(other_literal);
}
}
}
// Revert the temporary assignment done above.
for (const Literal literal : clause) {
if (literal != complementary_literal) {
assignment->UnassignLiteral(literal);
}
}
return result;
}
bool AddProblemClauses(const std::string& file_path,
DratChecker* drat_checker) {
int line_number = 0;
int num_variables = 0;
int num_clauses = 0;
std::vector<Literal> literals;
std::ifstream file(file_path);
std::string line;
bool result = true;
while (std::getline(file, line)) {
line_number++;
std::vector<absl::string_view> words =
absl::StrSplit(line, absl::ByAnyChar(" \t"), absl::SkipWhitespace());
if (words.empty() || words[0] == "c") {
// Ignore empty and comment lines.
continue;
}
if (words[0] == "p") {
if (num_clauses > 0 || words.size() != 4 || words[1] != "cnf" ||
!absl::SimpleAtoi(words[2], &num_variables) || num_variables <= 0 ||
!absl::SimpleAtoi(words[3], &num_clauses) || num_clauses <= 0) {
LOG(ERROR) << "Invalid content '" << line << "' at line " << line_number
<< " of " << file_path;
result = false;
break;
}
continue;
}
literals.clear();
for (int i = 0; i < words.size(); ++i) {
int signed_value;
if (!absl::SimpleAtoi(words[i], &signed_value) ||
std::abs(signed_value) > num_variables ||
(signed_value == 0 && i != words.size() - 1)) {
LOG(ERROR) << "Invalid content '" << line << "' at line " << line_number
<< " of " << file_path;
result = false;
break;
}
if (signed_value != 0) {
literals.push_back(Literal(signed_value));
}
}
drat_checker->AddProblemClause(literals);
}
file.close();
return result;
}
bool AddInferedAndDeletedClauses(const std::string& file_path,
DratChecker* drat_checker) {
int line_number = 0;
bool ends_with_empty_clause = false;
std::vector<Literal> literals;
std::ifstream file(file_path);
std::string line;
bool result = true;
while (std::getline(file, line)) {
line_number++;
std::vector<absl::string_view> words =
absl::StrSplit(line, absl::ByAnyChar(" \t"), absl::SkipWhitespace());
bool delete_clause = !words.empty() && words[0] == "d";
literals.clear();
for (int i = (delete_clause ? 1 : 0); i < words.size(); ++i) {
int signed_value;
if (!absl::SimpleAtoi(words[i], &signed_value) ||
(signed_value == 0 && i != words.size() - 1)) {
LOG(ERROR) << "Invalid content '" << line << "' at line " << line_number
<< " of " << file_path;
result = false;
break;
}
if (signed_value != 0) {
literals.push_back(Literal(signed_value));
}
}
if (delete_clause) {
drat_checker->DeleteClause(literals);
ends_with_empty_clause = false;
} else {
drat_checker->AddInferedClause(literals);
ends_with_empty_clause = literals.empty();
}
}
if (!ends_with_empty_clause) {
drat_checker->AddInferedClause({});
}
file.close();
return result;
}
bool PrintClauses(const std::string& file_path, SatFormat format,
const std::vector<std::vector<Literal>>& clauses,
int num_variables) {
std::ofstream output_stream(file_path, std::ofstream::out);
if (format == DIMACS) {
output_stream << "p cnf " << num_variables << " " << clauses.size() << "\n";
}
for (const auto& clause : clauses) {
for (Literal literal : clause) {
output_stream << literal.SignedValue() << " ";
}
output_stream << "0\n";
}
output_stream.close();
return output_stream.good();
}
} // namespace sat
} // namespace operations_research