34 int num_rectangles, absl::BitGenRef random) {
35 static constexpr int kSizeMax = 1'000'000;
36 std::vector<Rectangle> rectangles;
37 rectangles.reserve(num_rectangles);
39 {.x_min = 0, .x_max = kSizeMax, .y_min = 0, .y_max = kSizeMax});
40 while (rectangles.size() < num_rectangles) {
41 std::swap(rectangles.back(),
42 rectangles[absl::Uniform(random, 0ull, rectangles.size() - 1)]);
44 if (absl::Bernoulli(random, 0.5)) {
45 const IntegerValue cut = IntegerValue(
46 absl::Uniform(random, rec.
x_min.value(), rec.
x_max.value()));
51 const Rectangle new_range2 = {.x_min = cut,
55 if (new_range.
Area() == 0 || new_range2.
Area() == 0) {
58 rectangles.pop_back();
59 rectangles.push_back(new_range);
60 rectangles.push_back(new_range2);
62 const IntegerValue cut = IntegerValue(
63 absl::Uniform(random, rec.
y_min.value(), rec.
y_max.value()));
72 if (new_range.
Area() == 0 || new_range2.
Area() == 0) {
75 rectangles.pop_back();
76 rectangles.push_back(new_range);
77 rectangles.push_back(new_range2);
84 std::pair<IntegerValue, IntegerValue> bb,
int average_num_boxes,
85 absl::BitGenRef random) {
86 const double p = 0.01;
87 std::vector<Rectangle> rectangles;
90 std::sqrt(bb.first.value() * bb.second.value() / average_num_boxes);
91 const int64_t n_x =
static_cast<int64_t
>(average_size / p);
92 const int64_t n_y =
static_cast<int64_t
>(average_size / p);
93 while (num_retries < 4) {
96 std::pair<IntegerValue, IntegerValue> sizes;
98 sizes.first = std::binomial_distribution<>(n_x, p)(random);
99 }
while (sizes.first == 0 || sizes.first > bb.first);
101 sizes.second = std::binomial_distribution<>(n_y, p)(random);
102 }
while (sizes.second == 0 || sizes.second > bb.second);
104 std::vector<IntegerValue> possible_x_positions = {0};
105 std::vector<IntegerValue> possible_y_positions = {0};
106 for (
const Rectangle& rec : rectangles) {
107 possible_x_positions.push_back(rec.x_max);
108 possible_y_positions.push_back(rec.y_max);
110 std::sort(possible_x_positions.begin(), possible_x_positions.end());
111 std::sort(possible_y_positions.begin(), possible_y_positions.end());
112 bool found_position =
false;
113 for (
const IntegerValue x : possible_x_positions) {
114 for (
const IntegerValue y : possible_y_positions) {
115 if (x + sizes.first > bb.first || y + sizes.second > bb.second) {
119 .x_max = x + sizes.first,
121 .y_max = y + sizes.second};
122 bool conflict =
false;
124 if (!r.IsDisjoint(rec)) {
132 rectangles.push_back(rec);
133 found_position =
true;
137 if (found_position) {
141 if (found_position) {
149 absl::Span<const Rectangle> rectangles,
double slack_factor,
150 absl::BitGenRef random) {
151 IntegerValue size_max_x = 0;
152 IntegerValue size_max_y = 0;
153 for (
const Rectangle& rec : rectangles) {
154 size_max_x = std::max(size_max_x, rec.SizeX());
155 size_max_y = std::max(size_max_y, rec.SizeY());
157 std::vector<RectangleInRange> ranges;
158 ranges.reserve(rectangles.size());
159 const int max_slack_x = slack_factor * size_max_x.value();
160 const int max_slack_y = slack_factor * size_max_y.value();
162 for (
const Rectangle& rec : rectangles) {
164 range.
x_size = rec.x_max - rec.x_min;
165 range.
y_size = rec.y_max - rec.y_min;
169 rec.x_min - IntegerValue(absl::Uniform(random, 0, max_slack_x)),
171 rec.x_max + IntegerValue(absl::Uniform(random, 0, max_slack_x)),
173 rec.y_min - IntegerValue(absl::Uniform(random, 0, max_slack_y)),
175 rec.y_max + IntegerValue(absl::Uniform(random, 0, max_slack_y))};
176 ranges.push_back(range);
182 absl::Span<const Rectangle> rectangles,
double slack_factor,
183 absl::BitGenRef random) {
184 const std::vector<RectangleInRange> range_items =
186 std::vector<ItemWithVariableSize> items;
187 items.reserve(rectangles.size());
188 for (
int i = 0;
i < range_items.size(); ++
i) {
190 items.push_back({.index =
i,
206 absl::BitGenRef random) {
207 const std::vector<Rectangle> rectangles =
209 std::vector<ItemWithVariableSize> items =
215 std::vector<PairwiseRestriction> results;
218 CHECK(restriction.type !=
221 for (
int index : {restriction.first_index, restriction.second_index}) {
222 const auto& rec = rectangles[index];
223 items[index] = {.index = index,
224 .x = {.start_min = rec.x_min,
225 .start_max = rec.x_min,
226 .end_min = rec.x_max,
227 .end_max = rec.x_max},
228 .y = {.start_min = rec.y_min,
229 .start_max = rec.y_min,
230 .end_min = rec.y_max,
231 .end_max = rec.y_max}};
234 done = results.empty();