Files
ortools-clone/examples/python/cover_rectangle_sat.py

109 lines
3.7 KiB
Python
Raw Normal View History

2022-06-17 08:40:20 +02:00
# Copyright 2010-2022 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.
2018-12-23 21:24:23 +01:00
"""Fill a 72x37 rectangle by a minimum number of non-overlapping squares."""
2018-12-23 18:16:16 +01:00
from ortools.sat.python import cp_model
2018-12-23 21:24:23 +01:00
def cover_rectangle(num_squares):
"""Try to fill the rectangle with a given number of squares."""
2018-12-23 18:16:16 +01:00
size_x = 72
size_y = 37
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):
size = model.NewIntVar(1, size_y, 'size_%i' % i)
2018-12-24 14:25:12 +01:00
start_x = model.NewIntVar(0, size_x, 'sx_%i' % i)
end_x = model.NewIntVar(0, size_x, 'ex_%i' % i)
start_y = model.NewIntVar(0, size_y, 'sy_%i' % i)
end_y = model.NewIntVar(0, size_y, 'ey_%i' % i)
2018-12-23 18:16:16 +01:00
2018-12-24 14:25:12 +01:00
interval_x = model.NewIntervalVar(start_x, size, end_x, 'ix_%i' % i)
interval_y = model.NewIntervalVar(start_y, size, end_y, 'iy_%i' % i)
2018-12-23 18:16:16 +01:00
area = model.NewIntVar(1, size_y * size_y, 'area_%i' % i)
2022-09-20 10:58:11 +02:00
model.AddMultiplicationEquality(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.
2018-12-23 18:16:16 +01:00
model.AddNoOverlap2D(x_intervals, y_intervals)
2018-12-23 21:28:34 +01:00
# Redundant constraints.
2018-12-23 18:16:16 +01:00
model.AddCumulative(x_intervals, sizes, size_y)
model.AddCumulative(y_intervals, sizes, size_x)
2018-12-23 21:28:34 +01:00
# Forces the rectangle to be exactly covered.
2018-12-23 18:16:16 +01:00
model.Add(sum(areas) == size_x * size_y)
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):
model.Add(sizes[i] <= sizes[i + 1])
2018-12-24 14:25:12 +01:00
# Define same to be true iff sizes[i] == sizes[i + 1]
same = model.NewBoolVar('')
model.Add(sizes[i] == sizes[i + 1]).OnlyEnforceIf(same)
model.Add(sizes[i] < sizes[i + 1]).OnlyEnforceIf(same.Not())
# Tie break with starts.
model.Add(x_starts[i] <= x_starts[i + 1]).OnlyEnforceIf(same)
2018-12-23 18:43:35 +01:00
# Symmetry breaking 2: first square in one quadrant.
2018-12-23 23:15:18 +01:00
model.Add(x_starts[0] < 36)
model.Add(y_starts[0] < 19)
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()
status = solver.Solve(model)
2018-12-23 21:31:11 +01:00
print('%s found in %0.2fs' % (solver.StatusName(status), solver.WallTime()))
2018-12-23 18:16:16 +01:00
2018-12-23 21:28:34 +01:00
# Prints solution.
if status == cp_model.OPTIMAL:
2018-12-23 18:16:16 +01:00
display = [[' ' for _ in range(size_x)] for _ in range(size_y)]
for i in range(num_squares):
2018-12-23 23:15:18 +01:00
sol_x = solver.Value(x_starts[i])
sol_y = solver.Value(y_starts[i])
2018-12-23 21:28:34 +01:00
sol_s = solver.Value(sizes[i])
char = format(i, '01x')
for j in range(sol_s):
for k in range(sol_s):
if display[sol_y + j][sol_x + k] != ' ':
2018-12-23 18:16:16 +01:00
print('ERROR between %s and %s' %
2018-12-23 21:28:34 +01:00
(display[sol_y + j][sol_x + k], char))
display[sol_y + j][sol_x + k] = char
2018-12-23 18:16:16 +01:00
for line in range(size_y):
2018-12-23 21:24:23 +01:00
print(' '.join(display[line]))
2018-12-23 18:16:16 +01:00
return status == cp_model.FEASIBLE
2018-12-23 21:24:23 +01:00
for num in range(1, 15):
print('Trying with size =', num)
if cover_rectangle(num):
2018-12-23 18:16:16 +01:00
break