18 #include <type_traits>
43 const std::vector<IntegerVariable>& vars) {
44 std::vector<IntegerVariable> result(vars.size());
45 for (
int i = 0; i < vars.size(); ++i) {
57 <<
"Domain too large for full encoding.";
70 tmp_values_.push_back(v);
73 for (
const IntegerValue v : tmp_values_) {
85 if (
index >= is_fully_encoded_.
size())
return false;
88 if (is_fully_encoded_[
index])
return true;
95 const int64 initial_domain_size = (*domains_)[
var].Size();
96 if (equality_by_var_[
index].size() < initial_domain_size)
return false;
105 const auto& ref = equality_by_var_[
index];
109 if (i < ref.size() && v == ref[i].value) {
114 if (i == ref.size()) {
115 is_fully_encoded_[
index] =
true;
117 return is_fully_encoded_[
index];
120 std::vector<IntegerEncoder::ValueLiteralPair>
126 std::vector<IntegerEncoder::ValueLiteralPair>
130 if (
index >= equality_by_var_.size())
return {};
133 std::vector<ValueLiteralPair>& ref = equality_by_var_[
index];
134 for (
int i = 0; i < ref.size(); ++i) {
143 ref[new_size++] = pair;
145 ref.resize(new_size);
146 std::sort(ref.begin(), ref.end());
148 std::vector<IntegerEncoder::ValueLiteralPair> result = ref;
150 std::reverse(result.begin(), result.end());
159 void IntegerEncoder::AddImplications(
160 const std::map<IntegerValue, Literal>& map,
161 std::map<IntegerValue, Literal>::const_iterator it,
163 if (!add_implications_)
return;
169 if (after_it != map.end()) {
171 {after_it->second.Negated(), associated_lit});
175 if (it != map.begin()) {
179 {associated_lit.
Negated(), before_it->second});
185 add_implications_ =
true;
186 for (
const std::map<IntegerValue, Literal>& encoding : encoding_by_var_) {
188 for (
const auto value_literal : encoding) {
189 const Literal lit = value_literal.second;
194 previous = lit.
Index();
201 const IntegerVariable
var(i_lit.
var);
202 IntegerValue after(i_lit.
bound);
203 IntegerValue before(i_lit.
bound - 1);
208 if (before > previous && before <
interval.start) before = previous;
218 if (i_lit.
bound <= (*domains_)[i_lit.
var].Min()) {
221 if (i_lit.
bound > (*domains_)[i_lit.
var].Max()) {
233 ++num_created_variables_;
240 VLOG(1) <<
"Created a fixed literal for no reason!";
246 std::pair<PositiveOnlyIndex, IntegerValue> PositiveVarKey(IntegerVariable
var,
247 IntegerValue
value) {
254 IntegerVariable
var, IntegerValue
value)
const {
256 equality_to_associated_literal_.find(PositiveVarKey(
var,
value));
257 if (it != equality_to_associated_literal_.end()) {
258 return it->second.Index();
264 IntegerVariable
var, IntegerValue
value) {
267 equality_to_associated_literal_.find(PositiveVarKey(
var,
value));
268 if (it != equality_to_associated_literal_.end()) {
282 ++num_created_variables_;
292 VLOG(1) <<
"Created a fixed literal for no reason!";
299 const auto& domain = (*domains_)[i_lit.
var];
300 const IntegerValue
min(domain.Min());
301 const IntegerValue
max(domain.Max());
308 HalfAssociateGivenLiteral(pair.first,
literal);
309 HalfAssociateGivenLiteral(pair.second,
literal.Negated());
314 if (pair.first.bound ==
max) {
317 if (-pair.second.bound ==
min) {
325 IntegerValue
value) {
330 if (
value == 1 && domain.
Min() >= 0 && domain.
Max() <= 1) {
338 if (
value == -1 && domain.
Min() >= -1 && domain.
Max() <= 0) {
349 const auto insert_result = equality_to_associated_literal_.insert(
351 if (!insert_result.second) {
372 if (
index >= equality_by_var_.size()) {
373 equality_by_var_.resize(
index.value() + 1);
376 equality_by_var_[
index].push_back(
411 const int new_size = 1 +
literal.Index().value();
412 if (new_size > full_reverse_encoding_.
size()) {
413 full_reverse_encoding_.
resize(new_size);
423 void IntegerEncoder::HalfAssociateGivenLiteral(
IntegerLiteral i_lit,
426 const int new_size = 1 +
literal.Index().value();
427 if (new_size > reverse_encoding_.
size()) {
428 reverse_encoding_.
resize(new_size);
430 if (new_size > full_reverse_encoding_.
size()) {
431 full_reverse_encoding_.
resize(new_size);
435 if (i_lit.
var >= encoding_by_var_.size()) {
436 encoding_by_var_.resize(i_lit.
var.value() + 1);
438 auto& var_encoding = encoding_by_var_[i_lit.
var];
439 auto insert_result = var_encoding.insert({i_lit.
bound,
literal});
440 if (insert_result.second) {
441 AddImplications(var_encoding, insert_result.first,
literal);
444 newly_fixed_integer_literals_.push_back(i_lit);
452 const Literal associated(insert_result.first->second);
462 if (i.
var >= encoding_by_var_.size())
return false;
463 const std::map<IntegerValue, Literal>& encoding = encoding_by_var_[i.
var];
464 return encoding.find(i.
bound) != encoding.end();
469 const std::map<IntegerValue, Literal>& encoding = encoding_by_var_[i.
var];
470 const auto result = encoding.find(i.
bound);
472 return result->second.Index();
480 const std::map<IntegerValue, Literal>& encoding = encoding_by_var_[i.
var];
481 auto after_it = encoding.upper_bound(i.
bound);
484 *
bound = after_it->first;
485 return after_it->second.Index();
489 if (parameters_.log_search_progress() && num_decisions_to_break_loop_ > 0) {
490 LOG(
INFO) <<
"Num decisions to break propagation loop: "
491 << num_decisions_to_break_loop_;
502 if (level > integer_search_levels_.size()) {
503 integer_search_levels_.push_back(integer_trail_.size());
504 reason_decision_levels_.push_back(literals_reason_starts_.size());
510 var_to_current_lb_interval_index_.
SetLevel(level);
523 if (!
Enqueue(i_lit, {}, {}))
return false;
529 if (!
Enqueue(i_lit, {}, {}))
return false;
531 integer_literal_to_fix_.clear();
533 for (
const Literal lit : literal_to_fix_) {
538 literal_to_fix_.clear();
543 while (propagation_trail_index_ < trail->
Index()) {
549 if (!EnqueueAssociatedIntegerLiteral(i_lit,
literal)) {
562 var_to_current_lb_interval_index_.
SetLevel(level);
566 if (level < first_level_without_full_propagation_) {
567 first_level_without_full_propagation_ = -1;
572 if (level >= integer_search_levels_.size())
return;
573 const int target = integer_search_levels_[level];
574 integer_search_levels_.resize(level);
576 CHECK_LE(target, integer_trail_.size());
578 for (
int index = integer_trail_.size() - 1;
index >= target; --
index) {
579 const TrailEntry& entry = integer_trail_[
index];
580 if (entry.var < 0)
continue;
581 vars_[entry.var].current_trail_index = entry.prev_trail_index;
582 vars_[entry.var].current_bound =
583 integer_trail_[entry.prev_trail_index].bound;
585 integer_trail_.resize(target);
588 const int old_size = reason_decision_levels_[level];
589 reason_decision_levels_.resize(level);
590 if (old_size < literals_reason_starts_.size()) {
591 literals_reason_buffer_.resize(literals_reason_starts_[old_size]);
593 const int bound_start = bounds_reason_starts_[old_size];
594 bounds_reason_buffer_.resize(bound_start);
595 if (bound_start < trail_index_reason_buffer_.size()) {
596 trail_index_reason_buffer_.resize(bound_start);
599 literals_reason_starts_.resize(old_size);
600 bounds_reason_starts_.resize(old_size);
606 const int size = 2 * num_vars;
608 is_ignored_literals_.
reserve(size);
609 integer_trail_.reserve(size);
611 var_trail_index_cache_.
reserve(size);
612 tmp_var_to_trail_index_in_queue_.
reserve(size);
616 IntegerValue upper_bound) {
621 DCHECK(integer_search_levels_.empty());
624 const IntegerVariable i(vars_.
size());
626 vars_.
push_back({lower_bound,
static_cast<int>(integer_trail_.size())});
627 integer_trail_.push_back({lower_bound, i});
635 vars_.
push_back({-upper_bound,
static_cast<int>(integer_trail_.size())});
636 integer_trail_.push_back({-upper_bound,
NegationOf(i)});
637 domains_->
push_back(
Domain(-upper_bound.value(), -lower_bound.value()));
639 var_trail_index_cache_.
resize(vars_.
size(), integer_trail_.size());
640 tmp_var_to_trail_index_in_queue_.
resize(vars_.
size(), 0);
651 IntegerValue(domain.
Max()));
657 return (*domains_)[
var];
665 if (old_domain == domain)
return true;
667 if (domain.
IsEmpty())
return false;
668 (*domains_)[
var] = domain;
671 var_to_current_lb_interval_index_.
Set(
var, 0);
690 if (i == domain.
NumIntervals() || pair.value < domain[i].start) {
700 <<
"Domain intersection fixed " << num_fixed
701 <<
" equality literal corresponding to values outside the new domain.";
708 IntegerValue
value) {
712 insert.first->second = new_var;
719 return insert.first->second;
725 return (constant_map_.size() + 1) / 2;
729 int threshold)
const {
733 const int index_in_queue = tmp_var_to_trail_index_in_queue_[
var];
734 if (threshold <= index_in_queue) {
735 if (index_in_queue !=
kint32max) has_dependency_ =
true;
740 int trail_index = vars_[
var].current_trail_index;
743 if (trail_index > threshold) {
744 const int cached_index = var_trail_index_cache_[
var];
745 if (cached_index >= threshold && cached_index < trail_index &&
746 integer_trail_[cached_index].
var ==
var) {
747 trail_index = cached_index;
751 while (trail_index >= threshold) {
752 trail_index = integer_trail_[trail_index].prev_trail_index;
753 if (trail_index >= var_trail_index_cache_threshold_) {
754 var_trail_index_cache_[
var] = trail_index;
758 const int num_vars = vars_.
size();
759 return trail_index < num_vars ? -1 : trail_index;
762 int IntegerTrail::FindLowestTrailIndexThatExplainBound(
766 int trail_index = vars_[i_lit.
var].current_trail_index;
774 const int cached_index = var_trail_index_cache_[i_lit.
var];
775 if (cached_index < trail_index) {
776 const TrailEntry& entry = integer_trail_[cached_index];
777 if (entry.var == i_lit.
var && entry.bound >= i_lit.
bound) {
778 trail_index = cached_index;
783 int prev_trail_index = trail_index;
785 if (trail_index >= var_trail_index_cache_threshold_) {
786 var_trail_index_cache_[i_lit.
var] = trail_index;
788 const TrailEntry& entry = integer_trail_[trail_index];
789 if (entry.bound == i_lit.
bound)
return trail_index;
790 if (entry.bound < i_lit.
bound)
return prev_trail_index;
791 prev_trail_index = trail_index;
792 trail_index = entry.prev_trail_index;
798 IntegerValue slack, absl::Span<const IntegerValue> coeffs,
799 std::vector<IntegerLiteral>* reason)
const {
801 if (slack == 0)
return;
802 const int size = reason->size();
803 tmp_indices_.resize(size);
804 for (
int i = 0; i < size; ++i) {
807 tmp_indices_[i] = vars_[(*reason)[i].var].current_trail_index;
813 for (
const int i : tmp_indices_) {
815 integer_trail_[i].
bound));
820 IntegerValue slack, absl::Span<const IntegerValue> coeffs,
821 absl::Span<const IntegerVariable> vars,
822 std::vector<IntegerLiteral>* reason)
const {
823 tmp_indices_.clear();
824 for (
const IntegerVariable
var : vars) {
825 tmp_indices_.push_back(vars_[
var].current_trail_index);
828 for (
const int i : tmp_indices_) {
830 integer_trail_[i].
bound));
835 absl::Span<const IntegerValue> coeffs,
836 std::vector<int>* trail_indices)
const {
838 DCHECK(relax_heap_.empty());
845 const int size = coeffs.size();
846 const int num_vars = vars_.
size();
847 for (
int i = 0; i < size; ++i) {
848 const int index = (*trail_indices)[i];
851 if (
index < num_vars)
continue;
854 const IntegerValue coeff = coeffs[i];
856 (*trail_indices)[new_size++] =
index;
863 const TrailEntry& entry = integer_trail_[
index];
865 index <= tmp_var_to_trail_index_in_queue_[entry.var]) {
866 (*trail_indices)[new_size++] =
index;
871 const TrailEntry& previous_entry = integer_trail_[entry.prev_trail_index];
873 CapProd(coeff.value(), (entry.bound - previous_entry.bound).value());
875 (*trail_indices)[new_size++] =
index;
879 relax_heap_.push_back({
index, coeff, diff});
881 trail_indices->resize(new_size);
882 std::make_heap(relax_heap_.begin(), relax_heap_.end());
884 while (slack > 0 && !relax_heap_.empty()) {
885 const RelaxHeapEntry heap_entry = relax_heap_.front();
886 std::pop_heap(relax_heap_.begin(), relax_heap_.end());
887 relax_heap_.pop_back();
890 if (heap_entry.diff > slack) {
891 trail_indices->push_back(heap_entry.index);
896 slack -= heap_entry.diff;
897 const int index = integer_trail_[heap_entry.index].prev_trail_index;
900 if (
index < num_vars)
continue;
901 if (heap_entry.coeff > slack) {
902 trail_indices->push_back(
index);
905 const TrailEntry& entry = integer_trail_[
index];
907 index <= tmp_var_to_trail_index_in_queue_[entry.var]) {
908 trail_indices->push_back(
index);
912 const TrailEntry& previous_entry = integer_trail_[entry.prev_trail_index];
914 (entry.bound - previous_entry.bound).value());
916 trail_indices->push_back(
index);
919 relax_heap_.push_back({
index, heap_entry.coeff, diff});
920 std::push_heap(relax_heap_.begin(), relax_heap_.end());
925 for (
const RelaxHeapEntry& entry : relax_heap_) {
926 trail_indices->push_back(entry.index);
932 std::vector<IntegerLiteral>* reason)
const {
936 (*reason)[new_size++] =
literal;
938 reason->resize(new_size);
941 std::vector<Literal>* IntegerTrail::InitializeConflict(
942 IntegerLiteral integer_literal,
const LazyReasonFunction& lazy_reason,
943 absl::Span<const Literal> literals_reason,
944 absl::Span<const IntegerLiteral> bounds_reason) {
945 DCHECK(tmp_queue_.empty());
947 if (lazy_reason ==
nullptr) {
948 conflict->assign(literals_reason.begin(), literals_reason.end());
949 const int num_vars = vars_.
size();
951 const int trail_index = FindLowestTrailIndexThatExplainBound(
literal);
952 if (trail_index >= num_vars) tmp_queue_.push_back(trail_index);
957 lazy_reason(integer_literal, integer_trail_.size(), conflict, &tmp_queue_);
964 std::string ReasonDebugString(absl::Span<const Literal> literal_reason,
965 absl::Span<const IntegerLiteral> integer_reason) {
966 std::string result =
"literals:{";
967 for (
const Literal l : literal_reason) {
968 if (result.back() !=
'{') result +=
",";
969 result += l.DebugString();
971 result +=
"} bounds:{";
972 for (
const IntegerLiteral l : integer_reason) {
973 if (result.back() !=
'{') result +=
",";
974 result += l.DebugString();
982 std::string IntegerTrail::DebugString() {
983 std::string result =
"trail:{";
984 const int num_vars = vars_.
size();
986 std::min(num_vars + 30,
static_cast<int>(integer_trail_.size()));
987 for (
int i = num_vars; i < limit; ++i) {
988 if (result.back() !=
'{') result +=
",";
991 integer_trail_[i].
bound)
994 if (limit < integer_trail_.size()) {
1002 absl::Span<const Literal> literal_reason,
1003 absl::Span<const IntegerLiteral> integer_reason) {
1004 return EnqueueInternal(i_lit,
nullptr, literal_reason, integer_reason,
1005 integer_trail_.size());
1009 absl::Span<const Literal> literal_reason,
1010 absl::Span<const IntegerLiteral> integer_reason,
1011 int trail_index_with_same_reason) {
1012 return EnqueueInternal(i_lit,
nullptr, literal_reason, integer_reason,
1013 trail_index_with_same_reason);
1018 return EnqueueInternal(i_lit, lazy_reason, {}, {}, integer_trail_.size());
1021 bool IntegerTrail::ReasonIsValid(
1022 absl::Span<const Literal> literal_reason,
1023 absl::Span<const IntegerLiteral> integer_reason) {
1025 for (
const Literal lit : literal_reason) {
1028 for (
const IntegerLiteral i_lit : integer_reason) {
1029 if (i_lit.
bound > vars_[i_lit.
var].current_bound) {
1032 LOG(
INFO) <<
"Reason " << i_lit <<
" is not true!"
1033 <<
" optional variable:" << i_lit.
var
1036 <<
" current_lb:" << vars_[i_lit.
var].current_bound;
1038 LOG(
INFO) <<
"Reason " << i_lit <<
" is not true!"
1039 <<
" non-optional variable:" << i_lit.
var
1040 <<
" current_lb:" << vars_[i_lit.
var].current_bound;
1048 if (!integer_search_levels_.empty()) {
1049 int num_literal_assigned_after_root_node = 0;
1050 for (
const Literal lit : literal_reason) {
1051 if (trail_->
Info(lit.Variable()).
level > 0) {
1052 num_literal_assigned_after_root_node++;
1055 for (
const IntegerLiteral i_lit : integer_reason) {
1057 num_literal_assigned_after_root_node++;
1060 DLOG_IF(
INFO, num_literal_assigned_after_root_node == 0)
1061 <<
"Propagating a literal with no reason at a positive level!\n"
1062 <<
"level:" << integer_search_levels_.size() <<
" "
1063 << ReasonDebugString(literal_reason, integer_reason) <<
"\n"
1072 absl::Span<const IntegerLiteral> integer_reason) {
1073 EnqueueLiteralInternal(
literal,
nullptr, literal_reason, integer_reason);
1076 void IntegerTrail::EnqueueLiteralInternal(
1078 absl::Span<const Literal> literal_reason,
1079 absl::Span<const IntegerLiteral> integer_reason) {
1081 DCHECK(lazy_reason !=
nullptr ||
1082 ReasonIsValid(literal_reason, integer_reason));
1083 if (integer_search_levels_.empty()) {
1090 if (!integer_search_levels_.empty() && integer_reason.empty() &&
1091 literal_reason.empty() && lazy_reason ==
nullptr) {
1092 literal_to_fix_.push_back(
literal);
1095 const int trail_index = trail_->
Index();
1096 if (trail_index >= boolean_trail_index_to_integer_one_.size()) {
1097 boolean_trail_index_to_integer_one_.resize(trail_index + 1);
1099 boolean_trail_index_to_integer_one_[trail_index] = integer_trail_.size();
1101 int reason_index = literals_reason_starts_.size();
1102 if (lazy_reason !=
nullptr) {
1103 if (integer_trail_.size() >= lazy_reasons_.size()) {
1104 lazy_reasons_.resize(integer_trail_.size() + 1,
nullptr);
1106 lazy_reasons_[integer_trail_.size()] = lazy_reason;
1110 literals_reason_starts_.push_back(literals_reason_buffer_.size());
1111 literals_reason_buffer_.insert(literals_reason_buffer_.end(),
1112 literal_reason.begin(),
1113 literal_reason.end());
1114 bounds_reason_starts_.push_back(bounds_reason_buffer_.size());
1115 bounds_reason_buffer_.insert(bounds_reason_buffer_.end(),
1116 integer_reason.begin(), integer_reason.end());
1119 integer_trail_.push_back({IntegerValue(0),
1131 const int num_vars = vars_.
size();
1132 return (!integer_search_levels_.empty() &&
1133 integer_trail_.size() - integer_search_levels_.back() >
1135 parameters_.search_branching() != SatParameters::FIXED_SEARCH);
1142 ++num_decisions_to_break_loop_;
1143 std::vector<IntegerVariable> vars;
1144 for (
int i = integer_search_levels_.back(); i < integer_trail_.size(); ++i) {
1145 const IntegerVariable
var = integer_trail_[i].var;
1148 vars.push_back(
var);
1151 std::sort(vars.begin(), vars.end());
1152 IntegerVariable best_var = vars[0];
1155 for (
int i = 1; i < vars.size(); ++i) {
1156 if (vars[i] != vars[i - 1]) {
1160 if (count > best_count) {
1170 return first_level_without_full_propagation_ != -1;
1174 for (IntegerVariable
var(0);
var < vars_.
size();
var += 2) {
1181 bool IntegerTrail::EnqueueInternal(
1183 absl::Span<const Literal> literal_reason,
1184 absl::Span<const IntegerLiteral> integer_reason,
1185 int trail_index_with_same_reason) {
1186 DCHECK(lazy_reason !=
nullptr ||
1187 ReasonIsValid(literal_reason, integer_reason));
1189 const IntegerVariable
var(i_lit.
var);
1197 if (i_lit.
bound <= vars_[
var].current_bound)
return true;
1206 if ((*domains_)[
var].NumIntervals() > 1) {
1207 const auto& domain = (*domains_)[
var];
1209 const int size = domain.NumIntervals();
1210 while (index < size && i_lit.bound > domain[
index].end) {
1213 if (
index == size) {
1216 var_to_current_lb_interval_index_.
Set(
var,
index);
1227 Literal(is_ignored_literals_[
var]))) {
1230 auto* conflict = InitializeConflict(i_lit, lazy_reason, literal_reason,
1233 conflict->push_back(Literal(is_ignored_literals_[
var]));
1236 const int trail_index = FindLowestTrailIndexThatExplainBound(ub_reason);
1237 const int num_vars = vars_.
size();
1238 if (trail_index >= num_vars) tmp_queue_.push_back(trail_index);
1240 MergeReasonIntoInternal(conflict);
1247 const Literal is_ignored = Literal(is_ignored_literals_[
var]);
1248 if (integer_search_levels_.empty()) {
1255 if (lazy_reason !=
nullptr) {
1256 lazy_reason(i_lit, integer_trail_.size(), &lazy_reason_literals_,
1257 &lazy_reason_trail_indices_);
1258 std::vector<IntegerLiteral> temp;
1259 for (
const int trail_index : lazy_reason_trail_indices_) {
1260 const TrailEntry& entry = integer_trail_[trail_index];
1261 temp.push_back(IntegerLiteral(entry.var, entry.bound));
1269 bounds_reason_buffer_.push_back(ub_reason);
1289 if (i_lit.
bound - lb < (ub - lb) / 2) {
1290 if (first_level_without_full_propagation_ == -1) {
1298 for (SparseBitset<IntegerVariable>* bitset : watchers_) {
1299 bitset->Set(i_lit.
var);
1302 if (!integer_search_levels_.empty() && integer_reason.empty() &&
1303 literal_reason.empty() && lazy_reason ==
nullptr &&
1304 trail_index_with_same_reason >= integer_trail_.size()) {
1305 integer_literal_to_fix_.push_back(i_lit);
1321 const LiteralIndex literal_index =
1324 const Literal to_enqueue = Literal(literal_index);
1326 auto* conflict = InitializeConflict(i_lit, lazy_reason, literal_reason,
1328 conflict->push_back(to_enqueue);
1329 MergeReasonIntoInternal(conflict);
1339 EnqueueLiteralInternal(to_enqueue, lazy_reason, literal_reason,
1342 return EnqueueAssociatedIntegerLiteral(i_lit, to_enqueue);
1346 if (integer_search_levels_.empty()) {
1352 const int trail_index = trail_->
Index();
1353 if (trail_index >= boolean_trail_index_to_integer_one_.size()) {
1354 boolean_trail_index_to_integer_one_.resize(trail_index + 1);
1356 boolean_trail_index_to_integer_one_[trail_index] =
1357 trail_index_with_same_reason;
1364 if (integer_search_levels_.empty()) {
1365 ++num_level_zero_enqueues_;
1366 vars_[i_lit.
var].current_bound = i_lit.
bound;
1367 integer_trail_[i_lit.
var.value()].bound = i_lit.
bound;
1378 int reason_index = literals_reason_starts_.size();
1379 if (lazy_reason !=
nullptr) {
1380 if (integer_trail_.size() >= lazy_reasons_.size()) {
1381 lazy_reasons_.resize(integer_trail_.size() + 1,
nullptr);
1383 lazy_reasons_[integer_trail_.size()] = lazy_reason;
1385 }
else if (trail_index_with_same_reason >= integer_trail_.size()) {
1387 literals_reason_starts_.push_back(literals_reason_buffer_.size());
1388 if (!literal_reason.empty()) {
1389 literals_reason_buffer_.insert(literals_reason_buffer_.end(),
1390 literal_reason.begin(),
1391 literal_reason.end());
1393 bounds_reason_starts_.push_back(bounds_reason_buffer_.size());
1394 if (!integer_reason.empty()) {
1395 bounds_reason_buffer_.insert(bounds_reason_buffer_.end(),
1396 integer_reason.begin(),
1397 integer_reason.end());
1400 reason_index = integer_trail_[trail_index_with_same_reason].reason_index;
1403 const int prev_trail_index = vars_[i_lit.
var].current_trail_index;
1404 integer_trail_.push_back({i_lit.
bound,
1409 vars_[i_lit.
var].current_bound = i_lit.
bound;
1410 vars_[i_lit.
var].current_trail_index = integer_trail_.size() - 1;
1414 bool IntegerTrail::EnqueueAssociatedIntegerLiteral(IntegerLiteral i_lit,
1415 Literal literal_reason) {
1416 DCHECK(ReasonIsValid({literal_reason.Negated()}, {}));
1420 if (i_lit.bound <= vars_[i_lit.var].current_bound)
return true;
1428 return Enqueue(i_lit, {literal_reason.Negated()}, {});
1432 for (SparseBitset<IntegerVariable>* bitset : watchers_) {
1433 bitset->Set(i_lit.var);
1437 if (integer_search_levels_.empty()) {
1438 vars_[i_lit.var].current_bound = i_lit.bound;
1439 integer_trail_[i_lit.var.value()].bound = i_lit.bound;
1450 const int reason_index = literals_reason_starts_.size();
1451 CHECK_EQ(reason_index, bounds_reason_starts_.size());
1452 literals_reason_starts_.push_back(literals_reason_buffer_.size());
1453 bounds_reason_starts_.push_back(bounds_reason_buffer_.size());
1454 literals_reason_buffer_.push_back(literal_reason.Negated());
1456 const int prev_trail_index = vars_[i_lit.var].current_trail_index;
1457 integer_trail_.push_back({i_lit.bound,
1462 vars_[i_lit.var].current_bound = i_lit.bound;
1463 vars_[i_lit.var].current_trail_index = integer_trail_.size() - 1;
1467 void IntegerTrail::ComputeLazyReasonIfNeeded(
int trail_index)
const {
1468 const int reason_index = integer_trail_[trail_index].reason_index;
1469 if (reason_index == -1) {
1470 const TrailEntry& entry = integer_trail_[trail_index];
1471 const IntegerLiteral
literal(entry.var, entry.bound);
1472 lazy_reasons_[trail_index](
literal, trail_index, &lazy_reason_literals_,
1473 &lazy_reason_trail_indices_);
1477 absl::Span<const int> IntegerTrail::Dependencies(
int trail_index)
const {
1478 const int reason_index = integer_trail_[trail_index].reason_index;
1479 if (reason_index == -1) {
1480 return absl::Span<const int>(lazy_reason_trail_indices_);
1483 const int start = bounds_reason_starts_[reason_index];
1484 const int end = reason_index + 1 < bounds_reason_starts_.size()
1485 ? bounds_reason_starts_[reason_index + 1]
1486 : bounds_reason_buffer_.size();
1487 if (start == end)
return {};
1494 if (end > trail_index_reason_buffer_.size()) {
1495 trail_index_reason_buffer_.resize(end, -1);
1497 if (trail_index_reason_buffer_[start] == -1) {
1498 int new_end = start;
1499 const int num_vars = vars_.
size();
1500 for (
int i = start; i < end; ++i) {
1502 FindLowestTrailIndexThatExplainBound(bounds_reason_buffer_[i]);
1503 if (dep >= num_vars) {
1504 trail_index_reason_buffer_[new_end++] = dep;
1507 return absl::Span<const int>(&trail_index_reason_buffer_[start],
1513 return absl::Span<const int>(&trail_index_reason_buffer_[start],
1518 void IntegerTrail::AppendLiteralsReason(
int trail_index,
1519 std::vector<Literal>* output)
const {
1521 const int reason_index = integer_trail_[trail_index].reason_index;
1522 if (reason_index == -1) {
1523 for (
const Literal l : lazy_reason_literals_) {
1524 if (!added_variables_[l.Variable()]) {
1525 added_variables_.
Set(l.Variable());
1526 output->push_back(l);
1532 const int start = literals_reason_starts_[reason_index];
1533 const int end = reason_index + 1 < literals_reason_starts_.size()
1534 ? literals_reason_starts_[reason_index + 1]
1535 : literals_reason_buffer_.size();
1536 for (
int i = start; i < end; ++i) {
1537 const Literal l = literals_reason_buffer_[i];
1538 if (!added_variables_[l.Variable()]) {
1539 added_variables_.
Set(l.Variable());
1540 output->push_back(l);
1546 std::vector<Literal> reason;
1554 std::vector<Literal>* output)
const {
1555 DCHECK(tmp_queue_.empty());
1556 const int num_vars = vars_.
size();
1558 const int trail_index = FindLowestTrailIndexThatExplainBound(
literal);
1562 if (trail_index >= num_vars) tmp_queue_.push_back(trail_index);
1564 return MergeReasonIntoInternal(output);
1569 void IntegerTrail::MergeReasonIntoInternal(std::vector<Literal>* output)
const {
1572 DCHECK(std::all_of(tmp_var_to_trail_index_in_queue_.
begin(),
1573 tmp_var_to_trail_index_in_queue_.
end(),
1574 [](
int v) { return v == 0; }));
1577 for (
const Literal l : *output) {
1578 added_variables_.
Set(l.Variable());
1583 for (
const int trail_index : tmp_queue_) {
1585 DCHECK_LT(trail_index, integer_trail_.size());
1586 const TrailEntry& entry = integer_trail_[trail_index];
1587 tmp_var_to_trail_index_in_queue_[entry.var] =
1588 std::max(tmp_var_to_trail_index_in_queue_[entry.var], trail_index);
1593 std::make_heap(tmp_queue_.begin(), tmp_queue_.end());
1598 tmp_to_clear_.clear();
1599 while (!tmp_queue_.empty()) {
1600 const int trail_index = tmp_queue_.front();
1601 const TrailEntry& entry = integer_trail_[trail_index];
1602 std::pop_heap(tmp_queue_.begin(), tmp_queue_.end());
1603 tmp_queue_.pop_back();
1608 if (tmp_var_to_trail_index_in_queue_[entry.var] != trail_index) {
1615 var_trail_index_cache_threshold_ = trail_index;
1620 const LiteralIndex associated_lit =
1622 IntegerVariable(entry.var), entry.bound));
1625 const int reason_index = integer_trail_[trail_index].reason_index;
1628 const int start = literals_reason_starts_[reason_index];
1629 const int end = reason_index + 1 < literals_reason_starts_.size()
1630 ? literals_reason_starts_[reason_index + 1]
1631 : literals_reason_buffer_.size();
1633 CHECK_EQ(literals_reason_buffer_[start],
1634 Literal(associated_lit).Negated());
1637 const int start = bounds_reason_starts_[reason_index];
1638 const int end = reason_index + 1 < bounds_reason_starts_.size()
1639 ? bounds_reason_starts_[reason_index + 1]
1640 : bounds_reason_buffer_.size();
1654 tmp_var_to_trail_index_in_queue_[entry.var] = 0;
1655 has_dependency_ =
false;
1657 ComputeLazyReasonIfNeeded(trail_index);
1658 AppendLiteralsReason(trail_index, output);
1659 for (
const int next_trail_index : Dependencies(trail_index)) {
1660 if (next_trail_index < 0)
break;
1661 DCHECK_LT(next_trail_index, trail_index);
1662 const TrailEntry& next_entry = integer_trail_[next_trail_index];
1668 const int index_in_queue =
1669 tmp_var_to_trail_index_in_queue_[next_entry.var];
1670 if (index_in_queue !=
kint32max) has_dependency_ =
true;
1671 if (next_trail_index > index_in_queue) {
1672 tmp_var_to_trail_index_in_queue_[next_entry.var] = next_trail_index;
1674 std::push_heap(tmp_queue_.begin(), tmp_queue_.end());
1679 if (!has_dependency_) {
1680 tmp_to_clear_.push_back(entry.var);
1681 tmp_var_to_trail_index_in_queue_[entry.var] =
kint32max;
1686 for (
const IntegerVariable
var : tmp_to_clear_) {
1687 tmp_var_to_trail_index_in_queue_[
var] = 0;
1692 int trail_index)
const {
1693 const int index = boolean_trail_index_to_integer_one_[trail_index];
1697 ComputeLazyReasonIfNeeded(
index);
1698 AppendLiteralsReason(
index, reason);
1699 DCHECK(tmp_queue_.empty());
1700 for (
const int prev_trail_index : Dependencies(
index)) {
1701 if (prev_trail_index < 0)
break;
1703 tmp_queue_.push_back(prev_trail_index);
1705 MergeReasonIntoInternal(reason);
1715 const int end = vars_.
size();
1716 for (
int i = integer_trail_.size(); --i >= end;) {
1717 const TrailEntry& entry = integer_trail_[i];
1719 if (tmp_marked_[entry.var])
continue;
1721 tmp_marked_.
Set(entry.var);
1738 &id_to_greatest_common_level_since_last_call_);
1740 queue_by_priority_.resize(2);
1743 void GenericLiteralWatcher::UpdateCallingNeeds(
Trail* trail) {
1745 while (propagation_trail_index_ < trail->
Index()) {
1747 if (
literal.Index() >= literal_to_watcher_.
size())
continue;
1748 for (
const auto entry : literal_to_watcher_[
literal.Index()]) {
1749 if (!in_queue_[entry.id]) {
1750 in_queue_[entry.id] =
true;
1751 queue_by_priority_[id_to_priority_[entry.id]].push_back(entry.id);
1753 if (entry.watch_index >= 0) {
1754 id_to_watch_indices_[entry.id].push_back(entry.watch_index);
1761 if (
var.value() >= var_to_watcher_.
size())
continue;
1762 for (
const auto entry : var_to_watcher_[
var]) {
1763 if (!in_queue_[entry.id]) {
1764 in_queue_[entry.id] =
true;
1765 queue_by_priority_[id_to_priority_[entry.id]].push_back(entry.id);
1767 if (entry.watch_index >= 0) {
1768 id_to_watch_indices_[entry.id].push_back(entry.watch_index);
1774 const std::vector<IntegerVariable>& modified_vars =
1776 for (
const auto&
callback : level_zero_modified_variable_callback_) {
1789 for (
const int id : propagator_ids_to_call_at_level_zero_) {
1790 if (in_queue_[
id])
continue;
1791 in_queue_[id] =
true;
1792 queue_by_priority_[id_to_priority_[id]].push_back(
id);
1796 UpdateCallingNeeds(trail);
1801 for (
int priority = 0; priority < queue_by_priority_.size(); ++priority) {
1808 if (test_limit > 100) {
1813 std::deque<int>& queue = queue_by_priority_[priority];
1814 while (!queue.empty()) {
1815 const int id = queue.front();
1823 id_to_greatest_common_level_since_last_call_[IdType(
id)];
1824 const int high = id_to_level_at_last_call_[id];
1825 if (low < high || level > low) {
1826 id_to_level_at_last_call_[id] = level;
1827 id_to_greatest_common_level_since_last_call_.
MutableRef(IdType(
id)) =
1830 if (low < high) rev->SetLevel(low);
1831 if (level > low) rev->SetLevel(level);
1833 for (
int* rev_int : id_to_reversible_ints_[
id]) {
1834 rev_int_repository_->
SaveState(rev_int);
1841 const int64 old_boolean_timestamp = trail->
Index();
1844 std::vector<int>& watch_indices_ref = id_to_watch_indices_[id];
1846 watch_indices_ref.empty()
1847 ? watchers_[id]->Propagate()
1848 : watchers_[id]->IncrementalPropagate(watch_indices_ref);
1850 watch_indices_ref.clear();
1851 in_queue_[id] =
false;
1857 if (id_to_idempotence_[
id]) {
1861 UpdateCallingNeeds(trail);
1862 watch_indices_ref.clear();
1863 in_queue_[id] =
false;
1868 watch_indices_ref.clear();
1869 in_queue_[id] =
false;
1870 UpdateCallingNeeds(trail);
1876 if (trail->
Index() > old_boolean_timestamp) {
1888 if (integer_trail_->
num_enqueues() > old_integer_timestamp) {
1906 for (std::deque<int>& queue : queue_by_priority_) {
1907 for (
const int id : queue) {
1908 id_to_watch_indices_[id].clear();
1918 in_queue_.assign(watchers_.size(),
false);
1923 const int id = watchers_.size();
1924 watchers_.push_back(propagator);
1925 id_to_level_at_last_call_.push_back(0);
1926 id_to_greatest_common_level_since_last_call_.
GrowByOne();
1927 id_to_reversible_classes_.push_back(std::vector<ReversibleInterface*>());
1928 id_to_reversible_ints_.push_back(std::vector<int*>());
1929 id_to_watch_indices_.push_back(std::vector<int>());
1930 id_to_priority_.push_back(1);
1931 id_to_idempotence_.push_back(
true);
1940 in_queue_.push_back(
true);
1941 queue_by_priority_[1].push_back(
id);
1946 id_to_priority_[id] = priority;
1947 if (priority >= queue_by_priority_.size()) {
1948 queue_by_priority_.resize(priority + 1);
1954 id_to_idempotence_[id] =
false;
1958 propagator_ids_to_call_at_level_zero_.push_back(
id);
1963 id_to_reversible_classes_[id].push_back(rev);
1967 id_to_reversible_ints_[id].push_back(rev);
1971 std::function<void(
Model*)>
1979 std::vector<Literal> clause_to_exclude_solution;
1980 clause_to_exclude_solution.reserve(current_level);
1981 for (
int i = 0; i < current_level; ++i) {
1982 bool include_decision =
true;
1988 encoder->GetIntegerLiterals(decision);
1990 if (integer_trail->IsCurrentlyIgnored(
bound.var)) {
1996 clause_to_exclude_solution.push_back(
1997 integer_trail->IsIgnoredLiteral(
bound.var).Negated());
1998 include_decision =
false;
2002 if (include_decision) {
2003 clause_to_exclude_solution.push_back(decision.
Negated());