Explorar o código

its still broken :(

tarfeef101 %!s(int64=6) %!d(string=hai) anos
pai
achega
245452679d
Modificáronse 1 ficheiros con 27 adicións e 21 borrados
  1. 27 21
      a1/sudoku.py

+ 27 - 21
a1/sudoku.py

@@ -133,7 +133,7 @@ def gen2Domains(b):
 # recursive solver for forward-checking
 def solve(working, domains, unassigned, count):
     if (not unassigned):
-        return (working, count)
+        return (True, count)
 
     index = unassigned.pop()
     
@@ -145,23 +145,25 @@ def solve(working, domains, unassigned, count):
         # took too long
         if (count >= 10000):
           print("took too long")
-          return False
+          return (True, 10000)
 
         # check for invalidated nodes (empty domains)
         flag = True
         result = False
         for i in range(0, 9):
           for j in range(0, 9):
-            if (not newdomains[i][j]):
+            if (len(newdomains[i][j]) <= 0):
               flag = False
         
         if (flag): result = solve(working, newdomains, copy.deepcopy(unassigned), count)
-        if (result):
+        if (not result):
+          return (False, count)
+        if (result[0]):
             return result
         else:
-            continue
+            count = result[1]
     
-    return False
+    return (False, count)
 
 # forward checking solver
 def forward(start):
@@ -181,8 +183,9 @@ def forward(start):
                 domains = infer(domains, working, i, j, working[i][j])
             
     result = solve(working, domains, unassigned, 0)
-    if (result): return result[1]
-    else: return 10000
+    return result[1]
+    #if (result[0]): return result[1]
+    #else: return 10000
 
 
 # returns size of domain for a given index
@@ -277,7 +280,7 @@ def genVal(domains, working, unassigned):
 # recursive solver that uses heuristics to decide what node to explore
 def solveh(working, domains, unassigned, count):
     if (not unassigned):
-        return (working, count)
+        return (True, count)
     
     # while there are unassigned values keep trying
     while(unassigned):
@@ -301,18 +304,21 @@ def solveh(working, domains, unassigned, count):
         # took too long
         if (count >= 10000):
           print("took too long")
-          return False
+          return (False, count)
 
         # success! recurse
         if (flag): result = solveh(working, newdomains, copy.deepcopy(unassigned), count)
-        if (result):
+        if (not result): pass
+        elif (result[0]):
             return result
         elif (len(domains[index[0]][index[1]]) > 1): # remove from domain, keep going
             working[index[0]][index[1]] = 0
             domains[index[0]][index[1]].remove(val)
             unassigned.append(index)
+            if (flag): count = result[1]
         else: # no values worked :( return false
-            return False
+            if (flag): return (False, result[1])
+            return (False, count)
 
 
 # forward checking solver with heuristics
@@ -333,7 +339,7 @@ def heuristic(start):
                 domains = infer(domains, working, i, j, working[i][j])
             
     result = solveh(working, domains, unassigned, 0)
-    if (result): return result[1]
+    if (result[0]): return result[1]
     else: return 10000
 
 
@@ -345,9 +351,9 @@ def main():
     cverages = []
     
     for i in range(1, 72):
-        #avgA = 0
+        avgA = 0
         avgB = 0
-        #avgC = 0
+        avgC = 0
         for j in range(1, 11):
             filepath = "sudoku_problems/" + str(i) + "/" + str(j) + ".sd"
             
@@ -360,18 +366,18 @@ def main():
             for l in board:
                 board[board.index(l)] = list(map(lambda x: int(x), l.split()))
             
-            #avgA += naive(copy.deepcopy(board));print(i, j)
+            avgA += naive(copy.deepcopy(board));print(i, j)
             avgB += forward(copy.deepcopy(board));print(i, j)
-            #avgC += heuristic(copy.deepcopy(board));print(i, j)
+            avgC += heuristic(copy.deepcopy(board));print(i, j)
             
-        #averages.append(avgA / 10.0)
+        averages.append(avgA / 10.0)
         bverages.append(avgB / 10.0)
-        #cverages.append(avgC / 10.0)
+        cverages.append(avgC / 10.0)
         
     figure, axes = plt.subplots(1, 1, True)
-    #axes.plot(range(1, 72), averages, label='Naive Algorithm')
+    axes.plot(range(1, 72), averages, label='Naive Algorithm')
     axes.plot(range(1, 72), bverages, label='Forward-Checking Algorithm')
-    #axes.plot(range(1, 72), cverages, label='Heuristics')
+    axes.plot(range(1, 72), cverages, label='Heuristics')
     axes.legend()
     plt.xlabel("Number of Initial Valued Filled In")
     plt.ylabel("Average Number of Variable Assignments in 10 Runs")