瀏覽代碼

trying to split graphs onto 2 pages

Tareef 6 年之前
父節點
當前提交
49f3842b9d
共有 1 個文件被更改,包括 28 次插入16 次删除
  1. 28 16
      a2/tsp_local.py

+ 28 - 16
a2/tsp_local.py

@@ -4,6 +4,7 @@ import math
 from heapq import heappush, heappop
 from heapq import heappush, heappop
 from random import shuffle, sample, randint, SystemRandom
 from random import shuffle, sample, randint, SystemRandom
 import matplotlib.pyplot as plt
 import matplotlib.pyplot as plt
+from matplotlib.backends.backend_pdf import PdfPages
 
 
 # globals to contain reference solutions
 # globals to contain reference solutions
 neos14 = [318, 324, 336, 319, 351, 311, 272, 361, 274, 322]
 neos14 = [318, 324, 336, 319, 351, 311, 272, 361, 274, 322]
@@ -285,22 +286,33 @@ def main():
             scores.append(prob_scores / 100.0)
             scores.append(prob_scores / 100.0)
             good_sols.append(good_solutions)
             good_sols.append(good_solutions)
             
             
-    figure, axes = plt.subplots(3, 2, True)
+    with PdfPages("hill_climbing.pdf") as pdf:
+        figure, axes = plt.subplots(3, 1, True)
+        plt.title("Hill Climbing")
+        
+        for i in range(0, 3):
+            axes[i].plot(range(1, 11), solution_steps[i], label = "Steps to Solution", color = 'b')
+            axes[i].set_xlabel("Problem Instance w/ " + str(i + 14) + " cities")
+            axes[i].set_ylabel("Steps Taken", color = 'b')
+            axis_twin = axes[i].twinx()
+            axis_twin.plot(range(1, 11), solution_scores[i], label = "Solution Quality", color = 'r')
+            axis_twin.set_ylabel("Solution Quality", color = 'r')
+            #axes[i].legend()
+            #axis_twin.legend()
+        pdf.savefig()
+        plt.close()
+        
+        figure, axes = plt.subplots(3, 1, True)
+        plt.title("Hill Climbing Cont.")
+        
+        for i in range(0, 3):
+            axes[i].plot(range(1, 11), good_sol_counts[i], color = 'b')
+            axes[i].set_xlabel("Problem Instance w/ " + str(i + 14) + " cities")
+            axes[i].set_ylabel("% of runs <= NEOS", color = 'b')
             
             
-    for i in range(0, 3):
-        axes[i][0].plot(range(1, 11), solution_steps[i], label = "Steps to Solution", color = 'b')
-        axes[i][0].set_xlabel("Problem Instance w/ " + str(i + 14) + " cities")
-        axes[i][0].set_ylabel("Steps Taken", color = 'b')
-        axis_twin = axes[i][0].twinx()
-        axis_twin.plot(range(1, 11), solution_scores[i], label = "Solution Quality", color = 'r')
-        axis_twin.set_ylabel("Solution Quality", color = 'r')
-        #axes[i].legend()
-        #axis_twin.legend()
-    for i in range(0, 3):
-        axes[i][1].plot(range(1, 11), good_sol_counts[i], color = 'b')
-        axes[i][1].set_xlabel("Problem Instance w/ " + str(i + 14) + " cities")
-        axes[i][1].set_ylabel("% of runs <= NEOS", color = 'b')
-
-    plt.savefig("hill_climbing.pdf")
+        pdf.savefig()
+        plt.close()
+
+    #plt.savefig("hill_climbing.pdf")
 
 
 main()
 main()