30 : num_tasks_(helper->NumTasks()),
33 integer_trail_(integer_trail),
38 profile_.reserve(2 * num_tasks_ + 4);
41 forward_num_tasks_to_sweep_ = num_tasks_;
42 forward_tasks_to_sweep_.resize(num_tasks_);
43 backward_num_tasks_to_sweep_ = num_tasks_;
44 backward_tasks_to_sweep_.resize(num_tasks_);
46 num_profile_tasks_ = 0;
47 profile_tasks_.resize(num_tasks_);
48 positions_in_profile_tasks_.resize(num_tasks_);
51 starting_profile_height_ = IntegerValue(0);
53 for (
int t = 0; t < num_tasks_; ++t) {
54 forward_tasks_to_sweep_[t] = t;
55 backward_tasks_to_sweep_[t] = t;
56 profile_tasks_[t] = t;
57 positions_in_profile_tasks_[t] = t;
62 const int id = watcher->
Register(
this);
65 for (
int t = 0; t < num_tasks_; t++) {
80 if (!BuildProfile())
return false;
83 if (!SweepAllTasks(
true))
return false;
89 if (!SweepAllTasks(
false))
return false;
94 bool TimeTablingPerTask::BuildProfile() {
100 for (
int i = num_profile_tasks_; i < num_tasks_; ++i) {
101 const int t1 = profile_tasks_[i];
104 const int t2 = profile_tasks_[num_profile_tasks_];
105 profile_tasks_[i] = t2;
106 profile_tasks_[num_profile_tasks_] = t1;
107 positions_in_profile_tasks_[t1] = num_profile_tasks_;
108 positions_in_profile_tasks_[t2] = i;
109 num_profile_tasks_++;
129 IntegerValue current_height = starting_profile_height_;
133 int next_start = num_tasks_ - 1;
135 while (next_end < num_tasks_) {
136 const IntegerValue old_height = current_height;
138 IntegerValue t = by_end_min[next_end].time;
139 if (next_start >= 0) {
140 t =
std::min(t, by_decreasing_start_max[next_start].
time);
144 while (next_start >= 0 && by_decreasing_start_max[next_start].
time == t) {
145 const int task_index = by_decreasing_start_max[next_start].task_index;
146 if (IsInProfile(task_index)) current_height += DemandMin(task_index);
151 while (next_end < num_tasks_ && by_end_min[next_end].
time == t) {
152 const int task_index = by_end_min[next_end].task_index;
153 if (IsInProfile(task_index)) current_height -= DemandMin(task_index);
158 if (current_height != old_height) {
159 profile_.emplace_back(current_start, old_height);
160 if (current_height > profile_max_height_) {
161 profile_max_height_ = current_height;
162 max_height_start = t;
170 profile_.emplace_back(current_start, IntegerValue(0));
176 return IncreaseCapacity(max_height_start, profile_max_height_);
179 void TimeTablingPerTask::ReverseProfile() {
183 for (
int i = 1; i + 1 < profile_.size(); ++i) {
184 profile_[i].start = -profile_[i + 1].start;
186 std::reverse(profile_.begin() + 1, profile_.end() - 1);
189 bool TimeTablingPerTask::SweepAllTasks(
bool is_forward) {
191 const IntegerValue demand_threshold(
192 CapSub(CapacityMax().
value(), profile_max_height_.value()));
196 is_forward ? forward_num_tasks_to_sweep_ : backward_num_tasks_to_sweep_;
197 std::vector<int>& tasks =
198 is_forward ? forward_tasks_to_sweep_ : backward_tasks_to_sweep_;
203 for (
int i = num_tasks - 1; i >= 0; --i) {
204 const int t = tasks[i];
210 std::swap(tasks[i], tasks[--num_tasks]);
215 if (DemandMin(t) <= demand_threshold) {
216 if (DemandMax(t) == 0) {
218 std::swap(tasks[i], tasks[--num_tasks]);
227 if (helper_->
SizeMin(t) == 0) {
228 if (helper_->
SizeMax(t) == 0) {
229 std::swap(tasks[i], tasks[--num_tasks]);
234 if (!SweepTask(t))
return false;
240 bool TimeTablingPerTask::SweepTask(
int task_id) {
242 const IntegerValue size_min = helper_->
SizeMin(task_id);
243 const IntegerValue initial_start_min = helper_->
StartMin(task_id);
244 const IntegerValue initial_end_min = helper_->
EndMin(task_id);
246 IntegerValue new_start_min = initial_start_min;
247 IntegerValue new_end_min = initial_end_min;
251 DCHECK(std::is_sorted(profile_.begin(), profile_.end()));
253 std::upper_bound(profile_.begin(), profile_.end(), new_start_min,
254 [&](IntegerValue
value,
const ProfileRectangle& rect) {
255 return value < rect.start;
262 const IntegerValue conflict_height = CapacityMax() - DemandMin(task_id);
265 bool conflict_found =
false;
278 for (; profile_[rec_id].start < limit; ++rec_id) {
280 if (profile_[rec_id].height <= conflict_height)
continue;
282 conflict_found =
true;
286 new_start_min = profile_[rec_id + 1].start;
288 if (IsInProfile(task_id)) {
298 new_end_min =
std::max(new_end_min, new_start_min + size_min);
301 if (profile_[rec_id].start < initial_end_min) {
302 last_initial_conflict =
std::min(new_start_min, initial_end_min) - 1;
306 if (!conflict_found)
return true;
308 if (initial_start_min != new_start_min &&
309 !UpdateStartingTime(task_id, last_initial_conflict, new_start_min)) {
316 bool TimeTablingPerTask::UpdateStartingTime(
int task_id, IntegerValue left,
317 IntegerValue right) {
320 AddProfileReason(left, right);
338 void TimeTablingPerTask::AddProfileReason(IntegerValue left,
339 IntegerValue right) {
340 for (
int i = 0; i < num_profile_tasks_; ++i) {
341 const int t = profile_tasks_[i];
359 bool TimeTablingPerTask::IncreaseCapacity(IntegerValue
time,
360 IntegerValue new_min) {
361 if (new_min <= CapacityMin())
return true;