Skip to content

Commit b3d4c95

Browse files
committed
[CP-SAT] fix vivification bug; more work on encodings
1 parent fe9509f commit b3d4c95

19 files changed

+1222
-430
lines changed

ortools/sat/BUILD.bazel

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,9 @@ cc_library(
335335
"//ortools/util:time_limit",
336336
"@abseil-cpp//absl/algorithm:container",
337337
"@abseil-cpp//absl/base:core_headers",
338+
"@abseil-cpp//absl/cleanup",
338339
"@abseil-cpp//absl/container:btree",
340+
"@abseil-cpp//absl/log",
339341
"@abseil-cpp//absl/log:check",
340342
"@abseil-cpp//absl/types:span",
341343
],
@@ -393,8 +395,32 @@ cc_library(
393395
deps = [
394396
":cp_model_utils",
395397
":presolve_context",
398+
"//ortools/base:stl_util",
399+
"//ortools/util:bitset",
396400
"//ortools/util:sorted_interval_list",
401+
"@abseil-cpp//absl/algorithm:container",
402+
"@abseil-cpp//absl/container:flat_hash_map",
403+
"@abseil-cpp//absl/container:flat_hash_set",
404+
"@abseil-cpp//absl/container:inlined_vector",
397405
"@abseil-cpp//absl/log",
406+
"@abseil-cpp//absl/log:check",
407+
"@protobuf",
408+
],
409+
)
410+
411+
cc_test(
412+
name = "presolve_encoding_test",
413+
srcs = ["presolve_encoding_test.cc"],
414+
deps = [
415+
":cp_model_cc_proto",
416+
":model",
417+
":presolve_context",
418+
":presolve_encoding",
419+
"//ortools/base:gmock_main",
420+
"//ortools/base:parse_test_proto",
421+
"//ortools/util:sorted_interval_list",
422+
"@abseil-cpp//absl/container:flat_hash_map",
423+
"@abseil-cpp//absl/log:check",
398424
],
399425
)
400426

@@ -975,8 +1001,10 @@ cc_library(
9751001
"//ortools/util:sigint",
9761002
"//ortools/util:sorted_interval_list",
9771003
"//ortools/util:strong_integers",
1004+
"//ortools/util:testing_utils",
9781005
"//ortools/util:time_limit",
9791006
"@abseil-cpp//absl/base:core_headers",
1007+
"@abseil-cpp//absl/base:log_severity",
9801008
"@abseil-cpp//absl/cleanup",
9811009
"@abseil-cpp//absl/container:btree",
9821010
"@abseil-cpp//absl/container:flat_hash_map",

ortools/sat/clause.cc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,8 @@ bool ClauseManager::InprocessingRewriteClause(
529529
}
530530
const bool is_reason = ClauseIsUsedAsReason(clause);
531531

532-
CHECK(!is_reason || new_clause[0] == clause->PropagatedLiteral());
532+
CHECK(!is_reason || new_clause[0] == clause->PropagatedLiteral())
533+
<< new_clause << " old " << clause->AsSpan();
533534

534535
if (new_clause.empty()) return false; // UNSAT.
535536

@@ -682,12 +683,24 @@ SatClause* ClauseManager::NextNewClauseToMinimize() {
682683
}
683684

684685
SatClause* ClauseManager::NextClauseToMinimize() {
686+
const int old = to_first_minimize_index_;
685687
for (; to_minimize_index_ < clauses_.size(); ++to_minimize_index_) {
686688
if (clauses_[to_minimize_index_]->IsRemoved()) continue;
687689
if (!IsRemovable(clauses_[to_minimize_index_])) {
688690
return clauses_[to_minimize_index_++];
689691
}
690692
}
693+
694+
// Lets reset and try once more to find one.
695+
to_minimize_index_ = 0;
696+
++num_to_minimize_index_resets_;
697+
for (; to_minimize_index_ < old; ++to_minimize_index_) {
698+
if (clauses_[to_minimize_index_]->IsRemoved()) continue;
699+
if (!IsRemovable(clauses_[to_minimize_index_])) {
700+
return clauses_[to_minimize_index_++];
701+
}
702+
}
703+
691704
return nullptr;
692705
}
693706

ortools/sat/clause.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ class ClauseManager : public SatPropagator {
308308
// Returns the next clause to minimize that has never been minimized before.
309309
// Note that we only minimize clauses kept forever.
310310
SatClause* NextNewClauseToMinimize();
311+
311312
// Returns the next clause to minimize, this iterator will be reset to the
312313
// start so the clauses will be returned in round-robin order.
313314
// Note that we only minimize clauses kept forever.
@@ -324,7 +325,10 @@ class ClauseManager : public SatPropagator {
324325

325326
// Restart the scans.
326327
void ResetToProbeIndex() { to_probe_index_ = 0; }
327-
void ResetToMinimizeIndex() { to_minimize_index_ = 0; }
328+
int64_t NumToMinimizeIndexResets() const {
329+
return num_to_minimize_index_resets_;
330+
}
331+
328332
// Ensures that NextNewClauseToMinimize() returns only learned clauses.
329333
// This is a noop after the first call.
330334
void EnsureNewClauseIndexInitialized() {
@@ -499,6 +503,8 @@ class ClauseManager : public SatPropagator {
499503

500504
// TODO(user): If more indices are needed, switch to a generic API.
501505
int to_minimize_index_ = 0;
506+
507+
int num_to_minimize_index_resets_ = 0;
502508
int to_first_minimize_index_ = 0;
503509
int to_probe_index_ = 0;
504510

0 commit comments

Comments
 (0)