-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku_example_mt.py
executable file
·130 lines (104 loc) · 4.42 KB
/
sudoku_example_mt.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
#!/usr/bin/env python
# vim: et:sts=4:sw=4:
import sys
sys.path.append("./src")
from sat import SAT_solver
from sudoku import sudoku, printSudoku, processResult
from logConstructs import FlatCNF
print "================================================="
print "SUDOKU"
print "================================================="
solver = SAT_solver()
# define board as follows.
# board is array with nine arrays (rows).
# rows are arrays of nine elements.
# elements are None or int in [1,9].
# None - empty square.
board = [[None, 8, None, 1, 6, None, None, None, 7],
[1, None, 7, 4, None, 3, 6, None, None],
[3, None, None, 5, None, None, 4, 2, None],
[None, 9, None, None, 3, 2, 7, None, 4],
[None, None, None, None, None, None, None, None, None],
[2, None, 4, 8, 1, None, None, 6, None],
[None, 4, 1, None, None, 8, None, None, 6],
[None, None, 6, 7, None, 1, 9, None, 3],
[7, None, None, None, 9, 6, None, 4, None]]
# print sudoku from board definition.
print "Lab exercise:"
print printSudoku(board)
# construct logical formula from board definition.
formula = sudoku(board)
# solve formula using SAT solver(multithreading)
#result = solver.solve(FlatCNF(formula))
result = solver.solve(formula, True)
print "Solution:"
# process and print result of sat solver.
print printSudoku(processResult(result[1]))
medium_board = [[None, None, 5, None, 6, 3, 1, 2, None],
[None, None, 9, None, None, 1, None, 5, None],
[1, None, None, None, None, 8, 9, None, 6],
[None, None, None, None, 5, None, 8, None, 2],
[None, 5, None, None, None, None, None, 1, None],
[6, None, 1, None, 9, None, None, None, None],
[9, None, 6, 2, None, None, None, None, 1],
[None, 1, None, 6, None, None, 4, None, None],
[None, 4, 7, 3, 1, None, 2, None, None]]
print "Medium problem:"
print printSudoku(medium_board)
#result = solver.solve(FlatCNF(sudoku(medium_board)))
result = solver.solve(sudoku(medium_board), True)
print "Solution:"
# process and print result of sat solver.
print printSudoku(processResult(result[1]))
hard_board = [[None, None, 2, None, 8, None, None, 3, None],
[None, None, 5, 1, 9, None, None, None, 2],
[None, 8, None, None, None, None, None, 4, None],
[None, 9, 6, None, 5, None, None, None, None],
[2, None, 8, None, None, None, 3, None, 4],
[None, None, None, None, 3, None, 6, 9, None],
[None, 3, None, None, None, None, None, 2, None],
[8, None, None, None, 4, 6, 7, None, None],
[None, 6, None, None, 1, None, 5, None, None]]
print "Hard problem:"
print printSudoku(hard_board)
#result = solver.solve(FlatCNF(sudoku(hard_board)))
result = solver.solve(sudoku(hard_board), True)
print "Solution:"
# process and print result of sat solver.
print printSudoku(processResult(result[1]))
evil_board = [[None, 3, 6, None, 9, None, None, None, None],
[None, None, None, None, None, None, None, 4, 1],
[None, None, None, 7, 4, None, None, None, 3],
[None, 9, 1, None, None, None, None, None, None],
[2, None, None, 5, None, 3, None, None, 6],
[None, None, None, None, None, None, 2, 1, None],
[5, None, None, None, 2, 4, None, None, None],
[6, 8, None, None, None, None, None, None, None],
[None, None, None, None, 7, None, 6, 2, None]]
print "Evil problem:"
print printSudoku(evil_board)
#result = solver.solve(FlatCNF(sudoku(evil_board)))
result = solver.solve(sudoku(evil_board), True)
print "Solution:"
# process and print result of sat solver.
print printSudoku(processResult(result[1]))
#no solution sudoku
evil_board = [[None, 3, 6, None, 9, None, None, None, 3],
[None, None, None, None, None, None, None, 4, 1],
[None, None, None, 7, 4, None, None, None, 3],
[None, 9, 1, None, None, None, None, None, None],
[2, None, None, 5, None, 3, None, None, 6],
[None, None, None, None, None, None, 2, 1, None],
[5, None, None, None, 2, 4, None, None, None],
[6, 8, None, None, None, None, None, None, None],
[None, None, None, None, 7, None, 6, 2, None]]
print "Evil problem (no solution):"
print printSudoku(evil_board)
#result = solver.solve(FlatCNF(sudoku(evil_board)))
result = solver.solve(sudoku(evil_board), True)
print "Solution:"
if not result[0]:
print "No solution."
else:
# process and print result of sat solver.
print printSudoku(processResult(result[1]))