-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkakuro.py
280 lines (232 loc) · 9.5 KB
/
kakuro.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
import time
import csp
import search
import copy
import sys
# Definition of the problem
class Kakuro(csp.CSP):
def __init__(self, puzzle):
rows = len(puzzle)
cols = len(puzzle[0])
self.rows = rows
self.cols = cols
self.puzzleVals = copy.deepcopy(puzzle)
puzzleVar = [[ 0 for i in range(cols)] for i in range(rows)]
blacks = []
info = []
variables = []
domains = {}
neighbors = {}
conArrays = {}
#for variables
for i in range(rows):
for j in range(cols):
puzzleVar[i][j] = str(i)+'_'+str(j)
if puzzle[i][j] == '*':
blacks.append(str(i)+'_'+str(j))
elif puzzle[i][j] == '_':
variables.append(str(i)+'_'+str(j))
self.puzzleVals[i][j] = 0
else:
info.append(str(i)+'_'+str(j))
# for domain
d = [i for i in range(1,10)]
for var in variables:
domains[var] = d.copy()
neighbors[var] = []
# for neighbors
for i in range(rows):
for j in range(cols):
if puzzleVar[i][j] in variables:
# for Rows
for row in reversed(puzzleVar[i][:j]):
if (row in blacks) or (row in info):
break
else:
if row != puzzleVar[i][j]:
neighbors[puzzleVar[i][j]].append(row)
for row in puzzleVar[i][j:]:
if (row in blacks) or (row in info):
break
else:
if row != puzzleVar[i][j]:
neighbors[puzzleVar[i][j]].append(row)
# for Cols
for col in reversed([puz[j] for k,puz in enumerate(puzzleVar) if k<i]):
if (col in blacks) or (col in info):
break
else:
if col != puzzleVar[i][j]:
neighbors[puzzleVar[i][j]].append(col)
for col in [puz[j] for k,puz in enumerate(puzzleVar) if k>i]:
if (col in blacks) or (col in info):
break
else:
if col != puzzleVar[i][j]:
neighbors[puzzleVar[i][j]].append(col)
# for constraints info
# Get the array of variables for each constraint
for i in range(rows):
for j in range(cols):
if len(puzzle[i][j]) == 2:
if puzzle[i][j][1] != '':
k = j+1
tempInfo = []
while(puzzle[i][k] == '_'):
tempInfo.append(puzzleVar[i][k])
k += 1
if k> cols-1:
break
conArrays[str(puzzle[i][j][1])+'_'+puzzleVar[i][j]+'_O'] = tempInfo
del tempInfo
if puzzle[i][j][0] != '':
k = i+1
tempInfo = []
while(puzzle[k][j] == '_'):
tempInfo.append(puzzleVar[k][j])
k += 1
if k> rows-1:
break
conArrays[str(puzzle[i][j][0])+'_'+puzzleVar[i][j]+'_V'] = tempInfo
del tempInfo
self.puzzle = puzzle
self.conArrays = conArrays
self.puzzleVar = puzzleVar
self.neighbors = neighbors
self.variables = variables
self.domains = domains
super().__init__(variables, domains, neighbors,self.kakuroConstraints)
# find the needed sum
def getSumCon(self,con):
pos = con.find('_')
return int(con[:pos])
#get variables from constrain and convert them to values
#use the already assigned values from self.currAssignments
def varsToVals(self,con,A,B):
lis = []
for var in con:
#exclude already A or B because they will change
if (var in self.currAssignments):
if not (var == A or var == B):
lis.append(self.currAssignments[var])
return lis
def kakuroConstraints(self,A,a,B,b):
# find all conflicts
# get the same conflict of A and B
cons = set()
cons.update([con for con, conArr in self.conArrays.items() if (A in conArr) and (B in conArr)])
con = cons.pop()
#get array of variables
conArray = self.conArrays[con]
lis = []
# if neighbors and equal then false
if a == b:
return False
# Calculate the sum
#if length of lis is full of values then in means
#that every variable is assigned then the sum must be equal to constrain
lis = self.varsToVals(conArray,A,B)
if len(lis) + 2 == len(conArray):
if sum(lis) + a + b == self.getSumCon(con):
return True
else:
return False
# if length = 2 then the sum must be the constrain
elif len(conArray) == 2:
if a + b == self.getSumCon(con):
return True
else:
return False
#else if not then the sum can be less than the constrain
else:
if sum(lis) + a + b < self.getSumCon(con):
return True
else:
return False
def display(self, assignment=None):
for i, line in enumerate(self.puzzle):
puzzle = ""
for j, element in enumerate(line):
if element == '*':
puzzle += "[*]\t"
elif element == '_':
var1 = str(i)
if len(var1) == 1:
var1 = var1
var2 = str(j)
if len(var2) == 1:
var2 = var2
var = var1+ '_' +var2
if assignment is not None:
if isinstance(assignment[var], set) and len(assignment[var]) is 1:
puzzle += "[" + str(first(assignment[var])) + "]\t"
elif isinstance(assignment[var], int):
puzzle += "[" + str(assignment[var]) + "]\t"
else:
puzzle += "[_]\t"
else:
puzzle += "[_]\t"
else:
puzzle += str(element[0]) + "\\" + str(element[1]) + "\t"
print(puzzle)
if __name__ == "__main__":
#kakuro[1,2,3,4]
#[easy, easy, hard, very hard]
kakuroProblemNum = 0
kakuro = csp.kakuro1
try:
kakuroProblemNum = int(sys.argv[-1].split("kakuro")[-1])
except:
print("Wrong input \nrun simple kakuro1")
if kakuroProblemNum == 1:
kakuro = csp.kakuro1
elif kakuroProblemNum == 2:
kakuro = csp.kakuro2
elif kakuroProblemNum == 3:
kakuro = csp.kakuro3
elif kakuroProblemNum == 4:
kakuro = csp.kakuro4
else:
kakuro = csp.kakuro1
############### Backtracking ###############
problem = Kakuro(kakuro)
start = time.time()
BT_results = csp.backtracking_search(problem)
end = time.time()
problem.display(BT_results)
print ("BT time: " + str(end-start) + " assigns: " + str(problem.nassigns)+"\n\n")
# ############### Backtracking + MRV ###############
# problem = Kakuro(kakuro)
# start = time.time()
# BT_MRV_results = csp.backtracking_search(problem, select_unassigned_variable=csp.mrv)
# end = time.time()
# problem.display(BT_MRV_results)
# print ("Backtracking + MRV time: " + str(end-start) + " assigns: " + str(problem.nassigns)+"\n\n")
############### Forward check ###############
problem = Kakuro(kakuro)
start = time.time()
FC_results = csp.backtracking_search(problem, inference=csp.forward_checking)
end = time.time()
problem.display(FC_results)
print ("FC time: " + str(end-start) + " assigns: " + str(problem.nassigns)+"\n\n")
############### Forward check + MRV ###############
problem = Kakuro(kakuro)
start = time.time()
FC_MRV_results = csp.backtracking_search(problem, select_unassigned_variable=csp.mrv, inference=csp.forward_checking)
end = time.time()
problem.display(FC_MRV_results)
print ("FC + MRV time: " + str(end-start) + " assigns: " + str(problem.nassigns)+"\n\n")
############### MAC ###############
problem = Kakuro(kakuro)
start = time.time()
MAC_results = csp.backtracking_search(problem, inference=csp.mac)
end = time.time()
problem.display(MAC_results)
print ("MAC time: " + str(end-start) + " assigns: " + str(problem.nassigns)+"\n\n")
# ############### MAC + MRV ###############
# problem = Kakuro(kakuro)
# start = time.time()
# MAC_MRV_results = csp.backtracking_search(problem, select_unassigned_variable=csp.mrv, inference=csp.mac)
# end = time.time()
# problem.display(MAC_MRV_results)
# print ("MAC + MRV time: " + str(end-start) + " assigns: " + str(problem.nassigns)+"\n\n")