forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 9
/
diffn_util.h
655 lines (556 loc) · 24.8 KB
/
diffn_util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
// Copyright 2010-2024 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef OR_TOOLS_SAT_DIFFN_UTIL_H_
#define OR_TOOLS_SAT_DIFFN_UTIL_H_
#include <algorithm>
#include <iosfwd>
#include <limits>
#include <optional>
#include <ostream>
#include <string>
#include <string_view>
#include <tuple>
#include <utility>
#include <vector>
#include "absl/container/flat_hash_set.h"
#include "absl/container/inlined_vector.h"
#include "absl/log/check.h"
#include "absl/random/bit_gen_ref.h"
#include "absl/strings/str_format.h"
#include "absl/types/span.h"
#include "ortools/sat/integer.h"
#include "ortools/sat/intervals.h"
#include "ortools/util/strong_integers.h"
namespace operations_research {
namespace sat {
struct Rectangle {
IntegerValue x_min;
IntegerValue x_max;
IntegerValue y_min;
IntegerValue y_max;
void GrowToInclude(const Rectangle& other) {
x_min = std::min(x_min, other.x_min);
y_min = std::min(y_min, other.y_min);
x_max = std::max(x_max, other.x_max);
y_max = std::max(y_max, other.y_max);
}
IntegerValue Area() const { return SizeX() * SizeY(); }
IntegerValue SizeX() const { return x_max - x_min; }
IntegerValue SizeY() const { return y_max - y_min; }
bool IsDisjoint(const Rectangle& other) const;
// Returns an empty rectangle if no intersection.
Rectangle Intersect(const Rectangle& other) const;
IntegerValue IntersectArea(const Rectangle& other) const;
// Returns `this \ other` as a set of disjoint rectangles of non-empty area.
// The resulting vector will have at most four elements.
absl::InlinedVector<Rectangle, 4> RegionDifference(
const Rectangle& other) const;
template <typename Sink>
friend void AbslStringify(Sink& sink, const Rectangle& r) {
absl::Format(&sink, "rectangle(x(%i..%i), y(%i..%i))", r.x_min.value(),
r.x_max.value(), r.y_min.value(), r.y_max.value());
}
bool operator==(const Rectangle& other) const {
return std::tie(x_min, x_max, y_min, y_max) ==
std::tie(other.x_min, other.x_max, other.y_min, other.y_max);
}
bool operator!=(const Rectangle& other) const { return !(other == *this); }
static Rectangle GetEmpty() {
return Rectangle{.x_min = IntegerValue(0),
.x_max = IntegerValue(0),
.y_min = IntegerValue(0),
.y_max = IntegerValue(0)};
}
};
inline Rectangle Rectangle::Intersect(const Rectangle& other) const {
const IntegerValue ret_x_min = std::max(x_min, other.x_min);
const IntegerValue ret_y_min = std::max(y_min, other.y_min);
const IntegerValue ret_x_max = std::min(x_max, other.x_max);
const IntegerValue ret_y_max = std::min(y_max, other.y_max);
if (ret_x_min >= ret_x_max || ret_y_min >= ret_y_max) {
return GetEmpty();
} else {
return Rectangle{.x_min = ret_x_min,
.x_max = ret_x_max,
.y_min = ret_y_min,
.y_max = ret_y_max};
}
}
inline IntegerValue Rectangle::IntersectArea(const Rectangle& other) const {
const IntegerValue ret_x_min = std::max(x_min, other.x_min);
const IntegerValue ret_y_min = std::max(y_min, other.y_min);
const IntegerValue ret_x_max = std::min(x_max, other.x_max);
const IntegerValue ret_y_max = std::min(y_max, other.y_max);
if (ret_x_min >= ret_x_max || ret_y_min >= ret_y_max) {
return 0;
} else {
return (ret_x_max - ret_x_min) * (ret_y_max - ret_y_min);
}
}
// Creates a graph when two nodes are connected iff their rectangles overlap.
// Then partition into connected components.
//
// This method removes all singleton components. It will modify the
// active_rectangle span in place.
std::vector<absl::Span<int>> GetOverlappingRectangleComponents(
absl::Span<const Rectangle> rectangles, absl::Span<int> active_rectangles);
// Visible for testing. The algo is in O(n^4) so shouldn't be used directly.
// Returns true if there exist a bounding box with too much energy.
bool BoxesAreInEnergyConflict(absl::Span<const Rectangle> rectangles,
absl::Span<const IntegerValue> energies,
absl::Span<const int> boxes,
Rectangle* conflict = nullptr);
// Checks that there is indeed a conflict for the given bounding_box and
// report it. This returns false for convenience as we usually want to return
// false on a conflict.
//
// TODO(user): relax the bounding box dimension to have a relaxed explanation.
// We can also minimize the number of required intervals.
bool ReportEnergyConflict(Rectangle bounding_box, absl::Span<const int> boxes,
SchedulingConstraintHelper* x,
SchedulingConstraintHelper* y);
// A O(n^2) algorithm to analyze all the relevant X intervals and infer a
// threshold of the y size of a bounding box after which there is no point
// checking for energy overload.
//
// Returns false on conflict, and fill the bounding box that caused the
// conflict.
//
// If transpose is true, we analyze the relevant Y intervals instead.
bool AnalyzeIntervals(bool transpose, absl::Span<const int> boxes,
absl::Span<const Rectangle> rectangles,
absl::Span<const IntegerValue> rectangle_energies,
IntegerValue* x_threshold, IntegerValue* y_threshold,
Rectangle* conflict = nullptr);
// Removes boxes with a size above the thresholds. Also randomize the order.
// Because we rely on various heuristic, this allow to change the order from
// one call to the next.
absl::Span<int> FilterBoxesAndRandomize(
absl::Span<const Rectangle> cached_rectangles, absl::Span<int> boxes,
IntegerValue threshold_x, IntegerValue threshold_y, absl::BitGenRef random);
// Given the total energy of all rectangles (sum of energies[box]) we know that
// any box with an area greater than that cannot participate in any "bounding
// box" conflict. As we remove this box, the total energy decrease, so we might
// remove more. This works in O(n log n).
absl::Span<int> FilterBoxesThatAreTooLarge(
absl::Span<const Rectangle> cached_rectangles,
absl::Span<const IntegerValue> energies, absl::Span<int> boxes);
struct IndexedInterval {
int index;
IntegerValue start;
IntegerValue end;
bool operator==(const IndexedInterval& rhs) const {
return std::tie(start, end, index) ==
std::tie(rhs.start, rhs.end, rhs.index);
}
// NOTE(user): We would like to use TUPLE_DEFINE_STRUCT, but sadly it doesn't
// support //buildenv/target:non_prod.
struct ComparatorByStartThenEndThenIndex {
bool operator()(const IndexedInterval& a, const IndexedInterval& b) const {
return std::tie(a.start, a.end, a.index) <
std::tie(b.start, b.end, b.index);
}
};
struct ComparatorByStart {
bool operator()(const IndexedInterval& a, const IndexedInterval& b) const {
return a.start < b.start;
}
};
template <typename Sink>
friend void AbslStringify(Sink& sink, const IndexedInterval& interval) {
absl::Format(&sink, "[%v..%v] (#%v)", interval.start, interval.end,
interval.index);
}
};
// Given n fixed intervals, returns the subsets of intervals that overlap during
// at least one time unit. Note that we only return "maximal" subset and filter
// subset strictly included in another.
//
// All Intervals must have a positive size.
//
// The algo is in O(n log n) + O(result_size) which is usually O(n^2).
void ConstructOverlappingSets(bool already_sorted,
std::vector<IndexedInterval>* intervals,
std::vector<std::vector<int>>* result);
// Given n intervals, returns the set of connected components (using the overlap
// relation between 2 intervals). Components are sorted by their start, and
// inside a component, the intervals are also sorted by start.
// `intervals` is only sorted (by start), and not modified otherwise.
void GetOverlappingIntervalComponents(
std::vector<IndexedInterval>* intervals,
std::vector<std::vector<int>>* components);
// Similar to GetOverlappingIntervalComponents(), but returns the indices of
// all intervals whose removal would create one more connected component in the
// interval graph. Those are sorted by start. See:
// https://en.wikipedia.org/wiki/Glossary_of_graph_theory#articulation_point.
std::vector<int> GetIntervalArticulationPoints(
std::vector<IndexedInterval>* intervals);
struct ItemForPairwiseRestriction {
int index;
struct Interval {
IntegerValue start_min;
IntegerValue start_max;
IntegerValue end_min;
IntegerValue end_max;
};
Interval x;
Interval y;
};
struct PairwiseRestriction {
enum class PairwiseRestrictionType {
CONFLICT,
FIRST_BELOW_SECOND,
FIRST_ABOVE_SECOND,
FIRST_LEFT_OF_SECOND,
FIRST_RIGHT_OF_SECOND,
};
int first_index;
int second_index;
PairwiseRestrictionType type;
bool operator==(const PairwiseRestriction& rhs) const {
return first_index == rhs.first_index && second_index == rhs.second_index &&
type == rhs.type;
}
};
// Find pair of items that are either in conflict or could have their range
// shrinked to avoid conflict.
void AppendPairwiseRestrictions(
absl::Span<const ItemForPairwiseRestriction> items,
std::vector<PairwiseRestriction>* result);
// Same as above, but test `items` against `other_items` and append the
// restrictions found to `result`.
void AppendPairwiseRestrictions(
absl::Span<const ItemForPairwiseRestriction> items,
absl::Span<const ItemForPairwiseRestriction> other_items,
std::vector<PairwiseRestriction>* result);
// This class is used by the no_overlap_2d constraint to maintain the envelope
// of a set of rectangles. This envelope is not the convex hull, but the exact
// polyline (aligned with the x and y axis) that contains all the rectangles
// passed with the AddRectangle() call.
class CapacityProfile {
public:
// Simple start of a rectangle. This is used to represent the residual
// capacity profile.
struct Rectangle {
Rectangle(IntegerValue start, IntegerValue height)
: start(start), height(height) {}
bool operator<(const Rectangle& other) const { return start < other.start; }
bool operator==(const Rectangle& other) const {
return start == other.start && height == other.height;
}
IntegerValue start = IntegerValue(0);
IntegerValue height = IntegerValue(0);
};
void Clear();
// Adds a rectangle to the current shape.
void AddRectangle(IntegerValue x_min, IntegerValue x_max, IntegerValue y_min,
IntegerValue y_max);
// Adds a mandatory profile consumption. All mandatory usages will be
// subtracted from the y_max-y_min profile to build the residual capacity.
void AddMandatoryConsumption(IntegerValue x_min, IntegerValue x_max,
IntegerValue y_height);
// Returns the profile of the function:
// capacity(x) = max(y_max of rectangles overlapping x) - min(y_min of
// rectangle overlapping x) - sum(y_height of mandatory rectangles
// overlapping x) where a rectangle overlaps x if x_min <= x < x_max.
//
// Note the profile can contain negative heights in case the mandatory part
// exceeds the range on the y axis.
//
// Note that it adds a sentinel (kMinIntegerValue, 0) at the start. It is
// useful when we reverse the direction on the x axis.
void BuildResidualCapacityProfile(std::vector<Rectangle>* result);
// Returns the exact area of the bounding polyline of all rectangles added.
//
// Note that this will redo the computation each time.
IntegerValue GetBoundingArea();
private:
// Type for the capacity events.
enum EventType { START_RECTANGLE, END_RECTANGLE, CHANGE_MANDATORY_PROFILE };
// Individual events.
struct Event {
IntegerValue time;
IntegerValue y_min;
IntegerValue y_max;
EventType type;
int index;
bool operator<(const Event& other) const { return time < other.time; }
};
// Element of the integer_pq heap.
struct QueueElement {
int Index() const { return index; }
bool operator<(const QueueElement& o) const { return value < o.value; }
int index;
IntegerValue value;
};
static Event StartRectangleEvent(int index, IntegerValue x_min,
IntegerValue y_min, IntegerValue y_max) {
return {x_min, y_min, y_max, START_RECTANGLE, index};
}
static Event EndRectangleEvent(int index, IntegerValue x_max) {
return {x_max, kMinIntegerValue, kMinIntegerValue, END_RECTANGLE, index};
}
static Event ChangeMandatoryProfileEvent(IntegerValue x, IntegerValue delta) {
return {x, /*y_min=*/delta, /*y_max=*/kMinIntegerValue,
CHANGE_MANDATORY_PROFILE, /*index=*/-1};
}
std::vector<Event> events_;
int num_rectangles_added_ = 0;
};
// 1D counterpart of RectangleInRange::GetMinimumIntersectionArea.
// Finds the minimum possible overlap of a interval of size `size` that fits in
// [range_min, range_max] and a second interval [interval_min, interval_max].
IntegerValue Smallest1DIntersection(IntegerValue range_min,
IntegerValue range_max, IntegerValue size,
IntegerValue interval_min,
IntegerValue interval_max);
// A rectangle of size (`x_size`, `y_size`) that can freely move inside the
// `bounding_area` rectangle.
struct RectangleInRange {
int box_index;
Rectangle bounding_area;
IntegerValue x_size;
IntegerValue y_size;
enum Corner {
BOTTOM_LEFT = 0,
TOP_LEFT = 1,
BOTTOM_RIGHT = 2,
TOP_RIGHT = 3,
};
bool operator==(const RectangleInRange& other) const {
return box_index == other.box_index &&
bounding_area == other.bounding_area && x_size == other.x_size &&
y_size == other.y_size;
}
// Returns the position of the rectangle fixed to one of the corner of its
// range.
Rectangle GetAtCorner(Corner p) const {
switch (p) {
case Corner::BOTTOM_LEFT:
return Rectangle{.x_min = bounding_area.x_min,
.x_max = bounding_area.x_min + x_size,
.y_min = bounding_area.y_min,
.y_max = bounding_area.y_min + y_size};
case Corner::TOP_LEFT:
return Rectangle{.x_min = bounding_area.x_min,
.x_max = bounding_area.x_min + x_size,
.y_min = bounding_area.y_max - y_size,
.y_max = bounding_area.y_max};
case Corner::BOTTOM_RIGHT:
return Rectangle{.x_min = bounding_area.x_max - x_size,
.x_max = bounding_area.x_max,
.y_min = bounding_area.y_min,
.y_max = bounding_area.y_min + y_size};
case Corner::TOP_RIGHT:
return Rectangle{.x_min = bounding_area.x_max - x_size,
.x_max = bounding_area.x_max,
.y_min = bounding_area.y_max - y_size,
.y_max = bounding_area.y_max};
}
}
Rectangle GetBoudingBox() const { return bounding_area; }
// Returns an empty rectangle if it is possible for no intersection to happen.
Rectangle GetMinimumIntersection(const Rectangle& containing_area) const {
IntegerValue smallest_area = std::numeric_limits<IntegerValue>::max();
Rectangle best_intersection;
for (int corner_idx = 0; corner_idx < 4; ++corner_idx) {
const Corner p = static_cast<Corner>(corner_idx);
Rectangle intersection = containing_area.Intersect(GetAtCorner(p));
const IntegerValue intersection_area = intersection.Area();
if (intersection_area == 0) {
return Rectangle::GetEmpty();
}
if (intersection_area < smallest_area) {
smallest_area = intersection_area;
best_intersection = std::move(intersection);
}
}
return best_intersection;
}
IntegerValue GetMinimumIntersectionArea(
const Rectangle& containing_area) const {
return Smallest1DIntersection(bounding_area.x_min, bounding_area.x_max,
x_size, containing_area.x_min,
containing_area.x_max) *
Smallest1DIntersection(bounding_area.y_min, bounding_area.y_max,
y_size, containing_area.y_min,
containing_area.y_max);
}
Rectangle GetMandatoryRegion() const {
// Weird math to avoid overflow.
if (bounding_area.SizeX() - x_size >= x_size ||
bounding_area.SizeY() - y_size >= y_size) {
return Rectangle::GetEmpty();
}
return Rectangle{.x_min = bounding_area.x_max - x_size,
.x_max = bounding_area.x_min + x_size,
.y_min = bounding_area.y_max - y_size,
.y_max = bounding_area.y_min + y_size};
}
static RectangleInRange BiggestWithMinIntersection(
const Rectangle& containing_area, const RectangleInRange& original,
const IntegerValue& min_intersect_x,
const IntegerValue& min_intersect_y) {
const IntegerValue x_size = original.x_size;
const IntegerValue y_size = original.y_size;
RectangleInRange result;
result.x_size = x_size;
result.y_size = y_size;
result.box_index = original.box_index;
// We cannot intersect more units than the whole item.
DCHECK_GE(x_size, min_intersect_x);
DCHECK_GE(y_size, min_intersect_y);
// Units that can *not* intersect per dimension.
const IntegerValue x_headroom = x_size - min_intersect_x;
const IntegerValue y_headroom = y_size - min_intersect_y;
result.bounding_area.x_min = containing_area.x_min - x_headroom;
result.bounding_area.x_max = containing_area.x_max + x_headroom;
result.bounding_area.y_min = containing_area.y_min - y_headroom;
result.bounding_area.y_max = containing_area.y_max + y_headroom;
return result;
}
template <typename Sink>
friend void AbslStringify(Sink& sink, const RectangleInRange& r) {
absl::Format(&sink, "item(size=%vx%v, BB=%v)", r.x_size, r.y_size,
r.bounding_area);
}
};
// Cheaply test several increasingly smaller rectangles for energy conflict.
// More precisely, each call to `Shrink()` cost O(k + n) operations, where k is
// the number of points that shrinking the probing rectangle will cross and n is
// the number of items which are in a range that overlaps the probing rectangle
// in both sides in the dimension that is getting shrinked. When calling
// repeatedely `Shrink()` until the probing rectangle collapse into a single
// point, the O(k) component adds up to a O(M) cost, where M is the number of
// items. This means this procedure is linear in time if the ranges of the items
// are small.
//
// The energy is defined as the minimum occupied area inside the probing
// rectangle. For more details, see Clautiaux, François, et al. "A new
// constraint programming approach for the orthogonal packing problem."
// Computers & Operations Research 35.3 (2008): 944-959.
//
// This is used by FindRectanglesWithEnergyConflictMC() below.
class ProbingRectangle {
public:
// It will initialize with the bounding box of the whole set.
explicit ProbingRectangle(const std::vector<RectangleInRange>& intervals);
enum Edge { TOP = 0, LEFT = 1, BOTTOM = 2, RIGHT = 3 };
// Reset to the bounding box of the whole set.
void Reset();
// Shrink the rectangle by moving one of its four edges to the next
// "interesting" value. The interesting values for x or y are the ones that
// correspond to a boundary, ie., a value that corresponds to one of {min,
// min + size, max - size, max} of a rectangle.
void Shrink(Edge edge);
bool CanShrink(Edge edge) const;
bool IsMinimal() const {
// We only need to know if there is slack on both dimensions. Actually
// CanShrink(BOTTOM) == CanShrink(TOP) and conversely.
return !(CanShrink(Edge::BOTTOM) || CanShrink(Edge::LEFT));
}
// Test-only method that check that all internal incremental counts are
// correct by comparing with recalculating them from scratch.
void ValidateInvariants() const;
// How much of GetMinimumEnergy() will change if Shrink() is called.
IntegerValue GetShrinkDeltaEnergy(Edge edge) const {
return cached_delta_energy_[edge];
}
// How much of GetCurrentRectangleArea() will change if Shrink() is called.
IntegerValue GetShrinkDeltaArea(Edge edge) const;
Rectangle GetCurrentRectangle() const;
IntegerValue GetCurrentRectangleArea() const { return probe_area_; }
// This is equivalent of, for every item:
// - Call GetMinimumIntersectionArea() with GetCurrentRectangle().
// - Return the total sum of the areas.
IntegerValue GetMinimumEnergy() const { return minimum_energy_; }
const std::vector<RectangleInRange>& Intervals() const { return intervals_; }
enum Direction {
LEFT_AND_RIGHT = 0,
TOP_AND_BOTTOM = 1,
};
private:
void CacheShrinkDeltaEnergy(int dimension);
template <Edge edge>
void ShrinkImpl();
struct IntervalPoint {
IntegerValue value;
int index;
};
std::vector<IntervalPoint> interval_points_sorted_by_x_;
std::vector<IntervalPoint> interval_points_sorted_by_y_;
// Those two vectors are not strictly needed, we could instead iterate
// directly on the two vectors above, but the code would be much uglier.
struct PointsForCoordinate {
IntegerValue coordinate;
absl::Span<IntervalPoint> items_touching_coordinate;
};
std::vector<PointsForCoordinate> grouped_intervals_sorted_by_x_;
std::vector<PointsForCoordinate> grouped_intervals_sorted_by_y_;
const std::vector<RectangleInRange>& intervals_;
IntegerValue full_energy_;
IntegerValue minimum_energy_;
IntegerValue probe_area_;
int indexes_[4];
int next_indexes_[4];
absl::flat_hash_set<int> ranges_touching_both_boundaries_[2];
IntegerValue corner_count_[4] = {0, 0, 0, 0};
IntegerValue intersect_length_[4] = {0, 0, 0, 0};
IntegerValue cached_delta_energy_[4];
};
// Monte-Carlo inspired heuristic to find a rectangles with an energy conflict:
// - start with a rectangle equals to the full bounding box of the elements;
// - shrink the rectangle by an edge to the next "interesting" value. Choose
// the edge randomly, but biased towards the change that increases the ratio
// area_inside / area_rectangle;
// - collect a result at every conflict or every time the ratio
// used_energy/available_energy is more than `candidate_energy_usage_factor`;
// - stop when the rectangle is empty.
struct FindRectanglesResult {
// Known conflicts: the minimal energy used inside the rectangle is larger
// than the area of the rectangle.
std::vector<Rectangle> conflicts;
// Rectangles without a conflict but having used_energy/available_energy >
// candidate_energy_usage_factor. Those are good candidates for finding
// conflicts using more sophisticated heuristics. Those rectangles are
// ordered so the n-th rectangle is always fully inside the n-1-th one.
std::vector<Rectangle> candidates;
};
FindRectanglesResult FindRectanglesWithEnergyConflictMC(
const std::vector<RectangleInRange>& intervals, absl::BitGenRef random,
double temperature, double candidate_energy_usage_factor);
// Render a packing solution as a Graphviz dot file. Only works in the "neato"
// or "fdp" Graphviz backends.
std::string RenderDot(std::optional<Rectangle> bb,
absl::Span<const Rectangle> solution,
std::string_view extra_dot_payload = "");
// Given a bounding box and a list of rectangles inside that bounding box,
// returns a list of rectangles partitioning the empty area inside the bounding
// box.
std::vector<Rectangle> FindEmptySpaces(
const Rectangle& bounding_box, std::vector<Rectangle> ocupied_rectangles);
// Given two regions, each one of them defined by a vector of non-overlapping
// rectangles paving them, returns a vector of non-overlapping rectangles that
// paves the points that were part of the first region but not of the second.
// This can also be seen as the set difference of the points of the regions.
std::vector<Rectangle> PavedRegionDifference(
std::vector<Rectangle> original_region,
absl::Span<const Rectangle> area_to_remove);
// The two regions must be defined by non-overlapping rectangles.
inline bool RegionIncludesOther(absl::Span<const Rectangle> region,
absl::Span<const Rectangle> other) {
return PavedRegionDifference({other.begin(), other.end()}, region).empty();
}
} // namespace sat
} // namespace operations_research
#endif // OR_TOOLS_SAT_DIFFN_UTIL_H_