18 #include "absl/container/flat_hash_map.h"
19 #include "absl/strings/str_join.h"
37 std::vector<AffineExpression> sizes;
41 for (
int box = 0; box < y->
NumTasks(); ++box) {
42 IntegerVariable s_var = y->
SizeVars()[box];
52 const IntegerVariable min_start_var =
56 const IntegerVariable max_end_var =
63 const std::vector<int64> coeffs = {-
capacity.coeff.value(), -1, 1};
77 IntegerValue FindCanonicalValue(IntegerValue lb, IntegerValue ub) {
78 if (lb == ub)
return lb;
79 if (lb <= 0 && ub > 0)
return IntegerValue(0);
80 if (lb < 0 && ub <= 0) {
81 return -FindCanonicalValue(-ub, -lb);
85 IntegerValue candidate = ub;
86 for (
int o = 0; o < 62; ++o) {
88 const IntegerValue masked_ub(ub.value() & ~mask);
89 if (masked_ub >= lb) {
90 candidate = masked_ub;
98 void SplitDisjointBoxes(
const SchedulingConstraintHelper& x,
99 absl::Span<int> boxes,
100 std::vector<absl::Span<int>>* result) {
102 std::sort(boxes.begin(), boxes.end(),
103 [&x](
int a,
int b) { return x.StartMin(a) < x.StartMin(b); });
104 int current_start = 0;
105 std::size_t current_length = 1;
106 IntegerValue current_max_end = x.EndMax(boxes[0]);
108 for (
int b = 1;
b < boxes.size(); ++
b) {
109 const int box = boxes[
b];
110 if (x.StartMin(box) < current_max_end) {
113 current_max_end =
std::max(current_max_end, x.EndMax(box));
115 if (current_length > 1) {
116 result->emplace_back(&boxes[current_start], current_length);
120 current_max_end = x.EndMax(box);
125 if (current_length > 1) {
126 result->emplace_back(&boxes[current_start], current_length);
132 #define RETURN_IF_FALSE(f) \
133 if (!(f)) return false;
139 const int num_boxes = x_.
NumTasks();
143 active_boxes_.clear();
144 cached_areas_.resize(num_boxes);
145 cached_dimensions_.resize(num_boxes);
146 for (
int box = 0; box < num_boxes; ++box) {
148 if (cached_areas_[box] == 0)
continue;
151 Dimension& dimension = cached_dimensions_[box];
153 dimension.x_max = x_.
EndMax(box);
155 dimension.y_max = y_.
EndMax(box);
157 active_boxes_.push_back(box);
159 if (active_boxes_.size() <= 1)
return true;
161 SplitDisjointBoxes(x_, absl::MakeSpan(active_boxes_), &x_split_);
162 for (absl::Span<int> x_boxes : x_split_) {
163 SplitDisjointBoxes(y_, x_boxes, &y_split_);
164 for (absl::Span<int> y_boxes : y_split_) {
165 IntegerValue total_sum_of_areas(0);
166 for (
const int box : y_boxes) {
167 total_sum_of_areas += cached_areas_[box];
169 for (
const int box : y_boxes) {
171 FailWhenEnergyIsTooLarge(box, y_boxes, total_sum_of_areas));
181 const int id = watcher->
Register(
this);
189 void NonOverlappingRectanglesEnergyPropagator::SortBoxesIntoNeighbors(
190 int box, absl::Span<const int> local_boxes,
191 IntegerValue total_sum_of_areas) {
192 const Dimension& box_dim = cached_dimensions_[box];
195 for (
const int other_box : local_boxes) {
196 if (other_box == box)
continue;
197 const Dimension& other_dim = cached_dimensions_[other_box];
198 const IntegerValue span_x =
std::max(box_dim.x_max, other_dim.x_max) -
199 std::min(box_dim.x_min, other_dim.x_min);
200 const IntegerValue span_y =
std::max(box_dim.y_max, other_dim.y_max) -
201 std::min(box_dim.y_min, other_dim.y_min);
202 const IntegerValue bounding_area = span_x * span_y;
203 if (bounding_area < total_sum_of_areas) {
204 neighbors_.push_back({other_box, bounding_area});
207 std::sort(neighbors_.begin(), neighbors_.end());
210 bool NonOverlappingRectanglesEnergyPropagator::FailWhenEnergyIsTooLarge(
211 int box, absl::Span<const int> local_boxes,
212 IntegerValue total_sum_of_areas) {
213 SortBoxesIntoNeighbors(box, local_boxes, total_sum_of_areas);
215 Dimension area = cached_dimensions_[box];
216 IntegerValue sum_of_areas = cached_areas_[box];
218 const auto add_box_energy_in_rectangle_reason = [&](
int b) {
225 for (
int i = 0; i < neighbors_.size(); ++i) {
226 const int other_box = neighbors_[i].box;
227 CHECK_GT(cached_areas_[other_box], 0);
230 area.TakeUnionWith(cached_dimensions_[other_box]);
233 sum_of_areas += cached_areas_[other_box];
234 const IntegerValue bounding_area =
235 (area.x_max - area.x_min) * (area.y_max - area.y_min);
236 if (bounding_area >= total_sum_of_areas) {
241 if (sum_of_areas > bounding_area) {
244 add_box_energy_in_rectangle_reason(box);
245 for (
int j = 0; j <= i; ++j) {
246 add_box_energy_in_rectangle_reason(neighbors_[j].box);
264 x_(x->NumTasks(),
model),
265 y_(y->NumTasks(),
model),
268 overload_checker_(&x_),
269 forward_detectable_precedences_(true, &x_),
270 backward_detectable_precedences_(false, &x_),
271 forward_not_last_(true, &x_),
272 backward_not_last_(false, &x_),
273 forward_edge_finding_(true, &x_),
274 backward_edge_finding_(false, &x_) {}
280 int fast_priority,
int slow_priority) {
281 fast_id_ = watcher_->
Register(
this);
286 const int slow_id = watcher_->
Register(
this);
292 bool NonOverlappingRectanglesDisjunctivePropagator::
293 FindBoxesThatMustOverlapAHorizontalLineAndPropagate(
296 std::function<
bool()> inner_propagate) {
298 active_boxes_.clear();
299 events_time_.clear();
300 for (
int box = 0; box < x.
NumTasks(); ++box) {
309 active_boxes_.push_back(box);
314 if (active_boxes_.size() < 2)
return true;
318 events_overlapping_boxes_.resize(events_time_.size());
319 for (
int i = 0; i < events_time_.size(); ++i) {
320 events_overlapping_boxes_[i].clear();
322 for (
const int box : active_boxes_) {
326 for (
int i = 0; i < events_time_.size(); ++i) {
327 const IntegerValue t = events_time_[i];
330 events_overlapping_boxes_[i].push_back(box);
342 for (std::vector<int>& overlapping_boxes : events_overlapping_boxes_) {
343 if (overlapping_boxes.size() < 2) {
347 const std::vector<int>& previous_overlapping_boxes =
348 events_overlapping_boxes_[new_size - 1];
356 if (std::includes(overlapping_boxes.begin(), overlapping_boxes.end(),
357 previous_overlapping_boxes.begin(),
358 previous_overlapping_boxes.end())) {
363 std::swap(events_overlapping_boxes_[new_size], overlapping_boxes);
369 boxes_to_propagate_.clear();
370 reduced_overlapping_boxes_.clear();
371 for (
int i = 0; i < new_size; ++i) {
372 SplitDisjointBoxes(x, absl::MakeSpan(events_overlapping_boxes_[i]),
374 for (absl::Span<int> sub_boxes : disjoint_boxes_) {
378 const auto& insertion = reduced_overlapping_boxes_.insert(sub_boxes);
379 if (insertion.second) boxes_to_propagate_.push_back(sub_boxes);
385 for (
const absl::Span<const int> boxes : boxes_to_propagate_) {
392 for (
int i = 0; i < y_.
NumTasks(); ++i) {
406 const IntegerValue line_to_use_for_reason = FindCanonicalValue(lb, ub);
421 std::function<bool()> inner_propagate;
423 inner_propagate = [
this]() {
437 inner_propagate = [
this]() {
438 if (x_.
NumTasks() <= 2)
return true;
448 global_x_, global_y_, inner_propagate));
452 global_y_, global_x_, inner_propagate));
459 bool NonOverlappingRectanglesDisjunctivePropagator::PropagateTwoBoxes() {
464 const auto left_box_before_right_box = [
this](
int left,
int right) {
466 const IntegerValue left_end_min = x_.
EndMin(left);
467 if (left_end_min > x_.
StartMin(right)) {
475 const IntegerValue right_start_max = x_.
StartMax(right);
476 if (right_start_max < x_.
EndMax(left)) {
494 return left_box_before_right_box(0, 1);
497 return left_box_before_right_box(1, 0);
505 #undef RETURN_IF_FALSE