sudoku.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. import sys
  2. import copy
  3. from contextlib import suppress
  4. from heapq import heappush, heappop
  5. board = sys.argv[1]
  6. algotype = sys.argv[2]
  7. # import board
  8. with open(board) as file:
  9. board = file.read().splitlines()
  10. board = board[:-1]
  11. # convert to list of list of ints
  12. for l in board:
  13. board[board.index(l)] = list(map(lambda x: int(x), l.split()))
  14. # return a board that is like the board b, but has domains for each element of b (always 1-9)
  15. def genDomains(b):
  16. for row in range(0, 9):
  17. for cell in range(0, 9):
  18. if (b[row][cell] == 0):
  19. b[row][cell] = list(range(1, 10))
  20. return b
  21. # returns True if value is valid
  22. def valid(brd, row, col, val):
  23. # check row
  24. if (val in brd[row]):
  25. return False
  26. # check column
  27. for i in range(0, 9):
  28. if (brd[i][col] == val):
  29. return False
  30. # check "box"
  31. rownum = int(row / 3)
  32. colnum = int(col / 3)
  33. for i in range(rownum * 3, rownum * 3 + 3):
  34. for j in range(colnum * 3, colnum * 3 + 3):
  35. if (brd[i][j] == val):
  36. return False
  37. return True
  38. # naive backtracking solver
  39. def naive(start):
  40. working = copy.deepcopy(start) # this is only "filled in values" and 0s
  41. solution = genDomains(start)
  42. # unassigned will be a list of positions we have to fill
  43. unassigned = []
  44. for i in range(0, 9):
  45. for j in range(0, 9):
  46. if (isinstance(solution[i][j], list)):
  47. unassigned.append((i, j))
  48. assumptions = []
  49. if(len(unassigned) == 0):
  50. return True
  51. # count assignments
  52. count = 0
  53. # while there are unassigned vars, keep going
  54. while(len(unassigned)):
  55. index = unassigned[-1]
  56. success = False
  57. # iterate over all values in the domain list
  58. while solution[index[0]][index[1]]:
  59. i = solution[index[0]][index[1]].pop()
  60. # check if this part of the domain(solution) is valid
  61. if (valid(working, index[0], index[1], i)):
  62. count += 1
  63. if (count >= 10000):
  64. print("took too long")
  65. return False
  66. solution[index[0]][index[1]].append(i) # keep in the domain
  67. working[index[0]][index[1]] = i
  68. assumptions.append(index)
  69. unassigned.pop()
  70. success = True
  71. break
  72. if (success):
  73. continue
  74. else:
  75. # restore domain to full since we failed
  76. solution[index[0]][index[1]] = list(range(1, 10))
  77. working[index[0]][index[1]] = 0
  78. lastdex = assumptions.pop()
  79. solution[lastdex[0]][lastdex[1]].remove(working[lastdex[0]][lastdex[1]])
  80. working[lastdex[0]][lastdex[1]] = 0
  81. unassigned.append(lastdex)
  82. # if we exit without assigning everything, we should have failed
  83. if (unassigned): return False
  84. return working
  85. # returns a board (domains) where inferences are made for the cell at row, col
  86. def infer(solutions, brd, row, col, val):
  87. domains = copy.deepcopy(solutions)
  88. # remove from same row & col
  89. for i in range(0, 9):
  90. if (val in domains[row][i] and i != col):
  91. domains[row][i].remove(val)
  92. if (val in domains[i][col] and i != row):
  93. domains[i][col].remove(val)
  94. # remove for "box"
  95. rownum = int(row / 3)
  96. colnum = int(col / 3)
  97. for i in range(rownum * 3, rownum * 3 + 3):
  98. for j in range(colnum * 3, colnum * 3 + 3):
  99. if (val in domains[i][j] and (i != row and j != col)):
  100. domains[i][j].remove(val)
  101. return domains
  102. # generates domains in a format supporting forward checking
  103. def gen2Domains(b):
  104. for row in range(0, 9):
  105. for cell in range(0, 9):
  106. if (b[row][cell] == 0):
  107. b[row][cell] = list(range(1, 10))
  108. else:
  109. b[row][cell] = [b[row][cell]]
  110. return b
  111. # recursive solver for forward-checking
  112. def solve(working, domains, unassigned, count):
  113. if (not unassigned):
  114. return working
  115. index = unassigned.pop()
  116. # for every value in the domain, check if using it works. if all fail, backtrack.
  117. for i in domains[index[0]][index[1]]:
  118. working[index[0]][index[1]] = i
  119. newdomains = infer(domains, working, index[0], index[1], i)
  120. count += 1
  121. # took too long
  122. if (count >= 10000):
  123. print("took too long")
  124. return False
  125. result = solve(working, newdomains, copy.deepcopy(unassigned), count)
  126. if (result):
  127. return result
  128. else:
  129. continue
  130. return False
  131. # forward checking solver
  132. def forward(start):
  133. working = copy.deepcopy(start) # this is only "filled in values" and 0s
  134. domains = gen2Domains(start)
  135. # unassigned will be a list of positions we have to fill
  136. unassigned = []
  137. for i in range(0, 9):
  138. for j in range(0, 9):
  139. if (len(domains[i][j]) == 9):
  140. unassigned.append((i, j))
  141. # forward-checking on pre-assigned values
  142. for i in range(0, 9):
  143. for j in range(0, 9):
  144. if (working[i][j] != 0):
  145. domains = infer(domains, working, i, j, working[i][j])
  146. return solve(working, domains, unassigned, 0)
  147. # returns size of domain for a given index
  148. def domsize(domains, index):
  149. return (len(domains[index[0]][index[1]]))
  150. # returns the # of 0s that are in the same row, col, or box as index
  151. def related(brd, index):
  152. count = 0
  153. # count 0s in row + col
  154. for i in range(0, 9):
  155. if (brd[index[0]][i] == 0 and i != index[1]):
  156. ++count
  157. if (brd[i][index[1]] == 0 and i != index[0]):
  158. ++count
  159. # count for "box" as well
  160. rownum = int(index[0] / 3)
  161. colnum = int(index[1] / 3)
  162. for i in range(rownum * 3, rownum * 3 + 3):
  163. for j in range(colnum * 3, colnum * 3 + 3):
  164. if (brd[i][j] == 0 and (i != index[0] and j != index[1])):
  165. ++count
  166. return count
  167. # returns the # of constraints that will follow from assigning index with val
  168. def lcv(solutions, index, val):
  169. count = 0
  170. # count 0s in row + col
  171. for i in range(0, 9):
  172. if (val in solutions[index[0]][i] and i != index[1]):
  173. ++count
  174. if (val in solutions[i][index[1]] and i != index[0]):
  175. ++count
  176. # count for "box" as well
  177. rownum = int(index[0] / 3)
  178. colnum = int(index[1] / 3)
  179. for i in range(rownum * 3, rownum * 3 + 3):
  180. for j in range(colnum * 3, colnum * 3 + 3):
  181. if (val in solutions[i][j] and (i != index[0] and j != index[1])):
  182. ++count
  183. return count
  184. # return the correct node + val to try
  185. def genVal(domains, working, unassigned):
  186. # used to track intermediary values
  187. heap = []
  188. superheap = []
  189. bestrating = 1.0
  190. # get the best indices according to domain size
  191. for i in unassigned:
  192. rating = domsize(domains, i) / 9.0
  193. if (rating < bestrating):
  194. bestrating = rating
  195. heap = [i]
  196. elif (rating == bestrating):
  197. heap.append(i)
  198. # get the best indices according to degree(related cells)
  199. bestrating = 1
  200. for i in heap:
  201. rating = related(working, i) / 27.0
  202. if (rating < bestrating):
  203. bestrating = rating
  204. superheap = [i]
  205. elif (rating == bestrating):
  206. superheap.append(i)
  207. index = superheap[0]
  208. bestrating = 27
  209. val = working[index[0]][index[1]]
  210. # get best values according to LCV
  211. for i in domains[index[0]][index[1]]:
  212. rating = lcv(domains, index, i)
  213. if (rating <= bestrating):
  214. bestrating = rating
  215. val = i
  216. return (index, val)
  217. # recursive solver that uses heuristics to decide what node to explore
  218. def solveh(working, domains, unassigned, count):
  219. if (not unassigned):
  220. return working
  221. # while there are unassigned values keep trying
  222. while(unassigned):
  223. # get next value using heuristics, remove this node from assigned
  224. nextThing = genVal(domains, working, unassigned)
  225. index = nextThing[0]
  226. val = nextThing[1]
  227. working[index[0]][index[1]] = val
  228. unassigned.remove(index)
  229. # check for invalidated nodes (empty domain)
  230. flag = True
  231. result = False
  232. newdomains = infer(domains, working, index[0], index[1], val)
  233. for i in range(0, 9):
  234. for j in range(0, 9):
  235. if (not domains[i][j]):
  236. flag = False
  237. count += 1
  238. # took too long
  239. if (count >= 10000):
  240. print("took too long")
  241. return False
  242. # success! recurse
  243. if (flag): result = solveh(working, newdomains, copy.deepcopy(unassigned), count)
  244. if (result):
  245. return result
  246. elif (len(domains[index[0]][index[1]]) > 1): # remove from domain, keep going
  247. working[index[0]][index[1]] = 0
  248. domains[index[0]][index[1]].remove(val)
  249. unassigned.append(index)
  250. else: # no values worked :( return false
  251. return False
  252. # forward checking solver with heuristics
  253. def heuristic(start):
  254. working = copy.deepcopy(start) # this is only "filled in values" and 0s
  255. domains = gen2Domains(start)
  256. # unassigned will be a list of positions we have to fill
  257. unassigned = []
  258. for i in range(0, 9):
  259. for j in range(0, 9):
  260. if (len(domains[i][j]) == 9):
  261. unassigned.append((i, j))
  262. # initial inferences
  263. for i in range(0, 9):
  264. for j in range(0, 9):
  265. if (working[i][j] != 0):
  266. domains = infer(domains, working, i, j, working[i][j])
  267. return solveh(working, domains, unassigned, 0)
  268. def main():
  269. print("###########")
  270. print(*board, sep='\n')
  271. print("##########")
  272. if (algotype == str(0)):
  273. result = naive(board)
  274. elif (algotype == str(1)):
  275. result = forward(board)
  276. elif (algotype == str(2)):
  277. result = heuristic(board)
  278. else:
  279. print("No valid algorithm selected. RIP.")
  280. if (not result):
  281. print("No board to print")
  282. return
  283. print("###########")
  284. print(*result, sep='\n')
  285. print("##########")
  286. main()