This repository was archived by the owner on Dec 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver.py
More file actions
460 lines (343 loc) · 12.1 KB
/
Copy pathdriver.py
File metadata and controls
460 lines (343 loc) · 12.1 KB
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
# Project: Sudoku Solving AI proj 6
# Program name: driver.py
# Author: Greg Tystahl
# Date Created: 11/19/2020
# Purpose: Solve the puzzle using both AC3 and FC and putting that result in a finish file
# How to run: python3 driver.py. The name of the input file needs to be called in.txt and the name of the output
# file will be output.txt
import queue
import sys
import copy
def createConstraints(mat):
# This creates the contraints so I dont have to.
# This the list that will holds the constraints to be returned
ret = []
# This goes through each row of the matrix
for a in range(9):
# This will hold each item in the rows contraints
row = []
# Goes through each item in the row
for b in range(9):
# Grabs the item
item = mat[a][b]
# creates a list that will hold the items constraints
col = []
# The creates the row constraints and adds it to the col list if it isn't already in there
for item2 in mat[a]:
if not item2 in col and item2 != item:
col.append(item2)
# Adds the column constraints to col if they are not in there
for i in range(9):
item2 = mat[i][b]
if not item2 in col and item2 != item:
col.append(item2)
# Below is for the box it is in
# This x and y determine the cordinate of the box
# [0,0][1,0][2,0]
# [0,1][1,1][2,1]
# [0,2][1,2][2,2]
y = a // 3
x = b // 3
# This goes through each row of the box
for c in range(3):
# This goes through each item of the box
for d in range(3):
# This gets the item of the box
item2 = mat[(y * 3) + c][(x * 3) + d]
# If the item is not in the col list, add it
if not item2 in col and item2 != item:
col.append(item2)
# Add all of the constraints to the row
row.append(col)
# Add the row to the returning matrix
ret.append(row)
# Return the list made
return ret
def createMatrix():
# This creates the matrix.
# Letters in the form of a string cause I was lazy
letters = "A B C D E F G H I"
# Splits the letters into elements of a list
lets = letters.split(" ")
# This is the variable that will hold the matrix to be returned
ret = []
# Goes through each letter in the list of letters
for let in lets:
# This lst is the current row of the matrix
lst = []
# This goes through 1 - 10 to combine them to the letters
for i in range(1,10):
# Adds the combination to the row
lst.append(let + str(i))
# Adds the row to the matrix
ret.append(lst)
# Returns the finished matrix
return ret
# This creates the matrix of a 9x9. To configure for a different size, you must change a lot of things to scale
variable_matrix = createMatrix()
# This creates the constraints for the matrix given above. Configured for a 9x9
constraints = createConstraints(variable_matrix)
# For the inclass code all I changed was to scale what we did so that all the functions work for a 9x9
# Beginning of class stuff:
def BTS_search(csp):
return BTS(csp)
def BTS(csp):
if doneQ(csp):
return csp
var=MRV(csp)
if var=='':
return None
domains=csp[var][0]
for i in domains:
tempCSP=copy.deepcopy(csp)
tempCSP[var][0]=[i]
forwardCheck(tempCSP)
if not collisionTest(tempCSP,var):
result=BTS(tempCSP)
if result!=None:
return result
return None
def collisionTest(csp,val):
collisions=0
domain1=csp[val][0]
if len(domain1)==1:
constraints=csp[val][1]
for k in constraints:
domain2=csp[k][0]
if len(domain2)==1:
if domain1[0]==domain2[0]:
collisions=collisions+1
if collisions==0:
return False
return True
def DomainsComplete(csp):
for i in range(9):
for j in range(9):
key=variable_matrix[i][j]
domain=csp[key][0]
if len(domain)>1:
return False
return True
def generate_domain(board):
domain=[[],[],[],[],[],[],[],[],[]]
temp=list(board)
temp2=[]
for i in temp:
temp2.append(int(i))
for i in range(9):
for j in range(9):
domain[i].append([temp2[9*i+j]])
for i in range(9):
for j in range(9):
if domain[i][j][0]==0:
domain[i][j]=[1,2,3,4,5,6,7,8,9]
return domain
def forwardCheck(csp):
for i in range(9):
for j in range(9):
key=variable_matrix[i][j]
domainDi=csp[key][0]
if len(domainDi)!=1:
constraints=csp[key][1]
for keyJ in constraints:
domainDj=csp[keyJ][0]
if len(domainDj)==1 and len(domainDi)>1:
try:
csp[key][0].remove(domainDj[0])
except:
pass
def doneQ(csp):
total=0
for i in range(9):
for j in range(9):
tmp=variable_matrix[i][j][0]
if len(csp[variable_matrix[i][j]][0])==1:
total=total+1
if total==81 and not totalColTest(csp):
return True
return False
def totalColTest(csp):
collisions=0
for i in range(9):
for j in range(9):
val=variable_matrix[i][j]
if collisionTest(csp,val):
return True
return False
# End of class stuff
def Asearch(csp):
# This function is the same as BTS above but is changed to work for AC_3 instead
if doneQ(csp):
return csp
var = MRV(csp)
if var == '':
return None
domains = csp[var][0]
for i in domains:
tempCSP = copy.deepcopy(csp)
tempCSP[var][0] = [i]
# Here is where the AC_3 replaces the forward checking
AC_3(csp)
if not collisionTest(tempCSP, var):
# The name was also changed here to Asearch. Ran into a problem here earlier
result = Asearch(tempCSP)
if result != None:
return result
return None
def AC_3(csp):
# This is the AC_3 function. It uses pairs of items to filter out the numbers that cannot go in certain places
# This and revise follow the format of the book
# This queue holds the pairs of items
q = queue.Queue()
# This creates the arcs or as I have called them pairs for everything
createArcs(csp, q)
# While the queue is not empty
while not q.empty():
# Get the next pair of items
(xi, xj) = q.get()
# Revises the possible values of xi by the possibles of xj
if revise(csp,xi,xj):
# If there was a revision, check to make sure the new possibles of xi is not empty
if len(csp[xi][0]) == 0:
# If it is then there is a conflict. Return False
return False
# Go through all of the connections and add them back to the queue
for xk in csp[xi][1]:
if xk != xj:
q.put((xk, xi))
# Return true if it cannot revise any longer and there are no conflicts
return True
def revise(csp, xi, xj):
# This revises the possible values of xi based on the possible values of xj
# This is the variable that checks to see if there was a revision
revised = False
# This is the list that will hold the revised values of xi
lst = []
# Goes through each possible value of xi
for num1 in csp[xi][0]:
# This is a bool that checks to see if that value has a possible connection
good = False
# Goes through each possible value of xj
for num2 in csp[xj][0]:
# If there is a possible connection
if num1 != num2:
# Set good to True
good = True
# leave this for loop to save time
break
# If there is a connection
if good:
# Add the number back into xi possibles
lst.append(num1)
else:
#If there is not a connection, dont add it back and set revised to True
revised = True
# Set the possibles of xi to lst
csp[xi][0] = lst
# Return the state of revised
return revised
def createArcs(csp, q):
# This creates connections of all items in the matrix. Only used at the start to fill the queue
# For every item in the matrix
for key in csp:
# For each constraint of the item
for con in csp[key][1]:
# Add the pair to the queue
q.put((key, con))
# In-class stuff again:
def MRV(csp):
mrv=1000
minkey=''
for i in range(9):
for j in range(9):
key=variable_matrix[i][j]
domain=csp[key][0]
d1=len(domain)
if d1<mrv and d1!=1:
mrv=d1
minkey=key
return minkey
def display(csp):
for i in range(9):
line=[]
for j in range(9):
key=variable_matrix[i][j]
domainDi=csp[key][0]
line.append(domainDi)
print(str(line))
# End of in class stuff
def main():
# This clears out the old output
f = open("output.txt", "w")
f.close()
# This opens the input file
f = open("in.txt", "r")
# This goes through each line of the input file
for line in f:
# This gets rid of the newline
line = line.rstrip()
# Creates the csp
csp={}
# Gets the domain of the puzzle
domain=generate_domain(line)
# Fills the csp with the values needed
for i in range(9):
for j in range(9):
csp.update({variable_matrix[i][j]:[domain[i][j],constraints[i][j]]})
# Creates a copy for the forward checking portion
csp2 = copy.deepcopy(csp)
# Gets the solution based on AC3
solution = Asearch(csp)
# If the solution is not none
if solution:
# Creates the line to be added to the output
nl = ""
# Goes through the matrix and gets the values and adds it to nl
for key in solution:
nl += str(solution[key][0][0])
# Adds the AC3 label
nl += " AC3"
# Prints it to the screen to show its working
print(nl)
#Add it to output
f2 = open("output.txt", "a")
f2.write(nl + "\n")
f2.close()
else:
# If it is none, set nl to the old line and add FAIL
nl = line + " FAIL"
# Print the line to show it failed
print(nl)
# Add the fail to the output
f2 = open("output.txt", "a")
f2.write(nl + "\n")
f2.close()
# Gets the solution of forward checking
solution=BTS_search(csp2)
# If the solution is not none
if solution:
# Creates the line to be added to the output
nl = ""
# Goes through the matrix and gets the values and adds it to nl
for key in solution:
nl += str(solution[key][0][0])
# Adds the AC3 label
nl += " FC"
# Prints it to the screen to show its working
print(nl)
# Add it to output
f2 = open("output.txt", "a")
f2.write(nl + "\n")
f2.close()
else:
# If it is none, set nl to the old line and add FAIL
nl = line + " FAIL"
# Print the line to show it failed
print(nl)
# Add the fail to the output
f2 = open("output.txt", "a")
f2.write(nl + "\n")
f2.close()
f.close()
if __name__=="__main__":
main()