23#include "absl/log/check.h"
24#include "absl/meta/type_traits.h"
25#include "absl/strings/str_cat.h"
26#include "absl/types/span.h"
45 std::vector<AffineExpression> starts, std::vector<AffineExpression> ends,
46 std::vector<AffineExpression> sizes,
47 std::vector<LiteralIndex> reason_for_presence,
Model* model)
49 sat_solver_(model->GetOrCreate<
SatSolver>()),
54 starts_(
std::move(starts)),
55 ends_(
std::move(ends)),
56 sizes_(
std::move(sizes)),
57 reason_for_presence_(
std::move(reason_for_presence)),
58 capacity_(starts_.size()),
59 cached_size_min_(new IntegerValue[capacity_]),
60 cached_start_min_(new IntegerValue[capacity_]),
61 cached_end_min_(new IntegerValue[capacity_]),
62 cached_negated_start_max_(new IntegerValue[capacity_]),
63 cached_negated_end_max_(new IntegerValue[capacity_]),
64 cached_shifted_start_min_(new IntegerValue[capacity_]),
65 cached_negated_shifted_end_max_(new IntegerValue[capacity_]) {
66 DCHECK_EQ(starts_.size(), ends_.size());
67 DCHECK_EQ(starts_.size(), sizes_.size());
68 DCHECK_EQ(starts_.size(), reason_for_presence_.size());
70 minus_starts_.clear();
71 minus_starts_.reserve(starts_.size());
73 minus_ends_.reserve(starts_.size());
74 for (
int i = 0;
i < starts_.size(); ++
i) {
75 minus_starts_.push_back(starts_[
i].Negated());
76 minus_ends_.push_back(ends_[
i].Negated());
88 sat_solver_(model->GetOrCreate<
SatSolver>()),
93 cached_size_min_(new IntegerValue[capacity_]),
94 cached_start_min_(new IntegerValue[capacity_]),
95 cached_end_min_(new IntegerValue[capacity_]),
96 cached_negated_start_max_(new IntegerValue[capacity_]),
97 cached_negated_end_max_(new IntegerValue[capacity_]),
98 cached_shifted_start_min_(new IntegerValue[capacity_]),
99 cached_negated_shifted_end_max_(new IntegerValue[capacity_]) {
100 starts_.resize(num_tasks);
105 recompute_all_cache_ =
true;
106 for (
const int id : propagator_ids_) watcher_->CallOnNextPropagate(
id);
111 const std::vector<int>& watch_indices) {
112 for (
const int t : watch_indices) recompute_cache_.Set(t);
113 for (
const int id : propagator_ids_) watcher_->CallOnNextPropagate(
id);
118 const int id = watcher->
Register(
this);
119 const int num_tasks = starts_.size();
120 for (
int t = 0; t < num_tasks; ++t) {
132 watcher_->WatchLiteral(
Literal(reason_for_presence_[t]),
id);
138bool SchedulingConstraintHelper::UpdateCachedValues(
int t) {
141 IntegerValue smin = integer_trail_->
LowerBound(starts_[t]);
142 IntegerValue smax = integer_trail_->
UpperBound(starts_[t]);
143 IntegerValue emin = integer_trail_->
LowerBound(ends_[t]);
144 IntegerValue emax = integer_trail_->
UpperBound(ends_[t]);
152 std::max(IntegerValue(0), integer_trail_->
LowerBound(sizes_[t]));
153 IntegerValue dmax = integer_trail_->
UpperBound(sizes_[t]);
161 if (smin + dmin - emax > 0) {
168 if (smax + dmax - emin < 0) {
182 smin = std::max(smin, emin - dmax);
183 smax = std::min(smax, emax - dmin);
184 dmin = std::max(dmin, emin - smax);
185 emin = std::max(emin, smin + dmin);
186 emax = std::min(emax, smax + dmax);
188 if (emin != cached_end_min_[t]) {
189 recompute_energy_profile_ =
true;
194 recompute_by_start_max_ =
true;
195 recompute_by_end_min_ =
true;
197 cached_start_min_[t] = smin;
198 cached_end_min_[t] = emin;
199 cached_negated_start_max_[t] = -smax;
200 cached_negated_end_max_[t] = -emax;
201 cached_size_min_[t] = dmin;
204 const IntegerValue new_shifted_start_min = emin - dmin;
205 if (new_shifted_start_min != cached_shifted_start_min_[t]) {
206 recompute_energy_profile_ =
true;
207 recompute_shifted_start_min_ =
true;
208 cached_shifted_start_min_[t] = new_shifted_start_min;
210 const IntegerValue new_negated_shifted_end_max = -(smax + dmin);
211 if (new_negated_shifted_end_max != cached_negated_shifted_end_max_[t]) {
212 recompute_negated_shifted_end_max_ =
true;
213 cached_negated_shifted_end_max_[t] = new_negated_shifted_end_max;
220 current_time_direction_ = other.current_time_direction_;
222 const int num_tasks = tasks.size();
223 CHECK_LE(num_tasks, capacity_);
224 starts_.resize(num_tasks);
225 ends_.resize(num_tasks);
226 minus_ends_.resize(num_tasks);
227 minus_starts_.resize(num_tasks);
228 sizes_.resize(num_tasks);
229 reason_for_presence_.resize(num_tasks);
230 for (
int i = 0;
i < num_tasks; ++
i) {
231 const int t = tasks[
i];
232 starts_[
i] = other.starts_[t];
233 ends_[
i] = other.ends_[t];
234 minus_ends_[
i] = other.minus_ends_[t];
235 minus_starts_[
i] = other.minus_starts_[t];
236 sizes_[
i] = other.sizes_[t];
237 reason_for_presence_[
i] = other.reason_for_presence_[t];
244void SchedulingConstraintHelper::InitSortedVectors() {
245 const int num_tasks = starts_.size();
247 recompute_all_cache_ =
true;
248 recompute_cache_.
Resize(num_tasks);
249 for (
int t = 0; t < num_tasks; ++t) {
250 recompute_cache_.
Set(t);
254 CHECK_LE(num_tasks, capacity_);
256 task_by_increasing_start_min_.resize(num_tasks);
257 task_by_increasing_end_min_.resize(num_tasks);
258 task_by_increasing_negated_start_max_.resize(num_tasks);
259 task_by_decreasing_end_max_.resize(num_tasks);
260 task_by_increasing_shifted_start_min_.resize(num_tasks);
261 task_by_negated_shifted_end_max_.resize(num_tasks);
262 for (
int t = 0; t < num_tasks; ++t) {
263 task_by_increasing_start_min_[t].task_index = t;
264 task_by_increasing_end_min_[t].task_index = t;
265 task_by_increasing_negated_start_max_[t].task_index = t;
266 task_by_decreasing_end_max_[t].task_index = t;
268 task_by_increasing_shifted_start_min_[t].task_index = t;
269 task_by_increasing_shifted_start_min_[t].presence_lit =
270 reason_for_presence_[t];
271 task_by_negated_shifted_end_max_[t].task_index = t;
272 task_by_negated_shifted_end_max_[t].presence_lit = reason_for_presence_[t];
275 recompute_by_start_max_ =
true;
276 recompute_by_end_min_ =
true;
277 recompute_energy_profile_ =
true;
278 recompute_shifted_start_min_ =
true;
279 recompute_negated_shifted_end_max_ =
true;
283 if (current_time_direction_ != is_forward) {
284 current_time_direction_ = is_forward;
286 std::swap(starts_, minus_ends_);
287 std::swap(ends_, minus_starts_);
289 std::swap(task_by_increasing_start_min_, task_by_decreasing_end_max_);
290 std::swap(task_by_increasing_end_min_,
291 task_by_increasing_negated_start_max_);
292 std::swap(recompute_by_end_min_, recompute_by_start_max_);
293 std::swap(task_by_increasing_shifted_start_min_,
294 task_by_negated_shifted_end_max_);
296 recompute_energy_profile_ =
true;
297 std::swap(cached_start_min_, cached_negated_end_max_);
298 std::swap(cached_end_min_, cached_negated_start_max_);
299 std::swap(cached_shifted_start_min_, cached_negated_shifted_end_max_);
300 std::swap(recompute_shifted_start_min_, recompute_negated_shifted_end_max_);
310 if (sat_solver_->num_backtracks() != saved_num_backtracks_) {
311 recompute_all_cache_ =
true;
312 saved_num_backtracks_ = sat_solver_->num_backtracks();
315 if (recompute_all_cache_) {
316 for (
int t = 0; t < recompute_cache_.size(); ++t) {
317 if (!UpdateCachedValues(t))
return false;
320 for (
const int t : recompute_cache_) {
321 if (!UpdateCachedValues(t))
return false;
324 recompute_cache_.ClearAll();
325 recompute_all_cache_ =
false;
332 int a,
int b,
bool add_reason_if_after) {
342 const IntegerValue conditional_offset =
343 precedence_relations_->GetConditionalOffset(before.
var, after.
var);
344 const IntegerValue known = integer_trail_->LevelZeroLowerBound(after.
var) -
345 integer_trail_->LevelZeroUpperBound(before.
var);
346 const IntegerValue offset = std::max(conditional_offset, known);
349 const IntegerValue distance = offset - needed_offset;
350 if (add_reason_if_after && distance >= 0 && known < conditional_offset) {
351 for (
const Literal l : precedence_relations_->GetConditionalEnforcements(
353 literal_reason_.push_back(l.Negated());
366 CHECK_EQ(sat_solver_->CurrentDecisionLevel(), 0);
370 if (after.
coeff != 1)
return true;
371 if (before.
coeff != 1)
return true;
374 if (before.
var == after.
var) {
378 sat_solver_->NotifyThatModelIsUnsat();
383 if (precedence_relations_->Add(before.
var, after.
var, offset)) {
388 -offset.value(), model_);
393 {int64_t{1}, int64_t{-1}}, -offset.value(),
396 if (sat_solver_->ModelIsUnsat())
return false;
401absl::Span<const TaskTime>
403 for (
TaskTime& ref : task_by_increasing_start_min_) {
404 ref.time =
StartMin(ref.task_index);
407 task_by_increasing_start_min_.end());
408 return task_by_increasing_start_min_;
411absl::Span<const TaskTime>
413 if (!recompute_by_end_min_)
return task_by_increasing_end_min_;
414 for (
TaskTime& ref : task_by_increasing_end_min_) {
415 ref.time =
EndMin(ref.task_index);
418 task_by_increasing_end_min_.end());
419 recompute_by_end_min_ =
false;
420 return task_by_increasing_end_min_;
423absl::Span<const TaskTime>
425 if (!recompute_by_start_max_)
return task_by_increasing_negated_start_max_;
426 for (
TaskTime& ref : task_by_increasing_negated_start_max_) {
427 ref.time = cached_negated_start_max_[ref.task_index];
430 task_by_increasing_negated_start_max_.end());
431 recompute_by_start_max_ =
false;
432 return task_by_increasing_negated_start_max_;
435absl::Span<const TaskTime>
437 for (
TaskTime& ref : task_by_decreasing_end_max_) {
438 ref.time =
EndMax(ref.task_index);
441 task_by_decreasing_end_max_.end(), std::greater<TaskTime>());
442 return task_by_decreasing_end_max_;
445absl::Span<const CachedTaskBounds>
447 if (recompute_shifted_start_min_) {
448 recompute_shifted_start_min_ =
false;
449 bool is_sorted =
true;
453 is_sorted = is_sorted && ref.time >= previous;
456 if (is_sorted)
return task_by_increasing_shifted_start_min_;
458 task_by_increasing_shifted_start_min_.end());
460 return task_by_increasing_shifted_start_min_;
464const std::vector<SchedulingConstraintHelper::ProfileEvent>&
466 if (energy_profile_.empty()) {
468 for (
int t = 0; t < num_tasks; ++t) {
469 energy_profile_.push_back(
470 {cached_shifted_start_min_[t], t,
true});
471 energy_profile_.push_back({cached_end_min_[t], t,
false});
474 if (!recompute_energy_profile_)
return energy_profile_;
476 const int t = ref.task;
478 ref.time = cached_shifted_start_min_[t];
480 ref.time = cached_end_min_[t];
485 recompute_energy_profile_ =
false;
486 return energy_profile_;
492 AddOtherReason(before);
493 AddOtherReason(after);
497 std::vector<IntegerVariable> vars;
498 std::vector<IntegerValue> coeffs;
501 const IntegerValue smax_before =
StartMax(before);
502 if (smax_before >= integer_trail_->UpperBound(starts_[before])) {
504 vars.push_back(
NegationOf(starts_[before].var));
505 coeffs.push_back(starts_[before].coeff);
509 vars.push_back(
NegationOf(ends_[before].var));
510 coeffs.push_back(ends_[before].coeff);
513 vars.push_back(sizes_[before].var);
514 coeffs.push_back(sizes_[before].coeff);
519 const IntegerValue emin_after =
EndMin(after);
520 if (emin_after <= integer_trail_->
LowerBound(ends_[after])) {
522 vars.push_back(ends_[after].var);
523 coeffs.push_back(ends_[after].coeff);
527 vars.push_back(starts_[after].var);
528 coeffs.push_back(starts_[after].coeff);
531 vars.push_back(sizes_[after].var);
532 coeffs.push_back(sizes_[after].coeff);
536 DCHECK_LT(smax_before, emin_after);
537 const IntegerValue slack = emin_after - smax_before - 1;
538 integer_trail_->AppendRelaxedLinearReason(slack, coeffs, vars,
543 CHECK(other_helper_ ==
nullptr);
544 return integer_trail_->Enqueue(lit, literal_reason_, integer_reason_);
553 return integer_trail_->ConditionalEnqueue(
556 return integer_trail_->Enqueue(lit, literal_reason_, integer_reason_);
561bool SchedulingConstraintHelper::PushIntervalBound(
int t,
IntegerLiteral lit) {
564 if (!UpdateCachedValues(t))
return false;
565 recompute_cache_.
Clear(t);
590 return PushIntervalBound(t, ends_[t].
LowerOrEqual(value));
594 integer_trail_->EnqueueLiteral(l, literal_reason_, integer_reason_);
605 literal_reason_.push_back(
Literal(reason_for_presence_[t]).Negated());
609 integer_trail_->EnqueueLiteral(
Literal(reason_for_presence_[t]).Negated(),
610 literal_reason_, integer_reason_);
621 literal_reason_.push_back(
Literal(reason_for_presence_[t]));
625 integer_trail_->EnqueueLiteral(
Literal(reason_for_presence_[t]),
626 literal_reason_, integer_reason_);
632 return integer_trail_->ReportConflict(literal_reason_, integer_reason_);
639 propagator_ids_.push_back(
id);
642void SchedulingConstraintHelper::AddOtherReason(
int t) {
643 if (other_helper_ ==
nullptr || already_added_to_other_reasons_[t])
return;
644 already_added_to_other_reasons_[t] =
true;
645 const int mapped_t = map_to_other_helper_[t];
650void SchedulingConstraintHelper::ImportOtherReasons() {
651 if (other_helper_ !=
nullptr) ImportOtherReasons(*other_helper_);
656 literal_reason_.insert(literal_reason_.end(),
657 other_helper.literal_reason_.begin(),
658 other_helper.literal_reason_.end());
659 integer_reason_.insert(integer_reason_.end(),
660 other_helper.integer_reason_.begin(),
661 other_helper.integer_reason_.end());
665 return absl::StrCat(
"t=", t,
" is_present=",
670 "]",
" start=[",
StartMin(t).value(),
",",
672 ",",
EndMax(t).value(),
"]");
677 IntegerValue end)
const {
678 return std::min(std::min(end - start,
SizeMin(t)),
683 IntegerValue start_min, IntegerValue start_max, IntegerValue end_min,
684 IntegerValue end_max, IntegerValue size_min, IntegerValue demand_min,
685 absl::Span<const LiteralValueValue> filtered_energy,
686 IntegerValue window_start, IntegerValue window_end) {
687 if (window_end <= window_start)
return IntegerValue(0);
690 if (end_min <= window_start)
return IntegerValue(0);
691 if (start_max >= window_end)
return IntegerValue(0);
692 const IntegerValue window_size = window_end - window_start;
693 const IntegerValue simple_energy_min =
694 demand_min * std::min({end_min - window_start, window_end - start_max,
695 size_min, window_size});
696 if (filtered_energy.empty())
return simple_energy_min;
699 for (
const auto [lit, fixed_size, fixed_demand] : filtered_energy) {
700 const IntegerValue alt_end_min = std::max(end_min, start_min + fixed_size);
701 const IntegerValue alt_start_max =
702 std::min(start_max, end_max - fixed_size);
703 const IntegerValue energy_min =
705 std::min({alt_end_min - window_start, window_end - alt_start_max,
706 fixed_size, window_size});
707 result = std::min(result, energy_min);
710 return std::max(simple_energy_min, result);
714 absl::Span<const AffineExpression> demands,
718 sat_solver_(model->GetOrCreate<
SatSolver>()),
720 demands_(demands.begin(), demands.end()),
722 const int num_tasks = helper->
NumTasks();
723 decomposed_energies_.resize(num_tasks);
726 energy_is_quadratic_.resize(num_tasks,
false);
735 const int num_tasks = helper_->NumTasks();
736 if (demands_.size() != num_tasks)
return;
737 for (
int t = 0; t < num_tasks; ++t) {
740 decomposed_energies_[t] = product_decomposer_->TryToDecompose(size, demand);
744IntegerValue SchedulingDemandHelper::SimpleEnergyMin(
int t)
const {
749IntegerValue SchedulingDemandHelper::DecomposedEnergyMin(
int t)
const {
752 for (
const auto [lit, fixed_size, fixed_demand] : decomposed_energies_[t]) {
754 return fixed_size * fixed_demand;
757 result = std::min(result, fixed_size * fixed_demand);
763IntegerValue SchedulingDemandHelper::SimpleEnergyMax(
int t)
const {
768IntegerValue SchedulingDemandHelper::DecomposedEnergyMax(
int t)
const {
771 for (
const auto [lit, fixed_size, fixed_demand] : decomposed_energies_[t]) {
772 if (assignment_.LiteralIsTrue(lit)) {
773 return fixed_size * fixed_demand;
775 if (assignment_.LiteralIsFalse(lit))
continue;
776 result = std::max(result, fixed_size * fixed_demand);
783 const int num_tasks = cached_energies_min_.size();
784 const bool is_at_level_zero = sat_solver_->CurrentDecisionLevel() == 0;
785 for (
int t = 0; t < num_tasks; ++t) {
787 if (is_at_level_zero) {
789 for (
int i = 0;
i < decomposed_energies_[t].size(); ++
i) {
790 if (assignment_.LiteralIsFalse(decomposed_energies_[t][
i].literal)) {
793 decomposed_energies_[t][new_size++] = decomposed_energies_[t][
i];
795 decomposed_energies_[t].resize(new_size);
798 cached_energies_min_[t] =
799 std::max(SimpleEnergyMin(t), DecomposedEnergyMin(t));
801 energy_is_quadratic_[t] =
802 decomposed_energies_[t].empty() && !demands_.empty() &&
803 !integer_trail_->IsFixed(demands_[t]) && !helper_->SizeIsFixed(t);
804 cached_energies_max_[t] =
805 std::min(SimpleEnergyMax(t), DecomposedEnergyMax(t));
813 DCHECK_LT(t, demands_.size());
814 return integer_trail_->LowerBound(demands_[t]);
818 DCHECK_LT(t, demands_.size());
819 return integer_trail_->UpperBound(demands_[t]);
823 return integer_trail_->IsFixed(demands_[t]);
828 if (helper_->IsOptional(t)) {
829 return helper_->PushTaskAbsence(t);
831 return helper_->ReportConflict();
833 }
else if (!decomposed_energies_[t].empty()) {
834 for (
const auto [lit, fixed_size, fixed_demand] : decomposed_energies_[t]) {
835 if (fixed_size * fixed_demand > value) {
836 if (assignment_.LiteralIsTrue(lit))
return helper_->ReportConflict();
837 if (assignment_.LiteralIsFalse(lit))
continue;
838 if (!helper_->PushLiteral(lit.
Negated()))
return false;
843 VLOG(3) <<
"Cumulative energy missed propagation";
849 DCHECK_LT(t, demands_.size());
851 helper_->MutableIntegerReason()->push_back(
852 integer_trail_->LowerBoundAsLiteral(demands_[t].var));
857 IntegerValue min_demand) {
858 DCHECK_LT(t, demands_.size());
860 helper_->MutableIntegerReason()->push_back(
867 const IntegerValue value = cached_energies_min_[t];
868 if (DecomposedEnergyMin(t) >= value) {
869 auto* reason = helper_->MutableLiteralReason();
870 const int old_size = reason->size();
871 for (
const auto [lit, fixed_size, fixed_demand] : decomposed_energies_[t]) {
872 if (assignment_.LiteralIsTrue(lit)) {
873 reason->resize(old_size);
874 reason->push_back(lit.
Negated());
876 }
else if (fixed_size * fixed_demand < value &&
877 assignment_.LiteralIsFalse(lit)) {
878 reason->push_back(lit);
881 }
else if (SimpleEnergyMin(t) >= value) {
883 helper_->AddSizeMinReason(t);
889 if (helper_->IsPresent(t)) {
890 if (!decomposed_energies_[t].empty()) {
897 builder->
AddTerm(demands_[t], IntegerValue(1));
899 }
else if (!helper_->IsAbsent(t)) {
907 if (decomposed_energies_[index].empty())
return {};
908 if (sat_solver_->CurrentDecisionLevel() == 0) {
910 return decomposed_energies_[index];
914 std::vector<LiteralValueValue> result;
915 for (
const auto& e : decomposed_energies_[index]) {
916 if (assignment_.LiteralIsFalse(e.literal))
continue;
923 const std::vector<std::vector<LiteralValueValue>>& energies) {
924 DCHECK_EQ(energies.size(), helper_->NumTasks());
925 decomposed_energies_ = energies;
929 int t, IntegerValue window_start, IntegerValue window_end) {
931 helper_->StartMin(t), helper_->StartMax(t), helper_->EndMin(t),
932 helper_->EndMax(t), helper_->SizeMin(t),
DemandMin(t),
939 int t, IntegerValue window_start, IntegerValue window_end) {
940 const IntegerValue actual_energy_min =
942 if (actual_energy_min == 0)
return;
946 const IntegerValue start_max = helper_->StartMax(t);
947 const IntegerValue end_min = helper_->EndMin(t);
948 const IntegerValue min_overlap =
949 helper_->GetMinOverlap(t, window_start, window_end);
950 const IntegerValue simple_energy_min =
DemandMin(t) * min_overlap;
951 if (simple_energy_min == actual_energy_min) {
953 helper_->AddSizeMinReason(t);
954 helper_->AddStartMaxReason(t, start_max);
955 helper_->AddEndMinReason(t, end_min);
960 const IntegerValue start_min = helper_->StartMin(t);
961 const IntegerValue end_max = helper_->EndMax(t);
962 DCHECK(!decomposed_energies_[t].empty());
963 helper_->AddStartMinReason(t, start_min);
964 helper_->AddStartMaxReason(t, start_max);
965 helper_->AddEndMinReason(t, end_min);
966 helper_->AddEndMaxReason(t, end_max);
968 auto* literal_reason = helper_->MutableLiteralReason();
969 const int old_size = literal_reason->size();
971 DCHECK(!decomposed_energies_[t].empty());
972 for (
const auto [lit, fixed_size, fixed_demand] : decomposed_energies_[t]) {
974 if (assignment_.LiteralIsTrue(lit)) {
975 literal_reason->resize(old_size);
976 literal_reason->push_back(lit.
Negated());
979 if (assignment_.LiteralIsFalse(lit)) {
980 const IntegerValue alt_em = std::max(end_min, start_min + fixed_size);
981 const IntegerValue alt_sm = std::min(start_max, end_max - fixed_size);
982 const IntegerValue energy_min =
984 std::min({alt_em - window_start, window_end - alt_sm, fixed_size});
985 if (energy_min >= actual_energy_min)
continue;
986 literal_reason->push_back(lit);
993 std::vector<IntegerVariable>* vars,
996 for (
int t = 0; t < helper->
NumTasks(); ++t) {
999 vars->push_back(helper->
Starts()[t].var);
1003 vars->push_back(helper->
Sizes()[t].var);
1007 vars->push_back(helper->
Ends()[t].var);
1017 vars->push_back(view);
1024 Model* model, std::vector<IntegerVariable>* vars) {
1027 if (!integer_trail->IsFixed(demand_expr)) {
1028 vars->push_back(demand_expr.var);
1033 for (
const auto& lit_val_val : product) {
1038 vars->push_back(view);
1042 if (!integer_trail->IsFixed(capacity)) {
1043 vars->push_back(capacity.
var);
void Clear(IndexType i)
Sets the bit at position i to 0.
void Resize(IndexType size)
void Set(IndexType i)
Sets the bit at position i to 1.
void SetPropagatorPriority(int id, int priority)
void WatchIntegerVariable(IntegerVariable i, int id, int watch_index=-1)
int Register(PropagatorInterface *propagator)
Registers a propagator and returns its unique ids.
ABSL_MUST_USE_RESULT bool LiteralOrNegationHasView(Literal lit, IntegerVariable *view=nullptr, bool *view_is_direct=nullptr) const
IntegerValue LowerBound(IntegerVariable i) const
Returns the current lower/upper bound of the given integer variable.
IntegerValue UpperBound(IntegerVariable i) const
ABSL_MUST_USE_RESULT bool AddLiteralTerm(Literal lit, IntegerValue coeff=IntegerValue(1))
void AddTerm(IntegerVariable var, IntegerValue coeff)
T Add(std::function< T(Model *)> f)
Helper class to express a product as a linear constraint.
bool IncrementalPropagate(const std::vector< int > &watch_indices) final
absl::Span< const TaskTime > TaskByIncreasingStartMin()
void AddReasonForBeingBefore(int before, int after)
Produces a relaxed reason for StartMax(before) < EndMin(after).
void AddStartMaxReason(int t, IntegerValue upper_bound)
ABSL_MUST_USE_RESULT bool IncreaseEndMin(int t, IntegerValue value)
IntegerValue ShiftedStartMin(int t) const
void WatchAllTasks(int id)
void RegisterWith(GenericLiteralWatcher *watcher)
absl::Span< const TaskTime > TaskByIncreasingNegatedStartMax()
void AddSizeMaxReason(int t, IntegerValue upper_bound)
IntegerValue StartMax(int t) const
bool PropagatePrecedence(int a, int b)
ABSL_MUST_USE_RESULT bool DecreaseEndMax(int t, IntegerValue value)
int NumTasks() const
Returns the number of task.
ABSL_MUST_USE_RESULT bool ReportConflict()
absl::Span< const AffineExpression > Sizes() const
ABSL_MUST_USE_RESULT bool PushLiteral(Literal l)
const std::vector< ProfileEvent > & GetEnergyProfile()
ABSL_MUST_USE_RESULT bool PushTaskAbsence(int t)
bool IsPresent(int t) const
void SetTimeDirection(bool is_forward)
ABSL_MUST_USE_RESULT bool PushIntegerLiteralIfTaskPresent(int t, IntegerLiteral lit)
Literal PresenceLiteral(int index) const
void AddSizeMinReason(int t)
absl::Span< const AffineExpression > Starts() const
void AddEndMaxReason(int t, IntegerValue upper_bound)
absl::Span< const AffineExpression > Ends() const
ABSL_MUST_USE_RESULT bool ResetFromSubset(const SchedulingConstraintHelper &other, absl::Span< const int > tasks)
ABSL_MUST_USE_RESULT bool IncreaseStartMin(int t, IntegerValue value)
IntegerValue SizeMax(int t) const
void AddEndMinReason(int t, IntegerValue lower_bound)
IntegerValue StartMin(int t) const
void ClearReason()
Functions to clear and then set the current reason.
SchedulingConstraintHelper(std::vector< AffineExpression > starts, std::vector< AffineExpression > ends, std::vector< AffineExpression > sizes, std::vector< LiteralIndex > reason_for_presence, Model *model)
IntegerValue SizeMin(int t) const
absl::Span< const CachedTaskBounds > TaskByIncreasingShiftedStartMin()
absl::Span< const TaskTime > TaskByDecreasingEndMax()
ABSL_MUST_USE_RESULT bool SynchronizeAndSetTimeDirection(bool is_forward)
ABSL_MUST_USE_RESULT bool PushTaskPresence(int t)
IntegerValue EndMax(int t) const
bool IsAbsent(int t) const
IntegerValue EndMin(int t) const
bool IsOptional(int t) const
std::string TaskDebugString(int t) const
Returns a string with the current task bounds.
ABSL_MUST_USE_RESULT bool PushIntegerLiteral(IntegerLiteral lit)
void ImportOtherReasons(const SchedulingConstraintHelper &other_helper)
IntegerValue GetCurrentMinDistanceBetweenTasks(int a, int b, bool add_reason_if_after=false)
IntegerValue GetMinOverlap(int t, IntegerValue start, IntegerValue end) const
void AddStartMinReason(int t, IntegerValue lower_bound)
absl::Span< const TaskTime > TaskByIncreasingEndMin()
void AddDemandMinReason(int t)
std::vector< LiteralValueValue > FilteredDecomposedEnergy(int index)
bool DemandIsFixed(int t) const
bool CacheAllEnergyValues()
IntegerValue EnergyMinInWindow(int t, IntegerValue window_start, IntegerValue window_end)
const std::vector< std::vector< LiteralValueValue > > & DecomposedEnergies() const
void AddEnergyMinReason(int t)
void InitDecomposedEnergies()
void AddEnergyMinInWindowReason(int t, IntegerValue window_start, IntegerValue window_end)
IntegerValue DemandMax(int t) const
SchedulingDemandHelper(absl::Span< const AffineExpression > demands, SchedulingConstraintHelper *helper, Model *model)
void OverrideDecomposedEnergies(const std::vector< std::vector< LiteralValueValue > > &energies)
Visible for testing.
const std::vector< AffineExpression > & Demands() const
ABSL_MUST_USE_RESULT bool DecreaseEnergyMax(int t, IntegerValue value)
IntegerValue DemandMin(int t) const
ABSL_MUST_USE_RESULT bool AddLinearizedDemand(int t, LinearConstraintBuilder *builder) const
IntegerValue EnergyMin(int t) const
bool LiteralIsFalse(Literal literal) const
bool LiteralIsTrue(Literal literal) const
std::function< IntegerVariable(Model *)> NewIntegerVariableFromLiteral(Literal lit)
constexpr IntegerValue kMaxIntegerValue(std::numeric_limits< IntegerValue::ValueType >::max() - 1)
const LiteralIndex kNoLiteralIndex(-1)
std::function< int64_t(const Model &)> LowerBound(IntegerVariable v)
std::function< void(Model *)> LowerOrEqual(IntegerVariable v, int64_t ub)
std::vector< IntegerVariable > NegationOf(absl::Span< const IntegerVariable > vars)
Returns the vector of the negated variables.
constexpr IntegerValue kMinIntegerValue(-kMaxIntegerValue.value())
const IntegerVariable kNoIntegerVariable(-1)
void AddIntegerVariableFromIntervals(const SchedulingConstraintHelper *helper, Model *model, std::vector< IntegerVariable > *vars, int mask)
void AppendVariablesFromCapacityAndDemands(const AffineExpression &capacity, SchedulingDemandHelper *demands_helper, Model *model, std::vector< IntegerVariable > *vars)
void AddWeightedSumLowerOrEqual(absl::Span< const Literal > enforcement_literals, absl::Span< const IntegerVariable > vars, absl::Span< const int64_t > coefficients, int64_t upper_bound, Model *model)
enforcement_literals => sum <= upper_bound
std::function< void(Model *)> GreaterOrEqual(IntegerVariable v, int64_t lb)
IntegerValue CapProdI(IntegerValue a, IntegerValue b)
Overflows and saturated arithmetic.
IntegerValue ComputeEnergyMinInWindow(IntegerValue start_min, IntegerValue start_max, IntegerValue end_min, IntegerValue end_max, IntegerValue size_min, IntegerValue demand_min, absl::Span< const LiteralValueValue > filtered_energy, IntegerValue window_start, IntegerValue window_end)
In SWIG mode, we don't want anything besides these top-level includes.
void IncrementalSort(int max_comparisons, Iterator begin, Iterator end, Compare comp=Compare{}, bool is_stable=false)
IntegerLiteral Negated() const
The negation of x >= bound is x <= bound - 1.