|
1 | | -// Copyright 2010-2025 Google LLC |
2 | | -// Licensed under the Apache License, Version 2.0 (the "License"); |
3 | | -// you may not use this file except in compliance with the License. |
4 | | -// You may obtain a copy of the License at |
5 | | -// |
6 | | -// http://www.apache.org/licenses/LICENSE-2.0 |
7 | | -// |
8 | | -// Unless required by applicable law or agreed to in writing, software |
9 | | -// distributed under the License is distributed on an "AS IS" BASIS, |
10 | | -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
11 | | -// See the License for the specific language governing permissions and |
12 | | -// limitations under the License. |
13 | | - |
14 | | -using System; |
15 | | -using System.Collections.Generic; |
16 | | -using System.Linq; |
17 | | -using Google.OrTools.Sat; |
18 | | - |
19 | | -/// <summary> |
20 | | -/// Fill a 60x50 rectangle exactly using a minimum number of non-overlapping squares.""" |
21 | | -/// </summary> |
22 | | -class CoverRectangleSat |
23 | | -{ |
24 | | - static int sizeX = 60; |
25 | | - static int sizeY = 50; |
26 | | - |
27 | | - static bool CoverRectangle(int numSquares) |
28 | | - { |
29 | | - CpModel model = new CpModel(); |
30 | | - |
31 | | - var areas = new List<IntVar>(); |
32 | | - var sizes = new List<IntVar>(); |
33 | | - var xIntervals = new List<IntervalVar>(); |
34 | | - var yIntervals = new List<IntervalVar>(); |
35 | | - var xStarts = new List<IntVar>(); |
36 | | - var yStarts = new List<IntVar>(); |
37 | | - |
38 | | - // Creates intervals for the NoOverlap2D and size variables. |
39 | | - foreach (var i in Enumerable.Range(0, numSquares)) |
40 | | - { |
41 | | - var size = model.NewIntVar(1, sizeY, String.Format("size_{0}", i)); |
42 | | - var startX = model.NewIntVar(0, sizeX, String.Format("startX_{0}", i)); |
43 | | - var endX = model.NewIntVar(0, sizeX, String.Format("endX_{0}", i)); |
44 | | - var startY = model.NewIntVar(0, sizeY, String.Format("startY_{0}", i)); |
45 | | - var endY = model.NewIntVar(0, sizeY, String.Format("endY_{0}", i)); |
46 | | - |
47 | | - var intervalX = model.NewIntervalVar(startX, size, endX, String.Format("intervalX_{0}", i)); |
48 | | - var intervalY = model.NewIntervalVar(startY, size, endY, String.Format("intervalY_{0}", i)); |
49 | | - |
50 | | - var area = model.NewIntVar(1, sizeY * sizeY, String.Format("area_{0}", i)); |
51 | | - model.AddMultiplicationEquality(area, size, size); |
52 | | - |
53 | | - areas.Add(area); |
54 | | - xIntervals.Add(intervalX); |
55 | | - yIntervals.Add(intervalY); |
56 | | - sizes.Add(size); |
57 | | - xStarts.Add(startX); |
58 | | - yStarts.Add(startY); |
59 | | - } |
60 | | - |
61 | | - // Main constraint. |
62 | | - NoOverlap2dConstraint noOverlap2d = model.AddNoOverlap2D(); |
63 | | - foreach (var i in Enumerable.Range(0, numSquares)) |
64 | | - { |
65 | | - noOverlap2d.AddRectangle(xIntervals[i], yIntervals[i]); |
66 | | - } |
67 | | - |
68 | | - // Redundant constraints. |
69 | | - model.AddCumulative(sizeY).AddDemands(xIntervals, sizes); |
70 | | - model.AddCumulative(sizeX).AddDemands(yIntervals, sizes); |
71 | | - |
72 | | - // Forces the rectangle to be exactly covered. |
73 | | - model.Add(LinearExpr.Sum(areas) == sizeX * sizeY); |
74 | | - |
75 | | - // Symmetry breaking 1: sizes are ordered. |
76 | | - foreach (var i in Enumerable.Range(0, numSquares - 1)) |
77 | | - { |
78 | | - model.Add(sizes[i] <= sizes[i + 1]); |
79 | | - |
80 | | - // Define same to be true iff sizes[i] == sizes[i + 1] |
81 | | - var same = model.NewBoolVar(""); |
82 | | - model.Add(sizes[i] == sizes[i + 1]).OnlyEnforceIf(same); |
83 | | - model.Add(sizes[i] < sizes[i + 1]).OnlyEnforceIf(same.Not()); |
84 | | - |
85 | | - // Tie break with starts. |
86 | | - model.Add(xStarts[i] <= xStarts[i + 1]).OnlyEnforceIf(same); |
87 | | - } |
88 | | - |
89 | | - // Symmetry breaking 2: first square in one quadrant. |
90 | | - model.Add(xStarts[0] < (sizeX + 1) / 2); |
91 | | - model.Add(yStarts[0] < (sizeY + 1) / 2); |
92 | | - |
93 | | - // Creates a solver and solves. |
94 | | - var solver = new CpSolver(); |
95 | | - solver.StringParameters = "num_search_workers:16, log_search_progress: false, max_time_in_seconds:10"; |
96 | | - var status = solver.Solve(model); |
97 | | - Console.WriteLine(string.Format("{0} found in {1:0.00}s", status, solver.WallTime())); |
98 | | - |
99 | | - // Prints solution. |
100 | | - bool solution_found = status == CpSolverStatus.Optimal || status == CpSolverStatus.Feasible; |
101 | | - if (solution_found) |
102 | | - { |
103 | | - char[][] output = new char [sizeY][]; |
104 | | - foreach (var y in Enumerable.Range(0, sizeY)) |
105 | | - { |
106 | | - |
107 | | - output[y] = new char[sizeX]; |
108 | | - foreach (var x in Enumerable.Range(0, sizeX)) |
109 | | - { |
110 | | - output[y][x] = ' '; |
111 | | - } |
112 | | - } |
113 | | - |
114 | | - foreach (var s in Enumerable.Range(0, numSquares)) |
115 | | - { |
116 | | - int startX = (int)solver.Value(xStarts[s]); |
117 | | - int startY = (int)solver.Value(yStarts[s]); |
118 | | - int size = (int)solver.Value(sizes[s]); |
119 | | - char c = (char)(65 + s); |
120 | | - foreach (var x in Enumerable.Range(startX, size)) |
121 | | - { |
122 | | - foreach (var y in Enumerable.Range(startY, size)) |
123 | | - { |
124 | | - if (output[y][x] != ' ') |
125 | | - { |
126 | | - Console.WriteLine( |
127 | | - string.Format("Error at position x={0} y{1}, found {2}", x, y, output[y][x])); |
128 | | - } |
129 | | - output[y][x] = c; |
130 | | - } |
131 | | - } |
132 | | - } |
133 | | - foreach (var y in Enumerable.Range(0, sizeY)) |
134 | | - { |
135 | | - Console.WriteLine(new String(output[y], 0, sizeX)); |
136 | | - } |
137 | | - } |
138 | | - return solution_found; |
139 | | - } |
140 | | - |
141 | | - static void Main() |
142 | | - { |
143 | | - foreach (int numSquares in Enumerable.Range(1, 15)) |
144 | | - { |
145 | | - Console.WriteLine("Trying with size = {0}", numSquares); |
146 | | - if (CoverRectangle(numSquares)) |
147 | | - break; |
148 | | - } |
149 | | - } |
150 | | -} |
| 1 | +// Copyright 2010-2025 Google LLC |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +using System; |
| 15 | +using System.Collections.Generic; |
| 16 | +using System.Linq; |
| 17 | +using Google.OrTools.Sat; |
| 18 | + |
| 19 | +/// <summary> |
| 20 | +/// Fill a 60x50 rectangle exactly using a minimum number of non-overlapping squares.""" |
| 21 | +/// </summary> |
| 22 | +class CoverRectangleSat |
| 23 | +{ |
| 24 | + static int sizeX = 60; |
| 25 | + static int sizeY = 50; |
| 26 | + |
| 27 | + static bool CoverRectangle(int numSquares) |
| 28 | + { |
| 29 | + CpModel model = new CpModel(); |
| 30 | + |
| 31 | + var areas = new List<IntVar>(); |
| 32 | + var sizes = new List<IntVar>(); |
| 33 | + var xIntervals = new List<IntervalVar>(); |
| 34 | + var yIntervals = new List<IntervalVar>(); |
| 35 | + var xStarts = new List<IntVar>(); |
| 36 | + var yStarts = new List<IntVar>(); |
| 37 | + |
| 38 | + // Creates intervals for the NoOverlap2D and size variables. |
| 39 | + foreach (var i in Enumerable.Range(0, numSquares)) |
| 40 | + { |
| 41 | + var size = model.NewIntVar(1, sizeY, String.Format("size_{0}", i)); |
| 42 | + var startX = model.NewIntVar(0, sizeX, String.Format("startX_{0}", i)); |
| 43 | + var endX = model.NewIntVar(0, sizeX, String.Format("endX_{0}", i)); |
| 44 | + var startY = model.NewIntVar(0, sizeY, String.Format("startY_{0}", i)); |
| 45 | + var endY = model.NewIntVar(0, sizeY, String.Format("endY_{0}", i)); |
| 46 | + |
| 47 | + var intervalX = model.NewIntervalVar(startX, size, endX, String.Format("intervalX_{0}", i)); |
| 48 | + var intervalY = model.NewIntervalVar(startY, size, endY, String.Format("intervalY_{0}", i)); |
| 49 | + |
| 50 | + var area = model.NewIntVar(1, sizeY * sizeY, String.Format("area_{0}", i)); |
| 51 | + model.AddMultiplicationEquality(area, size, size); |
| 52 | + |
| 53 | + areas.Add(area); |
| 54 | + xIntervals.Add(intervalX); |
| 55 | + yIntervals.Add(intervalY); |
| 56 | + sizes.Add(size); |
| 57 | + xStarts.Add(startX); |
| 58 | + yStarts.Add(startY); |
| 59 | + } |
| 60 | + |
| 61 | + // Main constraint. |
| 62 | + NoOverlap2dConstraint noOverlap2d = model.AddNoOverlap2D(); |
| 63 | + foreach (var i in Enumerable.Range(0, numSquares)) |
| 64 | + { |
| 65 | + noOverlap2d.AddRectangle(xIntervals[i], yIntervals[i]); |
| 66 | + } |
| 67 | + |
| 68 | + // Redundant constraints. |
| 69 | + model.AddCumulative(sizeY).AddDemands(xIntervals, sizes); |
| 70 | + model.AddCumulative(sizeX).AddDemands(yIntervals, sizes); |
| 71 | + |
| 72 | + // Forces the rectangle to be exactly covered. |
| 73 | + model.Add(LinearExpr.Sum(areas) == sizeX * sizeY); |
| 74 | + |
| 75 | + // Symmetry breaking 1: sizes are ordered. |
| 76 | + foreach (var i in Enumerable.Range(0, numSquares - 1)) |
| 77 | + { |
| 78 | + model.Add(sizes[i] <= sizes[i + 1]); |
| 79 | + |
| 80 | + // Define same to be true iff sizes[i] == sizes[i + 1] |
| 81 | + var same = model.NewBoolVar(""); |
| 82 | + model.Add(sizes[i] == sizes[i + 1]).OnlyEnforceIf(same); |
| 83 | + model.Add(sizes[i] < sizes[i + 1]).OnlyEnforceIf(same.Not()); |
| 84 | + |
| 85 | + // Tie break with starts. |
| 86 | + model.Add(xStarts[i] <= xStarts[i + 1]).OnlyEnforceIf(same); |
| 87 | + } |
| 88 | + |
| 89 | + // Symmetry breaking 2: first square in one quadrant. |
| 90 | + model.Add(xStarts[0] < (sizeX + 1) / 2); |
| 91 | + model.Add(yStarts[0] < (sizeY + 1) / 2); |
| 92 | + |
| 93 | + // Creates a solver and solves. |
| 94 | + var solver = new CpSolver(); |
| 95 | + solver.StringParameters = "num_search_workers:16, log_search_progress: false, max_time_in_seconds:10"; |
| 96 | + var status = solver.Solve(model); |
| 97 | + Console.WriteLine(string.Format("{0} found in {1:0.00}s", status, solver.WallTime())); |
| 98 | + |
| 99 | + // Prints solution. |
| 100 | + bool solution_found = status == CpSolverStatus.Optimal || status == CpSolverStatus.Feasible; |
| 101 | + if (solution_found) |
| 102 | + { |
| 103 | + char[][] output = new char [sizeY][]; |
| 104 | + foreach (var y in Enumerable.Range(0, sizeY)) |
| 105 | + { |
| 106 | + |
| 107 | + output[y] = new char[sizeX]; |
| 108 | + foreach (var x in Enumerable.Range(0, sizeX)) |
| 109 | + { |
| 110 | + output[y][x] = ' '; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + foreach (var s in Enumerable.Range(0, numSquares)) |
| 115 | + { |
| 116 | + int startX = (int)solver.Value(xStarts[s]); |
| 117 | + int startY = (int)solver.Value(yStarts[s]); |
| 118 | + int size = (int)solver.Value(sizes[s]); |
| 119 | + char c = (char)(65 + s); |
| 120 | + foreach (var x in Enumerable.Range(startX, size)) |
| 121 | + { |
| 122 | + foreach (var y in Enumerable.Range(startY, size)) |
| 123 | + { |
| 124 | + if (output[y][x] != ' ') |
| 125 | + { |
| 126 | + Console.WriteLine( |
| 127 | + string.Format("Error at position x={0} y{1}, found {2}", x, y, output[y][x])); |
| 128 | + } |
| 129 | + output[y][x] = c; |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + foreach (var y in Enumerable.Range(0, sizeY)) |
| 134 | + { |
| 135 | + Console.WriteLine(new String(output[y], 0, sizeX)); |
| 136 | + } |
| 137 | + } |
| 138 | + return solution_found; |
| 139 | + } |
| 140 | + |
| 141 | + static void Main() |
| 142 | + { |
| 143 | + foreach (int numSquares in Enumerable.Range(1, 15)) |
| 144 | + { |
| 145 | + Console.WriteLine("Trying with size = {0}", numSquares); |
| 146 | + if (CoverRectangle(numSquares)) |
| 147 | + break; |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments