-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku_generator.py
429 lines (377 loc) · 10.6 KB
/
sudoku_generator.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
# Importing the libraries
from pysat.solvers import Solver
from pysat.card import *
import time
import random
import math
import csv
# This function is for printing the sudoku onto the terminal
def print_sudoku(sudoku, rem):
index = 0
c1=1
c2=1
for x1, i in enumerate(sudoku):
c2=1
for x2, ele in enumerate(i):
print("{: >3}".format(ele),end="")
if(c2%k==0 and c2!=k2):
print("{: >3}".format("|"),end="")
c2=c2+1
print()
if(c1%k==0 and c1!=k2):
for h in range(k2+k-1):
print("{: >3}".format("-"),end="")
print()
c1=c1+1
print("\n")
# These 2 functions are used to print the partially filled sudoku-pair onto the terminal
def print_unfilled_sudoku_1(sudoku, rem):
index = 0
c1=1
c2=1
for x1, i in enumerate(sudoku):
c2=1
for x2, ele in enumerate(i):
if index < len(rem) and cell_id(c1, c2, ele) == rem[index]:
print("{: >3}".format(ele),end="")
index += 1
else:
print('{: >3}'.format(0), end = '')
if(c2%k==0 and c2!=k2):
print("{: >3}".format("|"),end="")
c2=c2+1
print()
if(c1%k==0 and c1!=k2):
for h in range(k2+k-1):
print("{: >3}".format("-"),end="")
print()
c1=c1+1
print()
def print_unfilled_sudoku_2(sudoku, rem):
index = 0
c1=1
c2=1
for x1, i in enumerate(sudoku):
# if i in [3, 6]:
# print '------+-------+------'
c2=1
for x2, ele in enumerate(i):
if index < len(rem) and cell_id2(c1, c2, ele) == rem[index]:
print("{: >3}".format(ele),end="")
index += 1
else:
print("{: >3}".format(0),end="")
# index += 1
if(c2%k==0 and c2!=k2):
print("{: >3}".format("|"),end="")
c2=c2+1
print()
if(c1%k==0 and c1!=k2):
for h in range(k2+k-1):
print("{: >3}".format("-"),end="")
print()
c1=c1+1
print()
# This function is for writing the sudoku-pair to the csv file "sudoku.csv"
def wrcsv(sudoku1,rem1,sudoku2,rem2):
index = 0
c1=1
c2=1
ansls1=[]
ansls2=[]
for x1, i in enumerate(sudoku1):
c2=1
temp=[]
for x2, ele in enumerate(i):
if index < len(rem1) and cell_id(c1, c2, ele) == rem1[index]:
temp.append(ele)
index += 1
else:
temp.append(0)
# index += 1
c2=c2+1
ansls1.append(temp)
c1=c1+1
c1=1
index=0
for x1, i in enumerate(sudoku2):
c2=1
temp=[]
for x2, ele in enumerate(i):
if index < len(rem2) and cell_id2(c1, c2, ele) == rem2[index]:
temp.append(ele)
index += 1
else:
temp.append(0)
# index += 1
c2=c2+1
ansls2.append(temp)
c1=c1+1
with open ("sudoku.csv","w") as sudoku_csv:
writer=csv.writer(sudoku_csv)
writer.writerows(ansls1)
writer.writerows(ansls2)
# Inputting the value of k
k = int(input('Enter the value of k: '))
k2 = k**2
start = time.time()
'''
Below functions cell_id and cell_id2 are defined in order to get the variable (proposition) which
encodes the cell(p,q) having the number r
'''
def cell_id(p, q, r):
return (p-1)*(k**4) + (q-1)*(k**2) + r
def cell_id2(p, q, r):
return k**6 + cell_id(p, q, r)
'''
Now we encode the different constraints - this time we use some built-in functions in PySAT to give
us the encoding.
These functions are CardEnc.equals and CardEnc.atmost. We have used the pysat.card library
We also tried the manual encoding but here we found that the encoding obtained from the above two functions
result in faster execution of the program
'''
# Setting up the encoding for the row: each no should be present exactly once in each row
row = []
for p in range(0, k2):
t = []
for i in range(0, k**2):
temp = []
for j in range(0, k2):
temp.append(cell_id(p+1, j+1, i+1))
t.append(temp)
row.append(t)
for p in range(0, k2):
t = []
for i in range(0, k**2):
temp = []
for j in range(0, k2):
temp.append(cell_id2(p+1, j+1, i+1))
t.append(temp)
row.append(t)
# Setting up the encoding for the column: each no should be present exactly once in each column
col = []
for p in range(0, k2):
t = []
for i in range(0, k2):
temp = []
for j in range(0, k2):
temp.append(cell_id(j+1, p+1, i + 1))
t.append(temp)
col.append(t)
for p in range(0, k2):
t = []
for i in range(0, k2):
temp = []
for j in range(0, k2):
temp.append(cell_id2(j+1, p+1, i + 1))
t.append(temp)
col.append(t)
# Setting up the encoding for the subgrid of size k * k, each no should be present exactly once in each subgrid
box = []
for s in range(k):
for t in range(k):
r = s*k
c = t*k
for p in range(1, k2 + 1):
temp = []
for i in range(k):
for j in range(k):
temp.append(cell_id(i + r + 1, j + c + 1, p))
box.append(temp)
for s in range(k):
for t in range(k):
r = s*k
c = t*k
for p in range(1, k2 + 1):
temp = []
for i in range(k):
for j in range(k):
temp.append(cell_id2(i + r + 1, j + c + 1, p))
box.append(temp)
# Setting up the encoding for the cell: each cell should contain exactly one element
cell = []
for i in range(0, k2**2):
temp = []
for j in range(0, k2):
temp.append(i*k2 + 1 + j)
cell.append(temp)
for i in range(0, k2**2):
temp = []
for j in range(0, k2):
temp.append(i*k2 + 1 + j+k**6)
cell.append(temp)
# Setting up the encoding for the sudoku- pair, corresponding cells should have different values
pr=[]
for i in range(1,k2+1):
for j in range(1,k2+1):
for r in range(1,k2+1):
pr.append([cell_id(i,j,r),cell_id2(i,j,r)])
# All the above constraints are encoded in the below for loops
cnf_list = []
for ele in cell:
cnf = CardEnc.equals(lits=ele, bound = 1, encoding= EncType.pairwise)
cnf_list.append(cnf)
for i in row:
for ele in i:
cnf = CardEnc.equals(lits=ele, bound = 1, encoding= EncType.pairwise)
cnf_list.append(cnf)
for i in col:
for ele in i:
cnf = CardEnc.equals(lits=ele, bound = 1, encoding= EncType.pairwise)
cnf_list.append(cnf)
for ele in box:
cnf = CardEnc.equals(lits=ele, bound = 1, encoding= EncType.pairwise)
cnf_list.append(cnf)
for ele in pr:
cnf = CardEnc.atmost(lits=ele, bound = 1, encoding= EncType.pairwise)
cnf_list.append(cnf)
# Collect the constraint encoding in all_clauses
all_clauses=[]
for element in cnf_list:
for y in element.clauses:
all_clauses.append(y)
# Initialize the solver and add all the constraints
sol1 = Solver()
sol1.append_formula(all_clauses)
# Now we randomly fix the two cells in each of the sudokus (not corresponding cells)
# This ensures that we get a different sudoku-pair each time
ass=[]
q1=random.randrange(1,k2,1)
q2=random.randrange(1,k2,1)
q3=random.randrange(1,k2,1)
q4=random.randrange(1,k2,1)
ass.append(cell_id(1,1,q1))
ass.append(cell_id(k2,k2,q2))
ass.append(cell_id2(1,k2,q3))
ass.append(cell_id2(k2,1,q4))
sol1.solve(assumptions=ass)
# We get the model and store it in a variable
sol1_model = sol1.get_model()
sol1.delete()
# Collecting the positive elements of the model in order to build the sudoku
sol1_pos=[]
for ele in sol1_model:
if ele > 0:
sol1_pos.append(ele)
ans= []
ans2=[]
i = 0
temp = []
temp2=[]
for ele in sol1_pos:
i = i + 1
if(ele<=k**6):
if ((i > 1) and (i % k2 == 0)):
if ele % k2 != 0:
temp.append(ele % k2)
else:
temp.append(k2)
ans.append(temp)
temp = []
else:
if ele % k2 != 0:
temp.append(ele % k2)
else:
temp.append(k2)
if(ele>k**6):
if ((i > 1) and (i % k2 == 0)):
if ele % k2 != 0:
temp2.append(ele % k2)
else:
temp2.append(k2)
ans2.append(temp2)
temp2 = []
else:
if ele % k2 != 0:
temp2.append(ele % k2)
else:
temp2.append(k2)
'''
- Now we have the solution of the sudoku-pair. We now make a list of the cell nos (1 . . . .k^2 . . . .2k^2)
and randomly shuffle them
- Now we take an empty sudoku.
- We take elements from this list and insert the values corresponding to these cell nos into the sudoku
and check if they have a unique solution
- Once they have a unique solution, we break from that loop and consider all the elements which we have already
inserted into the sudoku. We check that if we get a unique solution on removing them. If yes, we can safely remove
that element from the sudoku. Else, we insert that element back into the sudoku.
- Finally, we have our sudoku-pair which is maximal and has a unique solution
'''
l = []
for i in range(1, k2**2 + 1):
l.append(i)
l.append(i + k2**2)
random.shuffle(l)
# This function returns the cell id of the cell
def get_coordinate(cell_no):
i = math.ceil(cell_no/k2)
if cell_no % k2 == 0:
j = k2
else:
j = cell_no % k2
return i, j
for ele in l:
if ele <= k2**2:
i, j = get_coordinate(ele)
inserted_elements = []
new_assump = []
rem_cell_no = []
s = Solver()
s.append_formula(all_clauses)
inserted_cell_id = 0
new_sol_pos= []
new_sol_pos_copy = []
not_filled = sol1_pos.copy()
cell_no_rem = []
for v, ele in enumerate(l):
if ele <= k2**2:
i, j = get_coordinate(ele)
new_sol_pos.append(cell_id(i,j,ans[i-1][j-1]))
inserted_cell_id = cell_id(i,j,ans[i-1][j-1])
# rem_cell_no.append(ele)
else:
i, j = get_coordinate(ele-k2**2)
new_sol_pos.append(cell_id2(i,j,ans2[i-1][j-1]))
inserted_cell_id = cell_id2(i,j,ans2[i-1][j-1])
# rem_cell_no.append(ele)
not_filled.remove(inserted_cell_id)
if (s.solve(assumptions = new_assump + [-1*inserted_cell_id]) == False):
f=1
new_assump.append(inserted_cell_id)
for element in not_filled:
if(s.solve(assumptions = new_sol_pos+ [-1*element]) == True):
f=0
break
if(f==1):
s.delete()
s1 = Solver()
s1.append_formula(all_clauses)
new_sol_pos_copy = new_sol_pos.copy()
last_index = len(new_sol_pos) - 1
for element in new_sol_pos:
new_sol_pos_copy.remove(element) # la =
if s1.solve(assumptions= new_sol_pos_copy + [-1*element]) == False:
continue
else:
new_sol_pos_copy.append(element)
s1.delete()
break
else:
None
else:
new_assump.append(inserted_cell_id)
inserted_elements = new_sol_pos_copy.copy()
s.delete()
inserted_elements.sort()
inserted_ele_1 = []
inserted_ele_2 = []
for element in inserted_elements:
if element <= k2**3:
inserted_ele_1.append(element)
else:
inserted_ele_2.append(element)
print_unfilled_sudoku_1(ans, inserted_ele_1)
print_unfilled_sudoku_2(ans2, inserted_ele_2)
wrcsv(ans, inserted_ele_1, ans2, inserted_ele_2)
end = time.time()
print("Time taken = {:.4f}s".format(end-start))