Kaynağa Gözat

in theory plotting shit

Tareef 6 yıl önce
ebeveyn
işleme
14071874ee
1 değiştirilmiş dosya ile 37 ekleme ve 34 silme
  1. 37 34
      a1/sudoku.py

+ 37 - 34
a1/sudoku.py

@@ -1,19 +1,7 @@
-import sys
 import copy
 from contextlib import suppress
 from heapq import heappush, heappop
-
-board = sys.argv[1]
-algotype = sys.argv[2]
-
-# import board
-with open(board) as file:
-    board = file.read().splitlines()
-    board = board[:-1]
-
-# convert to list of list of ints
-for l in board:
-    board[board.index(l)] = list(map(lambda x: int(x), l.split()))
+import matplotlib.pyplot as plt
 
 # return a board that is like the board b, but has domains for each element of b (always 1-9)
 def genDomains(b):
@@ -337,26 +325,41 @@ def heuristic(start):
 
 
 def main():
-    print("###########")
-    print(*board, sep='\n')
-    print("##########")
-
-    if (algotype == str(0)):
-      result = naive(board)
-    elif (algotype == str(1)):
-      result = forward(board)
-    elif (algotype == str(2)):
-      result = heuristic(board)
-    else:
-      print("No valid algorithm selected. RIP.")
-
-    if (not result or not result[0]):
-      print("No board to print")
-      return
-
-    print("count: ", result[1])
-    print("###########")
-    print(*result[0], sep='\n')
-    print("##########")
+    averages = []
+    bverages = []
+    cverages = []
+    
+    for i in range(0, 72):
+        avgA = 0
+        avgB = 0
+        avgC = 0
+        for j in range(1, 11):
+            filepath = "sudoku_problems/" + i + "/" + j + ".sd"
+            
+            # import board
+            with open(filepath) as file:
+                board = file.read().splitlines()
+                board = board[:-1]
+
+            # convert to list of list of ints
+            for l in board:
+                board[board.index(l)] = list(map(lambda x: int(x), l.split()))
+            
+            avgA += naive(board)
+            avgB += forward(board)
+            avgC += heuristic(board)
+            
+        averages.append(avgA / 10.0)
+        bverages.append(avgB / 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), bverages, label='Forward-Checking Algorithm')
+    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")
+    plt.savefig("graph.pdf")
     
 main()