14 #ifndef OR_TOOLS_SAT_INTERVALS_H_
15 #define OR_TOOLS_SAT_INTERVALS_H_
20 #include "absl/types/span.h"
58 IntervalVariable
CreateInterval(IntegerVariable start, IntegerVariable end,
59 IntegerVariable size, IntegerValue fixed_size,
60 LiteralIndex is_present);
77 IntegerVariable
SizeVar(IntervalVariable i)
const {
return size_vars_[i]; }
78 IntegerVariable
StartVar(IntervalVariable i)
const {
return start_vars_[i]; }
79 IntegerVariable
EndVar(IntervalVariable i)
const {
return end_vars_[i]; }
82 IntegerValue
MinSize(IntervalVariable i)
const {
83 const IntegerVariable size_var = size_vars_[i];
89 IntegerValue
MaxSize(IntervalVariable i)
const {
90 const IntegerVariable size_var = size_vars_[i];
97 std::vector<IntervalVariable> result;
154 absl::Span<const int> tasks);
157 int NumTasks()
const {
return start_vars_.size(); }
174 IntegerValue
EndMin(
int t)
const;
175 IntegerValue
EndMax(
int t)
const;
234 return &integer_reason_;
246 ABSL_MUST_USE_RESULT
bool IncreaseStartMin(
int t, IntegerValue new_min_start);
247 ABSL_MUST_USE_RESULT
bool DecreaseEndMax(
int t, IntegerValue new_max_end);
255 const std::vector<IntegerVariable>&
StartVars()
const {
return start_vars_; }
256 const std::vector<IntegerVariable>&
EndVars()
const {
return end_vars_; }
258 return duration_vars_;
264 bool watch_start_max =
true,
265 bool watch_end_max =
true)
const;
273 IntegerValue event) {
274 CHECK(other_helper !=
nullptr);
275 other_helper_ = other_helper;
276 event_for_other_helper_ = event;
288 void InitSortedVectors();
297 void AddOtherReason(
int t);
300 void ImportOtherReasons();
307 bool current_time_direction_ =
true;
311 std::vector<IntegerVariable> start_vars_;
312 std::vector<IntegerVariable> end_vars_;
313 std::vector<IntegerVariable> duration_vars_;
314 std::vector<IntegerValue> fixed_durations_;
315 std::vector<LiteralIndex> reason_for_presence_;
319 std::vector<IntegerVariable> minus_start_vars_;
320 std::vector<IntegerVariable> minus_end_vars_;
323 std::vector<TaskTime> task_by_increasing_start_min_;
324 std::vector<TaskTime> task_by_increasing_end_min_;
325 std::vector<TaskTime> task_by_decreasing_start_max_;
326 std::vector<TaskTime> task_by_decreasing_end_max_;
328 std::vector<TaskTime> task_by_increasing_shifted_start_min_;
329 std::vector<TaskTime> task_by_negated_shifted_end_max_;
330 int64 shifted_start_min_timestamp_ = -1;
331 int64 negated_shifted_end_max_timestamp_ = -1;
334 std::vector<Literal> literal_reason_;
335 std::vector<IntegerLiteral> integer_reason_;
339 IntegerValue event_for_other_helper_;
340 std::vector<bool> already_added_to_other_reasons_;
349 ? fixed_durations_[t]
350 : integer_trail_->
LowerBound(duration_vars_[t]);
355 ? fixed_durations_[t]
356 : integer_trail_->
UpperBound(duration_vars_[t]);
360 return integer_trail_->
LowerBound(start_vars_[t]);
364 return integer_trail_->
UpperBound(start_vars_[t]);
368 return integer_trail_->
LowerBound(end_vars_[t]);
372 return integer_trail_->
UpperBound(end_vars_[t]);
403 integer_reason_.clear();
404 literal_reason_.clear();
407 already_added_to_other_reasons_.assign(
NumTasks(),
false);
415 literal_reason_.push_back(
Literal(reason_for_presence_[t]).Negated());
422 integer_reason_.push_back(
428 int t, IntegerValue lower_bound) {
432 integer_reason_.push_back(
438 int t, IntegerValue lower_bound) {
439 DCHECK_GE(
StartMin(t), lower_bound);
441 integer_reason_.push_back(
446 int t, IntegerValue upper_bound) {
447 DCHECK_LE(
StartMax(t), upper_bound);
449 integer_reason_.push_back(
454 int t, IntegerValue lower_bound) {
456 if (
EndMin(t) < lower_bound) {
462 integer_reason_.push_back(
466 start_vars_[t], lower_bound - duration_min));
469 integer_reason_.push_back(
474 int t, IntegerValue upper_bound) {
475 DCHECK_LE(
EndMax(t), upper_bound);
477 integer_reason_.push_back(
482 int t, IntegerValue energy_min, IntegerValue
time) {
485 integer_reason_.push_back(
488 integer_reason_.push_back(
492 integer_reason_.push_back(
502 IntervalVariable v) {
508 inline std::function<IntegerVariable(
const Model&)>
EndVar(IntervalVariable v) {
515 IntervalVariable v) {
540 IntervalVariable v) {
558 IntegerVariable start, IntegerVariable end, IntegerVariable size) {
582 IntegerValue(size), is_present.
Index());
586 inline std::function<IntervalVariable(Model*)>
591 const IntegerVariable start =
593 const IntegerVariable end =
597 integer_trail->MarkIntegerVariableAsOptional(end, is_present);
604 IntegerVariable start, IntegerVariable end, IntegerVariable size,
608 start, end, size, IntegerValue(0), is_present.
Index());
612 inline std::function<IntervalVariable(Model*)>
627 IntervalVariable master,
const std::vector<IntervalVariable>& members) {
631 std::vector<Literal> presences;
632 std::vector<IntegerValue> durations;
635 std::vector<LiteralWithCoeff> sat_ct;
636 for (
const IntervalVariable member : members) {
639 sat_ct.push_back({is_present, Coefficient(1)});
648 presences.push_back(is_present);
649 durations.push_back(intervals->
MinSize(member));
658 std::vector<IntegerVariable> starts;
659 starts.reserve(members.size());
660 for (
const IntervalVariable member : members) {
661 starts.push_back(intervals->
StartVar(member));
667 std::vector<IntegerVariable> ends;
668 ends.reserve(members.size());
669 for (
const IntervalVariable member : members) {
670 ends.push_back(intervals->
EndVar(member));
680 #endif // OR_TOOLS_SAT_INTERVALS_H_