-
Notifications
You must be signed in to change notification settings - Fork 0
/
solve.py
266 lines (203 loc) · 7.68 KB
/
solve.py
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
import argparse
import random
import time
from typing import List, Optional, Tuple
import yaml
import matplotlib.pyplot as plt
from grid import (
allowed_xs_list,
allowed_ys_lists,
BLOCKED,
Grid,
)
from pieces import get_piece, NUM_PIECES, Piece, rot_list
def solve_recursive(
grid: Grid,
pieces: List[Piece],
index: int = 0,
check_at: int = 3,
) -> bool:
"""
Recursive function to solve a problem.
The idea is that, once a piece is positioned, the problem becomes an
easier problem, with one piece left and a different starting grid.
This function, given a grid state (with possibly some piece already
positioned) and a piece (as an index in a list of pieces), tries to find a
valid position for the piece. Once this is found, it recusively call itself.
In case the problem is solved, the grid will contain the configuration of
pieces (i.e. each cell will contain the index of the piece occupying it),
while `pieces` will contain the concrete pieces that solve the problem.
Args:
grid (Grid): Problem grid.
pieces (list): List of pieces for the problem.
index (int): Index (in the list) of the current piece.
check_at (int): Index from which checking if the grid is solvable
See grid.is_impossible() for details.
Returns:
True if the problem can be solved. False otherwise.
"""
if index == len(pieces):
# No more pieces to position => Solved!
return True
if index >= check_at and grid.is_impossible():
return False
for rot in rot_list:
for x in allowed_xs_list:
for y in allowed_ys_lists[x-1]:
piece = pieces[index].make_new(x, y, rot)
if grid.add_piece(piece):
if solve_recursive(grid, pieces, index + 1, check_at):
pieces[index] = piece
return True
grid.remove_piece(piece)
return False
# === Iterative solver ===
# Initial tests didn't show much advantage in avoiding recursion.
# Hence, this might not work now.
def config_gen(piece):
for rot in rot_list:
for x in allowed_xs_list:
for y in allowed_ys_lists[x-1]:
yield piece.make_new(x, y, rot)
def search_piece_position(grid, generator):
for piece in generator:
if grid.add_piece(piece):
return piece, generator
return None
def solve_iter(grid, pieces, check_at=5) -> bool:
generators = [config_gen(piece) for piece in pieces]
idx = 0
while idx < len(pieces):
piece, gen = pieces[idx], generators[idx]
res = search_piece_position(grid, gen)
if res is not None:
# If a position is found
if idx >= check_at and grid.check_isolated():
grid.remove_piece(res[0])
continue
pieces[idx] = res[0]
generators[idx] = res[1]
idx += 1
else:
# If no position is found
if idx == 0:
return False
generators[idx] = config_gen(piece)
idx -= 1
grid.remove_piece(pieces[idx])
return True
# === Iterative solver ===
def prepare_problem(filename: str) -> Tuple[Grid, List[Piece]]:
"""
Loads a problem from a YAML configuration file.
The config file should contain the following entries:
- 'blocked_grid_cells': This should be a list of (x, y) couples
corresponding to the x and y coordinates of the blocked grid cells.
- 'excluded_pieces': This should be a list of indexes corresponding to the
indexes of the pieces that should be excluded when solving the problem.
Can be empty.
Args:
filename (str): Configuration file name (yaml).
Returns:
Grid, List: Starting grid and list of available pieces.
"""
with open(filename, "r") as fp:
problem_conf = yaml.safe_load(fp)
grid = Grid()
for x, y in problem_conf["blocked_grid_cells"]:
grid.grid[y, x] = BLOCKED
assert not grid.is_impossible()
pieces = [
get_piece(i)
for i in range(1, NUM_PIECES+1)
if i not in problem_conf.get("excluded_pieces", ())
]
return grid, pieces
def save_solution_to_config(pieces: List[Piece], filename: str):
"""
Save a set of pieces as solution in the configuration file. If a solution
already exists in the config file, this does nothing.
Args:
pieces (list): List of pieces (supposedly solving the problem).
filename (str): Problem configuration file (yaml).
"""
with open(filename, "r") as fp:
problem_conf = yaml.safe_load(fp)
if not "solution" in problem_conf:
solution = {
piece.index: {
"base_x": piece.base_x,
"base_y": piece.base_y,
"rotation": piece.rotation,
}
for piece in pieces
}
with open(filename, "a") as fp:
yaml.safe_dump({"solution": solution}, fp)
def solve(
filename: str,
seed: Optional[int] = None,
check_at: int = 3,
save_solution: bool = True,
use_iterative: bool = False,
):
"""
Solves a problem.
The problem is loaded from a configuration file containing the initial
grid configuration (in terms of blocked cells) and the available pieces.
Args:
filename (str): Problem configuration file (yaml).
seed (int, optional): Seed for the random number generator. This
influences the order of the pieces. Default: None.
check_at (int): Number of pieces placed after which starting to check
if the grid is solvable. Default: 3.
save_solution (bool): Whether to save the solution in the input config
file (if not already present). Default: True.
use_iterative (bool): Ignored.
"""
grid, pieces = prepare_problem(filename)
random.seed(seed)
random.shuffle(pieces)
# solver = solve_iter if use_iterative else solve_recursive
solver = solve_recursive
print("Starting to solve problem...")
start = time.time()
solved = solver(grid, pieces, check_at=check_at)
end = time.time()
print(f"Ended in: {end - start:.2f} seconds")
if not solved:
print("The problem could not be solved! :'(")
else:
print("Problem solved! :D")
if solved and save_solution:
save_solution_to_config(pieces, filename)
fig, ax = plt.subplots(1, 1, figsize=(6, 6))
grid.draw(ax=ax)
ax.set(xlim=(2, 23), ylim=(-3, 18))
ax.set_aspect("equal")
plt.axis("off")
plt.tight_layout()
plt.show()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Solves a problem.")
parser.add_argument(
"config_file", help="Problem configuration file (YAML)"
)
parser.add_argument(
"--seed", type=int, default=None, help="Seed for the piece shuffle"
)
parser.add_argument(
"--check-at", type=int, default=3,
help="Index from which checking if the current grid is solvable",
)
parser.add_argument(
"--no-save-solution", action="store_false", dest="save_solution",
help="Do not save the solution in the input config file"
)
args = parser.parse_args()
solve(
filename=args.config_file,
seed=args.seed,
check_at=args.check_at,
save_solution=args.save_solution,
)