SIGN IN SIGN UP
google / or-tools UNCLAIMED

Google's Operations Research tools:

0 0 1 C++
#!/usr/bin/env python3
2025-01-10 11:35:44 +01:00
# Copyright 2010-2025 Google LLC
2018-12-23 18:16:16 +01:00
# 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.
2023-07-01 06:06:53 +02:00
2022-10-23 03:52:19 +01:00
"""Fill a 60x50 rectangle by a minimum number of non-overlapping squares."""
2018-12-23 18:16:16 +01:00
from typing import Sequence
from absl import app
2018-12-23 18:16:16 +01:00
from ortools.sat.python import cp_model
2023-11-16 19:46:56 +01:00
def cover_rectangle(num_squares: int) -> bool:
2018-12-23 21:24:23 +01:00
"""Try to fill the rectangle with a given number of squares."""
2022-10-23 03:52:19 +01:00
size_x = 60
size_y = 50
2018-12-23 18:16:16 +01:00
model = cp_model.CpModel()
areas = []
sizes = []
x_intervals = []
y_intervals = []
2018-12-23 23:15:18 +01:00
x_starts = []
y_starts = []
2018-12-23 18:16:16 +01:00
2018-12-23 21:28:34 +01:00
# Creates intervals for the NoOverlap2D and size variables.
2018-12-23 18:16:16 +01:00
for i in range(num_squares):
2023-11-16 19:46:56 +01:00
size = model.new_int_var(1, size_y, "size_%i" % i)
start_x = model.new_int_var(0, size_x, "sx_%i" % i)
end_x = model.new_int_var(0, size_x, "ex_%i" % i)
start_y = model.new_int_var(0, size_y, "sy_%i" % i)
end_y = model.new_int_var(0, size_y, "ey_%i" % i)
2018-12-23 18:16:16 +01:00
2023-11-16 19:46:56 +01:00
interval_x = model.new_interval_var(start_x, size, end_x, "ix_%i" % i)
interval_y = model.new_interval_var(start_y, size, end_y, "iy_%i" % i)
2018-12-23 18:16:16 +01:00
2023-11-16 19:46:56 +01:00
area = model.new_int_var(1, size_y * size_y, "area_%i" % i)
model.add_multiplication_equality(area, [size, size])
2018-12-23 18:16:16 +01:00
areas.append(area)
2018-12-23 21:24:23 +01:00
x_intervals.append(interval_x)
y_intervals.append(interval_y)
2018-12-23 18:16:16 +01:00
sizes.append(size)
2018-12-24 14:25:12 +01:00
x_starts.append(start_x)
y_starts.append(start_y)
2018-12-23 18:16:16 +01:00
2018-12-23 21:28:34 +01:00
# Main constraint.
2023-11-16 19:46:56 +01:00
model.add_no_overlap_2d(x_intervals, y_intervals)
2018-12-23 21:28:34 +01:00
# Redundant constraints.
2023-11-16 19:46:56 +01:00
model.add_cumulative(x_intervals, sizes, size_y)
model.add_cumulative(y_intervals, sizes, size_x)
2018-12-23 18:16:16 +01:00
2018-12-23 21:28:34 +01:00
# Forces the rectangle to be exactly covered.
2023-11-16 19:46:56 +01:00
model.add(sum(areas) == size_x * size_y)
2018-12-23 18:16:16 +01:00
2018-12-23 23:15:18 +01:00
# Symmetry breaking 1: sizes are ordered.
2018-12-23 18:16:16 +01:00
for i in range(num_squares - 1):
2023-11-16 19:46:56 +01:00
model.add(sizes[i] <= sizes[i + 1])
2018-12-23 18:16:16 +01:00
2018-12-24 14:25:12 +01:00
# Define same to be true iff sizes[i] == sizes[i + 1]
2023-11-16 19:46:56 +01:00
same = model.new_bool_var("")
model.add(sizes[i] == sizes[i + 1]).only_enforce_if(same)
model.add(sizes[i] < sizes[i + 1]).only_enforce_if(~same)
2018-12-24 14:25:12 +01:00
# Tie break with starts.
2023-11-16 19:46:56 +01:00
model.add(x_starts[i] <= x_starts[i + 1]).only_enforce_if(same)
2018-12-24 14:25:12 +01:00
2018-12-23 18:43:35 +01:00
# Symmetry breaking 2: first square in one quadrant.
2023-11-16 19:46:56 +01:00
model.add(x_starts[0] < (size_x + 1) // 2)
model.add(y_starts[0] < (size_y + 1) // 2)
2018-12-23 18:43:35 +01:00
2018-12-23 18:16:16 +01:00
# Creates a solver and solves.
solver = cp_model.CpSolver()
2023-11-16 19:46:56 +01:00
solver.parameters.num_workers = 8
solver.parameters.max_time_in_seconds = 10.0
2023-11-16 19:46:56 +01:00
status = solver.solve(model)
print("%s found in %0.2fs" % (solver.status_name(status), solver.wall_time))
2018-12-23 18:16:16 +01:00
2018-12-23 21:28:34 +01:00
# Prints solution.
2023-07-18 13:55:36 +02:00
solution_found = status == cp_model.OPTIMAL or status == cp_model.FEASIBLE
if solution_found:
2023-07-01 06:06:53 +02:00
display = [[" " for _ in range(size_x)] for _ in range(size_y)]
2018-12-23 18:16:16 +01:00
for i in range(num_squares):
2023-11-16 19:46:56 +01:00
sol_x = solver.value(x_starts[i])
sol_y = solver.value(y_starts[i])
sol_s = solver.value(sizes[i])
2023-07-01 06:06:53 +02:00
char = format(i, "01x")
2018-12-23 21:28:34 +01:00
for j in range(sol_s):
for k in range(sol_s):
2023-07-01 06:06:53 +02:00
if display[sol_y + j][sol_x + k] != " ":
print(
"ERROR between %s and %s"
% (display[sol_y + j][sol_x + k], char)
)
2018-12-23 21:28:34 +01:00
display[sol_y + j][sol_x + k] = char
2018-12-23 18:16:16 +01:00
for line in range(size_y):
2023-07-01 06:06:53 +02:00
print(" ".join(display[line]))
2023-07-18 13:55:36 +02:00
return solution_found
2018-12-23 18:16:16 +01:00
def main(argv: Sequence[str]) -> None:
if len(argv) > 1:
2023-07-01 06:06:53 +02:00
raise app.UsageError("Too many command-line arguments.")
for num_squares in range(1, 15):
2023-07-01 06:06:53 +02:00
print("Trying with size =", num_squares)
if cover_rectangle(num_squares):
break
2023-07-01 06:06:53 +02:00
if __name__ == "__main__":
app.run(main)