-
Notifications
You must be signed in to change notification settings - Fork 0
/
CS3243_P2_Sudoku_Final.py
179 lines (148 loc) · 5.88 KB
/
CS3243_P2_Sudoku_Final.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
# CS3243 Introduction to Artificial Intelligence
# Project 2, Part 1: Sudoku
import sys
import copy
# Running script: given code can be run with the command:
# python file.py, ./path/to/init_state.txt ./output/output.txt
class Sudoku(object):
def __init__(self, puzzle):
# you may add more attributes if you need
self.puzzle = puzzle # self.puzzle is a list of lists
self.ans = copy.deepcopy(puzzle) # self.ans is a list of lists
self.csp = CSP(puzzle)
def solve(self):
assignment = self.csp.backtrackSearch(self.csp.domains.copy())
for var in assignment:
(i, j) = var.coordinate
self.ans[i][j] = assignment[var]
# self.ans is a list of lists
return self.ans
class CSP(object):
def __init__(self, puzzle):
self.variables = list()
self.units = dict()
self.neighbors = dict()
self.domains = dict()
self.initialiseCSP(puzzle)
self.calculateDomain()
def initialiseCSP(self,puzzle):
for i in range(9):
for j in range(9):
self.variables.append(Variable((i, j), puzzle[i][j]))
for var1 in self.variables:
self.units[var1] = [[],[],[]] # col, row, square
self.neighbors[var1] = set() # add in variables which belongs to the same unit
self.domains[var1] = "123456789" # initialising domain
for var2 in self.variables:
if var1.isSameCol(var2):
self.units[var1][0].append(var2)
if var1 != var2:
self.neighbors[var1].add(var2)
if var1.isSameRow(var2):
self.units[var1][1].append(var2)
if var1 != var2:
self.neighbors[var1].add(var2)
if var1.isSameSquare(var2):
self.units[var1][2].append(var2)
if var1 != var2:
self.neighbors[var1].add(var2)
def calculateDomain(self):
# Sets each variables' domain
for var in self.variables:
if var.value != '0':
self.assign(self.domains, var, var.value)
return self.domains
def assign(self, domains, var, val):
temp = domains[var].replace(val, "")
for delVal in temp:
if not self.constraintsPropagation(domains, var, delVal):
return False
return domains
def constraintsPropagation(self, domains, var, delVal):
# check exists
if delVal not in domains[var]:
return domains
# delete delVal from domains
domains[var] = domains[var].replace(delVal, "")
# when var can only take on a val, assign it
if not len(domains[var]):
return False
elif len(domains[var]) == 1:
delVal2 = domains[var] # edited by Hussain
for neighbor in self.neighbors[var]:
if not self.constraintsPropagation(domains, neighbor, delVal2):
return False
# when only one var left in a unit for a val, assign it
for u in self.units[var]:
remainings = []
for v in u:
if delVal in domains[v]:
remainings.append(v)
if not len(remainings):
return False
elif len(remainings) == 1:
if not self.assign(domains, remainings[0], delVal):
return False
return domains
def backtrackSearch(self, domains):
if domains is False:
return False
if self.checkSolved(domains):
return domains
var = self.mrv(domains)
for d in domains[var]:
result = self.backtrackSearch(self.assign(domains.copy(),var, d))
if result:
return result
return False
def checkSolved(self,domains):
for var in self.variables:
if len(domains[var]) != 1:
return False
return True
def mrv(self, domains):
# Minmum remaining values heuristic domains = {a: 1, b 123, c 12345} return b
return min(self.variables,key=lambda var: len(domains[var])\
if (len(domains[var]) > 1) else 10)
#if len(domain) == 1 then return 10 to ensure it is not minimum
class Variable(object):
def __init__(self, coordinate, value):
self.coordinate = coordinate # (x, y)
self.value = str(value)
def __hash__(self):
return hash(self.coordinate)
def isSameRow(self, var):
return self.coordinate[0] == var.coordinate[0]
def isSameCol(self, var):
return self.coordinate[1] == var.coordinate[1]
def isSameSquare(self, var):
''' square refers to 3*3 square '''
return (self.coordinate[0]//3, self.coordinate[1]//3) == (var.coordinate[0]//3, var.coordinate[1]//3)
if __name__ == "__main__":
# STRICTLY do NOT modify the code in the main function here
if len(sys.argv) != 3:
print ("\nUsage: python CS3243_P2_Sudoku_XX.py input.txt output.txt\n")
raise ValueError("Wrong number of arguments!")
try:
f = open(sys.argv[1], 'r')
except IOError:
print ("\nUsage: python CS3243_P2_Sudoku_XX.py input.txt output.txt\n")
raise IOError("Input file not found!")
puzzle = [[0 for i in range(9)] for j in range(9)]
lines = f.readlines()
i, j = 0, 0
for line in lines:
for number in line:
if '0' <= number <= '9':
puzzle[i][j] = int(number)
j += 1
if j == 9:
i += 1
j = 0
sudoku = Sudoku(puzzle)
ans = sudoku.solve()
with open(sys.argv[2], 'a') as f:
for i in range(9):
for j in range(9):
f.write(str(ans[i][j]) + " ")
f.write("\n")