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) {
55 CHECK(!(*domains_)[
var].IsEmpty());
56 CHECK_LT((*domains_)[
var].Size(), 100000)
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;
164 DCHECK_EQ(it->second, associated_lit);
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);
204 CHECK_GE(before, (*domains_)[
var].Min());
205 CHECK_LE(after, (*domains_)[
var].Max());
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();
495 if (level > integer_search_levels_.size()) {
496 integer_search_levels_.push_back(integer_trail_.size());
497 reason_decision_levels_.push_back(literals_reason_starts_.size());
503 var_to_current_lb_interval_index_.
SetLevel(level);
516 if (!
Enqueue(i_lit, {}, {}))
return false;
523 while (propagation_trail_index_ < trail->
Index()) {
529 if (!EnqueueAssociatedIntegerLiteral(i_lit,
literal)) {
542 var_to_current_lb_interval_index_.
SetLevel(level);
548 if (level >= integer_search_levels_.size())
return;
549 const int target = integer_search_levels_[level];
550 integer_search_levels_.resize(level);
551 CHECK_GE(target, vars_.
size());
552 CHECK_LE(target, integer_trail_.size());
554 for (
int index = integer_trail_.size() - 1;
index >= target; --
index) {
555 const TrailEntry& entry = integer_trail_[
index];
556 if (entry.var < 0)
continue;
557 vars_[entry.var].current_trail_index = entry.prev_trail_index;
558 vars_[entry.var].current_bound =
559 integer_trail_[entry.prev_trail_index].bound;
561 integer_trail_.resize(target);
564 const int old_size = reason_decision_levels_[level];
565 reason_decision_levels_.resize(level);
566 if (old_size < literals_reason_starts_.size()) {
567 literals_reason_buffer_.resize(literals_reason_starts_[old_size]);
569 const int bound_start = bounds_reason_starts_[old_size];
570 bounds_reason_buffer_.resize(bound_start);
571 if (bound_start < trail_index_reason_buffer_.size()) {
572 trail_index_reason_buffer_.resize(bound_start);
575 literals_reason_starts_.resize(old_size);
576 bounds_reason_starts_.resize(old_size);
582 const int size = 2 * num_vars;
584 is_ignored_literals_.
reserve(size);
585 integer_trail_.reserve(size);
587 var_trail_index_cache_.
reserve(size);
588 tmp_var_to_trail_index_in_queue_.
reserve(size);
592 IntegerValue upper_bound) {
597 DCHECK(integer_search_levels_.empty());
598 DCHECK_EQ(vars_.
size(), integer_trail_.size());
600 const IntegerVariable i(vars_.
size());
602 vars_.
push_back({lower_bound,
static_cast<int>(integer_trail_.size())});
603 integer_trail_.push_back({lower_bound, i});
611 vars_.
push_back({-upper_bound,
static_cast<int>(integer_trail_.size())});
612 integer_trail_.push_back({-upper_bound,
NegationOf(i)});
613 domains_->
push_back(
Domain(-upper_bound.value(), -lower_bound.value()));
615 var_trail_index_cache_.
resize(vars_.
size(), integer_trail_.size());
616 tmp_var_to_trail_index_in_queue_.
resize(vars_.
size(), 0);
627 IntegerValue(domain.
Max()));
633 return (*domains_)[
var];
641 if (old_domain == domain)
return true;
643 if (domain.
IsEmpty())
return false;
644 (*domains_)[
var] = domain;
647 var_to_current_lb_interval_index_.
Set(
var, 0);
666 if (i == domain.
NumIntervals() || pair.value < domain[i].start) {
676 <<
"Domain intersection fixed " << num_fixed
677 <<
" equality literal corresponding to values outside the new domain.";
684 IntegerValue
value) {
688 insert.first->second = new_var;
695 return insert.first->second;
701 return (constant_map_.size() + 1) / 2;
705 int threshold)
const {
709 const int index_in_queue = tmp_var_to_trail_index_in_queue_[
var];
710 if (threshold <= index_in_queue) {
711 if (index_in_queue !=
kint32max) has_dependency_ =
true;
715 DCHECK_GE(threshold, vars_.
size());
716 int trail_index = vars_[
var].current_trail_index;
719 if (trail_index > threshold) {
720 const int cached_index = var_trail_index_cache_[
var];
721 if (cached_index >= threshold && cached_index < trail_index &&
722 integer_trail_[cached_index].
var ==
var) {
723 trail_index = cached_index;
727 while (trail_index >= threshold) {
728 trail_index = integer_trail_[trail_index].prev_trail_index;
729 if (trail_index >= var_trail_index_cache_threshold_) {
730 var_trail_index_cache_[
var] = trail_index;
734 const int num_vars = vars_.
size();
735 return trail_index < num_vars ? -1 : trail_index;
738 int IntegerTrail::FindLowestTrailIndexThatExplainBound(
740 DCHECK_LE(i_lit.
bound, vars_[i_lit.
var].current_bound);
742 int trail_index = vars_[i_lit.
var].current_trail_index;
750 const int cached_index = var_trail_index_cache_[i_lit.
var];
751 if (cached_index < trail_index) {
752 const TrailEntry& entry = integer_trail_[cached_index];
753 if (entry.var == i_lit.
var && entry.bound >= i_lit.
bound) {
754 trail_index = cached_index;
759 int prev_trail_index = trail_index;
761 if (trail_index >= var_trail_index_cache_threshold_) {
762 var_trail_index_cache_[i_lit.
var] = trail_index;
764 const TrailEntry& entry = integer_trail_[trail_index];
765 if (entry.bound == i_lit.
bound)
return trail_index;
766 if (entry.bound < i_lit.
bound)
return prev_trail_index;
767 prev_trail_index = trail_index;
768 trail_index = entry.prev_trail_index;
774 IntegerValue slack, absl::Span<const IntegerValue> coeffs,
775 std::vector<IntegerLiteral>* reason)
const {
777 if (slack == 0)
return;
778 const int size = reason->size();
779 tmp_indices_.resize(size);
780 for (
int i = 0; i < size; ++i) {
782 CHECK_GE(coeffs[i], 0);
783 tmp_indices_[i] = vars_[(*reason)[i].var].current_trail_index;
789 for (
const int i : tmp_indices_) {
791 integer_trail_[i].
bound));
796 IntegerValue slack, absl::Span<const IntegerValue> coeffs,
797 absl::Span<const IntegerVariable> vars,
798 std::vector<IntegerLiteral>* reason)
const {
799 tmp_indices_.clear();
800 for (
const IntegerVariable
var : vars) {
801 tmp_indices_.push_back(vars_[
var].current_trail_index);
804 for (
const int i : tmp_indices_) {
806 integer_trail_[i].
bound));
814 absl::Span<const IntegerValue> coeffs,
815 std::vector<int>* trail_indices)
const {
817 DCHECK(relax_heap_.empty());
824 const int size = coeffs.size();
825 const int num_vars = vars_.
size();
826 for (
int i = 0; i < size; ++i) {
827 const int index = (*trail_indices)[i];
830 if (
index < num_vars)
continue;
833 const IntegerValue coeff = coeffs[i];
835 (*trail_indices)[new_size++] =
index;
840 const TrailEntry& entry = integer_trail_[
index];
841 const TrailEntry& previous_entry = integer_trail_[entry.prev_trail_index];
843 CapProd(coeff.value(), (entry.bound - previous_entry.bound).value());
845 (*trail_indices)[new_size++] =
index;
849 relax_heap_.push_back({
index, coeff, diff});
851 trail_indices->resize(new_size);
852 std::make_heap(relax_heap_.begin(), relax_heap_.end());
854 while (slack > 0 && !relax_heap_.empty()) {
855 const RelaxHeapEntry heap_entry = relax_heap_.front();
856 std::pop_heap(relax_heap_.begin(), relax_heap_.end());
857 relax_heap_.pop_back();
860 if (heap_entry.diff > slack) {
861 trail_indices->push_back(heap_entry.index);
866 slack -= heap_entry.diff;
867 const int index = integer_trail_[heap_entry.index].prev_trail_index;
870 if (
index < num_vars)
continue;
871 if (heap_entry.coeff > slack) {
872 trail_indices->push_back(
index);
875 const TrailEntry& entry = integer_trail_[
index];
876 const TrailEntry& previous_entry = integer_trail_[entry.prev_trail_index];
878 (entry.bound - previous_entry.bound).value());
880 trail_indices->push_back(
index);
883 relax_heap_.push_back({
index, heap_entry.coeff, diff});
884 std::push_heap(relax_heap_.begin(), relax_heap_.end());
889 for (
const RelaxHeapEntry& entry : relax_heap_) {
890 trail_indices->push_back(entry.index);
896 std::vector<IntegerLiteral>* reason)
const {
900 (*reason)[new_size++] =
literal;
902 reason->resize(new_size);
905 std::vector<Literal>* IntegerTrail::InitializeConflict(
906 IntegerLiteral integer_literal,
const LazyReasonFunction& lazy_reason,
907 absl::Span<const Literal> literals_reason,
908 absl::Span<const IntegerLiteral> bounds_reason) {
909 DCHECK(tmp_queue_.empty());
911 if (lazy_reason ==
nullptr) {
912 conflict->assign(literals_reason.begin(), literals_reason.end());
913 const int num_vars = vars_.
size();
915 const int trail_index = FindLowestTrailIndexThatExplainBound(
literal);
916 if (trail_index >= num_vars) tmp_queue_.push_back(trail_index);
921 lazy_reason(integer_literal, integer_trail_.size(), conflict, &tmp_queue_);
928 std::string ReasonDebugString(absl::Span<const Literal> literal_reason,
929 absl::Span<const IntegerLiteral> integer_reason) {
930 std::string result =
"literals:{";
931 for (
const Literal l : literal_reason) {
932 if (result.back() !=
'{') result +=
",";
933 result += l.DebugString();
935 result +=
"} bounds:{";
936 for (
const IntegerLiteral l : integer_reason) {
937 if (result.back() !=
'{') result +=
",";
938 result += l.DebugString();
946 std::string IntegerTrail::DebugString() {
947 std::string result =
"trail:{";
948 const int num_vars = vars_.
size();
950 std::min(num_vars + 30,
static_cast<int>(integer_trail_.size()));
951 for (
int i = num_vars; i < limit; ++i) {
952 if (result.back() !=
'{') result +=
",";
955 integer_trail_[i].
bound)
958 if (limit < integer_trail_.size()) {
966 absl::Span<const Literal> literal_reason,
967 absl::Span<const IntegerLiteral> integer_reason) {
968 return EnqueueInternal(i_lit,
nullptr, literal_reason, integer_reason,
969 integer_trail_.size());
973 absl::Span<const Literal> literal_reason,
974 absl::Span<const IntegerLiteral> integer_reason,
975 int trail_index_with_same_reason) {
976 return EnqueueInternal(i_lit,
nullptr, literal_reason, integer_reason,
977 trail_index_with_same_reason);
982 return EnqueueInternal(i_lit, lazy_reason, {}, {}, integer_trail_.size());
985 bool IntegerTrail::ReasonIsValid(
986 absl::Span<const Literal> literal_reason,
987 absl::Span<const IntegerLiteral> integer_reason) {
989 for (
const Literal lit : literal_reason) {
992 for (
const IntegerLiteral i_lit : integer_reason) {
993 if (i_lit.
bound > vars_[i_lit.
var].current_bound) {
996 LOG(INFO) <<
"Reason " << i_lit <<
" is not true!"
997 <<
" optional variable:" << i_lit.
var
1000 <<
" current_lb:" << vars_[i_lit.
var].current_bound;
1002 LOG(INFO) <<
"Reason " << i_lit <<
" is not true!"
1003 <<
" non-optional variable:" << i_lit.
var
1004 <<
" current_lb:" << vars_[i_lit.
var].current_bound;
1012 if (!integer_search_levels_.empty()) {
1013 int num_literal_assigned_after_root_node = 0;
1014 for (
const Literal lit : literal_reason) {
1015 if (trail_->
Info(lit.Variable()).
level > 0) {
1016 num_literal_assigned_after_root_node++;
1019 for (
const IntegerLiteral i_lit : integer_reason) {
1021 num_literal_assigned_after_root_node++;
1024 DLOG_IF(INFO, num_literal_assigned_after_root_node == 0)
1025 <<
"Propagating a literal with no reason at a positive level!\n"
1026 <<
"level:" << integer_search_levels_.size() <<
" "
1027 << ReasonDebugString(literal_reason, integer_reason) <<
"\n"
1036 absl::Span<const IntegerLiteral> integer_reason) {
1037 EnqueueLiteralInternal(
literal,
nullptr, literal_reason, integer_reason);
1040 void IntegerTrail::EnqueueLiteralInternal(
1042 absl::Span<const Literal> literal_reason,
1043 absl::Span<const IntegerLiteral> integer_reason) {
1045 DCHECK(lazy_reason !=
nullptr ||
1046 ReasonIsValid(literal_reason, integer_reason));
1047 if (integer_search_levels_.empty()) {
1053 const int trail_index = trail_->
Index();
1054 if (trail_index >= boolean_trail_index_to_integer_one_.size()) {
1055 boolean_trail_index_to_integer_one_.resize(trail_index + 1);
1057 boolean_trail_index_to_integer_one_[trail_index] = integer_trail_.size();
1059 int reason_index = literals_reason_starts_.size();
1060 if (lazy_reason !=
nullptr) {
1061 if (integer_trail_.size() >= lazy_reasons_.size()) {
1062 lazy_reasons_.resize(integer_trail_.size() + 1,
nullptr);
1064 lazy_reasons_[integer_trail_.size()] = lazy_reason;
1068 literals_reason_starts_.push_back(literals_reason_buffer_.size());
1069 literals_reason_buffer_.insert(literals_reason_buffer_.end(),
1070 literal_reason.begin(),
1071 literal_reason.end());
1072 bounds_reason_starts_.push_back(bounds_reason_buffer_.size());
1073 bounds_reason_buffer_.insert(bounds_reason_buffer_.end(),
1074 integer_reason.begin(), integer_reason.end());
1077 integer_trail_.push_back({IntegerValue(0),
1085 bool IntegerTrail::EnqueueInternal(
1086 IntegerLiteral i_lit, LazyReasonFunction lazy_reason,
1087 absl::Span<const Literal> literal_reason,
1088 absl::Span<const IntegerLiteral> integer_reason,
1089 int trail_index_with_same_reason) {
1090 DCHECK(lazy_reason !=
nullptr ||
1091 ReasonIsValid(literal_reason, integer_reason));
1093 const IntegerVariable
var(i_lit.var);
1101 if (i_lit.bound <= vars_[
var].current_bound)
return true;
1110 if ((*domains_)[
var].NumIntervals() > 1) {
1111 const auto& domain = (*domains_)[
var];
1113 const int size = domain.NumIntervals();
1114 while (index < size && i_lit.bound > domain[
index].end) {
1117 if (
index == size) {
1120 var_to_current_lb_interval_index_.
Set(
var,
index);
1121 i_lit.bound =
std::max(i_lit.bound, IntegerValue(domain[
index].start));
1131 Literal(is_ignored_literals_[
var]))) {
1134 auto* conflict = InitializeConflict(i_lit, lazy_reason, literal_reason,
1137 conflict->push_back(Literal(is_ignored_literals_[
var]));
1140 const int trail_index = FindLowestTrailIndexThatExplainBound(ub_reason);
1141 const int num_vars = vars_.
size();
1142 if (trail_index >= num_vars) tmp_queue_.push_back(trail_index);
1144 MergeReasonIntoInternal(conflict);
1151 const Literal is_ignored = Literal(is_ignored_literals_[
var]);
1152 if (integer_search_levels_.empty()) {
1159 if (lazy_reason !=
nullptr) {
1160 lazy_reason(i_lit, integer_trail_.size(), &lazy_reason_literals_,
1161 &lazy_reason_trail_indices_);
1162 std::vector<IntegerLiteral> temp;
1163 for (
const int trail_index : lazy_reason_trail_indices_) {
1164 const TrailEntry& entry = integer_trail_[trail_index];
1165 temp.push_back(IntegerLiteral(entry.var, entry.bound));
1173 bounds_reason_buffer_.push_back(ub_reason);
1180 for (SparseBitset<IntegerVariable>* bitset : watchers_) {
1181 bitset->Set(i_lit.var);
1197 const LiteralIndex literal_index =
1200 const Literal to_enqueue = Literal(literal_index);
1202 auto* conflict = InitializeConflict(i_lit, lazy_reason, literal_reason,
1204 conflict->push_back(to_enqueue);
1205 MergeReasonIntoInternal(conflict);
1215 EnqueueLiteralInternal(to_enqueue, lazy_reason, literal_reason,
1218 return EnqueueAssociatedIntegerLiteral(i_lit, to_enqueue);
1222 if (integer_search_levels_.empty()) {
1228 const int trail_index = trail_->
Index();
1229 if (trail_index >= boolean_trail_index_to_integer_one_.size()) {
1230 boolean_trail_index_to_integer_one_.resize(trail_index + 1);
1232 boolean_trail_index_to_integer_one_[trail_index] =
1233 trail_index_with_same_reason;
1240 if (integer_search_levels_.empty()) {
1241 ++num_level_zero_enqueues_;
1242 vars_[i_lit.var].current_bound = i_lit.bound;
1243 integer_trail_[i_lit.var.value()].bound = i_lit.bound;
1254 int reason_index = literals_reason_starts_.size();
1255 if (lazy_reason !=
nullptr) {
1256 if (integer_trail_.size() >= lazy_reasons_.size()) {
1257 lazy_reasons_.resize(integer_trail_.size() + 1,
nullptr);
1259 lazy_reasons_[integer_trail_.size()] = lazy_reason;
1261 }
else if (trail_index_with_same_reason >= integer_trail_.size()) {
1263 literals_reason_starts_.push_back(literals_reason_buffer_.size());
1264 if (!literal_reason.empty()) {
1265 literals_reason_buffer_.insert(literals_reason_buffer_.end(),
1266 literal_reason.begin(),
1267 literal_reason.end());
1269 bounds_reason_starts_.push_back(bounds_reason_buffer_.size());
1270 if (!integer_reason.empty()) {
1271 bounds_reason_buffer_.insert(bounds_reason_buffer_.end(),
1272 integer_reason.begin(),
1273 integer_reason.end());
1276 reason_index = integer_trail_[trail_index_with_same_reason].reason_index;
1279 const int prev_trail_index = vars_[i_lit.var].current_trail_index;
1280 integer_trail_.push_back({i_lit.bound,
1285 vars_[i_lit.var].current_bound = i_lit.bound;
1286 vars_[i_lit.var].current_trail_index = integer_trail_.size() - 1;
1290 bool IntegerTrail::EnqueueAssociatedIntegerLiteral(IntegerLiteral i_lit,
1291 Literal literal_reason) {
1292 DCHECK(ReasonIsValid({literal_reason.Negated()}, {}));
1296 if (i_lit.bound <= vars_[i_lit.var].current_bound)
return true;
1304 return Enqueue(i_lit, {literal_reason.Negated()}, {});
1308 for (SparseBitset<IntegerVariable>* bitset : watchers_) {
1309 bitset->Set(i_lit.var);
1313 if (integer_search_levels_.empty()) {
1314 vars_[i_lit.var].current_bound = i_lit.bound;
1315 integer_trail_[i_lit.var.value()].bound = i_lit.bound;
1326 const int reason_index = literals_reason_starts_.size();
1327 CHECK_EQ(reason_index, bounds_reason_starts_.size());
1328 literals_reason_starts_.push_back(literals_reason_buffer_.size());
1329 bounds_reason_starts_.push_back(bounds_reason_buffer_.size());
1330 literals_reason_buffer_.push_back(literal_reason.Negated());
1332 const int prev_trail_index = vars_[i_lit.var].current_trail_index;
1333 integer_trail_.push_back({i_lit.bound,
1338 vars_[i_lit.var].current_bound = i_lit.bound;
1339 vars_[i_lit.var].current_trail_index = integer_trail_.size() - 1;
1343 void IntegerTrail::ComputeLazyReasonIfNeeded(
int trail_index)
const {
1344 const int reason_index = integer_trail_[trail_index].reason_index;
1345 if (reason_index == -1) {
1346 const TrailEntry& entry = integer_trail_[trail_index];
1347 const IntegerLiteral
literal(entry.var, entry.bound);
1348 lazy_reasons_[trail_index](
literal, trail_index, &lazy_reason_literals_,
1349 &lazy_reason_trail_indices_);
1353 absl::Span<const int> IntegerTrail::Dependencies(
int trail_index)
const {
1354 const int reason_index = integer_trail_[trail_index].reason_index;
1355 if (reason_index == -1) {
1356 return absl::Span<const int>(lazy_reason_trail_indices_);
1359 const int start = bounds_reason_starts_[reason_index];
1360 const int end = reason_index + 1 < bounds_reason_starts_.size()
1361 ? bounds_reason_starts_[reason_index + 1]
1362 : bounds_reason_buffer_.size();
1363 if (start == end)
return {};
1370 if (end > trail_index_reason_buffer_.size()) {
1371 trail_index_reason_buffer_.resize(end, -1);
1373 if (trail_index_reason_buffer_[start] == -1) {
1374 int new_end = start;
1375 const int num_vars = vars_.
size();
1376 for (
int i = start; i < end; ++i) {
1378 FindLowestTrailIndexThatExplainBound(bounds_reason_buffer_[i]);
1379 if (dep >= num_vars) {
1380 trail_index_reason_buffer_[new_end++] = dep;
1383 return absl::Span<const int>(&trail_index_reason_buffer_[start],
1389 return absl::Span<const int>(&trail_index_reason_buffer_[start],
1394 void IntegerTrail::AppendLiteralsReason(
int trail_index,
1395 std::vector<Literal>* output)
const {
1396 CHECK_GE(trail_index, vars_.
size());
1397 const int reason_index = integer_trail_[trail_index].reason_index;
1398 if (reason_index == -1) {
1399 for (
const Literal l : lazy_reason_literals_) {
1400 if (!added_variables_[l.Variable()]) {
1401 added_variables_.
Set(l.Variable());
1402 output->push_back(l);
1408 const int start = literals_reason_starts_[reason_index];
1409 const int end = reason_index + 1 < literals_reason_starts_.size()
1410 ? literals_reason_starts_[reason_index + 1]
1411 : literals_reason_buffer_.size();
1412 for (
int i = start; i < end; ++i) {
1413 const Literal l = literals_reason_buffer_[i];
1414 if (!added_variables_[l.Variable()]) {
1415 added_variables_.
Set(l.Variable());
1416 output->push_back(l);
1422 std::vector<Literal> reason;
1430 std::vector<Literal>* output)
const {
1431 DCHECK(tmp_queue_.empty());
1432 const int num_vars = vars_.
size();
1434 const int trail_index = FindLowestTrailIndexThatExplainBound(
literal);
1438 if (trail_index >= num_vars) tmp_queue_.push_back(trail_index);
1440 return MergeReasonIntoInternal(output);
1445 void IntegerTrail::MergeReasonIntoInternal(std::vector<Literal>* output)
const {
1448 DCHECK(std::all_of(tmp_var_to_trail_index_in_queue_.
begin(),
1449 tmp_var_to_trail_index_in_queue_.
end(),
1450 [](
int v) { return v == 0; }));
1453 for (
const Literal l : *output) {
1454 added_variables_.
Set(l.Variable());
1459 for (
const int trail_index : tmp_queue_) {
1460 DCHECK_GE(trail_index, vars_.
size());
1461 DCHECK_LT(trail_index, integer_trail_.size());
1462 const TrailEntry& entry = integer_trail_[trail_index];
1463 tmp_var_to_trail_index_in_queue_[entry.var] =
1464 std::max(tmp_var_to_trail_index_in_queue_[entry.var], trail_index);
1469 std::make_heap(tmp_queue_.begin(), tmp_queue_.end());
1474 tmp_to_clear_.clear();
1475 while (!tmp_queue_.empty()) {
1476 const int trail_index = tmp_queue_.front();
1477 const TrailEntry& entry = integer_trail_[trail_index];
1478 std::pop_heap(tmp_queue_.begin(), tmp_queue_.end());
1479 tmp_queue_.pop_back();
1484 if (tmp_var_to_trail_index_in_queue_[entry.var] != trail_index) {
1491 var_trail_index_cache_threshold_ = trail_index;
1496 const LiteralIndex associated_lit =
1498 IntegerVariable(entry.var), entry.bound));
1501 const int reason_index = integer_trail_[trail_index].reason_index;
1502 CHECK_NE(reason_index, -1);
1504 const int start = literals_reason_starts_[reason_index];
1505 const int end = reason_index + 1 < literals_reason_starts_.size()
1506 ? literals_reason_starts_[reason_index + 1]
1507 : literals_reason_buffer_.size();
1508 CHECK_EQ(start + 1, end);
1509 CHECK_EQ(literals_reason_buffer_[start],
1510 Literal(associated_lit).Negated());
1513 const int start = bounds_reason_starts_[reason_index];
1514 const int end = reason_index + 1 < bounds_reason_starts_.size()
1515 ? bounds_reason_starts_[reason_index + 1]
1516 : bounds_reason_buffer_.size();
1517 CHECK_EQ(start, end);
1530 tmp_var_to_trail_index_in_queue_[entry.var] = 0;
1531 has_dependency_ =
false;
1533 ComputeLazyReasonIfNeeded(trail_index);
1534 AppendLiteralsReason(trail_index, output);
1535 for (
const int next_trail_index : Dependencies(trail_index)) {
1536 if (next_trail_index < 0)
break;
1537 DCHECK_LT(next_trail_index, trail_index);
1538 const TrailEntry& next_entry = integer_trail_[next_trail_index];
1544 const int index_in_queue =
1545 tmp_var_to_trail_index_in_queue_[next_entry.var];
1546 if (index_in_queue !=
kint32max) has_dependency_ =
true;
1547 if (next_trail_index > index_in_queue) {
1548 tmp_var_to_trail_index_in_queue_[next_entry.var] = next_trail_index;
1550 std::push_heap(tmp_queue_.begin(), tmp_queue_.end());
1555 if (!has_dependency_) {
1556 tmp_to_clear_.push_back(entry.var);
1557 tmp_var_to_trail_index_in_queue_[entry.var] =
kint32max;
1562 for (
const IntegerVariable
var : tmp_to_clear_) {
1563 tmp_var_to_trail_index_in_queue_[
var] = 0;
1568 int trail_index)
const {
1569 const int index = boolean_trail_index_to_integer_one_[trail_index];
1573 ComputeLazyReasonIfNeeded(
index);
1574 AppendLiteralsReason(
index, reason);
1575 DCHECK(tmp_queue_.empty());
1576 for (
const int prev_trail_index : Dependencies(
index)) {
1577 if (prev_trail_index < 0)
break;
1578 DCHECK_GE(prev_trail_index, vars_.
size());
1579 tmp_queue_.push_back(prev_trail_index);
1581 MergeReasonIntoInternal(reason);
1591 const int end = vars_.
size();
1592 for (
int i = integer_trail_.size(); --i >= end;) {
1593 const TrailEntry& entry = integer_trail_[i];
1595 if (tmp_marked_[entry.var])
continue;
1597 tmp_marked_.
Set(entry.var);
1614 &id_to_greatest_common_level_since_last_call_);
1616 queue_by_priority_.resize(2);
1619 void GenericLiteralWatcher::UpdateCallingNeeds(
Trail* trail) {
1621 while (propagation_trail_index_ < trail->
Index()) {
1623 if (
literal.Index() >= literal_to_watcher_.
size())
continue;
1624 for (
const auto entry : literal_to_watcher_[
literal.Index()]) {
1625 if (!in_queue_[entry.id]) {
1626 in_queue_[entry.id] =
true;
1627 queue_by_priority_[id_to_priority_[entry.id]].push_back(entry.id);
1629 if (entry.watch_index >= 0) {
1630 id_to_watch_indices_[entry.id].push_back(entry.watch_index);
1637 if (
var.value() >= var_to_watcher_.
size())
continue;
1638 for (
const auto entry : var_to_watcher_[
var]) {
1639 if (!in_queue_[entry.id]) {
1640 in_queue_[entry.id] =
true;
1641 queue_by_priority_[id_to_priority_[entry.id]].push_back(entry.id);
1643 if (entry.watch_index >= 0) {
1644 id_to_watch_indices_[entry.id].push_back(entry.watch_index);
1650 const std::vector<IntegerVariable>& modified_vars =
1652 for (
const auto&
callback : level_zero_modified_variable_callback_) {
1665 for (
const int id : propagator_ids_to_call_at_level_zero_) {
1666 if (in_queue_[
id])
continue;
1667 in_queue_[id] =
true;
1668 queue_by_priority_[id_to_priority_[id]].push_back(
id);
1672 UpdateCallingNeeds(trail);
1677 for (
int priority = 0; priority < queue_by_priority_.size(); ++priority) {
1684 if (test_limit > 100) {
1689 std::deque<int>& queue = queue_by_priority_[priority];
1690 while (!queue.empty()) {
1691 const int id = queue.front();
1699 id_to_greatest_common_level_since_last_call_[IdType(
id)];
1700 const int high = id_to_level_at_last_call_[id];
1701 if (low < high || level > low) {
1702 id_to_level_at_last_call_[id] = level;
1703 id_to_greatest_common_level_since_last_call_.
MutableRef(IdType(
id)) =
1706 if (low < high) rev->SetLevel(low);
1707 if (level > low) rev->SetLevel(level);
1709 for (
int* rev_int : id_to_reversible_ints_[
id]) {
1710 rev_int_repository_->
SaveState(rev_int);
1717 const int64 old_boolean_timestamp = trail->
Index();
1720 std::vector<int>& watch_indices_ref = id_to_watch_indices_[id];
1722 watch_indices_ref.empty()
1723 ? watchers_[id]->Propagate()
1724 : watchers_[id]->IncrementalPropagate(watch_indices_ref);
1726 watch_indices_ref.clear();
1727 in_queue_[id] =
false;
1733 if (id_to_idempotence_[
id]) {
1737 UpdateCallingNeeds(trail);
1738 watch_indices_ref.clear();
1739 in_queue_[id] =
false;
1744 watch_indices_ref.clear();
1745 in_queue_[id] =
false;
1746 UpdateCallingNeeds(trail);
1752 if (trail->
Index() > old_boolean_timestamp) {
1764 if (integer_trail_->
num_enqueues() > old_integer_timestamp) {
1782 for (std::deque<int>& queue : queue_by_priority_) {
1783 for (
const int id : queue) {
1784 id_to_watch_indices_[id].clear();
1794 in_queue_.assign(watchers_.size(),
false);
1799 const int id = watchers_.size();
1800 watchers_.push_back(propagator);
1801 id_to_level_at_last_call_.push_back(0);
1802 id_to_greatest_common_level_since_last_call_.
GrowByOne();
1803 id_to_reversible_classes_.push_back(std::vector<ReversibleInterface*>());
1804 id_to_reversible_ints_.push_back(std::vector<int*>());
1805 id_to_watch_indices_.push_back(std::vector<int>());
1806 id_to_priority_.push_back(1);
1807 id_to_idempotence_.push_back(
true);
1816 in_queue_.push_back(
true);
1817 queue_by_priority_[1].push_back(
id);
1822 id_to_priority_[id] = priority;
1823 if (priority >= queue_by_priority_.size()) {
1824 queue_by_priority_.resize(priority + 1);
1830 id_to_idempotence_[id] =
false;
1834 propagator_ids_to_call_at_level_zero_.push_back(
id);
1839 id_to_reversible_classes_[id].push_back(rev);
1843 id_to_reversible_ints_[id].push_back(rev);
1847 std::function<void(
Model*)>
1855 std::vector<Literal> clause_to_exclude_solution;
1856 clause_to_exclude_solution.reserve(current_level);
1857 for (
int i = 0; i < current_level; ++i) {
1858 bool include_decision =
true;
1864 encoder->GetIntegerLiterals(decision);
1866 if (integer_trail->IsCurrentlyIgnored(
bound.var)) {
1872 clause_to_exclude_solution.push_back(
1873 integer_trail->IsIgnoredLiteral(
bound.var).Negated());
1874 include_decision =
false;
1878 if (include_decision) {
1879 clause_to_exclude_solution.push_back(decision.
Negated());