32#include "absl/container/flat_hash_map.h"
33#include "absl/strings/str_cat.h"
34#include "absl/strings/str_format.h"
35#include "absl/strings/str_join.h"
36#include "absl/strings/string_view.h"
57bool StartMinLessThan(Task*
const w1, Task*
const w2) {
58 return (w1->interval->StartMin() < w2->interval->StartMin());
65bool ShortestDurationStartMinLessThan(Task*
const w1, Task*
const w2) {
66 return w1->interval->EndMin() - w1->interval->DurationMin() <
67 w2->interval->EndMin() - w2->interval->DurationMin();
71bool StartMaxLessThan(Task*
const w1, Task*
const w2) {
72 return (w1->interval->StartMax() < w2->interval->StartMax());
76bool EndMinLessThan(Task*
const w1, Task*
const w2) {
77 return (w1->interval->EndMin() < w2->interval->EndMin());
81bool EndMaxLessThan(Task*
const w1, Task*
const w2) {
82 return (w1->interval->EndMax() < w2->interval->EndMax());
85bool IntervalStartMinLessThan(IntervalVar* i1, IntervalVar* i2) {
86 return i1->StartMin() < i2->StartMin();
95struct DisjunctiveTask {
96 explicit DisjunctiveTask(IntervalVar*
const interval_)
99 std::string DebugString()
const {
return interval->DebugString(); }
110struct CumulativeTask {
111 CumulativeTask(IntervalVar*
const interval_, int64_t demand_)
114 int64_t EnergyMin()
const {
return interval->DurationMin() *
demand; }
116 int64_t DemandMin()
const {
return demand; }
118 void WhenAnything(Demon*
const demon) {
interval->WhenAnything(demon); }
120 std::string DebugString()
const {
121 return absl::StrFormat(
"Task{ %s, demand: %d }",
interval->DebugString(),
136struct VariableCumulativeTask {
137 VariableCumulativeTask(IntervalVar*
const interval_, IntVar* demand_)
140 int64_t EnergyMin()
const {
return interval->DurationMin() *
demand->Min(); }
142 int64_t DemandMin()
const {
return demand->Min(); }
144 void WhenAnything(Demon*
const demon) {
149 std::string DebugString()
const {
150 return absl::StrFormat(
"Task{ %s, demand: %s }",
interval->DebugString(),
171 explicit ThetaNode(
const IntervalVar*
const interval)
184 void Compute(
const ThetaNode& left,
const ThetaNode& right) {
190 bool IsIdentity()
const {
192 total_ect == std::numeric_limits<int64_t>::min();
195 std::string DebugString()
const {
196 return absl::StrCat(
"ThetaNode{ p = ", total_processing,
197 ", e = ", total_ect < 0LL ? -1LL : total_ect,
" }");
211class ThetaTree :
public MonoidOperationTree<ThetaNode> {
213 explicit ThetaTree(
int size) : MonoidOperationTree<ThetaNode>(
size) {}
215 int64_t Ect()
const {
return result().total_ect; }
217 void Insert(
const DisjunctiveTask*
const task) {
218 Set(task->index, ThetaNode(task->interval));
221 void Remove(
const DisjunctiveTask*
const task) { Reset(task->index); }
223 bool IsInserted(
const DisjunctiveTask*
const task)
const {
224 return !GetOperand(task->index).IsIdentity();
234struct LambdaThetaNode {
248 LambdaThetaNode(int64_t capacity,
const CumulativeTask& task)
249 :
energy(task.EnergyMin()),
257 LambdaThetaNode(int64_t capacity,
const CumulativeTask& task,
int index)
269 LambdaThetaNode(int64_t capacity,
const VariableCumulativeTask& task)
270 :
energy(task.EnergyMin()),
278 LambdaThetaNode(int64_t capacity,
const VariableCumulativeTask& task,
291 explicit LambdaThetaNode(
const IntervalVar*
const interval)
301 LambdaThetaNode(
const IntervalVar*
const interval,
int index)
318 void Compute(
const LambdaThetaNode& left,
const LambdaThetaNode& right) {
321 CapAdd(left.energetic_end_min, right.energy));
322 const int64_t energy_left_opt =
CapAdd(left.energy_opt, right.energy);
323 const int64_t energy_right_opt =
CapAdd(left.energy, right.energy_opt);
324 if (energy_left_opt > energy_right_opt) {
331 const int64_t ect1 = right.energetic_end_min_opt;
332 const int64_t ect2 =
CapAdd(left.energetic_end_min, right.energy_opt);
333 const int64_t ect3 =
CapAdd(left.energetic_end_min_opt, right.energy);
334 if (ect1 >= ect2 && ect1 >= ect3) {
337 }
else if (ect2 >= ect1 && ect2 >= ect3) {
346 DCHECK(energy_opt >= energy);
350 DCHECK((argmax_energy_opt != kNone) || (energy_opt == energy));
376const int LambdaThetaNode::kNone = -1;
379class DisjunctiveLambdaThetaTree :
public MonoidOperationTree<LambdaThetaNode> {
381 explicit DisjunctiveLambdaThetaTree(
int size)
382 : MonoidOperationTree<LambdaThetaNode>(
size) {}
384 void Insert(
const DisjunctiveTask& task) {
385 Set(task.index, LambdaThetaNode(task.interval));
388 void Grey(
const DisjunctiveTask& task) {
389 const int index = task.index;
390 Set(
index, LambdaThetaNode(task.interval,
index));
393 int64_t Ect()
const {
return result().energetic_end_min; }
394 int64_t EctOpt()
const {
return result().energetic_end_min_opt; }
395 int ResponsibleOpt()
const {
return result().argmax_energetic_end_min_opt; }
399class CumulativeLambdaThetaTree :
public MonoidOperationTree<LambdaThetaNode> {
401 CumulativeLambdaThetaTree(
int size, int64_t capacity_max)
402 : MonoidOperationTree<LambdaThetaNode>(
size),
403 capacity_max_(capacity_max) {}
405 void Init(int64_t capacity_max) {
407 capacity_max_ = capacity_max;
410 void Insert(
const CumulativeTask& task) {
411 Set(task.index, LambdaThetaNode(capacity_max_, task));
414 void Grey(
const CumulativeTask& task) {
415 const int index = task.index;
416 Set(
index, LambdaThetaNode(capacity_max_, task,
index));
419 void Insert(
const VariableCumulativeTask& task) {
420 Set(task.index, LambdaThetaNode(capacity_max_, task));
423 void Grey(
const VariableCumulativeTask& task) {
424 const int index = task.index;
425 Set(
index, LambdaThetaNode(capacity_max_, task,
index));
430 return result().energetic_end_min_opt;
432 int64_t Ect()
const {
435 int64_t EctOpt()
const {
439 return result().argmax_energetic_end_min_opt;
443 int64_t capacity_max_;
452 NotLast(Solver* solver,
const std::vector<IntervalVar*>& intervals,
453 bool mirror,
bool strict);
460 ThetaTree theta_tree_;
461 std::vector<DisjunctiveTask*> by_start_min_;
462 std::vector<DisjunctiveTask*> by_end_max_;
463 std::vector<DisjunctiveTask*> by_start_max_;
464 std::vector<int64_t> new_lct_;
468NotLast::NotLast(Solver*
const solver,
469 const std::vector<IntervalVar*>& intervals,
bool mirror,
471 : theta_tree_(intervals.
size()),
472 by_start_min_(intervals.
size()),
473 by_end_max_(intervals.
size()),
474 by_start_max_(intervals.
size()),
475 new_lct_(intervals.
size(), -1LL),
478 for (
int i = 0;
i < intervals.size(); ++
i) {
479 IntervalVar*
const underlying =
480 mirror ? solver->MakeMirrorInterval(intervals[i]) : intervals[
i];
481 IntervalVar*
const relaxed = solver->MakeIntervalRelaxedMin(underlying);
482 by_start_min_[
i] =
new DisjunctiveTask(relaxed);
483 by_end_max_[
i] = by_start_min_[
i];
484 by_start_max_[
i] = by_start_min_[
i];
488bool NotLast::Propagate() {
490 std::sort(by_start_max_.begin(), by_start_max_.end(),
491 StartMaxLessThan<DisjunctiveTask>);
492 std::sort(by_end_max_.begin(), by_end_max_.end(),
493 EndMaxLessThan<DisjunctiveTask>);
495 std::sort(by_start_min_.begin(), by_start_min_.end(),
496 StartMinLessThan<DisjunctiveTask>);
497 for (
int i = 0;
i < by_start_min_.size(); ++
i) {
498 by_start_min_[
i]->index =
i;
501 for (
int i = 0;
i < by_start_min_.size(); ++
i) {
502 new_lct_[
i] = by_start_min_[
i]->interval->EndMax();
507 for (DisjunctiveTask*
const twi : by_end_max_) {
508 while (j < by_start_max_.size() &&
509 twi->interval->EndMax() > by_start_max_[j]->interval->StartMax()) {
510 if (j > 0 && theta_tree_.Ect() > by_start_max_[j]->interval->StartMax()) {
511 const int64_t new_end_max = by_start_max_[j - 1]->interval->StartMax();
512 new_lct_[by_start_max_[j]->index] =
513 std::min(new_lct_[by_start_max_[j]->
index], new_end_max);
515 theta_tree_.Insert(by_start_max_[j]);
518 const bool inserted = theta_tree_.IsInserted(twi);
520 theta_tree_.Remove(twi);
522 const int64_t ect_theta_less_i = theta_tree_.Ect();
524 theta_tree_.Insert(twi);
527 if (ect_theta_less_i > twi->interval->StartMax() && j > 0) {
528 const int64_t new_end_max = by_start_max_[j - 1]->interval->StartMax();
529 if (new_end_max < new_lct_[twi->index]) {
530 new_lct_[twi->index] = new_end_max;
536 bool modified =
false;
537 for (
int i = 0;
i < by_start_min_.size(); ++
i) {
538 IntervalVar*
const var = by_start_min_[
i]->interval;
539 if ((strict_ ||
var->DurationMin() > 0) &&
var->EndMax() > new_lct_[i]) {
541 var->SetEndMax(new_lct_[i]);
552class EdgeFinderAndDetectablePrecedences {
554 EdgeFinderAndDetectablePrecedences(Solver* solver,
555 const std::vector<IntervalVar*>& intervals,
556 bool mirror,
bool strict);
557 ~EdgeFinderAndDetectablePrecedences() {
560 int64_t
size()
const {
return by_start_min_.size(); }
563 void OverloadChecking();
564 bool DetectablePrecedences();
568 Solver*
const solver_;
579 ThetaTree theta_tree_;
580 std::vector<DisjunctiveTask*> by_end_min_;
581 std::vector<DisjunctiveTask*> by_start_min_;
582 std::vector<DisjunctiveTask*> by_end_max_;
583 std::vector<DisjunctiveTask*> by_start_max_;
585 std::vector<int64_t> new_est_;
587 std::vector<int64_t> new_lct_;
588 DisjunctiveLambdaThetaTree lt_tree_;
592EdgeFinderAndDetectablePrecedences::EdgeFinderAndDetectablePrecedences(
593 Solver*
const solver,
const std::vector<IntervalVar*>& intervals,
594 bool mirror,
bool strict)
596 theta_tree_(intervals.
size()),
597 lt_tree_(intervals.
size()),
600 for (IntervalVar*
const interval : intervals) {
601 IntervalVar*
const underlying =
603 IntervalVar*
const relaxed = solver->MakeIntervalRelaxedMax(underlying);
604 DisjunctiveTask*
const task =
new DisjunctiveTask(relaxed);
605 by_end_min_.push_back(task);
606 by_start_min_.push_back(task);
607 by_end_max_.push_back(task);
608 by_start_max_.push_back(task);
609 new_est_.push_back(std::numeric_limits<int64_t>::min());
613void EdgeFinderAndDetectablePrecedences::UpdateEst() {
614 std::sort(by_start_min_.begin(), by_start_min_.end(),
615 ShortestDurationStartMinLessThan<DisjunctiveTask>);
616 for (
int i = 0;
i <
size(); ++
i) {
617 by_start_min_[
i]->index =
i;
621void EdgeFinderAndDetectablePrecedences::OverloadChecking() {
624 std::sort(by_end_max_.begin(), by_end_max_.end(),
625 EndMaxLessThan<DisjunctiveTask>);
628 for (DisjunctiveTask*
const task : by_end_max_) {
629 theta_tree_.Insert(task);
630 if (theta_tree_.Ect() > task->interval->EndMax()) {
636bool EdgeFinderAndDetectablePrecedences::DetectablePrecedences() {
639 new_est_.assign(
size(), std::numeric_limits<int64_t>::min());
642 std::sort(by_end_min_.begin(), by_end_min_.end(),
643 EndMinLessThan<DisjunctiveTask>);
644 std::sort(by_start_max_.begin(), by_start_max_.end(),
645 StartMaxLessThan<DisjunctiveTask>);
648 for (DisjunctiveTask*
const task_i : by_end_min_) {
650 DisjunctiveTask* task_j = by_start_max_[j];
651 while (task_i->interval->EndMin() > task_j->interval->StartMax()) {
652 theta_tree_.Insert(task_j);
654 if (j ==
size())
break;
655 task_j = by_start_max_[j];
658 const int64_t esti = task_i->interval->StartMin();
659 bool inserted = theta_tree_.IsInserted(task_i);
661 theta_tree_.Remove(task_i);
663 const int64_t oesti = theta_tree_.Ect();
665 theta_tree_.Insert(task_i);
668 new_est_[task_i->index] = oesti;
670 new_est_[task_i->index] = std::numeric_limits<int64_t>::min();
675 bool modified =
false;
676 for (
int i = 0;
i <
size(); ++
i) {
677 IntervalVar*
const var = by_start_min_[
i]->interval;
678 if (new_est_[i] != std::numeric_limits<int64_t>::min() &&
679 (strict_ ||
var->DurationMin() > 0)) {
681 by_start_min_[
i]->interval->SetStartMin(new_est_[i]);
687bool EdgeFinderAndDetectablePrecedences::EdgeFinder() {
690 for (
int i = 0;
i <
size(); ++
i) {
691 new_est_[
i] = by_start_min_[
i]->interval->StartMin();
695 std::sort(by_end_max_.begin(), by_end_max_.end(),
696 EndMaxLessThan<DisjunctiveTask>);
698 for (
int i = 0;
i <
size(); ++
i) {
699 lt_tree_.Insert(*by_start_min_[i]);
700 DCHECK_EQ(i, by_start_min_[i]->
index);
702 for (
int j =
size() - 2; j >= 0; --j) {
703 lt_tree_.Grey(*by_end_max_[j + 1]);
704 DisjunctiveTask*
const twj = by_end_max_[j];
706 DCHECK_LE(lt_tree_.Ect(), twj->interval->EndMax());
707 while (lt_tree_.EctOpt() > twj->interval->EndMax()) {
708 const int i = lt_tree_.ResponsibleOpt();
710 if (lt_tree_.Ect() > new_est_[i]) {
711 new_est_[
i] = lt_tree_.Ect();
718 bool modified =
false;
719 for (
int i = 0;
i <
size(); ++
i) {
720 IntervalVar*
const var = by_start_min_[
i]->interval;
721 if (
var->StartMin() < new_est_[i] && (strict_ ||
var->DurationMin() > 0)) {
723 var->SetStartMin(new_est_[i]);
733class RankedPropagator :
public Constraint {
735 RankedPropagator(Solver*
const solver,
const std::vector<IntVar*>& nexts,
736 const std::vector<IntervalVar*>& intervals,
737 const std::vector<IntVar*>& slacks,
738 DisjunctiveConstraint*
const disjunctive)
739 : Constraint(solver),
741 intervals_(intervals),
743 disjunctive_(disjunctive),
744 partial_sequence_(intervals.
size()),
745 previous_(intervals.
size() + 2, 0) {}
747 ~RankedPropagator()
override {}
749 void Post()
override {
750 Demon*
const delayed =
751 solver()->MakeDelayedConstraintInitialPropagateCallback(
this);
752 for (
int i = 0;
i < intervals_.size(); ++
i) {
753 nexts_[
i]->WhenBound(delayed);
754 intervals_[
i]->WhenAnything(delayed);
755 slacks_[
i]->WhenRange(delayed);
757 nexts_.back()->WhenBound(delayed);
760 void InitialPropagate()
override {
765 void PropagateNexts() {
766 Solver*
const s = solver();
767 const int ranked_first = partial_sequence_.NumFirstRanked();
768 const int ranked_last = partial_sequence_.NumLastRanked();
772 : partial_sequence_[intervals_.size() - ranked_last] + 1;
775 while (nexts_[first]->Bound()) {
776 DCHECK_NE(first, nexts_[first]->Min());
777 first = nexts_[first]->Min();
778 if (first == sentinel) {
781 if (++counter > ranked_first) {
782 DCHECK(intervals_[first - 1]->MayBePerformed());
783 partial_sequence_.RankFirst(s, first - 1);
784 VLOG(2) <<
"RankFirst " << first - 1 <<
" -> "
785 << partial_sequence_.DebugString();
788 previous_.assign(previous_.size(), -1);
789 for (
int i = 0;
i < nexts_.size(); ++
i) {
790 if (nexts_[i]->Bound()) {
791 previous_[nexts_[
i]->Min()] =
i;
794 int last = previous_.size() - 1;
796 while (previous_[last] != -1) {
797 last = previous_[last];
798 if (++counter > ranked_last) {
799 partial_sequence_.RankLast(s, last - 1);
800 VLOG(2) <<
"RankLast " << last - 1 <<
" -> "
801 << partial_sequence_.DebugString();
806 void PropagateSequence() {
807 const int last_position = intervals_.size() - 1;
808 const int first_sentinel = partial_sequence_.NumFirstRanked();
809 const int last_sentinel = last_position - partial_sequence_.NumLastRanked();
811 for (
int i = 0;
i < first_sentinel - 1; ++
i) {
812 IntervalVar*
const interval = RankedInterval(i);
813 IntervalVar*
const next_interval = RankedInterval(i + 1);
814 IntVar*
const slack = RankedSlack(i);
815 const int64_t transition_time = RankedTransitionTime(i, i + 1);
816 next_interval->SetStartRange(
821 for (
int i = last_position;
i > last_sentinel + 1; --
i) {
822 IntervalVar*
const interval = RankedInterval(i - 1);
823 IntervalVar*
const next_interval = RankedInterval(i);
824 IntVar*
const slack = RankedSlack(i - 1);
825 const int64_t transition_time = RankedTransitionTime(i - 1, i);
827 CapAdd(slack->Max(), transition_time)),
828 CapSub(next_interval->StartMax(),
829 CapAdd(slack->Min(), transition_time)));
832 IntervalVar*
const first_interval =
833 first_sentinel > 0 ? RankedInterval(first_sentinel - 1) : nullptr;
834 IntVar*
const first_slack =
835 first_sentinel > 0 ? RankedSlack(first_sentinel - 1) : nullptr;
836 IntervalVar*
const last_interval = last_sentinel < last_position
837 ? RankedInterval(last_sentinel + 1)
841 if (first_interval ==
nullptr && last_interval ==
nullptr) {
846 for (
int i = first_sentinel;
i <= last_sentinel; ++
i) {
847 IntervalVar*
const interval = RankedInterval(i);
848 IntVar*
const slack = RankedSlack(i);
851 if (first_interval !=
nullptr) {
852 const int64_t transition_time =
853 RankedTransitionTime(first_sentinel - 1, i);
855 CapAdd(first_interval->StartMin(),
856 CapAdd(first_slack->Min(), transition_time)),
857 CapAdd(first_interval->StartMax(),
858 CapAdd(first_slack->Max(), transition_time)));
860 first_interval->SetStartRange(
862 CapAdd(first_slack->Max(), transition_time)),
864 CapAdd(first_slack->Min(), transition_time)));
867 if (last_interval !=
nullptr) {
868 const int64_t transition_time =
869 RankedTransitionTime(i, last_sentinel + 1);
871 CapSub(last_interval->StartMin(),
872 CapAdd(slack->Max(), transition_time)),
873 CapSub(last_interval->StartMax(),
874 CapAdd(slack->Min(), transition_time)));
876 last_interval->SetStartRange(
878 CapAdd(slack->Min(), transition_time)),
880 CapAdd(slack->Max(), transition_time)));
887 for (
int i = std::min(first_sentinel - 2, last_position - 1);
i >= 0; --
i) {
888 IntervalVar*
const interval = RankedInterval(i);
889 IntervalVar*
const next_interval = RankedInterval(i + 1);
890 IntVar*
const slack = RankedSlack(i);
891 const int64_t transition_time = RankedTransitionTime(i, i + 1);
893 CapAdd(slack->Max(), transition_time)),
894 CapSub(next_interval->StartMax(),
895 CapAdd(slack->Min(), transition_time)));
898 for (
int i = last_sentinel + 1;
i < last_position - 1; ++
i) {
899 IntervalVar*
const interval = RankedInterval(i);
900 IntervalVar*
const next_interval = RankedInterval(i + 1);
901 IntVar*
const slack = RankedSlack(i);
902 const int64_t transition_time = RankedTransitionTime(i, i + 1);
903 next_interval->SetStartRange(
910 IntervalVar* RankedInterval(
int i)
const {
911 const int index = partial_sequence_[
i];
912 return intervals_[
index];
915 IntVar* RankedSlack(
int i)
const {
916 const int index = partial_sequence_[
i];
917 return slacks_[
index];
920 int64_t RankedTransitionTime(
int before,
int after)
const {
921 const int before_index = partial_sequence_[before];
922 const int after_index = partial_sequence_[after];
924 return disjunctive_->TransitionTime(before_index, after_index);
927 std::string DebugString()
const override {
928 return absl::StrFormat(
929 "RankedPropagator([%s], nexts = [%s], intervals = [%s])",
934 void Accept(ModelVisitor*
const visitor)
const override {
935 LOG(FATAL) <<
"Not yet implemented";
940 std::vector<IntVar*> nexts_;
941 std::vector<IntervalVar*> intervals_;
942 std::vector<IntVar*> slacks_;
943 DisjunctiveConstraint*
const disjunctive_;
944 RevPartialSequence partial_sequence_;
945 std::vector<int> previous_;
951class FullDisjunctiveConstraint :
public DisjunctiveConstraint {
953 FullDisjunctiveConstraint(Solver*
const s,
954 const std::vector<IntervalVar*>& intervals,
955 const std::string&
name,
bool strict)
956 : DisjunctiveConstraint(s, intervals,
name),
957 sequence_var_(nullptr),
958 straight_(s, intervals,
false, strict),
959 mirror_(s, intervals,
true, strict),
960 straight_not_last_(s, intervals,
false, strict),
961 mirror_not_last_(s, intervals,
true, strict),
965 FullDisjunctiveConstraint(
const FullDisjunctiveConstraint&) =
delete;
966 FullDisjunctiveConstraint& operator=(
const FullDisjunctiveConstraint&) =
969 ~FullDisjunctiveConstraint()
override {}
971 void Post()
override {
973 solver(),
this, &FullDisjunctiveConstraint::InitialPropagate,
975 for (int32_t i = 0;
i < straight_.size(); ++
i) {
976 straight_.interval(i)->WhenAnything(d);
980 void InitialPropagate()
override {
981 bool all_optional_or_unperformed =
true;
982 for (
const IntervalVar*
const interval : intervals_) {
984 all_optional_or_unperformed =
false;
988 if (all_optional_or_unperformed) {
992 bool all_times_fixed =
true;
993 for (
const IntervalVar*
const interval : intervals_) {
998 all_times_fixed =
false;
1003 if (all_times_fixed) {
1004 PropagatePerformed();
1011 straight_.OverloadChecking();
1012 }
while (straight_.DetectablePrecedences() ||
1013 mirror_.DetectablePrecedences());
1014 }
while (straight_not_last_.Propagate() ||
1015 mirror_not_last_.Propagate());
1016 }
while (straight_.EdgeFinder() || mirror_.EdgeFinder());
1020 bool Intersect(IntervalVar*
const i1, IntervalVar*
const i2)
const {
1021 return i1->StartMin() < i2->EndMax() && i2->StartMin() < i1->EndMax();
1024 void PropagatePerformed() {
1027 for (IntervalVar*
const interval : intervals_) {
1030 }
else if (
interval->MayBePerformed()) {
1035 if (performed_.empty())
return;
1036 std::sort(performed_.begin(), performed_.end(), IntervalStartMinLessThan);
1037 for (
int i = 0;
i < performed_.size() - 1; ++
i) {
1038 if (performed_[i]->EndMax() > performed_[i + 1]->StartMin()) {
1044 if (optional_.empty())
return;
1046 const int num_performed = performed_.size();
1047 std::sort(optional_.begin(), optional_.end(), IntervalStartMinLessThan);
1048 for (IntervalVar*
const candidate : optional_) {
1049 const int64_t
start = candidate->StartMin();
1053 if (
index == num_performed)
return;
1054 if (Intersect(candidate, performed_[
index]) ||
1055 (
index < num_performed - 1 &&
1056 Intersect(candidate, performed_[
index + 1]))) {
1057 candidate->SetPerformed(
false);
1062 void Accept(ModelVisitor*
const visitor)
const override {
1063 visitor->BeginVisitConstraint(ModelVisitor::kDisjunctive,
this);
1064 visitor->VisitIntervalArrayArgument(ModelVisitor::kIntervalsArgument,
1066 if (sequence_var_ !=
nullptr) {
1067 visitor->VisitSequenceArgument(ModelVisitor::kSequenceArgument,
1070 visitor->EndVisitConstraint(ModelVisitor::kDisjunctive,
this);
1073 SequenceVar* MakeSequenceVar()
override {
1074 BuildNextModelIfNeeded();
1075 if (sequence_var_ ==
nullptr) {
1076 solver()->SaveValue(
reinterpret_cast<void**
>(&sequence_var_));
1077 sequence_var_ = solver()->RevAlloc(
1078 new SequenceVar(solver(), intervals_, nexts_,
name()));
1080 return sequence_var_;
1083 std::string DebugString()
const override {
1084 return absl::StrFormat(
"FullDisjunctiveConstraint([%s], %i)",
1088 const std::vector<IntVar*>& nexts()
const override {
return nexts_; }
1090 const std::vector<IntVar*>& actives()
const override {
return actives_; }
1092 const std::vector<IntVar*>& time_cumuls()
const override {
1093 return time_cumuls_;
1096 const std::vector<IntVar*>& time_slacks()
const override {
1097 return time_slacks_;
1101 int64_t
Distance(int64_t activity_plus_one, int64_t next_activity_plus_one) {
1102 return (activity_plus_one == 0 ||
1103 next_activity_plus_one > intervals_.size())
1105 : transition_time_(activity_plus_one - 1,
1106 next_activity_plus_one - 1);
1109 void BuildNextModelIfNeeded() {
1110 if (!nexts_.empty()) {
1113 Solver*
const s = solver();
1114 const std::string& ct_name =
name();
1115 const int num_intervals = intervals_.size();
1116 const int num_nodes = intervals_.size() + 1;
1117 int64_t horizon = 0;
1118 for (
int i = 0;
i < intervals_.size(); ++
i) {
1119 if (intervals_[i]->MayBePerformed()) {
1120 horizon = std::max(horizon, intervals_[i]->EndMax());
1125 s->MakeIntVarArray(num_nodes, 1, num_nodes, ct_name +
"_nexts", &nexts_);
1127 s->AddConstraint(s->MakeAllDifferent(nexts_));
1129 actives_.resize(num_nodes);
1130 for (
int i = 0;
i < num_intervals; ++
i) {
1131 actives_[
i + 1] = intervals_[
i]->PerformedExpr()->Var();
1133 s->MakeIsDifferentCstCt(nexts_[i + 1], i + 1, actives_[i + 1]));
1135 std::vector<IntVar*> short_actives(actives_.begin() + 1, actives_.end());
1136 actives_[0] = s->MakeMax(short_actives)->Var();
1139 s->AddConstraint(s->MakeNoCycle(nexts_, actives_));
1142 time_cumuls_.resize(num_nodes + 1);
1144 time_slacks_.resize(num_nodes);
1146 time_slacks_[0] = s->MakeIntVar(0, horizon,
"initial_slack");
1148 time_cumuls_[0] = s->MakeIntConst(0);
1150 for (int64_t i = 0;
i < num_intervals; ++
i) {
1151 IntervalVar*
const var = intervals_[
i];
1152 if (
var->MayBePerformed()) {
1153 const int64_t duration_min =
var->DurationMin();
1154 time_slacks_[
i + 1] = s->MakeIntVar(
1155 duration_min, horizon, absl::StrFormat(
"time_slacks(%d)", i + 1));
1157 time_cumuls_[
i + 1] =
var->SafeStartExpr(
var->StartMin())->Var();
1158 if (
var->DurationMax() != duration_min) {
1159 s->AddConstraint(s->MakeGreaterOrEqual(
1160 time_slacks_[i + 1],
var->SafeDurationExpr(duration_min)));
1163 time_slacks_[
i + 1] = s->MakeIntVar(
1164 0, horizon, absl::StrFormat(
"time_slacks(%d)", i + 1));
1165 time_cumuls_[
i + 1] = s->MakeIntConst(horizon);
1169 time_cumuls_[num_nodes] = s->MakeIntVar(0, 2 * horizon, ct_name +
"_ect");
1170 s->AddConstraint(s->MakePathCumul(
1171 nexts_, actives_, time_cumuls_, time_slacks_,
1172 [
this](int64_t
x, int64_t
y) { return Distance(x, y); }));
1174 std::vector<IntVar*> short_slacks(time_slacks_.begin() + 1,
1175 time_slacks_.end());
1176 s->AddConstraint(s->RevAlloc(
1177 new RankedPropagator(s, nexts_, intervals_, short_slacks,
this)));
1180 SequenceVar* sequence_var_;
1181 EdgeFinderAndDetectablePrecedences straight_;
1182 EdgeFinderAndDetectablePrecedences mirror_;
1183 NotLast straight_not_last_;
1184 NotLast mirror_not_last_;
1185 std::vector<IntVar*> nexts_;
1186 std::vector<IntVar*> actives_;
1187 std::vector<IntVar*> time_cumuls_;
1188 std::vector<IntVar*> time_slacks_;
1189 std::vector<IntervalVar*> performed_;
1190 std::vector<IntervalVar*> optional_;
1200struct DualCapacityThetaNode {
1202 static const int kNone;
1205 DualCapacityThetaNode()
1211 DualCapacityThetaNode(int64_t capacity, int64_t residual_capacity,
1212 const CumulativeTask& task)
1213 :
energy(task.EnergyMin()),
1219 DualCapacityThetaNode(int64_t capacity, int64_t residual_capacity,
1220 const VariableCumulativeTask& task)
1221 :
energy(task.EnergyMin()),
1232 void Compute(
const DualCapacityThetaNode& left,
1233 const DualCapacityThetaNode& right) {
1236 right.energetic_end_min);
1238 std::max(
CapAdd(left.residual_energetic_end_min, right.energy),
1239 right.residual_energetic_end_min);
1253const int DualCapacityThetaNode::kNone = -1;
1256class DualCapacityThetaTree
1257 :
public MonoidOperationTree<DualCapacityThetaNode> {
1261 explicit DualCapacityThetaTree(
int size)
1262 : MonoidOperationTree<DualCapacityThetaNode>(
size),
1264 residual_capacity_(-1) {}
1267 DualCapacityThetaTree(
const DualCapacityThetaTree&) =
delete;
1268 DualCapacityThetaTree& operator=(
const DualCapacityThetaTree&) =
delete;
1270 virtual ~DualCapacityThetaTree() {}
1272 void Init(int64_t capacity_max, int64_t residual_capacity) {
1273 DCHECK_LE(0, residual_capacity);
1274 DCHECK_LE(residual_capacity, capacity_max);
1276 capacity_max_ = capacity_max;
1277 residual_capacity_ = residual_capacity;
1280 void Insert(
const CumulativeTask* task) {
1282 DualCapacityThetaNode(capacity_max_, residual_capacity_, *task));
1285 void Insert(
const VariableCumulativeTask* task) {
1287 DualCapacityThetaNode(capacity_max_, residual_capacity_, *task));
1291 int64_t capacity_max_;
1292 int64_t residual_capacity_;
1295const int64_t DualCapacityThetaTree::kNotInitialized = -1LL;
1307class EnvJCComputeDiver {
1310 explicit EnvJCComputeDiver(
int energy_threshold)
1311 : energy_threshold_(energy_threshold),
1314 void OnArgumentReached(
int index,
const DualCapacityThetaNode& argument) {
1315 energy_alpha_ = argument.energy;
1316 energetic_end_min_alpha_ = argument.energetic_end_min;
1321 bool ChooseGoLeft(
const DualCapacityThetaNode& current,
1322 const DualCapacityThetaNode& left_child,
1324 if (
right_child.residual_energetic_end_min > energy_threshold_) {
1331 void OnComeBackFromLeft(
const DualCapacityThetaNode& current,
1332 const DualCapacityThetaNode& left_child,
1339 void OnComeBackFromRight(
const DualCapacityThetaNode& current,
1340 const DualCapacityThetaNode& left_child,
1344 energetic_end_min_alpha_ =
1345 std::max(energetic_end_min_alpha_,
1346 CapAdd(left_child.energetic_end_min, energy_alpha_));
1347 energy_alpha_ += left_child.energy;
1349 int64_t GetEnvJC(
const DualCapacityThetaNode& root)
const {
1350 const int64_t
energy = root.energy;
1351 const int64_t energy_beta =
CapSub(
energy, energy_alpha_);
1352 return CapAdd(energetic_end_min_alpha_, energy_beta);
1361 int64_t energy_threshold_;
1367 int64_t energy_alpha_;
1372 int64_t energetic_end_min_alpha_;
1375const int64_t EnvJCComputeDiver::kNotAvailable = -1LL;
1384class UpdatesForADemand {
1386 explicit UpdatesForADemand(
int size)
1387 : updates_(
size, 0), up_to_date_(
false) {}
1390 UpdatesForADemand(
const UpdatesForADemand&) =
delete;
1391 UpdatesForADemand& operator=(
const UpdatesForADemand&) =
delete;
1393 int64_t Update(
int index) {
return updates_[
index]; }
1394 void Reset() { up_to_date_ =
false; }
1395 void SetUpdate(
int index, int64_t update) {
1396 DCHECK(!up_to_date_);
1397 DCHECK_LT(
index, updates_.size());
1398 updates_[
index] = update;
1400 bool up_to_date()
const {
return up_to_date_; }
1401 void set_up_to_date() { up_to_date_ =
true; }
1404 std::vector<int64_t> updates_;
1409template <
class Task>
1410class EdgeFinder :
public Constraint {
1412 EdgeFinder(Solver*
const solver,
const std::vector<Task*>& tasks,
1413 IntVar*
const capacity)
1414 : Constraint(solver),
1415 capacity_(capacity),
1417 by_start_min_(tasks.
size()),
1418 by_end_max_(tasks.
size()),
1419 by_end_min_(tasks.
size()),
1420 lt_tree_(tasks.
size(), capacity_->Max()),
1421 dual_capacity_tree_(tasks.
size()),
1422 has_zero_demand_tasks_(
true) {}
1425 EdgeFinder(
const EdgeFinder&) =
delete;
1426 EdgeFinder& operator=(
const EdgeFinder&) =
delete;
1428 ~EdgeFinder()
override {
1433 void Post()
override {
1436 solver(),
this, &EdgeFinder::InitialPropagate,
"RangeChanged");
1437 for (Task*
const task : tasks_) {
1440 task->WhenAnything(demon);
1442 capacity_->WhenRange(demon);
1447 void InitialPropagate()
override {
1449 PropagateBasedOnEndMinGreaterThanEndMax();
1451 PropagateBasedOnEnergy();
1455 void Accept(ModelVisitor*
const visitor)
const override {
1456 LOG(FATAL) <<
"Should Not Be Visited";
1459 std::string DebugString()
const override {
return "EdgeFinder"; }
1462 UpdatesForADemand* GetOrMakeUpdate(int64_t demand_min) {
1464 if (update ==
nullptr) {
1465 update =
new UpdatesForADemand(tasks_.size());
1466 update_map_[demand_min] = update;
1472 void InitPropagation() {
1474 start_min_update_.clear();
1476 if (has_zero_demand_tasks_.Value()) {
1477 by_start_min_.clear();
1478 by_end_min_.clear();
1479 by_end_max_.clear();
1481 bool zero_demand =
false;
1482 for (Task*
const task : tasks_) {
1483 if (task->DemandMin() > 0) {
1484 by_start_min_.push_back(task);
1485 by_end_min_.push_back(task);
1486 by_end_max_.push_back(task);
1492 has_zero_demand_tasks_.SetValue(solver(),
false);
1497 std::sort(by_start_min_.begin(), by_start_min_.end(),
1498 StartMinLessThan<Task>);
1499 for (
int i = 0;
i < by_start_min_.size(); ++
i) {
1500 by_start_min_[
i]->index =
i;
1503 std::sort(by_end_max_.begin(), by_end_max_.end(), EndMaxLessThan<Task>);
1505 std::sort(by_end_min_.begin(), by_end_min_.end(), EndMinLessThan<Task>);
1507 lt_tree_.Init(capacity_->Max());
1509 for (
const auto& entry : update_map_) {
1510 entry.second->Reset();
1518 void ComputeConditionalStartMins(UpdatesForADemand* updates,
1519 int64_t demand_min) {
1520 DCHECK_GT(demand_min, 0);
1521 DCHECK(updates !=
nullptr);
1522 const int64_t capacity_max = capacity_->Max();
1523 const int64_t residual_capacity =
CapSub(capacity_max, demand_min);
1524 dual_capacity_tree_.Init(capacity_max, residual_capacity);
1529 int64_t update = IntervalVar::kMinValidValue;
1530 for (
int i = 0;
i < by_end_max_.size(); ++
i) {
1531 Task*
const task = by_end_max_[
i];
1532 if (task->EnergyMin() == 0)
continue;
1533 const int64_t current_end_max = task->interval->EndMax();
1534 dual_capacity_tree_.Insert(task);
1535 const int64_t energy_threshold = residual_capacity * current_end_max;
1536 const DualCapacityThetaNode& root = dual_capacity_tree_.result();
1537 const int64_t res_energetic_end_min = root.residual_energetic_end_min;
1538 if (res_energetic_end_min > energy_threshold) {
1539 EnvJCComputeDiver diver(energy_threshold);
1540 dual_capacity_tree_.DiveInTree(&diver);
1541 const int64_t enjv = diver.GetEnvJC(dual_capacity_tree_.result());
1542 const int64_t numerator =
CapSub(enjv, energy_threshold);
1543 const int64_t diff = MathUtil::CeilOfRatio(numerator, demand_min);
1544 update = std::max(update, diff);
1546 updates->SetUpdate(i, update);
1548 updates->set_up_to_date();
1553 int64_t ConditionalStartMin(
const Task& task_to_push,
int end_max_index) {
1554 if (task_to_push.EnergyMin() == 0) {
1555 return task_to_push.interval->StartMin();
1557 const int64_t demand_min = task_to_push.DemandMin();
1558 UpdatesForADemand*
const updates = GetOrMakeUpdate(demand_min);
1559 if (!updates->up_to_date()) {
1560 ComputeConditionalStartMins(updates, demand_min);
1562 DCHECK(updates->up_to_date());
1563 return updates->Update(end_max_index);
1571 void PropagateBasedOnEndMinGreaterThanEndMax() {
1572 int end_max_index = 0;
1573 int64_t max_start_min = std::numeric_limits<int64_t>::min();
1574 for (Task*
const task : by_end_min_) {
1575 const int64_t
end_min = task->interval->EndMin();
1576 while (end_max_index < by_start_min_.size() &&
1577 by_end_max_[end_max_index]->interval->EndMax() <=
end_min) {
1578 max_start_min = std::max(
1579 max_start_min, by_end_max_[end_max_index]->
interval->StartMin());
1582 if (end_max_index > 0 && task->interval->StartMin() <= max_start_min &&
1583 task->interval->EndMax() > task->interval->EndMin()) {
1584 DCHECK_LE(by_end_max_[end_max_index - 1]->
interval->EndMax(),
end_min);
1597 const int64_t update = ConditionalStartMin(*task, end_max_index - 1);
1598 start_min_update_.push_back(std::make_pair(task->interval, update));
1605 for (Task*
const task : by_end_max_) {
1606 lt_tree_.Insert(*task);
1608 const int64_t max_feasible =
1609 CapProd(capacity_->Max(), task->interval->EndMax());
1610 if (lt_tree_.energetic_end_min() > max_feasible) {
1618 void PropagateBasedOnEnergy() {
1619 for (
int j = by_start_min_.size() - 2; j >= 0; --j) {
1620 lt_tree_.Grey(*by_end_max_[j + 1]);
1621 Task*
const twj = by_end_max_[j];
1623 const int64_t max_feasible =
1624 CapProd(capacity_->Max(), twj->interval->EndMax());
1625 DCHECK_LE(lt_tree_.energetic_end_min(), max_feasible);
1626 while (lt_tree_.energetic_end_min_opt() > max_feasible) {
1627 const int i = lt_tree_.argmax_energetic_end_min_opt();
1629 PropagateTaskCannotEndBefore(i, j);
1637 void PropagateTaskCannotEndBefore(
int index,
int end_max_index) {
1638 Task*
const task_to_push = by_start_min_[
index];
1639 const int64_t update = ConditionalStartMin(*task_to_push, end_max_index);
1640 start_min_update_.push_back(std::make_pair(task_to_push->interval, update));
1644 void ApplyNewBounds() {
1645 for (
const std::pair<IntervalVar*, int64_t>& update : start_min_update_) {
1646 update.first->SetStartMin(update.second);
1651 IntVar*
const capacity_;
1654 std::vector<Task*> tasks_;
1657 std::vector<Task*> by_start_min_;
1660 std::vector<Task*> by_end_max_;
1663 std::vector<Task*> by_end_min_;
1666 CumulativeLambdaThetaTree lt_tree_;
1669 DualCapacityThetaTree dual_capacity_tree_;
1672 std::vector<std::pair<IntervalVar*, int64_t>> start_min_update_;
1677 absl::flat_hash_map<int64_t, UpdatesForADemand*> update_map_;
1680 Rev<bool> has_zero_demand_tasks_;
1704struct ProfileDelta {
1705 ProfileDelta(int64_t _time, int64_t _delta) :
time(_time),
delta(_delta) {}
1710bool TimeLessThan(
const ProfileDelta& delta1,
const ProfileDelta& delta2) {
1711 return delta1.time < delta2.time;
1726template <
class Task>
1727class CumulativeTimeTable :
public Constraint {
1729 CumulativeTimeTable(Solver*
const solver,
const std::vector<Task*>& tasks,
1730 IntVar*
const capacity)
1731 : Constraint(solver), by_start_min_(tasks), capacity_(capacity) {
1734 const int profile_max_size = 2 * by_start_min_.size() + 2;
1735 profile_non_unique_time_.reserve(profile_max_size);
1736 profile_unique_time_.reserve(profile_max_size);
1740 CumulativeTimeTable(
const CumulativeTimeTable&) =
delete;
1741 CumulativeTimeTable& operator=(
const CumulativeTimeTable&) =
delete;
1745 void InitialPropagate()
override {
1752 void Post()
override {
1754 solver(),
this, &CumulativeTimeTable::InitialPropagate,
1755 "InitialPropagate");
1756 for (Task*
const task : by_start_min_) {
1757 task->WhenAnything(demon);
1759 capacity_->WhenRange(demon);
1762 void Accept(ModelVisitor*
const visitor)
const override {
1763 LOG(FATAL) <<
"Should not be visited";
1766 std::string DebugString()
const override {
return "CumulativeTimeTable"; }
1770 void BuildProfile() {
1772 profile_non_unique_time_.clear();
1773 for (
const Task*
const task : by_start_min_) {
1774 const IntervalVar*
const interval = task->interval;
1778 const int64_t demand_min = task->DemandMin();
1779 if (demand_min > 0) {
1780 profile_non_unique_time_.emplace_back(
start_max, +demand_min);
1781 profile_non_unique_time_.emplace_back(
end_min, -demand_min);
1786 std::sort(profile_non_unique_time_.begin(), profile_non_unique_time_.end(),
1789 profile_unique_time_.clear();
1790 profile_unique_time_.emplace_back(std::numeric_limits<int64_t>::min(), 0);
1792 for (
const ProfileDelta& step : profile_non_unique_time_) {
1793 if (step.time == profile_unique_time_.back().time) {
1794 profile_unique_time_.back().delta += step.delta;
1796 profile_unique_time_.push_back(step);
1799 usage += step.delta;
1802 DCHECK_EQ(0, usage);
1804 int64_t max_usage = 0;
1805 for (
const ProfileDelta& step : profile_unique_time_) {
1806 usage += step.delta;
1807 if (usage > max_usage) {
1811 DCHECK_EQ(0, usage);
1812 capacity_->SetMin(max_usage);
1814 profile_unique_time_.emplace_back(std::numeric_limits<int64_t>::max(), 0);
1819 std::sort(by_start_min_.begin(), by_start_min_.end(),
1820 StartMinLessThan<Task>);
1822 int profile_index = 0;
1823 for (
const Task*
const task : by_start_min_) {
1824 const IntervalVar*
const interval = task->interval;
1829 while (
interval->StartMin() > profile_unique_time_[profile_index].time) {
1830 DCHECK(profile_index < profile_unique_time_.size());
1832 usage += profile_unique_time_[profile_index].delta;
1834 PushTask(task, profile_index, usage);
1842 void PushTask(
const Task*
const task,
int profile_index, int64_t usage) {
1844 const IntervalVar*
const interval = task->interval;
1845 const int64_t demand_min = task->DemandMin();
1846 if (demand_min == 0) {
1849 const int64_t residual_capacity =
CapSub(capacity_->Max(), demand_min);
1850 const int64_t duration = task->interval->DurationMin();
1851 const ProfileDelta& first_prof_delta = profile_unique_time_[profile_index];
1853 int64_t new_start_min =
interval->StartMin();
1855 DCHECK_GE(first_prof_delta.time,
interval->StartMin());
1857 if (first_prof_delta.time >
interval->StartMin()) {
1862 DCHECK((
interval->StartMax() >= first_prof_delta.time) ||
1866 const int64_t usage_at_start_min =
CapSub(usage, first_prof_delta.delta);
1867 if (usage_at_start_min > residual_capacity) {
1868 new_start_min = profile_unique_time_[profile_index].time;
1876 ProfileDelta delta_end(
end_min, 0);
1878 delta_start.delta = +demand_min;
1879 delta_end.delta = -demand_min;
1881 while (profile_unique_time_[profile_index].
time <
1882 CapAdd(duration, new_start_min)) {
1883 const ProfileDelta& profile_delta = profile_unique_time_[profile_index];
1884 DCHECK(profile_index < profile_unique_time_.size());
1886 if (profile_delta.time == delta_start.time) {
1887 usage -= delta_start.delta;
1889 if (profile_delta.time == delta_end.time) {
1890 usage -= delta_end.delta;
1894 DCHECK(profile_index < profile_unique_time_.size());
1896 if (usage > residual_capacity) {
1897 new_start_min = profile_unique_time_[profile_index].time;
1899 usage += profile_unique_time_[profile_index].delta;
1901 task->interval->SetStartMin(new_start_min);
1904 typedef std::vector<ProfileDelta> Profile;
1906 Profile profile_unique_time_;
1907 Profile profile_non_unique_time_;
1908 std::vector<Task*> by_start_min_;
1909 IntVar*
const capacity_;
1922template <
class Task>
1923class TimeTableSync :
public Constraint {
1925 TimeTableSync(Solver*
const solver,
const std::vector<Task*>& tasks,
1926 IntVar*
const capacity)
1927 : Constraint(solver), tasks_(tasks), capacity_(capacity) {
1928 num_tasks_ = tasks_.size();
1931 pos_ = std::numeric_limits<int64_t>::min();
1932 next_pos_ = std::numeric_limits<int64_t>::min();
1934 start_min_.reserve(num_tasks_);
1935 start_max_.reserve(num_tasks_);
1936 end_min_.reserve(num_tasks_);
1937 durations_.reserve(num_tasks_);
1938 demands_.reserve(num_tasks_);
1943 void InitialPropagate()
override {
1946 while (!events_scp_.empty() && !events_ecp_.empty()) {
1948 pos_ = NextEventTime();
1953 capacity_->SetMin(capacity_->Max() - gap_);
1955 next_pos_ = NextScpTime();
1963 void Post()
override {
1965 solver(),
this, &TimeTableSync::InitialPropagate,
"InitialPropagate");
1966 for (Task*
const task : tasks_) {
1967 task->WhenAnything(demon);
1969 capacity_->WhenRange(demon);
1972 void Accept(ModelVisitor*
const visitor)
const override {
1973 LOG(FATAL) <<
"Should not be visited";
1976 std::string DebugString()
const override {
return "TimeTableSync"; }
1980 enum State { NONE, READY, CHECK, CONFLICT };
1982 inline int64_t NextScpTime() {
1983 return !events_scp_.empty() ? events_scp_.top().first
1984 : std::numeric_limits<int64_t>::max();
1987 inline int64_t NextEventTime() {
1988 int64_t
time = std::numeric_limits<int64_t>::max();
1989 if (!events_pr_.empty()) {
1990 time = events_pr_.top().first;
1992 if (!events_scp_.empty()) {
1993 int64_t t = events_scp_.top().first;
1996 if (!events_ecp_.empty()) {
1997 int64_t t = events_ecp_.top().first;
2003 void ProcessEventsScp() {
2004 while (!events_scp_.empty() && events_scp_.top().first == pos_) {
2005 const int64_t task_id = events_scp_.top().second;
2007 const int64_t old_end_min = end_min_[task_id];
2008 if (states_[task_id] == State::CONFLICT) {
2010 const int64_t new_end_min = pos_ + durations_[task_id];
2011 start_min_[task_id] = pos_;
2012 end_min_[task_id] = new_end_min;
2014 tasks_[task_id]->interval->SetStartMin(pos_);
2017 states_[task_id] = State::READY;
2019 if (pos_ < end_min_[task_id]) {
2020 gap_ -= demands_[task_id];
2021 if (old_end_min <= pos_) {
2022 events_ecp_.push(kv(end_min_[task_id], task_id));
2028 void ProcessEventsEcp() {
2029 while (!events_ecp_.empty() && events_ecp_.top().first == pos_) {
2030 const int64_t task_id = events_ecp_.top().second;
2033 if (pos_ < end_min_[task_id]) {
2034 events_ecp_.push(kv(end_min_[task_id], task_id));
2036 gap_ += demands_[task_id];
2041 void ProcessEventsPr() {
2042 while (!events_pr_.empty() && events_pr_.top().first == pos_) {
2043 const int64_t task_id = events_pr_.top().second;
2046 if (demands_[task_id] > gap_) {
2047 states_[task_id] = State::CONFLICT;
2048 conflict_.push(kv(demands_[task_id], task_id));
2052 if (next_pos_ < end_min_[task_id]) {
2053 states_[task_id] = State::CHECK;
2054 check_.push(kv(demands_[task_id], task_id));
2058 states_[task_id] = State::READY;
2064 capacity_->SetMin(capacity_->Max() - gap_);
2066 if (gap_ < prev_gap_) {
2068 while (!check_.empty() && demands_[check_.top().second] > gap_) {
2069 const int64_t task_id = check_.top().second;
2071 if (states_[task_id] == State::CHECK && pos_ < end_min_[task_id]) {
2072 states_[task_id] = State::CONFLICT;
2073 conflict_.push(kv(demands_[task_id], task_id));
2076 states_[task_id] = State::READY;
2081 if (gap_ > prev_gap_) {
2083 while (!conflict_.empty() && demands_[conflict_.top().second] <= gap_) {
2084 const int64_t task_id = conflict_.top().second;
2086 if (states_[task_id] != State::CONFLICT) {
2089 const int64_t old_end_min = end_min_[task_id];
2091 start_min_[task_id] = pos_;
2092 end_min_[task_id] = pos_ + durations_[task_id];
2094 tasks_[task_id]->interval->SetStartMin(pos_);
2096 if (next_pos_ < end_min_[task_id]) {
2097 states_[task_id] = State::CHECK;
2098 check_.push(kv(demands_[task_id], task_id));
2100 states_[task_id] = State::READY;
2103 const int64_t
start_max = start_max_[task_id];
2105 events_ecp_.push(kv(end_min_[task_id], task_id));
2112 void BuildEvents() {
2114 pos_ = std::numeric_limits<int64_t>::min();
2115 next_pos_ = std::numeric_limits<int64_t>::min();
2116 gap_ = capacity_->Max();
2117 prev_gap_ = capacity_->Max();
2119 conflict_ = min_heap();
2120 check_ = max_heap();
2122 events_pr_ = min_heap();
2123 events_scp_ = min_heap();
2124 events_ecp_ = min_heap();
2133 for (
int i = 0;
i < num_tasks_;
i++) {
2134 const int64_t s_min = tasks_[
i]->interval->StartMin();
2135 const int64_t s_max = tasks_[
i]->interval->StartMax();
2136 const int64_t e_min = tasks_[
i]->interval->EndMin();
2138 start_min_.push_back(s_min);
2139 start_max_.push_back(s_max);
2140 end_min_.push_back(e_min);
2141 durations_.push_back(tasks_[i]->
interval->DurationMin());
2142 demands_.push_back(tasks_[i]->DemandMin());
2144 states_.push_back(State::NONE);
2146 events_scp_.push(kv(s_max, i));
2148 if (s_min != s_max) {
2149 events_pr_.push(kv(s_min, i));
2152 if (s_max < e_min) {
2153 events_ecp_.push(kv(e_min, i));
2159 std::vector<Task*> tasks_;
2160 IntVar*
const capacity_;
2162 std::vector<int64_t> start_min_;
2163 std::vector<int64_t> start_max_;
2164 std::vector<int64_t> end_min_;
2165 std::vector<int64_t> end_max_;
2166 std::vector<int64_t> durations_;
2167 std::vector<int64_t> demands_;
2170 typedef std::pair<int64_t, int64_t> kv;
2171 typedef std::priority_queue<kv, std::vector<kv>, std::greater<kv>> min_heap;
2172 typedef std::priority_queue<kv, std::vector<kv>, std::less<kv>> max_heap;
2175 min_heap events_pr_;
2176 min_heap events_scp_;
2177 min_heap events_ecp_;
2180 std::vector<State> states_;
2191class CumulativeConstraint :
public Constraint {
2193 CumulativeConstraint(Solver*
const s,
2194 const std::vector<IntervalVar*>& intervals,
2195 const std::vector<int64_t>& demands,
2196 IntVar*
const capacity, absl::string_view
name)
2198 capacity_(capacity),
2199 intervals_(intervals),
2201 tasks_.reserve(intervals.size());
2202 for (
int i = 0;
i < intervals.size(); ++
i) {
2203 tasks_.push_back(CumulativeTask(intervals[i], demands[i]));
2208 CumulativeConstraint(
const CumulativeConstraint&) =
delete;
2209 CumulativeConstraint& operator=(
const CumulativeConstraint&) =
delete;
2211 void Post()
override {
2215 const ConstraintSolverParameters& params = solver()->const_parameters();
2216 if (params.use_cumulative_time_table()) {
2217 if (params.use_cumulative_time_table_sync()) {
2218 PostOneSidedConstraint(
false,
false,
true);
2219 PostOneSidedConstraint(
true,
false,
true);
2221 PostOneSidedConstraint(
false,
false,
false);
2222 PostOneSidedConstraint(
true,
false,
false);
2225 if (params.use_cumulative_edge_finder()) {
2226 PostOneSidedConstraint(
false,
true,
false);
2227 PostOneSidedConstraint(
true,
true,
false);
2229 if (params.use_sequence_high_demand_tasks()) {
2230 PostHighDemandSequenceConstraint();
2232 if (params.use_all_possible_disjunctions()) {
2233 PostAllDisjunctions();
2237 void InitialPropagate()
override {
2241 void Accept(ModelVisitor*
const visitor)
const override {
2243 visitor->BeginVisitConstraint(ModelVisitor::kCumulative,
this);
2244 visitor->VisitIntervalArrayArgument(ModelVisitor::kIntervalsArgument,
2246 visitor->VisitIntegerArrayArgument(ModelVisitor::kDemandsArgument,
2248 visitor->VisitIntegerExpressionArgument(ModelVisitor::kCapacityArgument,
2250 visitor->EndVisitConstraint(ModelVisitor::kCumulative,
this);
2253 std::string DebugString()
const override {
2254 return absl::StrFormat(
"CumulativeConstraint([%s], %s)",
2256 capacity_->DebugString());
2261 void PostAllDisjunctions() {
2262 for (
int i = 0;
i < intervals_.size(); ++
i) {
2263 IntervalVar*
const interval_i = intervals_[
i];
2264 if (interval_i->MayBePerformed()) {
2265 for (
int j = i + 1; j < intervals_.size(); ++j) {
2266 IntervalVar*
const interval_j = intervals_[j];
2267 if (interval_j->MayBePerformed()) {
2269 Constraint*
const constraint =
2270 solver()->MakeTemporalDisjunction(interval_i, interval_j);
2271 solver()->AddConstraint(constraint);
2281 void PostHighDemandSequenceConstraint() {
2282 Constraint* constraint =
nullptr;
2284 std::vector<IntervalVar*> high_demand_intervals;
2285 high_demand_intervals.reserve(intervals_.size());
2286 for (
int i = 0;
i < demands_.size(); ++
i) {
2287 const int64_t
demand = tasks_[
i].demand;
2294 if (
demand * 2 > capacity_->Max() &&
2295 tasks_[i].interval->MayBePerformed()) {
2296 high_demand_intervals.push_back(tasks_[i].
interval);
2299 if (high_demand_intervals.size() >= 2) {
2302 std::string seq_name = absl::StrCat(
name(),
"-HighDemandSequence");
2303 constraint = solver()->MakeDisjunctiveConstraint(high_demand_intervals,
2307 if (constraint !=
nullptr) {
2308 solver()->AddConstraint(constraint);
2314 void PopulateVectorUsefulTasks(
2315 bool mirror, std::vector<CumulativeTask*>*
const useful_tasks) {
2316 DCHECK(useful_tasks->empty());
2317 for (
int i = 0;
i < tasks_.size(); ++
i) {
2318 const CumulativeTask& original_task = tasks_[
i];
2319 IntervalVar*
const interval = original_task.interval;
2321 if (original_task.demand > capacity_->Max()) {
2326 if (
interval->MayBePerformed() && original_task.demand > 0) {
2327 Solver*
const s = solver();
2328 IntervalVar*
const original_interval = original_task.interval;
2330 mirror ? s->MakeMirrorInterval(original_interval)
2331 : original_interval;
2332 IntervalVar*
const relaxed_max = s->MakeIntervalRelaxedMax(
interval);
2333 useful_tasks->push_back(
2334 new CumulativeTask(relaxed_max, original_task.demand));
2341 Constraint* MakeOneSidedConstraint(
bool mirror,
bool edge_finder,
2343 std::vector<CumulativeTask*> useful_tasks;
2344 PopulateVectorUsefulTasks(mirror, &useful_tasks);
2345 if (useful_tasks.empty()) {
2348 Solver*
const s = solver();
2350 const ConstraintSolverParameters& params = solver()->const_parameters();
2351 return useful_tasks.size() < params.max_edge_finder_size()
2352 ? s->RevAlloc(
new EdgeFinder<CumulativeTask>(s, useful_tasks,
2358 new TimeTableSync<CumulativeTask>(s, useful_tasks, capacity_));
2361 new CumulativeTimeTable<CumulativeTask>(s, useful_tasks, capacity_));
2366 void PostOneSidedConstraint(
bool mirror,
bool edge_finder,
bool tt_sync) {
2367 Constraint*
const constraint =
2368 MakeOneSidedConstraint(mirror, edge_finder, tt_sync);
2369 if (constraint !=
nullptr) {
2370 solver()->AddConstraint(constraint);
2375 IntVar*
const capacity_;
2378 std::vector<CumulativeTask> tasks_;
2381 const std::vector<IntervalVar*> intervals_;
2383 const std::vector<int64_t> demands_;
2386class VariableDemandCumulativeConstraint :
public Constraint {
2388 VariableDemandCumulativeConstraint(Solver*
const s,
2389 const std::vector<IntervalVar*>& intervals,
2390 const std::vector<IntVar*>& demands,
2391 IntVar*
const capacity,
2392 absl::string_view
name)
2394 capacity_(capacity),
2395 intervals_(intervals),
2397 tasks_.reserve(intervals.size());
2398 for (
int i = 0;
i < intervals.size(); ++
i) {
2399 tasks_.push_back(VariableCumulativeTask(intervals[i], demands[i]));
2404 VariableDemandCumulativeConstraint(
2405 const VariableDemandCumulativeConstraint&) =
delete;
2406 VariableDemandCumulativeConstraint& operator=(
2407 const VariableDemandCumulativeConstraint&) =
delete;
2409 void Post()
override {
2413 const ConstraintSolverParameters& params = solver()->const_parameters();
2414 if (params.use_cumulative_time_table()) {
2415 PostOneSidedConstraint(
false,
false,
false);
2416 PostOneSidedConstraint(
true,
false,
false);
2418 if (params.use_cumulative_edge_finder()) {
2419 PostOneSidedConstraint(
false,
true,
false);
2420 PostOneSidedConstraint(
true,
true,
false);
2422 if (params.use_sequence_high_demand_tasks()) {
2423 PostHighDemandSequenceConstraint();
2425 if (params.use_all_possible_disjunctions()) {
2426 PostAllDisjunctions();
2430 void InitialPropagate()
override {
2434 void Accept(ModelVisitor*
const visitor)
const override {
2436 visitor->BeginVisitConstraint(ModelVisitor::kCumulative,
this);
2437 visitor->VisitIntervalArrayArgument(ModelVisitor::kIntervalsArgument,
2439 visitor->VisitIntegerVariableArrayArgument(ModelVisitor::kDemandsArgument,
2441 visitor->VisitIntegerExpressionArgument(ModelVisitor::kCapacityArgument,
2443 visitor->EndVisitConstraint(ModelVisitor::kCumulative,
this);
2446 std::string DebugString()
const override {
2447 return absl::StrFormat(
"VariableDemandCumulativeConstraint([%s], %s)",
2449 capacity_->DebugString());
2454 void PostAllDisjunctions() {
2455 for (
int i = 0;
i < intervals_.size(); ++
i) {
2456 IntervalVar*
const interval_i = intervals_[
i];
2457 if (interval_i->MayBePerformed()) {
2458 for (
int j = i + 1; j < intervals_.size(); ++j) {
2459 IntervalVar*
const interval_j = intervals_[j];
2460 if (interval_j->MayBePerformed()) {
2461 if (
CapAdd(tasks_[i].
demand->Min(), tasks_[j].demand->Min()) >
2463 Constraint*
const constraint =
2464 solver()->MakeTemporalDisjunction(interval_i, interval_j);
2465 solver()->AddConstraint(constraint);
2475 void PostHighDemandSequenceConstraint() {
2476 Constraint* constraint =
nullptr;
2478 std::vector<IntervalVar*> high_demand_intervals;
2479 high_demand_intervals.reserve(intervals_.size());
2480 for (
int i = 0;
i < demands_.size(); ++
i) {
2481 const int64_t
demand = tasks_[
i].demand->Min();
2488 if (
demand * 2 > capacity_->Max() &&
2489 tasks_[i].interval->MayBePerformed()) {
2490 high_demand_intervals.push_back(tasks_[i].
interval);
2493 if (high_demand_intervals.size() >= 2) {
2496 const std::string seq_name =
2497 absl::StrCat(
name(),
"-HighDemandSequence");
2498 constraint = solver()->MakeStrictDisjunctiveConstraint(
2499 high_demand_intervals, seq_name);
2502 if (constraint !=
nullptr) {
2503 solver()->AddConstraint(constraint);
2509 void PopulateVectorUsefulTasks(
2510 bool mirror, std::vector<VariableCumulativeTask*>*
const useful_tasks) {
2511 DCHECK(useful_tasks->empty());
2512 for (
int i = 0;
i < tasks_.size(); ++
i) {
2513 const VariableCumulativeTask& original_task = tasks_[
i];
2514 IntervalVar*
const interval = original_task.interval;
2516 if (original_task.demand->Min() > capacity_->Max()) {
2521 if (
interval->MayBePerformed() && original_task.demand->Max() > 0) {
2522 Solver*
const s = solver();
2523 IntervalVar*
const original_interval = original_task.interval;
2525 mirror ? s->MakeMirrorInterval(original_interval)
2526 : original_interval;
2527 IntervalVar*
const relaxed_max = s->MakeIntervalRelaxedMax(
interval);
2528 useful_tasks->push_back(
2529 new VariableCumulativeTask(relaxed_max, original_task.demand));
2536 Constraint* MakeOneSidedConstraint(
bool mirror,
bool edge_finder,
2538 std::vector<VariableCumulativeTask*> useful_tasks;
2539 PopulateVectorUsefulTasks(mirror, &useful_tasks);
2540 if (useful_tasks.empty()) {
2543 Solver*
const s = solver();
2546 new EdgeFinder<VariableCumulativeTask>(s, useful_tasks, capacity_));
2549 return s->RevAlloc(
new TimeTableSync<VariableCumulativeTask>(
2550 s, useful_tasks, capacity_));
2552 return s->RevAlloc(
new CumulativeTimeTable<VariableCumulativeTask>(
2553 s, useful_tasks, capacity_));
2558 void PostOneSidedConstraint(
bool mirror,
bool edge_finder,
bool tt_sync) {
2559 Constraint*
const constraint =
2560 MakeOneSidedConstraint(mirror, edge_finder, tt_sync);
2561 if (constraint !=
nullptr) {
2562 solver()->AddConstraint(constraint);
2567 IntVar*
const capacity_;
2570 std::vector<VariableCumulativeTask> tasks_;
2573 const std::vector<IntervalVar*> intervals_;
2575 const std::vector<IntVar*> demands_;
2583DisjunctiveConstraint::DisjunctiveConstraint(
2584 Solver*
const s,
const std::vector<IntervalVar*>& intervals,
2587 if (!
name.empty()) {
2596 std::function<int64_t(int64_t, int64_t)> transition_time) {
2597 if (transition_time !=
nullptr) {
2607 const std::vector<IntervalVar*>& intervals,
const std::string&
name) {
2608 return RevAlloc(
new FullDisjunctiveConstraint(
this, intervals,
name,
false));
2612 const std::vector<IntervalVar*>& intervals,
const std::string&
name) {
2613 return RevAlloc(
new FullDisjunctiveConstraint(
this, intervals,
name,
true));
2619 const std::vector<int64_t>& demands,
2620 int64_t capacity,
const std::string&
name) {
2621 CHECK_EQ(intervals.size(), demands.size());
2622 for (
int i = 0; i < intervals.size(); ++i) {
2623 CHECK_GE(demands[i], 0);
2628 return RevAlloc(
new CumulativeConstraint(
this, intervals, demands,
2633 const std::vector<int>& demands,
2634 int64_t capacity,
const std::string&
name) {
2639 const std::vector<int64_t>& demands,
2641 absl::string_view
name) {
2642 CHECK_EQ(intervals.size(), demands.size());
2643 for (
int i = 0; i < intervals.size(); ++i) {
2644 CHECK_GE(demands[i], 0);
2647 new CumulativeConstraint(
this, intervals, demands, capacity,
name));
2651 const std::vector<int>& demands,
2653 const std::string&
name) {
2660 const std::vector<IntVar*>& demands,
2661 int64_t capacity,
const std::string&
name) {
2662 CHECK_EQ(intervals.size(), demands.size());
2663 for (
int i = 0; i < intervals.size(); ++i) {
2664 CHECK_GE(demands[i]->Min(), 0);
2667 std::vector<int64_t> fixed_demands(demands.size());
2668 for (
int i = 0; i < demands.size(); ++i) {
2669 fixed_demands[i] = demands[i]->Value();
2673 return RevAlloc(
new VariableDemandCumulativeConstraint(
2678 const std::vector<IntVar*>& demands,
2680 const std::string&
name) {
2681 CHECK_EQ(intervals.size(), demands.size());
2682 for (
int i = 0; i < intervals.size(); ++i) {
2683 CHECK_GE(demands[i]->Min(), 0);
2686 std::vector<int64_t> fixed_demands(demands.size());
2687 for (
int i = 0; i < demands.size(); ++i) {
2688 fixed_demands[i] = demands[i]->Value();
2692 return RevAlloc(
new VariableDemandCumulativeConstraint(
2693 this, intervals, demands, capacity,
name));
Solver::IndexEvaluator2 transition_time_
~DisjunctiveConstraint() override
void SetTransitionTime(Solver::IndexEvaluator2 transition_time)
static IntegralType CeilOfRatio(IntegralType numerator, IntegralType denominator)
virtual std::string name() const
Object naming.
Constraint * MakeCumulative(const std::vector< IntervalVar * > &intervals, const std::vector< int64_t > &demands, int64_t capacity, const std::string &name)
Demands are constant.
DisjunctiveConstraint * MakeDisjunctiveConstraint(const std::vector< IntervalVar * > &intervals, const std::string &name)
-------— Factory methods -------—
DisjunctiveConstraint * MakeStrictDisjunctiveConstraint(const std::vector< IntervalVar * > &intervals, const std::string &name)
IntVar * MakeIntConst(int64_t val, const std::string &name)
IntConst will create a constant expression.
const std::string name
A name for logging purposes.
const Collection::value_type::second_type FindPtrOrNull(const Collection &collection, const typename Collection::value_type::first_type &key)
void STLDeleteValues(T *v)
void STLDeleteElements(T *container)
For infeasible and unbounded see Not checked if options check_solutions_if_inf_or_unbounded and the If options first_solution_only is false
problem is infeasible or unbounded (default).
double Distance(const VectorXd &vector1, const VectorXd &vector2, const Sharder &sharder)
In SWIG mode, we don't want anything besides these top-level includes.
int64_t CapAdd(int64_t x, int64_t y)
std::string JoinDebugStringPtr(const std::vector< T > &v, absl::string_view separator)
Join v[i]->DebugString().
int64_t CapSub(int64_t x, int64_t y)
Demon * MakeDelayedConstraintDemon0(Solver *const s, T *const ct, void(T::*method)(), const std::string &name)
int64_t CapProd(int64_t x, int64_t y)
std::vector< int64_t > ToInt64Vector(const std::vector< int > &input)
--— Vector manipulations --—
bool AreAllOnes(const std::vector< T > &values)
bool AreAllBound(const std::vector< IntVar * > &vars)
std::string JoinDebugString(const std::vector< T > &v, absl::string_view separator)
Join v[i].DebugString().
static const int kNone
Special value for task indices meaning 'no such task'.
int64_t residual_energetic_end_min
Max_{subset S of Theta} (residual_capacity * start_min(S) + energy(S))
int64_t energetic_end_min
Max_{subset S of Theta} (capacity * start_min(S) + energy(S))
int64_t energetic_end_min_opt
static const int64_t kNotInitialized
int64_t energy_opt
Max_{i in Lambda} (energy(Theta union {i}))
int argmax_energetic_end_min_opt
static const int64_t kNotAvailable