14 #ifndef OR_TOOLS_SAT_INTERVALS_H_
15 #define OR_TOOLS_SAT_INTERVALS_H_
21 #include "absl/types/span.h"
62 IntervalVariable
CreateInterval(IntegerVariable start, IntegerVariable end,
63 IntegerVariable size, IntegerValue fixed_size,
64 LiteralIndex is_present);
67 LiteralIndex is_present,
68 bool add_linear_relation);
98 IntegerVariable
SizeVar(IntervalVariable i)
const {
103 return sizes_[i].var;
105 IntegerVariable
StartVar(IntervalVariable i)
const {
110 return starts_[i].var;
112 IntegerVariable
EndVar(IntervalVariable i)
const {
121 IntegerValue
MinSize(IntervalVariable i)
const {
126 IntegerValue
MaxSize(IntervalVariable i)
const {
132 std::vector<IntervalVariable> result;
200 absl::Span<const int> tasks);
224 IntegerValue
SizeMin(
int t)
const {
return cached_duration_min_[t]; }
229 IntegerValue
StartMin(
int t)
const {
return cached_start_min_[t]; }
230 IntegerValue
EndMin(
int t)
const {
return cached_end_min_[t]; }
231 IntegerValue
StartMax(
int t)
const {
return -cached_negated_start_max_[t]; }
232 IntegerValue
EndMax(
int t)
const {
return -cached_negated_end_max_[t]; }
249 return cached_shifted_start_min_[t];
298 return &integer_reason_;
310 ABSL_MUST_USE_RESULT
bool IncreaseStartMin(
int t, IntegerValue new_start_min);
311 ABSL_MUST_USE_RESULT
bool DecreaseEndMax(
int t, IntegerValue new_start_max);
320 const std::vector<AffineExpression>&
Starts()
const {
return starts_; }
321 const std::vector<AffineExpression>&
Ends()
const {
return ends_; }
322 const std::vector<AffineExpression>&
Sizes()
const {
return sizes_; }
331 bool watch_start_max =
true,
332 bool watch_end_max =
true)
const;
340 IntegerValue event) {
341 CHECK(other_helper !=
nullptr);
342 other_helper_ = other_helper;
343 event_for_other_helper_ = event;
360 void InitSortedVectors();
361 void UpdateCachedValues(
int t);
370 void AddOtherReason(
int t);
373 void ImportOtherReasons();
380 bool current_time_direction_ =
true;
384 std::vector<AffineExpression> starts_;
385 std::vector<AffineExpression> ends_;
386 std::vector<AffineExpression> sizes_;
387 std::vector<LiteralIndex> reason_for_presence_;
391 std::vector<AffineExpression> minus_starts_;
392 std::vector<AffineExpression> minus_ends_;
395 int previous_level_ = 0;
398 std::vector<IntegerValue> cached_duration_min_;
399 std::vector<IntegerValue> cached_start_min_;
400 std::vector<IntegerValue> cached_end_min_;
401 std::vector<IntegerValue> cached_negated_start_max_;
402 std::vector<IntegerValue> cached_negated_end_max_;
403 std::vector<IntegerValue> cached_shifted_start_min_;
404 std::vector<IntegerValue> cached_negated_shifted_end_max_;
407 std::vector<TaskTime> task_by_increasing_start_min_;
408 std::vector<TaskTime> task_by_increasing_end_min_;
409 std::vector<TaskTime> task_by_decreasing_start_max_;
410 std::vector<TaskTime> task_by_decreasing_end_max_;
414 std::vector<TaskTime> task_by_increasing_shifted_start_min_;
415 std::vector<TaskTime> task_by_negated_shifted_end_max_;
416 bool recompute_shifted_start_min_ =
true;
417 bool recompute_negated_shifted_end_max_ =
true;
421 bool recompute_all_cache_ =
true;
422 std::vector<bool> recompute_cache_;
425 std::vector<Literal> literal_reason_;
426 std::vector<IntegerLiteral> integer_reason_;
430 IntegerValue event_for_other_helper_;
431 std::vector<bool> already_added_to_other_reasons_;
439 return integer_trail_->
IsFixed(starts_[t]);
443 return integer_trail_->
IsFixed(ends_[t]);
447 return integer_trail_->
IsFixed(sizes_[t]);
465 integer_reason_.clear();
466 literal_reason_.clear();
469 already_added_to_other_reasons_.assign(
NumTasks(),
false);
477 literal_reason_.push_back(
Literal(reason_for_presence_[t]).Negated());
485 literal_reason_.push_back(
Literal(reason_for_presence_[t]));
492 integer_reason_.push_back(
526 integer_reason_.push_back(
530 integer_reason_.push_back(
548 integer_reason_.push_back(
552 integer_reason_.push_back(
568 int t, IntegerValue energy_min, IntegerValue
time) {
589 IntervalVariable v) {
595 inline std::function<IntegerVariable(
const Model&)>
EndVar(IntervalVariable v) {
602 IntervalVariable v) {
608 inline std::function<int64_t(
const Model&)>
MinSize(IntervalVariable v) {
614 inline std::function<int64_t(
const Model&)>
MaxSize(IntervalVariable v) {
627 IntervalVariable v) {
645 IntegerVariable start, IntegerVariable end, IntegerVariable size) {
653 int64_t min_start, int64_t max_end, int64_t min_size, int64_t max_size) {
664 int64_t min_start, int64_t max_end, int64_t size,
Literal is_present) {
669 IntegerValue(size), is_present.
Index());
673 inline std::function<IntervalVariable(Model*)>
675 int64_t size,
Literal is_present) {
678 const IntegerVariable start =
680 const IntegerVariable end =
684 integer_trail->MarkIntegerVariableAsOptional(end, is_present);
691 IntegerVariable start, IntegerVariable end, IntegerVariable size,
695 start, end, size, IntegerValue(0), is_present.
Index());
699 inline std::function<IntervalVariable(Model*)>
701 int64_t min_size, int64_t max_size,
714 IntervalVariable parent,
const std::vector<IntervalVariable>& members) {
719 std::vector<Literal> presences;
720 std::vector<IntegerValue> sizes;
723 std::vector<LiteralWithCoeff> sat_ct;
724 for (
const IntervalVariable member : members) {
726 const Literal is_present = intervals->PresenceLiteral(member);
727 sat_ct.push_back({is_present, Coefficient(1)});
735 CHECK(integer_trail->IsFixed(intervals->Size(member)));
736 presences.push_back(is_present);
737 sizes.push_back(intervals->MinSize(member));
740 model->Add(
IsOneOf(intervals->SizeVar(parent), presences, sizes));
746 std::vector<IntegerVariable> starts;
747 starts.reserve(members.size());
748 for (
const IntervalVariable member : members) {
749 starts.push_back(intervals->StartVar(member));
755 std::vector<IntegerVariable> ends;
756 ends.reserve(members.size());
757 for (
const IntervalVariable member : members) {
758 ends.push_back(intervals->EndVar(member));
#define DCHECK_LE(val1, val2)
#define CHECK_EQ(val1, val2)
#define DCHECK_GE(val1, val2)
#define DCHECK(condition)
An Assignment is a variable -> domains mapping, used to report solutions to the user.
bool IsFixed(IntegerVariable i) const
IntegerLiteral LowerBoundAsLiteral(IntegerVariable i) const
bool InPropagationLoop() const
IntegerValue UpperBound(IntegerVariable i) const
void MarkIntegerVariableAsOptional(IntegerVariable i, Literal is_considered)
IntegerValue LowerBound(IntegerVariable i) const
bool IsOptional(IntegerVariable i) const
IntegerVariable SizeVar(IntervalVariable i) const
AffineExpression End(IntervalVariable i) const
IntegerValue MaxSize(IntervalVariable i) const
AffineExpression Start(IntervalVariable i) const
Literal PresenceLiteral(IntervalVariable i) const
IntegerVariable StartVar(IntervalVariable i) const
IntegerValue MinSize(IntervalVariable i) const
IntervalsRepository(Model *model)
IntegerVariable EndVar(IntervalVariable i) const
bool IsPresent(IntervalVariable i) const
AffineExpression Size(IntervalVariable i) const
std::vector< IntervalVariable > AllIntervals() const
bool IsAbsent(IntervalVariable i) const
bool IsOptional(IntervalVariable i) const
IntervalVariable CreateInterval(IntegerVariable start, IntegerVariable end, IntegerVariable size, IntegerValue fixed_size, LiteralIndex is_present)
LiteralIndex Index() const
Class that owns everything related to a particular optimization model.
bool StartIsFixed(int t) const
IntegerValue ShiftedStartMin(int t) const
IntegerValue EndMin(int t) const
ABSL_MUST_USE_RESULT bool PushIntegerLiteral(IntegerLiteral lit)
const std::vector< TaskTime > & TaskByDecreasingEndMax()
void SetLevel(int level) final
std::vector< Literal > * MutableLiteralReason()
ABSL_MUST_USE_RESULT bool PushTaskAbsence(int t)
SchedulingConstraintHelper(const std::vector< IntervalVariable > &tasks, Model *model)
void AddSizeMinReason(int t)
const std::vector< TaskTime > & TaskByIncreasingStartMin()
void AddStartMinReason(int t, IntegerValue lower_bound)
void WatchAllTasks(int id, GenericLiteralWatcher *watcher, bool watch_start_max=true, bool watch_end_max=true) const
bool IncrementalPropagate(const std::vector< int > &watch_indices) final
void AddPresenceReason(int t)
const std::vector< TaskTime > & TaskByIncreasingEndMin()
void ResetFromSubset(const SchedulingConstraintHelper &other, absl::Span< const int > tasks)
std::vector< IntegerLiteral > * MutableIntegerReason()
bool IsPresent(int t) const
bool SizeIsFixed(int t) const
void AddEnergyAfterReason(int t, IntegerValue energy_min, IntegerValue time)
bool InPropagationLoop() const
ABSL_MUST_USE_RESULT bool PushIntegerLiteralIfTaskPresent(int t, IntegerLiteral lit)
void RegisterWith(GenericLiteralWatcher *watcher)
std::string TaskDebugString(int t) const
void AddEndMinReason(int t, IntegerValue lower_bound)
bool EndIsFixed(int t) const
ABSL_MUST_USE_RESULT bool IncreaseStartMin(int t, IntegerValue new_start_min)
void SynchronizeAndSetTimeDirection(bool is_forward)
bool IsAbsent(int t) const
IntegerValue EndMax(int t) const
ABSL_MUST_USE_RESULT bool ReportConflict()
const std::vector< AffineExpression > & Starts() const
ABSL_MUST_USE_RESULT bool DecreaseEndMax(int t, IntegerValue new_start_max)
bool IsOptional(int t) const
IntegerValue StartMin(int t) const
const std::vector< TaskTime > & TaskByDecreasingStartMax()
ABSL_MUST_USE_RESULT bool PushTaskPresence(int t)
void AddAbsenceReason(int t)
Literal PresenceLiteral(int index) const
void AddEndMaxReason(int t, IntegerValue upper_bound)
void SetOtherHelper(SchedulingConstraintHelper *other_helper, IntegerValue event)
IntegerValue StartMax(int t) const
const std::vector< TaskTime > & TaskByIncreasingShiftedStartMin()
void AddReasonForBeingBefore(int before, int after)
const std::vector< AffineExpression > & Sizes() const
void AddStartMaxReason(int t, IntegerValue upper_bound)
IntegerValue SizeMax(int t) const
IntegerValue SizeMin(int t) const
const std::vector< AffineExpression > & Ends() const
const VariablesAssignment & Assignment() const
bool LiteralIsTrue(Literal literal) const
bool LiteralIsFalse(Literal literal) const
std::function< IntegerVariable(const Model &)> SizeVar(IntervalVariable v)
std::function< void(Model *)> GreaterOrEqual(IntegerVariable v, int64_t lb)
std::function< IntervalVariable(Model *)> NewInterval(int64_t min_start, int64_t max_end, int64_t size)
std::function< IntervalVariable(Model *)> NewOptionalIntervalWithVariableSize(int64_t min_start, int64_t max_end, int64_t min_size, int64_t max_size, Literal is_present)
std::function< Literal(const Model &)> IsPresentLiteral(IntervalVariable v)
const LiteralIndex kNoLiteralIndex(-1)
std::function< void(Model *)> PartialIsOneOfVar(IntegerVariable target_var, const std::vector< IntegerVariable > &vars, const std::vector< Literal > &selectors)
std::function< void(Model *)> IsOneOf(IntegerVariable var, const std::vector< Literal > &selectors, const std::vector< IntegerValue > &values)
const IntegerVariable kNoIntegerVariable(-1)
const IntervalVariable kNoIntervalVariable(-1)
std::function< IntegerVariable(const Model &)> EndVar(IntervalVariable v)
std::function< IntervalVariable(Model *)> NewIntervalWithVariableSize(int64_t min_start, int64_t max_end, int64_t min_size, int64_t max_size)
std::function< int64_t(const Model &)> MinSize(IntervalVariable v)
std::function< int64_t(const Model &)> MaxSize(IntervalVariable v)
DEFINE_INT_TYPE(ClauseIndex, int)
std::function< bool(const Model &)> IsOptional(IntervalVariable v)
std::function< void(Model *)> BooleanLinearConstraint(int64_t lower_bound, int64_t upper_bound, std::vector< LiteralWithCoeff > *cst)
std::function< void(Model *)> LowerOrEqual(IntegerVariable v, int64_t ub)
std::function< IntegerVariable(Model *)> NewIntegerVariable(int64_t lb, int64_t ub)
std::function< IntegerVariable(const Model &)> StartVar(IntervalVariable v)
std::function< void(Model *)> Equality(IntegerVariable v, int64_t value)
std::function< void(Model *)> IntervalWithAlternatives(IntervalVariable parent, const std::vector< IntervalVariable > &members)
std::function< IntervalVariable(Model *)> NewOptionalInterval(int64_t min_start, int64_t max_end, int64_t size, Literal is_present)
std::function< IntervalVariable(Model *)> NewOptionalIntervalWithOptionalVariables(int64_t min_start, int64_t max_end, int64_t size, Literal is_present)
Collection of objects used to extend the Constraint Solver library.
bool operator<(TaskTime other) const
bool operator>(TaskTime other) const