瀏覽代碼

hopefully i can run just the things to generate the graphs i need now, which now include tabu

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

+ 112 - 5
a2/tsp_local.py

@@ -142,7 +142,7 @@ def tenzing_norgay(cities):
             curpath = child[0]
             tabu.add(get_hashable(curpath))
             continue
-        if (child[1] == curcost and laterals < n):
+        if (child[1] == curcost and laterals < len(cities)):
             laterals += 1
             curcost = child[1]
             curpath = child[0]
@@ -250,10 +250,10 @@ def solver(problem, algo, *x):
         result = beck_weathers(cities, x)
     
     return result
-    
 
-# main function to get aggregate data and graph stuff
-def main():
+
+# function to generate PDF results for basic hill climbing
+def sir_edmund_hillary_graph():
     plt.ioff()
     plt.switch_backend('agg')
     solution_steps = []
@@ -354,6 +354,113 @@ def main():
         pdf.savefig()
         plt.close()
 
-    #plt.savefig("hill_climbing.pdf")
+
+# function to generate PDF results for basic hill climbing
+def tenzing_norgay_graph():
+    plt.ioff()
+    plt.switch_backend('agg')
+    solution_steps = []
+    solution_scores = []
+    good_sol_counts = []
+    
+    for i in range(14, 17):
+        steps = []
+        solution_steps.append(steps)
+        scores = []
+        solution_scores.append(scores)
+        good_sols = []
+        good_sol_counts.append(good_sols)
+        
+        for j in range(1, 11):
+            filepath = "tsp_problems/" + str(i) + "/instance_" + str(j) + ".txt"
+            prob_steps = 0
+            prob_scores = 0
+            good_solutions = 0
+            # run problem 100 times
+            for k in range(0, 100):
+                # path, cost, steps returned
+                result = solver(filepath, 1)
+                prob_steps += result[2]
+                prob_scores += (result[1] / neos[i][j - 1])
+                if (result[1] <= neos[i][j- 1]):
+                    good_solutions += 1
+            
+            steps.append(prob_steps / 100.0)
+            scores.append(prob_scores / 100.0)
+            good_sols.append(good_solutions)
+            
+    with PdfPages("hill_climbing_tabu.pdf") as pdf:
+        figure, axes = plt.subplots(3, 1, True)
+        figure.suptitle("Hill Climbing - Tabu")
+        
+        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()
+
+        plt.tight_layout()
+        plt.subplots_adjust(top=0.92)
+        pdf.savefig()
+        plt.close()
+        
+        figure, axes = plt.subplots(3, 1, True)
+        figure.suptitle("Hill Climbing - Tabu 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')
+            
+        plt.tight_layout()
+        plt.subplots_adjust(top=0.92)
+        pdf.savefig()
+        plt.close()
+        
+        # now we can aggregate and replace the old values
+        for i in range(0, 3):
+            tempsteps = 0
+            tempqual = 0
+            tempgood = 0
+            for j in range(0, 10):
+               tempsteps += solution_steps[i][j]
+               tempqual += solution_scores[i][j]
+               tempgood += good_sol_counts[i][j]
+            solution_steps[i] = tempsteps / 10.0
+            solution_scores[i] = tempqual / 10.0
+            good_sol_counts[i] = tempgood / 10.0
+        
+        figure, axes = plt.subplots(3, 1, True)
+        figure.suptitle("Hill Climbing - Tabu Aggegated (Averages)")
+        
+        axes[0].plot(range(14, 17), solution_steps, color = 'b')
+        axes[0].set_xlabel("Problem Instances w/ x cities")
+        axes[0].set_ylabel("Steps Used", color = 'b')
+        axes[0].set_xticks([14, 15, 16])
+        
+        axes[1].plot(range(14, 17), solution_scores, color = 'b')
+        axes[1].set_xlabel("Problem Instances w/ x cities")
+        axes[1].set_ylabel("Solution Quality", color = 'b')
+        axes[1].set_xticks([14, 15, 16])
+        
+        axes[2].plot(range(14, 17), good_sol_counts, color = 'b')
+        axes[2].set_xlabel("Problem Instances w/ x cities")
+        axes[2].set_ylabel("% of runs <= NEOS", color = 'b')
+        axes[2].set_xticks([14, 15, 16])
+            
+        plt.tight_layout()
+        plt.subplots_adjust(top=0.92)
+        pdf.savefig()
+        plt.close()
+        
+
+# main function that will call needed graphing function(s)
+def main():
+    #sir_edmund_hillary_graph()
+    tenzing_norgay_graph()
 
 main()