19#include "absl/log/check.h"
33 : num_tasks_(helper->NumTasks()),
39 mandatory_energy_before_end_max_.resize(num_tasks_);
40 mandatory_energy_before_start_min_.resize(num_tasks_);
43 size_free_.resize(num_tasks_);
44 energy_free_.resize(num_tasks_);
48 const int id = watcher->
Register(
this);
51 for (
int t = 0; t < num_tasks_; t++) {
60 if (!TimeTableEdgeFindingPass())
return false;
62 if (!TimeTableEdgeFindingPass())
return false;
66void TimeTableEdgeFinding::BuildTimeTable() {
74 const IntegerValue
start_max = -negated_smax;
82 const int t = task_time.task_index;
84 if (helper_->
StartMax(t) < task_time.time) {
85 ecp_.push_back(task_time);
89 DCHECK_EQ(scp_.size(), ecp_.size());
94 IntegerValue
height = IntegerValue(0);
95 IntegerValue
energy = IntegerValue(0);
99 IntegerValue previous_time = IntegerValue(0);
104 int index_emax = num_tasks_ - 1;
106 while (index_emax >= 0) {
108 IntegerValue
time = by_decreasing_end_max[index_emax].time;
109 if (index_smin < num_tasks_) {
112 if (index_scp < scp_.size()) {
115 if (index_ecp < ecp_.size()) {
121 previous_time =
time;
124 while (index_smin < num_tasks_ && by_start_min[index_smin].
time ==
time) {
125 mandatory_energy_before_start_min_[by_start_min[index_smin].task_index] =
131 while (index_emax >= 0 && by_decreasing_end_max[index_emax].
time ==
time) {
132 mandatory_energy_before_end_max_[by_decreasing_end_max[index_emax]
138 while (index_scp < scp_.size() && scp_[index_scp].time ==
time) {
144 while (index_ecp < ecp_.size() && ecp_[index_ecp].time ==
time) {
151bool TimeTableEdgeFinding::TimeTableEdgeFindingPass() {
156 for (
int t = 0; t < num_tasks_; ++t) {
160 const IntegerValue demand_min = demands_->
DemandMin(t);
161 IntegerValue mandatory_energy(0);
164 size_free_[t] = helper_->
SizeMin(t);
167 size_free_[t] = helper_->
SizeMin(t) - mandatory_size;
168 mandatory_energy = mandatory_size * demand_min;
171 const IntegerValue min_energy = demands_->
EnergyMin(t);
172 energy_free_[t] = min_energy - mandatory_energy;
173 DCHECK_GE(energy_free_[t], 0);
188 const int end_task = end_task_time.task_index;
191 if (!helper_->
IsPresent(end_task))
continue;
192 if (energy_free_[end_task] == 0)
continue;
195 if (end_task_time.time == previous_end)
continue;
196 previous_end = end_task_time.time;
200 IntegerValue energy_free_parts = IntegerValue(0);
201 reason_tasks_fully_included_in_window_.clear();
202 reason_tasks_partially_included_in_window_.clear();
208 IntegerValue free_energy_of_max_task_in_window(0);
212 const IntegerValue window_max = end_task_time.time;
214 const int begin_task = begin_task_time.task_index;
218 const IntegerValue window_min = begin_task_time.time;
221 if (window_max <= window_min)
continue;
224 if (!helper_->
IsPresent(begin_task))
continue;
225 if (energy_free_[begin_task] == 0)
continue;
246 reason_tasks_fully_included_in_window_.push_back(begin_task);
247 energy_free_parts += energy_free_[begin_task];
249 const IntegerValue demand_min = demands_->
DemandMin(begin_task);
250 const IntegerValue extra_energy =
251 std::min(size_free_[begin_task], (window_max - window_min)) *
256 const IntegerValue free_energy_in_window =
257 std::max(IntegerValue(0),
258 size_free_[begin_task] - (
end_max - window_max)) *
263 if (extra_energy > extra_energy_required_by_max_task) {
264 if (max_task != -1 && free_energy_of_max_task_in_window > 0) {
265 reason_tasks_partially_included_in_window_.push_back(max_task);
268 max_task = begin_task;
269 extra_energy_required_by_max_task = extra_energy;
273 energy_free_parts += free_energy_of_max_task_in_window;
274 free_energy_of_max_task_in_window = free_energy_in_window;
275 }
else if (free_energy_in_window > 0) {
276 reason_tasks_partially_included_in_window_.push_back(begin_task);
277 energy_free_parts += free_energy_in_window;
290 if (max_task == -1 || demands_->
DemandMin(max_task) == 0)
continue;
293 const IntegerValue window_energy =
294 CapacityMax() * (window_max - window_min);
295 const IntegerValue energy_mandatory =
296 mandatory_energy_before_end_max_[end_task] -
297 mandatory_energy_before_start_min_[begin_task];
298 const IntegerValue available_energy =
299 window_energy - energy_free_parts - energy_mandatory;
305 if (extra_energy_required_by_max_task <= available_energy) {
312 if (energy_free_[max_task] > available_energy &&
313 helper_->
EndMin(max_task) <= window_max) {
314 FillEnergyInWindowReason(window_min, window_max, max_task);
317 if (!helper_->
IncreaseEndMin(max_task, window_max + 1))
return false;
328 const IntegerValue mandatory_size_in_window =
329 std::max(IntegerValue(0),
330 std::min(window_max, helper_->
EndMin(max_task)) -
331 std::max(window_min, helper_->
StartMax(max_task)));
334 const IntegerValue max_free_size_that_fit =
335 available_energy / demands_->
DemandMin(max_task);
336 const IntegerValue new_start =
337 window_max - mandatory_size_in_window - max_free_size_that_fit;
340 if (helper_->
StartMin(max_task) < new_start) {
341 FillEnergyInWindowReason(window_min, window_max, max_task);
356void TimeTableEdgeFinding::FillEnergyInWindowReason(IntegerValue window_min,
357 IntegerValue window_max,
368 for (
int t = 0; t < num_tasks_; ++t) {
369 if (t == task_index)
continue;
371 const IntegerValue smax = helper_->
StartMax(t);
372 const IntegerValue emin = helper_->
EndMin(t);
373 if (smax >= emin)
continue;
374 if (emin <= window_min)
continue;
375 if (smax >= window_max)
continue;
386 for (
const int t : reason_tasks_fully_included_in_window_) {
387 DCHECK_NE(t, task_index);
389 DCHECK_GT(helper_->
EndMax(t), window_min);
390 DCHECK_LT(helper_->
StartMin(t), window_max);
391 DCHECK_GE(helper_->
StartMin(t), window_min);
398 for (
const int t : reason_tasks_partially_included_in_window_) {
399 DCHECK_NE(t, task_index);
401 DCHECK_GT(helper_->
EndMax(t), window_min);
402 DCHECK_LT(helper_->
StartMin(t), window_max);
403 DCHECK_GE(helper_->
StartMin(t), window_min);
void NotifyThatPropagatorMayNotReachFixedPointInOnePass(int id)
void WatchUpperBound(IntegerVariable var, int id, int watch_index=-1)
void WatchLowerBound(IntegerVariable var, int id, int watch_index=-1)
void SetPropagatorPriority(int id, int priority)
int Register(PropagatorInterface *propagator)
Registers a propagator and returns its unique ids.
IntegerLiteral UpperBoundAsLiteral(IntegerVariable i) const
void AddPresenceReason(int t)
absl::Span< const TaskTime > TaskByIncreasingStartMin()
void WatchAllTasks(int id, bool watch_max_side=true)
void AddStartMaxReason(int t, IntegerValue upper_bound)
ABSL_MUST_USE_RESULT bool IncreaseEndMin(int t, IntegerValue value)
std::vector< IntegerLiteral > * MutableIntegerReason()
absl::Span< const TaskTime > TaskByIncreasingNegatedStartMax()
IntegerValue StartMax(int t) const
bool IsPresent(int t) const
void AddSizeMinReason(int t)
void AddEndMaxReason(int t, IntegerValue upper_bound)
ABSL_MUST_USE_RESULT bool IncreaseStartMin(int t, IntegerValue value)
void AddEndMinReason(int t, IntegerValue lower_bound)
IntegerValue StartMin(int t) const
void ClearReason()
Functions to clear and then set the current reason.
IntegerValue SizeMin(int t) const
absl::Span< const TaskTime > TaskByDecreasingEndMax()
ABSL_MUST_USE_RESULT bool SynchronizeAndSetTimeDirection(bool is_forward)
IntegerValue EndMax(int t) const
IntegerValue EndMin(int t) const
void AddStartMinReason(int t, IntegerValue lower_bound)
absl::Span< const TaskTime > TaskByIncreasingEndMin()
void AddDemandMinReason(int t)
void AddEnergyMinReason(int t)
void CacheAllEnergyValues()
const std::vector< AffineExpression > & Demands() const
IntegerValue DemandMin(int t) const
IntegerValue EnergyMin(int t) const
ReverseView< Container > reversed_view(const Container &c)
constexpr IntegerValue kMaxIntegerValue(std::numeric_limits< IntegerValue::ValueType >::max() - 1)
constexpr IntegerValue kMinIntegerValue(-kMaxIntegerValue.value())
const IntegerVariable kNoIntegerVariable(-1)
In SWIG mode, we don't want anything besides these top-level includes.