forked from python4geeks/Hactoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSudoku Solver.py
492 lines (398 loc) · 14.8 KB
/
Sudoku Solver.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
import math
import random
import time
import sys
import numpy as np
nodesVisited = 0
def main():
print_help()
# take multiple inputs
inputList = []
maxLengthList = 3
while len(inputList) < maxLengthList:
if (len(inputList) == 1 or len(inputList) == 0):
item = input("Enter your difficulty level: ")
inputList.append(item)
if len(inputList) >= 1:
item = input("Enter your method: ")
inputList.append(item)
if len(inputList) >= 2:
item = input("Enter your number of trial: ")
inputList.append(item)
if len(inputList) < 1:
print_help()
return 0
if len(inputList) == 3:
n_trials = inputList[2]
else:
n_trials = 50
print("Number of trials: " , n_trials)
run_times = []
### Easy Puzzle
easy = [[0, 3, 0, 0, 8, 0, 0, 0, 6],
[5, 0, 0, 2, 9, 4, 7, 1, 0],
[0, 0, 0, 3, 0, 0, 5, 0, 0],
[0, 0, 5, 0, 1, 0, 8, 0, 4],
[4, 2, 0, 8, 0, 5, 0, 3, 9],
[1, 0, 8, 0, 3, 0, 6, 0, 0],
[0, 0, 3, 0, 0, 7, 0, 0, 0],
[0, 4, 1, 6, 5, 3, 0, 0, 2],
[2, 0, 0, 0, 4, 0, 0, 6, 0]]
### Medium puzzle
medium = [[3, 0, 8, 2, 9, 6, 0, 0, 0],
[0, 4, 0, 0, 0, 8, 0, 0, 0],
[5, 0, 2, 1, 0, 0, 0, 8, 7],
[0, 1, 3, 0, 0, 0, 0, 0, 0],
[7, 8, 0, 0, 0, 0, 0, 3, 5],
[0, 0, 0, 0, 0, 0, 4, 1, 0],
[1, 2, 0, 0, 0, 7, 8, 0, 3],
[0, 0, 0, 8, 0, 0, 0, 2, 0],
[0, 0, 0, 5, 4, 2, 1, 0, 6]]
### Hard Puzzle
hard = [[7, 0, 0, 0, 0, 0, 0, 0, 0],
[6, 0, 0, 4, 1, 0, 2, 5, 0],
[0, 1, 3, 0, 9, 5, 0, 0, 0],
[8, 6, 0, 0, 0, 0, 0, 0, 0],
[3, 0, 1, 0, 0, 0, 4, 0, 5],
[0, 0, 0, 0, 0, 0, 0, 8, 6],
[0, 0, 0, 8, 4, 0, 5, 3, 0],
[0, 4, 2, 0, 3, 6, 0, 0, 7],
[0, 0, 0, 0, 0, 0, 0, 0, 9]]
### EVIL Puzzle
evil = [[0, 6, 0, 8, 0, 0, 0, 0, 0],
[0, 0, 4, 0, 6, 0, 0, 0, 9],
[1, 0, 0, 0, 4, 3, 0, 6, 0],
[0, 5, 2, 0, 0, 0, 0, 0, 0],
[0, 0, 8, 6, 0, 9, 3, 0, 0],
[0, 0, 0, 0, 0, 0, 5, 7, 0],
[0, 1, 0, 4, 8, 0, 0, 0, 5],
[8, 0, 0, 0, 1, 0, 2, 0, 0],
[0, 0, 0, 0, 0, 5, 0, 4, 0]]
puzzles = [easy, medium, hard, evil]
puzzle = puzzles[int(inputList[0])-1]
print("Solving the following puzzle: ")
print_puzzle(puzzle)
start_time = time.time()
method_type = int(inputList[1])
if method_type == 1:
print("Using backtracking search.")
result = solve_backtrack(puzzle)
elif method_type == 2:
print("Using backtracking with forward checking.")
result = solve_btfc(puzzle)
else:
print("Using backtracking with forward checking and heurisitics.")
result = solve_btfch(puzzle)
if result:
print("It took", time.time() - start_time, "seconds to solve this puzzle.")
else:
print("Failed to find a solution! D:")
for i in range(int(n_trials)):
start_time = time.time()
if not solve_backtrack(puzzle ):
print ("Failed to find a solution! D:")
else:
elapsed = time.time() - start_time
print ("It took", time.time() - start_time, "seconds to solve this puzzle.")
print ("elapsed:", elapsed)
run_times.append( elapsed )
#print("Nodes Visited: ", nodesVisited)
run_times
ar = np.array(run_times)
#print(run_times)
print ("Average run time:", sum(run_times)/int(n_trials))
print("Standard Deviation: ", ar.std())
print("Nodes Visited: ", nodesVisited-int(n_trials))
# Recursive backtracking algorithm to solve puzzle
def solve_backtrack(puzzle):
global nodesVisited
nodesVisited += 1
# store all the possible values remaining for a square
domain = list(range(1, 10))
# get a list of the empty squares (remaining variables)
empty_squares = get_empty_squares(puzzle)
# if there are no remaining empty squares we're done
if len(empty_squares) == 0:
print("Woohoo, success! Check it out:")
print_puzzle(puzzle)
print("Nodes Visited: ", nodesVisited)
return 1
square = get_random_square(empty_squares)
row = square[0]
col = square[1]
while len(domain) != 0:
# get a random value out of the list of remaining possible values
value = domain[int(math.floor(random.random() * len(domain)))]
#domain.remove(value)
domain = [i for i in domain if i != value]
# check the value against theconstraints
if check_row(square, value, puzzle):
if check_col(square, value, puzzle):
if check_block(square, value, puzzle):
puzzle[row][col] = value
if solve_backtrack(puzzle):
return 1
else:
puzzle[row][col] = 0
return 0
# Backtracking with forward-checking algorithm
# Stores a list with all the variables and their potentially legal
# values - stops when there are no empty squares remaining
# Randomly selects the next cell and the value
def solve_btfc(puzzle):
global nodesVisited
nodesVisited += 1
# get a list of the empty squares (remaining variables)
empty_squares = get_empty_squares(puzzle)
# if there are no remaining empty squares we're done
if len(empty_squares) == 0:
print("Woohoo, success! Check it out:")
print_puzzle(puzzle)
return 1
square = get_random_square(empty_squares)
row = square[0]
col = square[1]
remaining_values = get_remaining_values(puzzle)
values = list(remaining_values[col + row * 9])
while len(values) != 0:
value = values[int(math.floor(random.random() * len(values)))]
values.remove(value)
if forward_check(remaining_values, value, row, col):
puzzle[row][col] = value
if solve_btfc(puzzle):
return 1
else:
puzzle[row][col] = 0
return 0
# Solves the sudoku puzzle using forward checking and 3 heuristics:
# minimum remaining values, degree, and least constraining value heuristics
def solve_btfch(puzzle):
global nodesVisited
nodesVisited += 1
# get a list of the empty squares (remaining variables)
empty_squares = get_empty_squares(puzzle)
# if there are no remaining empty squares we're done
if len(empty_squares) == 0:
print("Woohoo, success! Check it out:")
print_puzzle(puzzle)
return 1
# find the most constrained square (one with least remaining values)
remaining_values = get_remaining_values(puzzle)
mrv_list = []
[mrv_list.append(len(remaining_values[square[0] * 9 + square[1]])) for square in empty_squares]
# make a list of the squares with the minimum remaining values (mrv)
mrv_squares = []
minimum = min(mrv_list)
for i in range(len(mrv_list)):
value = mrv_list[i]
if value == minimum:
mrv_squares.append(empty_squares[i])
# if there are no ties, take the square with the MRV
if len(mrv_squares) == 1:
square = mrv_squares[0]
else:
# otherwise, find the most constraining variable (variable with highest degree)
degree_list = []
for cell in mrv_squares:
degree = get_degree(cell, puzzle)
degree_list.append(degree)
max_degree = max(degree_list)
max_degree_squares = []
for i in range(len(degree_list)):
value = degree_list[i]
if value == max_degree:
max_degree_squares.append(mrv_squares[i])
# just take the first square as a tie-breaker
square = max_degree_squares[0]
row = square[0]
col = square[1]
values = list(remaining_values[col + row * 9])
while len(values) != 0:
lcv_list = get_lcv(values, row, col, remaining_values)
# take the least constraining value
value = values[lcv_list.index(min(lcv_list))]
values.remove(value)
if forward_check(remaining_values, value, row, col):
puzzle[row][col] = value
if solve_btfch(puzzle):
return 1
else:
puzzle[row][col] = 0
return 0
# counts the number of times a value appears in constrained cells
def get_lcv(values, row, col, remaining_values):
lcv_list = []
for value in values:
count = 0
for i in range(9):
if i == col:
continue
x = remaining_values[row * 9 + i]
if value in x:
count += 1
for i in range(9):
if i == row:
continue
x = remaining_values[col + 9 * i]
if value in x:
count += 1
block_row = int(row / 3)
block_col = int(col / 3)
for i in range(3):
for j in range(3):
if [block_row * 3 + i, block_col * 3 + j] == [row, col]:
continue
x = remaining_values[block_col * 3 + j + (block_row * 3 + i) * 9]
if value in x:
count += 1
lcv_list.append(count)
return lcv_list
# returns the number of variables constrained by the specified square
def get_degree(square, puzzle):
row = square[0]
col = square[1]
degree = 0
for i in range(9):
if i == col:
continue
if puzzle[row][i] == 0:
degree += 1
for i in range(9):
if i == row:
continue
if puzzle[i][col] == 0:
degree += 1
block_row = int(row / 3)
block_col = int(col / 3)
for i in range(3):
for j in range(3):
if [block_row * 3 + i, block_col * 3 + j] == [row, col]:
continue
if puzzle[block_row * 3 + i][block_col * 3 + j] == 0:
degree += 1
return degree
# checks to see if the value being removed is the only one left
def forward_check(remaining_values, value, row, col):
for i in range(9):
if i == col:
continue
x = remaining_values[row * 9 + i]
if len(x) == 1:
if x[0] == value:
return 0
for i in range(9):
if i == row:
continue
x = remaining_values[col + 9 * i]
if len(x) == 1:
if x[0] == value:
return 0
block_row = int(row / 3)
block_col = int(col / 3)
for i in range(3):
for j in range(3):
if [block_row * 3 + i, block_col * 3 + j] == [row, col]:
continue
x = remaining_values[block_col * 3 + j + (block_row * 3 + i) * 9]
if len(x) == 1:
if x[0] == value:
return 0
return 1
# Returns a list of the remaining potential values for each of the 81 squares
# The list is structured row by row with respect to the puzzle
# Only gets called once, at the beginning of the BT-FC search to initialize
def get_remaining_values(puzzle):
remaining_values = []
# initialize all remaining values to the full domain
[remaining_values.append(range(1, 10)) for i in range(81)]
for row in range(len(puzzle)):
for col in range(len(puzzle[1])):
if puzzle[row][col] != 0:
# remove the value from the constrained squares
value = puzzle[row][col]
remaining_values = remove_values(row, col, value, remaining_values)
return remaining_values
# Removes the specified value from constrained squares and returns the new list
def remove_values(row, col, value, remaining_values):
# use a value of zero to indicate that the square is assigned
remaining_values[col + row * 9] = [0]
# Remove the specified value from each row, column, and block if it's there
for x in remaining_values[row * 9:row * 9 + 9]:
try:
#x.remove(value)
x = [i for i in x if i != value]
except ValueError:
pass
for i in range(9):
try:
#remaining_values[col + 9 * i].remove(value)
remaining_values[col + 9 * i] = [i for i in remaining_values[col + 9 * i] if i != value]
except ValueError:
pass
block_row = int(row / 3)
block_col = int(col / 3)
for i in range(3):
for j in range(3):
try:
#remaining_values[block_col * 3 + j + (block_row * 3 + i) * 9].remove(value)
remaining_values[block_col * 3 + j + (block_row * 3 + i) * 9] = [i for i in remaining_values[block_col * 3 + j + (block_row * 3 + i) * 9] if i != value]
except ValueError:
pass
return remaining_values
# return a randomly selected square from the list of empties
def get_random_square(empty_squares):
# randomly pick one of the empty squares to expand and return it
return empty_squares[int(math.floor(random.random() * len(empty_squares)))]
# return the list of empty squares indices for the puzzle
def get_empty_squares(puzzle):
empty_squares = []
# scan the whole puzzle for empty cells
for row in range(len(puzzle)):
for col in range(len(puzzle[1])):
if puzzle[row][col] == 0:
empty_squares.append([row, col])
return empty_squares
# checks the 9x9 block to which the square belongs
def check_block(square, value, puzzle):
row = square[0]
col = square[1]
block_row = int(row / 3)
block_col = int(col / 3)
for i in range(3):
for j in range(3):
if [i, j] == square:
continue
if puzzle[block_row * 3 + i][block_col * 3 + j] == value:
return 0
return 1
# checks the row of the specified square for the same value
def check_row(square, value, puzzle):
row = square[0]
col = square[1]
for i in range(len(puzzle)):
if i == square[0]:
continue
if puzzle[row][i] == value:
return 0
return 1
# checks the column of the specified square for the same value
def check_col(square, value, puzzle):
row = square[0]
col = square[1]
for i in range(len(puzzle[1])):
if i == square[1]:
continue
if puzzle[i][col] == value:
return 0
return 1
def print_puzzle(puzzle):
for row in puzzle:
print(row)
def print_help():
print("Usage: python sudoku.py <difficulty=1,2,3,4> <method=1,2,3>")
print("Method 1: Backtracking Search")
print("Method 2: Backtracking with Forward Checking")
print("Method 3: Backtracking with Forward Checking and Heurisitics")
# y = input()
if __name__ == "__main__":
main()