This repository has been archived by the owner on Dec 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathold.py
529 lines (399 loc) · 14.3 KB
/
old.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
from minesweeper._global import *
import pysat.solvers
import copy
import heapq
from enum import Enum
def combinations(iterable, r):
"Python implementation of itertools.combinations"
# combinations('ABCD', 2) --> AB AC AD BC BD CD
# combinations(range(4), 3) --> 012 013 023 123
pool = tuple(iterable)
n = len(pool)
if r > n:
return
indices = list(range(r))
yield tuple(pool[i] for i in indices)
while True:
for i in reversed(range(r)):
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range(i + 1, r):
indices[j] = indices[j - 1] + 1
yield tuple(pool[i] for i in indices)
def construct_CNF_clauses(field):
height = len(field)
width = len(field[0])
clauses = []
vars_ = set()
# Add CNF clauses for each opened cell.
for i in range(height):
for j in range(width):
# We only care about "opened" cells, since only them can provide information
# to create clauses.
# We also ignore "opened cell" that is 0, because in the minesweeper game,
# every cell around it has already been opened by default.
if field[i][j] == 0 or field[i][j] == FLAGGED_VAL or field[i][j] == UNOPENED_VAL:
continue
surrounded_mines = field[i][j]
neighbors = []
for y in range(max(0, i - 1), min(height, i + 2)):
for x in range(max(0, j - 1), min(width, j + 2)):
if y == i and x == j:
continue
if field[y][x] == FLAGGED_VAL:
surrounded_mines -= 1
continue
if field[y][x] != UNOPENED_VAL:
continue
# Convert 2D coordinates to 1D (starting from 1)
pos = y * width + x + 1
neighbors.append(pos)
vars_.add(pos)
# Encode "at most" constraint as CNF clauses
for c in combinations(neighbors, surrounded_mines + 1):
clauses.append([-x for x in c])
# Encode "at least" constraint as CNF clauses
for c in combinations(neighbors, len(neighbors) - surrounded_mines + 1):
clauses.append([x for x in c])
return clauses, vars_
def pysat_solve(field):
height = len(field)
width = len(field[0])
clauses, vars_ = construct_CNF_clauses(field)
solver = pysat.solvers.Solver(bootstrap_with=clauses)
# Check if the grid is valid (solvable) or not (satisfiable check)
if not solver.solve():
raise ValueError("Unsolvable grid")
# For every variable appears in the KB, we use resolution refutation to check if that
# variable is true, which means the corresponding cell contains a mine.
flagged_field = copy.deepcopy(field)
for var in vars_:
if not solver.solve(assumptions=[-var]):
flagged_field[(var - 1) // width][(var - 1) % width] = FLAGGED_VAL
solver.delete()
return flagged_field
class KB:
def __init__(self, from_clauses: list[list[int]] = None, vars_: set[int] = None):
self.clauses: list[list[int]] = []
self.vars: set[int] = set()
if from_clauses is not None:
self.clauses = from_clauses
if vars_ is not None:
self.vars = vars_
def add_clause(self, clause: list[int]):
self.clauses.append(clause)
self.vars.update(map(abs, clause))
def add_clauses(self, clauses: list[list[int]]):
self.clauses.extend(clauses)
for clause in clauses:
self.vars.update(map(abs, clause))
def is_satisfied(self, model: dict[int, bool]) -> bool:
"""Check if a model (can be partial) satisfies all clauses
Args:
model (dict[int, bool]): The model to check
Returns:
bool: True if the model satisfies all clauses, False otherwise
"""
for clause in self.clauses:
correct = False
for var in clause:
if abs(var) in model:
if var > 0:
if model[abs(var)]:
correct = True
break
else:
if not model[abs(var)]:
correct = True
break
if not correct:
return False
return True
def is_satisfied_t(self, model: dict[int, bool]) -> Answer:
"""Check if a model (can be partial) satisfies all clauses
Args:
model (dict[int, bool]): The model to check
Returns:
bool: True if the model satisfies all clauses, False otherwise
"""
for clause in self.clauses:
correct = False
has_unassigned = False
for var in clause:
if abs(var) in model:
if var > 0:
if model[abs(var)]:
correct = True
break
else:
if not model[abs(var)]:
correct = True
break
else:
has_unassigned = True
if not correct:
if has_unassigned:
return Answer.UNKNOWN
else:
return Answer.FALSE
return Answer.TRUE
class KB_l:
def __init__(self, from_clauses: list[list[int]] = None, vars_: list[int] = None):
self.clauses: list[list[int]] = []
self.vars: list[int] = []
if from_clauses is not None:
self.clauses = from_clauses
if vars_ is not None:
self.vars = vars_
def add_clause(self, clause: list[int]):
self.clauses.append(clause)
self.vars.update(map(abs, clause))
def add_clauses(self, clauses: list[list[int]]):
self.clauses.extend(clauses)
for clause in clauses:
self.vars.update(map(abs, clause))
def is_satisfied(self, model: dict[int, bool]) -> bool:
"""Check if a model (can be partial) satisfies all clauses
Args:
model (dict[int, bool]): The model to check
Returns:
bool: True if the model satisfies all clauses, False otherwise
"""
for clause in self.clauses:
correct = False
for var in clause:
if abs(var) in model:
if var > 0:
if model[abs(var)]:
correct = True
break
else:
if not model[abs(var)]:
correct = True
break
if not correct:
return False
return True
def is_satisfied_t(self, model: dict[int, bool]) -> Answer:
"""Check if a model (can be partial) satisfies all clauses
Args:
model (dict[int, bool]): The model to check
Returns:
bool: True if the model satisfies all clauses, False otherwise
"""
for clause in self.clauses:
correct = False
has_unassigned = False
for var in clause:
if abs(var) in model:
if var > 0:
if model[abs(var)]:
correct = True
break
else:
if not model[abs(var)]:
correct = True
break
else:
has_unassigned = True
if not correct:
if has_unassigned:
return Answer.UNKNOWN
else:
return Answer.FALSE
return Answer.TRUE
def undetermined_clauses(kb: KB, model: dict[int, bool]) -> int | None:
"""Get the number of undetermined clauses when applying in a model.
Args:
cnf (CNF): clauses
model (dict[int, bool]): model to check
Returns:
int | None: return the number unless the model does not satisfy KB
"""
if kb.is_satisfied(model):
return 0
else:
return 1
count = 0
for clause in kb.clauses:
correct = False
has_unknown_var = False
for var in clause:
if abs(var) in model:
if var > 0:
if model[abs(var)]:
correct = True
break
else:
if not model[abs(var)]:
correct = True
break
else:
has_unknown_var = True
if correct:
continue
elif has_unknown_var:
count += 1
else:
return None
return count
def child_models(cnf: KB, parent_model: dict[int, bool]):
for var in cnf.vars:
if var in parent_model:
continue
for val in (True, False):
child_model = copy.deepcopy(parent_model)
child_model[var] = val
yield child_model
class hashable_dict(dict):
def __hash__(self):
return hash(tuple(sorted(self.items())))
class Node:
__slots__ = ["model", "h"]
def __init__(self, model: hashable_dict[int, bool], h: int):
self.model = model
self.h = h
def __lt__(self, other):
return self.h < other.h
def a_star_search(kb: KB, init_model: hashable_dict[int, bool]) -> bool:
h = undetermined_clauses(kb, init_model)
# if h is None:
# return False
node = Node(copy.deepcopy(init_model), h)
frontier = []
heapq.heappush(frontier, node)
explored = set()
while True:
if not frontier:
return False
node = heapq.heappop(frontier)
if node.model in explored:
continue
# if kb.is_satisfied(node.model):
# return True
if node.h == 0:
return True
explored.add(node.model)
for child_model in child_models(kb, node.model):
if child_model in explored:
continue
h = undetermined_clauses(kb, child_model)
if h is None:
continue
heapq.heappush(frontier, Node(child_model, h))
def a_star_solve(field):
clauses, vars_ = construct_CNF_clauses(field)
kb = KB(clauses, vars_)
# if not a_star_search(kb, hashable_dict()):
# raise ValueError("Unsolvable grid")
# print("ok")
height = len(field)
width = len(field[0])
flagged_field = copy.deepcopy(field)
for var in vars_:
if not a_star_search(kb, hashable_dict({var: False})):
flagged_field[(var - 1) // width][(var - 1) % width] = FLAGGED_VAL
return flagged_field
def brute_force_solve(field):
clauses, vars_ = construct_CNF_clauses(field)
kb = KB(clauses, vars_)
# for true_vars in combinations(vars_, len(vars_)):
# model = {var: True if var in true_vars else False for var in vars_}
# if kb.is_satisfied(model):
# break
height = len(field)
width = len(field[0])
print(clauses, vars_)
flagged_field = copy.deepcopy(field)
for var in vars_:
new_vars = [x for x in vars_ if x != var]
to_flag = True
for i in range(1, len(vars_)):
for true_vars in combinations(new_vars, i):
model = {var: True if var in true_vars else False for var in new_vars}
model[var] = False
if kb.is_satisfied(model):
to_flag = False
break
if not to_flag:
break
if to_flag:
flagged_field[(var - 1) // width][(var - 1) % width] = FLAGGED_VAL
return flagged_field
def backtracking_search(kb: KB, model: dict[int, bool]) -> bool:
if kb.is_satisfied(model):
return True
assigned = set(model.keys())
for var in kb.vars:
if var in assigned:
continue
child_model = copy.deepcopy(model)
child_model[var] = False
if backtracking_search(kb, child_model):
return True
child_model = copy.deepcopy(model)
child_model[var] = True
if backtracking_search(kb, child_model):
return True
return False
def test(kb: KB_l, model: dict[int, bool], exclude, idx=0) -> bool:
ans = kb.is_satisfied_t(model)
if ans == Answer.TRUE:
return True
if ans == Answer.FALSE:
return False
if idx == exclude:
return test(kb, model, exclude, idx + 1)
if idx == len(kb.vars):
return False
for child in (False, True):
child_model = model.copy()
child_model[kb.vars[idx]] = child
if test(kb, child_model, exclude, idx + 1):
return True
return False
def test2(kb: KB, model: dict[int, bool]) -> bool:
ans = kb.is_satisfied_t(model)
if ans == Answer.TRUE:
return True
if ans == Answer.FALSE:
return False
for var in kb.vars:
if var in model:
continue
for val in (False, True):
child_model = model.copy()
child_model[var] = val
if test2(kb, child_model):
return True
break
return False
def backtracking_solve(field):
clauses, vars_ = construct_CNF_clauses(field)
kb = KB(clauses, vars_)
# TODO: checking if the model is solvable or not
height = len(field)
width = len(field[0])
flagged_field = copy.deepcopy(field)
for i, var in enumerate(vars_):
model = dict()
model[var] = False
if not test2(kb, model):
flagged_field[(var - 1) // width][(var - 1) % width] = FLAGGED_VAL
return flagged_field
def backtracking_solve2(field):
clauses, vars_ = construct_CNF_clauses(field)
kb = KB_l(clauses, list(vars_))
# TODO: checking if the model is solvable or not
height = len(field)
width = len(field[0])
flagged_field = copy.deepcopy(field)
for i, var in enumerate(vars_):
model = dict()
model[var] = False
if not test(kb, model, i):
flagged_field[(var - 1) // width][(var - 1) % width] = FLAGGED_VAL
return flagged_field