-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_smt__.py
54 lines (38 loc) · 1.12 KB
/
main_smt__.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
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]] == 1 else '0'
print(buff)
grid = [
[2, 3, 1]
]
x_len = len(grid) # -> i
y_len = len(grid[0]) # -> j
var = [[Real('_' + str(j) + '_' + str(i)) for i in range(y_len + 1)] for j in range(x_len + 1)]
s = Solver()
#print(var)
for i in range(x_len + 1):
for j in range(y_len + 1):
s.add(Or(var[i][j] == 0, var[i][j] == 1))
# s.add(var[i][j] + var[i+1][j] + var[i][j+1] + var[i+1][j+1] == grid[i][j])
# # 0 0
# s.add(var[0][0] + var[1][0] + var[0][1] + var[1][1] == grid[0][0])
# # 0 1
# s.add(var[0][1] + var[1][1] + var[0][2] + var[1][2] == grid[0][1])
# # 0 2
# s.add(var[0][2] + var[1][2] + var[0][3] + var[1][3] == grid[0][2])
#print(s.check())
for i in range(x_len):
for j in range(y_len):
s.add(var[i][j] + var[i+1][j] + var[i][j+1] + var[i+1][j+1] == grid[i][j])
#print(s.check())
while s.check() == sat:
model = s.model()
term = []
for i in range(x_len):
for j in range(y_len):
term.append(model[var[i][j]] != var[i][j])
print_model(model, var)
s.add(Or(term))