Browse Source

added give-up mechanic

Tareef 6 years ago
parent
commit
bd40993bd5
1 changed files with 26 additions and 7 deletions
  1. 26 7
      a1/sudoku.py

+ 26 - 7
a1/sudoku.py

@@ -60,6 +60,9 @@ def naive(start):
                     
     if(len(unassigned) == 0):
         return True
+        
+    # count assignments
+    count = 0
     
     # while there are unassigned vars, keep going
     while(len(unassigned)):
@@ -69,6 +72,11 @@ def naive(start):
         # iterate over all values in the domain list
         while solution[index[0]][index[1]]:
             i = solution[index[0]][index[1]].pop()
+            count += 1
+            # took too long
+            if (count >= 10,000):
+              print("took too long")
+              return False
             # check if this part of the domain(solution) is valid
             if (valid(working, index[0], index[1], i)):
                 solution[index[0]][index[1]].append(i) # keep in the domain
@@ -130,17 +138,22 @@ def gen2Domains(b):
 
 
 # recursive solver for forward-checking
-def solve(working, domains, unassigned):
+def solve(working, domains, unassigned, count):
     if (not unassigned):
         return working
-    
+
     index = unassigned.pop()
     
     # for every value in the domain, check if using it works. if all fail, backtrack.
     for i in domains[index[0]][index[1]]:
         working[index[0]][index[1]] = i
+        count += 1
+        # took too long
+        if (count >= 10,000):
+          print("took too long")
+          return False
         newdomains = infer(domains, working, index[0], index[1], i)
-        result = solve(working, newdomains, copy.deepcopy(unassigned))
+        result = solve(working, newdomains, copy.deepcopy(unassigned), count)
         if (result):
             return result
         else:
@@ -165,7 +178,7 @@ def forward(start):
             if (working[i][j] != 0):
                 domains = infer(domains, working, i, j, working[i][j])
             
-    return solve(working, domains, unassigned)
+    return solve(working, domains, unassigned, 0)
 
 
 # returns size of domain for a given index
@@ -258,7 +271,7 @@ def genVal(domains, working, unassigned):
 
 
 # recursive solver that uses heuristics to decide what node to explore
-def solveh(working, domains, unassigned):
+def solveh(working, domains, unassigned, count):
     if (not unassigned):
         return working
     
@@ -270,6 +283,12 @@ def solveh(working, domains, unassigned):
         val = nextThing[1]
         working[index[0]][index[1]] = val
         unassigned.remove(index)
+        
+        count += 1
+        # took too long
+        if (count >= 10,000):
+          print("took too long")
+          return False
     
         # check for invalidated nodes (empty domain)
         flag = True
@@ -281,7 +300,7 @@ def solveh(working, domains, unassigned):
                     flag = False
         
         # success! recurse
-        if (flag): result = solveh(working, newdomains, copy.deepcopy(unassigned))
+        if (flag): result = solveh(working, newdomains, copy.deepcopy(unassigned), count)
         if (result):
             return result
         elif (len(domains[index[0]][index[1]]) > 1): # remove from domain, keep going
@@ -309,7 +328,7 @@ def heuristic(start):
             if (working[i][j] != 0):
                 domains = infer(domains, working, i, j, working[i][j])
             
-    return solveh(working, domains, unassigned)
+    return solveh(working, domains, unassigned, 0)
 
 
 def main():