22#include "absl/container/flat_hash_set.h"
23#include "absl/random/bit_gen_ref.h"
24#include "absl/types/span.h"
41 const std::vector<Rectangle>& rectangles,
42 absl::Span<int> active_rectangles) {
43 if (active_rectangles.empty())
return {};
45 std::vector<absl::Span<int>> result;
46 const int size = active_rectangles.size();
51 for (
int j =
end; j < size; ++j) {
52 if (!rectangles[active_rectangles[i]].IsDisjoint(
53 rectangles[active_rectangles[j]])) {
54 std::swap(active_rectangles[
end++], active_rectangles[j]);
59 result.push_back(active_rectangles.subspan(
start,
end -
start));
71 IntegerValue total_energy(0);
72 for (
const int b : boxes) {
75 if (x_min < bounding_box.x_min || x_max > bounding_box.
x_max)
continue;
78 if (y_min < bounding_box.y_min || y_max > bounding_box.
y_max)
continue;
90 if (total_energy > bounding_box.
Area())
break;
99 const std::vector<IntegerValue>& energies,
100 absl::Span<const int> boxes,
103 std::vector<IntegerValue> x_starts;
104 std::vector<TaskTime> boxes_by_increasing_x_max;
105 for (
const int b : boxes) {
106 x_starts.push_back(rectangles[
b].x_min);
107 boxes_by_increasing_x_max.push_back({
b, rectangles[
b].x_max});
110 std::sort(boxes_by_increasing_x_max.begin(), boxes_by_increasing_x_max.end());
112 std::vector<IntegerValue> y_starts;
113 std::vector<IntegerValue> energy_sum;
114 std::vector<TaskTime> boxes_by_increasing_y_max;
116 std::vector<std::vector<int>> stripes(x_starts.size());
117 for (
int i = 0; i < boxes_by_increasing_x_max.size(); ++i) {
118 const int b = boxes_by_increasing_x_max[i].task_index;
119 const IntegerValue x_min = rectangles[
b].x_min;
120 const IntegerValue x_max = rectangles[
b].x_max;
121 for (
int j = 0; j < x_starts.size(); ++j) {
122 if (x_starts[j] > x_min)
break;
123 stripes[j].push_back(
b);
128 boxes_by_increasing_y_max.clear();
129 for (
const int b : stripes[j]) {
130 y_starts.push_back(rectangles[
b].y_min);
131 boxes_by_increasing_y_max.push_back({
b, rectangles[
b].y_max});
134 std::sort(boxes_by_increasing_y_max.begin(),
135 boxes_by_increasing_y_max.end());
137 const IntegerValue x_size = x_max - x_starts[j];
138 energy_sum.assign(y_starts.size(), IntegerValue(0));
139 for (
int i = 0; i < boxes_by_increasing_y_max.size(); ++i) {
140 const int b = boxes_by_increasing_y_max[i].task_index;
141 const IntegerValue y_min = rectangles[
b].y_min;
142 const IntegerValue y_max = rectangles[
b].y_max;
143 for (
int j = 0; j < y_starts.size(); ++j) {
144 if (y_starts[j] > y_min)
break;
145 energy_sum[j] += energies[
b];
146 if (energy_sum[j] > x_size * (y_max - y_starts[j])) {
147 if (conflict !=
nullptr) {
148 *conflict = rectangles[
b];
149 for (
int k = 0; k < i; ++k) {
150 const int task_index = boxes_by_increasing_y_max[k].task_index;
151 if (rectangles[task_index].y_min >= y_starts[j]) {
166 const std::vector<Rectangle>& rectangles,
167 const std::vector<IntegerValue>& rectangle_energies,
168 IntegerValue* x_threshold, IntegerValue* y_threshold,
175 std::vector<IntegerValue> starts;
176 std::vector<TaskTime> task_by_increasing_x_max;
177 for (
const int t : local_boxes) {
178 const IntegerValue x_min =
179 transpose ? rectangles[t].y_min : rectangles[t].x_min;
180 const IntegerValue x_max =
181 transpose ? rectangles[t].y_max : rectangles[t].x_max;
182 starts.push_back(x_min);
183 task_by_increasing_x_max.push_back({t, x_max});
189 std::sort(task_by_increasing_x_max.begin(), task_by_increasing_x_max.end());
193 IntegerValue max_conflict_height(0);
196 absl::flat_hash_set<std::pair<IntegerValue, IntegerValue>> stripes;
199 std::vector<IntegerValue> energies(starts.size(), IntegerValue(0));
202 std::vector<IntegerValue> energy_at_max_y(starts.size(), IntegerValue(0));
203 std::vector<IntegerValue> energy_at_min_y(starts.size(), IntegerValue(0));
210 const IntegerValue threshold = transpose ? *y_threshold : *x_threshold;
211 for (
int i = 0; i < task_by_increasing_x_max.size(); ++i) {
212 const int t = task_by_increasing_x_max[i].task_index;
214 const IntegerValue
energy = rectangle_energies[t];
215 IntegerValue x_min = rectangles[t].x_min;
216 IntegerValue x_max = rectangles[t].x_max;
217 IntegerValue y_min = rectangles[t].y_min;
218 IntegerValue y_max = rectangles[t].y_max;
225 while (first_j + 1 < starts.size() && x_max - starts[first_j] > threshold) {
228 for (
int j = first_j; starts[j] <= x_min; ++j) {
229 const IntegerValue old_energy_at_max = energy_at_max_y[j];
230 const IntegerValue old_energy_at_min = energy_at_min_y[j];
234 const bool is_disjoint = y_min >= y_maxs[j] || y_max <= y_mins[j];
236 if (y_min <= y_mins[j]) {
237 if (y_min < y_mins[j]) {
239 energy_at_min_y[j] =
energy;
241 energy_at_min_y[j] +=
energy;
245 if (y_max >= y_maxs[j]) {
246 if (y_max > y_maxs[j]) {
248 energy_at_max_y[j] =
energy;
250 energy_at_max_y[j] +=
energy;
257 if (is_disjoint)
continue;
259 const IntegerValue width = x_max - starts[j];
260 IntegerValue conflict_height =
CeilRatio(energies[j], width) - 1;
261 if (y_max - y_min > conflict_height)
continue;
262 if (conflict_height >= y_maxs[j] - y_mins[j]) {
264 if (conflict !=
nullptr) {
265 *conflict = rectangles[t];
266 for (
int k = 0; k < i; ++k) {
267 const int task_index = task_by_increasing_x_max[k].task_index;
268 const IntegerValue task_x_min = transpose
269 ? rectangles[task_index].
y_min
270 : rectangles[task_index].x_min;
271 if (task_x_min < starts[j])
continue;
280 IntegerValue can_remove =
std::min(old_energy_at_min, old_energy_at_max);
281 if (old_energy_at_min < old_energy_at_max) {
282 if (y_maxs[j] - y_min >=
283 CeilRatio(energies[j] - old_energy_at_min, width)) {
286 can_remove = old_energy_at_max;
288 }
else if (old_energy_at_max < old_energy_at_min) {
289 if (y_max - y_mins[j] >=
290 CeilRatio(energies[j] - old_energy_at_max, width)) {
291 can_remove = old_energy_at_min;
294 conflict_height =
CeilRatio(energies[j] - can_remove, width) - 1;
298 if (y_max - y_min > conflict_height)
continue;
300 if (
VLOG_IS_ON(2)) stripes.insert({starts[j], x_max});
301 max_conflict_height =
std::max(max_conflict_height, conflict_height);
305 VLOG(2) <<
" num_starts: " << starts.size() - 1 <<
"/" << local_boxes.size()
306 <<
" conflict_height: " << max_conflict_height
307 <<
" num_stripes:" << stripes.size() <<
" (<= " << threshold <<
")";
310 *x_threshold =
std::min(*x_threshold, max_conflict_height);
312 *y_threshold =
std::min(*y_threshold, max_conflict_height);
318 const std::vector<Rectangle>& cached_rectangles, absl::Span<int> boxes,
319 IntegerValue threshold_x, IntegerValue threshold_y,
320 absl::BitGenRef random) {
322 for (
const int b : boxes) {
324 if (dim.
x_max - dim.
x_min > threshold_x)
continue;
325 if (dim.
y_max - dim.
y_min > threshold_y)
continue;
326 boxes[new_size++] =
b;
328 if (new_size == 0)
return {};
329 std::shuffle(&boxes[0], &boxes[0] + new_size, random);
330 return {&boxes[0], new_size};
334 const std::vector<Rectangle>& cached_rectangles,
335 const std::vector<IntegerValue>& energies, absl::Span<int> boxes) {
337 std::sort(boxes.begin(), boxes.end(), [&cached_rectangles](
int a,
int b) {
338 return cached_rectangles[a].Area() < cached_rectangles[b].Area();
341 IntegerValue total_energy(0);
342 for (
const int box : boxes) total_energy += energies[box];
346 int new_size = boxes.size();
347 while (new_size > 0 &&
348 cached_rectangles[boxes[new_size - 1]].Area() >= total_energy) {
350 total_energy -= energies[boxes[new_size]];
352 return boxes.subspan(0, new_size);
361 std::vector<IndexedInterval>* intervals,
362 std::vector<std::vector<int>>* result) {
364 if (already_sorted) {
365 DCHECK(std::is_sorted(intervals->begin(), intervals->end(),
368 std::sort(intervals->begin(), intervals->end(),
373 const int size = intervals->size();
379 for (
int end_index = 0; end_index < size;) {
380 const IntegerValue
time = (*intervals)[end_index].start;
385 if (min_end_in_set <=
time) {
386 result->push_back({});
388 for (
int i = start_index; i < end_index; ++i) {
389 result->back().push_back((*intervals)[i].
index);
390 if ((*intervals)[i].
end <=
time) {
391 std::swap((*intervals)[start_index++], (*intervals)[i]);
393 min_end_in_set =
std::min(min_end_in_set, (*intervals)[i].
end);
398 if (result->back().size() == 1) result->pop_back();
403 min_end_in_set =
std::min(min_end_in_set, (*intervals)[end_index].
end);
405 }
while (end_index < size && (*intervals)[end_index].
start ==
time);
410 std::vector<IndexedInterval>* intervals,
411 std::vector<std::vector<int>>* components) {
413 if (intervals->empty())
return;
414 if (intervals->size() == 1) {
415 components->push_back({intervals->front().
index});
426 std::sort(intervals->begin(), intervals->end(),
429 IntegerValue end_max_so_far = (*intervals)[0].end;
430 components->push_back({(*intervals)[0].index});
431 for (
int i = 1; i < intervals->size(); ++i) {
433 if (
interval.start >= end_max_so_far) {
443 std::vector<IndexedInterval>* intervals) {
444 std::vector<int> articulation_points;
445 if (intervals->size() < 3)
return articulation_points;
452 std::sort(intervals->begin(), intervals->end(),
455 IntegerValue end_max_so_far = (*intervals)[0].end;
456 int index_of_max = 0;
458 for (
int i = 1; i < intervals->size(); ++i) {
460 if (
interval.start >= end_max_so_far) {
471 if (articulation_points.empty() ||
472 articulation_points.back() != index_of_max) {
473 articulation_points.push_back(index_of_max);
477 if (
interval.end > end_max_so_far) {
478 prev_end_max = end_max_so_far;
481 }
else if (
interval.end > prev_end_max) {
486 for (
int&
index : articulation_points)
index = (*intervals)[
index].index;
487 return articulation_points;
492 num_rectangles_added_ = 0;
496 IntegerValue y_min, IntegerValue y_max) {
498 if (x_min == x_max)
return;
501 StartRectangleEvent(num_rectangles_added_, x_min, y_min, y_max));
502 events_.push_back(EndRectangleEvent(num_rectangles_added_, x_max));
503 ++num_rectangles_added_;
508 IntegerValue y_height) {
510 if (x_min == x_max)
return;
512 events_.push_back(ChangeMandatoryProfileEvent(x_min, y_height));
513 events_.push_back(ChangeMandatoryProfileEvent(x_max, -y_height));
517 std::vector<CapacityProfile::Rectangle>* result) {
518 std::sort(events_.begin(), events_.end());
521 IntegerValue mandatory_capacity(0);
527 for (
int i = 0; i < events_.size();) {
528 const IntegerValue current_time = events_[i].time;
529 for (; i < events_.size(); ++i) {
530 const Event&
event = events_[i];
531 if (event.time != current_time)
break;
533 switch (events_[i].type) {
534 case START_RECTANGLE: {
535 min_pq.
Add({
event.index, -
event.y_min});
536 max_pq.
Add({
event.index,
event.y_max});
539 case END_RECTANGLE: {
540 min_pq.
Remove(event.index);
541 max_pq.
Remove(event.index);
544 case CHANGE_MANDATORY_PROFILE: {
545 mandatory_capacity +=
event.y_min;
552 const IntegerValue new_height =
555 : max_pq.
Top().value + min_pq.
Top().value - mandatory_capacity;
556 if (new_height != result->back().height) {
557 result->push_back({current_time, new_height});
563 std::sort(events_.begin(), events_.end());
567 IntegerValue area(0);
569 IntegerValue previous_height(0);
571 for (
int i = 0; i < events_.size();) {
572 const IntegerValue current_time = events_[i].time;
573 for (; i < events_.size(); ++i) {
574 const Event&
event = events_[i];
575 if (event.time != current_time)
break;
577 switch (event.type) {
578 case START_RECTANGLE: {
579 min_pq.
Add({
event.index, -
event.y_min});
580 max_pq.
Add({
event.index,
event.y_max});
583 case END_RECTANGLE: {
584 min_pq.
Remove(event.index);
585 max_pq.
Remove(event.index);
588 case CHANGE_MANDATORY_PROFILE: {
593 const IntegerValue new_height =
594 max_pq.
IsEmpty() ? IntegerValue(0)
595 : max_pq.
Top().value + min_pq.
Top().value;
596 if (previous_height != 0) {
597 area += previous_height * (current_time - previous_time);
599 previous_time = current_time;
600 previous_height = new_height;
#define DCHECK_LE(val1, val2)
#define CHECK_GT(val1, val2)
#define DCHECK_LT(val1, val2)
#define DCHECK(condition)
#define VLOG(verboselevel)
void Add(Element element)
void AddMandatoryConsumption(IntegerValue x_min, IntegerValue x_max, IntegerValue y_height)
void AddRectangle(IntegerValue x_min, IntegerValue x_max, IntegerValue y_min, IntegerValue y_max)
void BuildResidualCapacityProfile(std::vector< Rectangle > *result)
IntegerValue GetBoundingArea()
int index() const
Returns the index of the interval constraint in the model.
IntegerValue ShiftedStartMin(int t) const
void AddPresenceReason(int t)
IntegerValue ShiftedEndMax(int t) const
ABSL_MUST_USE_RESULT bool ReportConflict()
void ImportOtherReasons(const SchedulingConstraintHelper &other_helper)
void AddEnergyMinInIntervalReason(int t, IntegerValue min, IntegerValue max)
IntegerValue SizeMin(int t) const
void STLSortAndRemoveDuplicates(T *v, const LessFunc &less_func)
void swap(IdMap< K, V > &a, IdMap< K, V > &b)
constexpr IntegerValue kMaxIntegerValue(std::numeric_limits< IntegerValue::ValueType >::max() - 1)
std::ostream & operator<<(std::ostream &os, const BoolVar &var)
void GetOverlappingIntervalComponents(std::vector< IndexedInterval > *intervals, std::vector< std::vector< int > > *components)
IntegerValue CeilRatio(IntegerValue dividend, IntegerValue positive_divisor)
std::vector< int > GetIntervalArticulationPoints(std::vector< IndexedInterval > *intervals)
std::vector< absl::Span< int > > GetOverlappingRectangleComponents(const std::vector< Rectangle > &rectangles, absl::Span< int > active_rectangles)
absl::Span< int > FilterBoxesAndRandomize(const std::vector< Rectangle > &cached_rectangles, absl::Span< int > boxes, IntegerValue threshold_x, IntegerValue threshold_y, absl::BitGenRef random)
constexpr IntegerValue kMinIntegerValue(-kMaxIntegerValue.value())
bool AnalyzeIntervals(bool transpose, absl::Span< const int > local_boxes, const std::vector< Rectangle > &rectangles, const std::vector< IntegerValue > &rectangle_energies, IntegerValue *x_threshold, IntegerValue *y_threshold, Rectangle *conflict)
bool ReportEnergyConflict(Rectangle bounding_box, absl::Span< const int > boxes, SchedulingConstraintHelper *x, SchedulingConstraintHelper *y)
void ConstructOverlappingSets(bool already_sorted, std::vector< IndexedInterval > *intervals, std::vector< std::vector< int > > *result)
bool BoxesAreInEnergyConflict(const std::vector< Rectangle > &rectangles, const std::vector< IntegerValue > &energies, absl::Span< const int > boxes, Rectangle *conflict)
absl::Span< int > FilterBoxesThatAreTooLarge(const std::vector< Rectangle > &cached_rectangles, const std::vector< IntegerValue > &energies, absl::Span< int > boxes)
Collection of objects used to extend the Constraint Solver library.
std::optional< int64_t > end
IntegerValue Area() const
void TakeUnionWith(const Rectangle &other)
bool IsDisjoint(const Rectangle &other) const
#define VLOG_IS_ON(verboselevel)