17 #ifndef OR_TOOLS_SAT_CLAUSE_H_
18 #define OR_TOOLS_SAT_CLAUSE_H_
25 #include "absl/container/flat_hash_map.h"
26 #include "absl/container/flat_hash_set.h"
27 #include "absl/container/inlined_vector.h"
28 #include "absl/types/span.h"
60 void operator delete(
void* p) {
65 int size()
const {
return size_; }
66 int empty()
const {
return size_ == 0; }
70 const Literal*
const end()
const {
return &(literals_[size_]); }
86 return absl::Span<const Literal>(&(literals_[1]), size_ - 1);
90 absl::Span<const Literal>
AsSpan()
const {
91 return absl::Span<const Literal>(&(literals_[0]), size_);
117 Literal* literals() {
return &(literals_[0]); }
122 void Clear() { size_ = 0; }
126 void Rewrite(absl::Span<const Literal> new_clause) {
128 for (
const Literal l : new_clause) literals_[size_++] = l;
135 Literal literals_[0];
166 void Resize(
int num_variables);
170 absl::Span<const Literal>
Reason(
const Trail& trail,
171 int trail_index)
const final;
180 bool AddClause(absl::Span<const Literal> literals);
224 return &clauses_info_;
230 return num_inspected_clause_literals_;
240 drat_proof_handler_ = drat_proof_handler;
247 for (; to_minimize_index_ < clauses_.size(); ++to_minimize_index_) {
248 if (!clauses_[to_minimize_index_]->IsAttached())
continue;
250 return clauses_[to_minimize_index_++];
278 SatClause* clause, absl::Span<const Literal> new_clause);
313 return watchers_on_false_[false_literal.
Index()];
323 bool PropagateOnFalse(
Literal false_literal,
Trail* trail);
337 std::vector<SatClause*> reasons_;
342 bool is_clean_ =
true;
347 int64 num_inspected_clauses_;
348 int64 num_inspected_clause_literals_;
349 int64 num_watched_clauses_;
353 bool all_clauses_are_attached_ =
true;
362 std::vector<SatClause*> clauses_;
364 int to_minimize_index_ = 0;
367 absl::flat_hash_map<SatClause*, ClauseInfo> clauses_info_;
393 if (p.first > p.second) std::swap(p.first, p.second);
394 if (set_.find(p) == set_.end()) {
396 newly_added_.push_back(c);
403 const std::vector<BinaryClause>&
newly_added()
const {
return newly_added_; }
407 absl::flat_hash_set<std::pair<int, int>> set_;
408 std::vector<BinaryClause> newly_added_;
460 stats_(
"BinaryImplicationGraph"),
470 LOG(INFO) <<
"num_redundant_implications " << num_redundant_implications_;
476 absl::Span<const Literal>
Reason(
const Trail& trail,
477 int trail_index)
const final;
480 void Resize(
int num_variables);
483 bool IsEmpty() {
return num_implications_ == 0 && at_most_ones_.
empty(); }
504 ABSL_MUST_USE_RESULT
bool AddAtMostOne(absl::Span<const Literal> at_most_one);
517 std::vector<Literal>* c);
521 const Trail& trail, std::vector<Literal>* c,
543 bool IsDag()
const {
return is_dag_; }
549 return reverse_topological_order_;
555 return implications_[l.
Index()];
562 if (l.
Index() >= representative_of_.
size())
return l;
589 int64 max_num_explored_nodes = 1e8);
601 const std::vector<Literal>& literals,
602 const std::vector<double>& lp_values);
622 CHECK_EQ(num_redundant_literals_ % 2, 0);
623 return num_redundant_literals_;
628 return num_redundant_implications_;
645 template <
typename Output>
650 absl::flat_hash_set<std::pair<LiteralIndex, LiteralIndex>>
652 for (LiteralIndex i(0); i < implications_.size(); ++i) {
654 for (
const Literal b : implications_[i]) {
660 duplicate_detection.insert({a.Index(), b.Index()}).second) {
661 out->AddBinaryClause(
a,
b);
668 drat_proof_handler_ = drat_proof_handler;
676 reasons_[trail_index] = new_reason.
Negated();
694 return estimated_sizes_[
literal.Index()];
707 BooleanVariable
var, std::deque<std::vector<Literal>>* postsolve_clauses);
716 bool FixLiteral(
Literal true_literal);
722 bool PropagateOnTrue(
Literal true_literal,
Trail* trail);
725 void RemoveRedundantLiterals(std::vector<Literal>* conflict);
729 void MarkDescendants(
Literal root);
735 std::vector<Literal> ExpandAtMostOne(
736 const absl::Span<const Literal> at_most_one);
739 std::vector<Literal> ExpandAtMostOneWithWeight(
740 const absl::Span<const Literal> at_most_one,
748 bool CleanUpAndAddAtMostOnes(
const int base_index);
758 std::deque<Literal> reasons_;
771 int64 num_implications_ = 0;
781 std::vector<Literal> at_most_one_buffer_;
784 std::vector<std::vector<Literal>> tmp_cuts_;
787 int64 num_propagations_ = 0;
788 int64 num_inspections_ = 0;
789 int64 num_minimization_ = 0;
790 int64 num_literals_removed_ = 0;
791 int64 num_redundant_implications_ = 0;
792 int64 num_redundant_literals_ = 0;
802 std::vector<Literal> dfs_stack_;
806 int64 work_done_in_mark_descendants_ = 0;
809 bool is_dag_ =
false;
810 std::vector<LiteralIndex> reverse_topological_order_;
815 std::vector<Literal> direct_implications_;
816 std::vector<Literal> direct_implications_of_negated_literal_;
822 int num_processed_fixed_variables_ = 0;
830 #endif // OR_TOOLS_SAT_CLAUSE_H_