-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_algs.py
112 lines (94 loc) · 3.71 KB
/
run_algs.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
from forwardtracking import (
sudokuSolver as lookahead_solver
)
from backtracking import sudokuSolver as backtracking_solver
from simulated_annealing import sudoku_solver as sim_annealing_solver
from exact_cover import solve_sudoku as exact_cover_solver
from utils import print_sudoku_puzzle, validate_sudoku_solution
import json
import time
class Puzzle(object):
def __init__(self, include, type, puzzle):
self.include = include
self.type = type
self.puzzle = puzzle
with open('puzzles.json') as data_file:
data = json.load(data_file)
puzzles = data["puzzles"]
for puzzleObject in puzzles:
newPuzzle = Puzzle(
puzzleObject["include"], puzzleObject["type"],
puzzleObject["puzzle"]
)
if newPuzzle.include:
puzzle = newPuzzle.puzzle
print("="*60 + "\n" + "="*60 + "\n")
print(
"Puzzle type: " + newPuzzle.type +
"\nInitial puzzle: \n"
)
print_sudoku_puzzle(puzzle)
# Backtracking solver
print ("Solving with backtracking: \n")
result = None
startTime = time.time() * 1000
result = backtracking_solver(puzzle)
endTime = time.time() * 1000
totalTime = endTime - startTime
if result is not None:
print_sudoku_puzzle(result)
print (
"\nThis solution is valid: " +
str(validate_sudoku_solution(result)) +
"\nRuntime was: " + str(totalTime) + "ms" + "\n"
)
else:
print("No solution found by backtracking algorithm")
# Lookadhead solver
print ("Solving with propagation_forwardtracking: \n")
result = None
startTime = time.time() * 1000
result = lookahead_solver(puzzle)
endTime = time.time() * 1000
totalTime = endTime - startTime
if result is not None:
print_sudoku_puzzle(result)
print (
"This solution is valid: " +
str(validate_sudoku_solution(result)) + "\n" +
"Runtime was: " + str(totalTime) + "ms" + "\n"
)
else:
print("No solution found by lookahead algorithm")
# Sim annealing solver
print ("Solving with sim_annealing_solver: \n")
result = None
startTime = time.time() * 1000
result = sim_annealing_solver(puzzle)
endTime = time.time() * 1000
totalTime = endTime - startTime
if result is not None:
print_sudoku_puzzle(result)
print (
"This solution is valid: " +
str(validate_sudoku_solution(result)) +
"\nRuntime was: " + str(totalTime) + "ms" + "\n"
)
else:
print("No solution found by simulated annealing algorithm")
# Exact cover
print ("Solving with exact cover: \n")
result = None
startTime = time.time() * 1000
result = exact_cover_solver((3, 3), puzzle)
endTime = time.time() * 1000
totalTime = endTime - startTime
if result is not None:
print_sudoku_puzzle(result)
print (
"\nThis solution is valid: " +
str(validate_sudoku_solution(result)) +
"\nRuntime was: " + str(totalTime) + "ms" + "\n"
)
else:
print("No solution found by exact cover algorithm")