-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_sat.py
81 lines (66 loc) · 2.5 KB
/
main_sat.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
from z3 import *
def print_model(model, var):
for i in range(x_len + 1):
buff = ''
for j in range(y_len + 1):
buff += '1' if model[var[i][j]] else '0'
print(buff)
print('--------------------------------------')
grid = [
[2, 3, 1, 0, 0],
[1, 3, 2, 1, 2],
[2, 3, 2, 2, 3],
[4, 3, 2, 3, 2],
[2, 1, 2, 4, 2]
]
# grid = [[1]]
x_len = len(grid) # -> i
y_len = len(grid[0]) # -> j
var = [[Bool('_' + str(j) + '_' + str(i)) for i in range(x_len + 1)] for j in range(y_len + 1)]
s = Solver()
# print(s.check())
for i in range(x_len):
for j in range(y_len):
if grid[i][j] == 0:
s.add(And(Not(var[i][j]), Not(var[i+1][j]), Not(var[i][j+1]), Not(var[i+1][j+1])))
elif grid[i][j] == 1:
s.add(
Or(
And(var[i][j], Not(var[i+1][j]), Not(var[i][j+1]), Not(var[i+1][j+1])),
And(Not(var[i][j]), var[i+1][j], Not(var[i][j+1]), Not(var[i+1][j+1])),
And(Not(var[i][j]), Not(var[i+1][j]), var[i][j+1], Not(var[i+1][j+1])),
And(Not(var[i][j]), Not(var[i+1][j]), Not(var[i][j+1]), var[i+1][j+1])
)
)
elif grid[i][j] == 2:
s.add(
Or(
And(var[i][j], var[i+1][j], Not(var[i][j+1]), Not(var[i+1][j+1])),
And(var[i][j], Not(var[i+1][j]), var[i][j+1], Not(var[i+1][j+1])),
And(var[i][j], Not(var[i+1][j]), Not(var[i][j+1]), var[i+1][j+1]),
And(Not(var[i][j]), var[i+1][j], var[i][j+1], Not(var[i+1][j+1])),
And(Not(var[i][j]), var[i+1][j], Not(var[i][j+1]), var[i+1][j+1]),
And(Not(var[i][j]), Not(var[i+1][j]), var[i][j+1], var[i+1][j+1]),
)
)
elif grid[i][j] == 3:
s.add(
Or(
And(var[i][j], var[i+1][j], var[i][j+1], Not(var[i+1][j+1])),
And(var[i][j], var[i+1][j], Not(var[i][j+1]), var[i+1][j+1]),
And(var[i][j], Not(var[i+1][j]), var[i][j+1], var[i+1][j+1]),
And(Not(var[i][j]), var[i+1][j], var[i][j+1], var[i+1][j+1])
)
)
# print(s.check())
while s.check() == sat:
model = s.model()
term = []
for i in range(x_len+1):
for j in range(y_len+1):
term.append(model[var[i][j]] != var[i][j])
print(model)
print(term)
print(s.sexpr())
print_model(model, var)
s.add(Or(term))