-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.py
116 lines (87 loc) · 2.95 KB
/
Main.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
#!/usr/bin/env python3
import sys
import os
import math
import SudokuBoard
import Constraint
import ConstraintNetwork
import BTSolver
import Trail
import time
"""
Main driver file, which is responsible for interfacing with the
command line and properly starting the backtrack solver.
"""
def main ( ):
args = sys.argv
# Important Variables
file = "";
var_sh = "";
val_sh = "";
cc = "";
for arg in [args[i] for i in range(1, len(args))]:
if arg == "MRV":
var_sh = "MinimumRemainingValue"
elif arg == "MAD":
var_sh = "MRVwithTieBreaker"
elif arg == "LCV":
val_sh = "LeastConstrainingValue"
elif arg == "FC":
cc = "forwardChecking"
elif arg == "NOR":
cc = "norvigCheck"
elif arg == "TOURN":
var_sh = "tournVar"
val_sh = "tournVal"
cc = "tournCC"
else:
file = arg;
trail = Trail.Trail();
if file == "":
sudokudata = SudokuBoard.SudokuBoard( 3, 3, 7 )
print(sudokudata)
solver = BTSolver.BTSolver( sudokudata, trail, val_sh, var_sh, cc )
if cc in ["forwardChecking","norvigCheck","tournCC"]:
solver.checkConsistency()
solver.solve()
if solver.hassolution:
print( solver.getSolution() )
print( "Trail Pushes: " + str(trail.getPushCount()) )
print( "Backtracks: " + str(trail.getUndoCount()) )
else:
print( "Failed to find a solution" )
return
if os.path.isdir(file):
listOfBoards = None
try:
listOfBoards = os.listdir ( file )
except:
print ( "[ERROR] Failed to open directory." )
return
numSolutions = 0
for f in listOfBoards:
print ( "Running board: " + str(f) )
sudokudata = SudokuBoard.SudokuBoard( filepath=os.path.join( file, f ) )
solver = BTSolver.BTSolver( sudokudata, trail, val_sh, var_sh, cc )
if cc in ["forwardChecking","norvigCheck","tournCC"]:
solver.checkConsistency()
solver.solve()
if solver.hassolution:
numSolutions += 1;
print ( "Solutions Found: " + str(numSolutions) )
print ( "Trail Pushes: " + str(trail.getPushCount()) )
print ( "Backtracks: " + str(trail.getUndoCount()) )
return
sudokudata = SudokuBoard.SudokuBoard( filepath=os.path.abspath( file ) )
print(sudokudata)
solver = BTSolver.BTSolver( sudokudata, trail, val_sh, var_sh, cc )
if cc in ["forwardChecking","norvigCheck","tournCC"]:
solver.checkConsistency()
solver.solve()
if solver.hassolution:
print( solver.getSolution() )
print( "Trail Pushes: " + str(trail.getPushCount()) )
print( "Backtracks: " + str(trail.getUndoCount()) )
else:
print( "Failed to find a solution" )
main()