27 const std::vector<Rectangle>& rectangles,
28 absl::Span<int> active_rectangles) {
29 if (active_rectangles.empty())
return {};
31 std::vector<absl::Span<int>> result;
32 const int size = active_rectangles.size();
33 for (
int start = 0; start < size;) {
36 for (
int i = start; i < end; i++) {
37 for (
int j = end; j < size; ++j) {
38 if (!rectangles[active_rectangles[i]].IsDisjoint(
39 rectangles[active_rectangles[j]])) {
40 std::swap(active_rectangles[end++], active_rectangles[j]);
44 if (end > start + 1) {
45 result.push_back(active_rectangles.subspan(start, end - start));
57 IntegerValue total_energy(0);
58 for (
const int b : boxes) {
61 if (x_min < bounding_box.x_min || x_max > bounding_box.
x_max)
continue;
64 if (y_min < bounding_box.y_min || y_max > bounding_box.
y_max)
continue;
73 if (total_energy > bounding_box.
Area())
break;
82 const std::vector<IntegerValue>& energies,
83 absl::Span<const int> boxes,
86 std::vector<IntegerValue> x_starts;
87 std::vector<TaskTime> boxes_by_increasing_x_max;
88 for (
const int b : boxes) {
89 x_starts.push_back(rectangles[
b].x_min);
90 boxes_by_increasing_x_max.push_back({
b, rectangles[
b].x_max});
93 std::sort(boxes_by_increasing_x_max.begin(), boxes_by_increasing_x_max.end());
95 std::vector<IntegerValue> y_starts;
96 std::vector<IntegerValue> energy_sum;
97 std::vector<TaskTime> boxes_by_increasing_y_max;
99 std::vector<std::vector<int>> stripes(x_starts.size());
100 for (
int i = 0; i < boxes_by_increasing_x_max.size(); ++i) {
101 const int b = boxes_by_increasing_x_max[i].task_index;
102 const IntegerValue x_min = rectangles[
b].x_min;
103 const IntegerValue x_max = rectangles[
b].x_max;
104 for (
int j = 0; j < x_starts.size(); ++j) {
105 if (x_starts[j] > x_min)
break;
106 stripes[j].push_back(
b);
111 boxes_by_increasing_y_max.clear();
112 for (
const int b : stripes[j]) {
113 y_starts.push_back(rectangles[
b].y_min);
114 boxes_by_increasing_y_max.push_back({
b, rectangles[
b].y_max});
117 std::sort(boxes_by_increasing_y_max.begin(),
118 boxes_by_increasing_y_max.end());
120 const IntegerValue x_size = x_max - x_starts[j];
121 energy_sum.assign(y_starts.size(), IntegerValue(0));
122 for (
int i = 0; i < boxes_by_increasing_y_max.size(); ++i) {
123 const int b = boxes_by_increasing_y_max[i].task_index;
124 const IntegerValue y_min = rectangles[
b].y_min;
125 const IntegerValue y_max = rectangles[
b].y_max;
126 for (
int j = 0; j < y_starts.size(); ++j) {
127 if (y_starts[j] > y_min)
break;
128 energy_sum[j] += energies[
b];
129 if (energy_sum[j] > x_size * (y_max - y_starts[j])) {
130 if (conflict !=
nullptr) {
131 *conflict = rectangles[
b];
132 for (
int k = 0; k < i; ++k) {
133 const int task_index = boxes_by_increasing_y_max[k].task_index;
134 if (rectangles[task_index].y_min >= y_starts[j]) {
149 const std::vector<Rectangle>& rectangles,
150 const std::vector<IntegerValue>& rectangle_energies,
151 IntegerValue* x_threshold, IntegerValue* y_threshold,
158 std::vector<IntegerValue> starts;
159 std::vector<TaskTime> task_by_increasing_x_max;
160 for (
const int t : local_boxes) {
161 const IntegerValue x_min =
162 transpose ? rectangles[t].y_min : rectangles[t].x_min;
163 const IntegerValue x_max =
164 transpose ? rectangles[t].y_max : rectangles[t].x_max;
165 starts.push_back(x_min);
166 task_by_increasing_x_max.push_back({t, x_max});
172 std::sort(task_by_increasing_x_max.begin(), task_by_increasing_x_max.end());
176 IntegerValue max_conflict_height(0);
179 absl::flat_hash_set<std::pair<IntegerValue, IntegerValue>> stripes;
182 std::vector<IntegerValue> energies(starts.size(), IntegerValue(0));
185 std::vector<IntegerValue> energy_at_max_y(starts.size(), IntegerValue(0));
186 std::vector<IntegerValue> energy_at_min_y(starts.size(), IntegerValue(0));
193 const IntegerValue threshold = transpose ? *y_threshold : *x_threshold;
194 for (
int i = 0; i < task_by_increasing_x_max.size(); ++i) {
195 const int t = task_by_increasing_x_max[i].task_index;
197 const IntegerValue
energy = rectangle_energies[t];
198 IntegerValue x_min = rectangles[t].x_min;
199 IntegerValue x_max = rectangles[t].x_max;
200 IntegerValue y_min = rectangles[t].y_min;
201 IntegerValue y_max = rectangles[t].y_max;
208 while (first_j + 1 < starts.size() && x_max - starts[first_j] > threshold) {
211 for (
int j = first_j; starts[j] <= x_min; ++j) {
212 const IntegerValue old_energy_at_max = energy_at_max_y[j];
213 const IntegerValue old_energy_at_min = energy_at_min_y[j];
217 const bool is_disjoint = y_min >= y_maxs[j] || y_max <= y_mins[j];
219 if (y_min <= y_mins[j]) {
220 if (y_min < y_mins[j]) {
222 energy_at_min_y[j] =
energy;
224 energy_at_min_y[j] +=
energy;
228 if (y_max >= y_maxs[j]) {
229 if (y_max > y_maxs[j]) {
231 energy_at_max_y[j] =
energy;
233 energy_at_max_y[j] +=
energy;
240 if (is_disjoint)
continue;
242 const IntegerValue width = x_max - starts[j];
243 IntegerValue conflict_height =
CeilRatio(energies[j], width) - 1;
244 if (y_max - y_min > conflict_height)
continue;
245 if (conflict_height >= y_maxs[j] - y_mins[j]) {
247 if (conflict !=
nullptr) {
248 *conflict = rectangles[t];
249 for (
int k = 0; k < i; ++k) {
250 const int task_index = task_by_increasing_x_max[k].task_index;
251 const IntegerValue task_x_min = transpose
252 ? rectangles[task_index].y_min
253 : rectangles[task_index].x_min;
254 if (task_x_min < starts[j])
continue;
263 IntegerValue can_remove =
std::min(old_energy_at_min, old_energy_at_max);
264 if (old_energy_at_min < old_energy_at_max) {
265 if (y_maxs[j] - y_min >=
266 CeilRatio(energies[j] - old_energy_at_min, width)) {
269 can_remove = old_energy_at_max;
271 }
else if (old_energy_at_max < old_energy_at_min) {
272 if (y_max - y_mins[j] >=
273 CeilRatio(energies[j] - old_energy_at_max, width)) {
274 can_remove = old_energy_at_min;
277 conflict_height =
CeilRatio(energies[j] - can_remove, width) - 1;
281 if (y_max - y_min > conflict_height)
continue;
283 if (
VLOG_IS_ON(2)) stripes.insert({starts[j], x_max});
284 max_conflict_height =
std::max(max_conflict_height, conflict_height);
288 VLOG(2) <<
" num_starts: " << starts.size() - 1 <<
"/" << local_boxes.size()
289 <<
" conflict_height: " << max_conflict_height
290 <<
" num_stripes:" << stripes.size() <<
" (<= " << threshold <<
")";
293 *x_threshold =
std::min(*x_threshold, max_conflict_height);
295 *y_threshold =
std::min(*y_threshold, max_conflict_height);
301 const std::vector<Rectangle>& cached_rectangles, absl::Span<int> boxes,
302 IntegerValue threshold_x, IntegerValue threshold_y,
303 absl::BitGenRef random) {
305 for (
const int b : boxes) {
307 if (dim.
x_max - dim.
x_min > threshold_x)
continue;
308 if (dim.
y_max - dim.
y_min > threshold_y)
continue;
309 boxes[new_size++] =
b;
311 if (new_size == 0)
return {};
312 std::shuffle(&boxes[0], &boxes[0] + new_size, random);
313 return {&boxes[0], new_size};
317 const std::vector<Rectangle>& cached_rectangles,
318 const std::vector<IntegerValue>& energies, absl::Span<int> boxes) {
320 std::sort(boxes.begin(), boxes.end(), [&cached_rectangles](
int a,
int b) {
321 return cached_rectangles[
a].Area() < cached_rectangles[
b].Area();
324 IntegerValue total_energy(0);
325 for (
const int box : boxes) total_energy += energies[box];
329 int new_size = boxes.size();
330 while (new_size > 0 &&
331 cached_rectangles[boxes[new_size - 1]].Area() >= total_energy) {
333 total_energy -= energies[boxes[new_size]];
335 return boxes.subspan(0, new_size);
344 std::vector<IndexedInterval>* intervals,
345 std::vector<std::vector<int>>* result) {
347 if (already_sorted) {
348 DCHECK(std::is_sorted(intervals->begin(), intervals->end(),
351 std::sort(intervals->begin(), intervals->end(),
356 const int size = intervals->size();
362 for (
int end_index = 0; end_index < size;) {
363 const IntegerValue
time = (*intervals)[end_index].start;
368 if (min_end_in_set <=
time) {
369 result->push_back({});
371 for (
int i = start_index; i < end_index; ++i) {
372 result->back().push_back((*intervals)[i].
index);
373 if ((*intervals)[i].end <=
time) {
374 std::swap((*intervals)[start_index++], (*intervals)[i]);
376 min_end_in_set =
std::min(min_end_in_set, (*intervals)[i].end);
381 if (result->back().size() == 1) result->pop_back();
386 min_end_in_set =
std::min(min_end_in_set, (*intervals)[end_index].end);
388 }
while (end_index < size && (*intervals)[end_index].start ==
time);
393 std::vector<IndexedInterval>* intervals,
394 std::vector<std::vector<int>>* components) {
396 if (intervals->empty())
return;
397 if (intervals->size() == 1) {
398 components->push_back({intervals->front().index});
409 std::sort(intervals->begin(), intervals->end(),
412 IntegerValue end_max_so_far = (*intervals)[0].end;
413 components->push_back({(*intervals)[0].index});
414 for (
int i = 1; i < intervals->size(); ++i) {
416 if (
interval.start >= end_max_so_far) {
426 std::vector<IndexedInterval>* intervals) {
427 std::vector<int> articulation_points;
428 if (intervals->size() < 3)
return articulation_points;
435 std::sort(intervals->begin(), intervals->end(),
438 IntegerValue end_max_so_far = (*intervals)[0].end;
439 int index_of_max = 0;
441 for (
int i = 1; i < intervals->size(); ++i) {
443 if (
interval.start >= end_max_so_far) {
454 if (articulation_points.empty() ||
455 articulation_points.back() != index_of_max) {
456 articulation_points.push_back(index_of_max);
460 if (
interval.end > end_max_so_far) {
461 prev_end_max = end_max_so_far;
464 }
else if (
interval.end > prev_end_max) {
469 for (
int&
index : articulation_points)
index = (*intervals)[
index].index;
470 return articulation_points;
IntegerValue SizeMin(int t) const
void GetOverlappingIntervalComponents(std::vector< IndexedInterval > *intervals, std::vector< std::vector< int >> *components)
constexpr IntegerValue kMinIntegerValue(-kMaxIntegerValue)
absl::Span< int > FilterBoxesAndRandomize(const std::vector< Rectangle > &cached_rectangles, absl::Span< int > boxes, IntegerValue threshold_x, IntegerValue threshold_y, absl::BitGenRef random)
#define CHECK_GT(val1, val2)
#define VLOG(verboselevel)
int index() const
Returns the index of the interval constraint in the model.
void swap(IdMap< K, V > &a, IdMap< K, V > &b)
void ImportOtherReasons(const SchedulingConstraintHelper &other_helper)
void STLSortAndRemoveDuplicates(T *v, const LessFunc &less_func)
constexpr IntegerValue kMaxIntegerValue(std::numeric_limits< IntegerValue::ValueType >::max() - 1)
IntegerValue ShiftedEndMax(int t) const
void AddEnergyMinInIntervalReason(int t, IntegerValue min, IntegerValue max)
IntegerValue Area() const
void ConstructOverlappingSets(bool already_sorted, std::vector< IndexedInterval > *intervals, std::vector< std::vector< int >> *result)
void TakeUnionWith(const Rectangle &other)
std::vector< absl::Span< int > > GetOverlappingRectangleComponents(const std::vector< Rectangle > &rectangles, absl::Span< int > active_rectangles)
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)
ABSL_MUST_USE_RESULT bool ReportConflict()
#define DCHECK(condition)
bool BoxesAreInEnergyConflict(const std::vector< Rectangle > &rectangles, const std::vector< IntegerValue > &energies, absl::Span< const int > boxes, Rectangle *conflict)
bool ReportEnergyConflict(Rectangle bounding_box, absl::Span< const int > boxes, SchedulingConstraintHelper *x, SchedulingConstraintHelper *y)
IntegerValue CeilRatio(IntegerValue dividend, IntegerValue positive_divisor)
Collection of objects used to extend the Constraint Solver library.
absl::Span< int > FilterBoxesThatAreTooLarge(const std::vector< Rectangle > &cached_rectangles, const std::vector< IntegerValue > &energies, absl::Span< int > boxes)
std::vector< int > GetIntervalArticulationPoints(std::vector< IndexedInterval > *intervals)
IntegerValue ShiftedStartMin(int t) const
std::ostream & operator<<(std::ostream &os, const BoolVar &var)
bool IsDisjoint(const Rectangle &other) const
#define VLOG_IS_ON(verboselevel)
#define DCHECK_LT(val1, val2)