Browse Source

tracking percentage of good solutions now, changed solution cost to relative quality.

tarfeef101 6 years ago
parent
commit
887e9deeb3
1 changed files with 12 additions and 3 deletions
  1. 12 3
      a2/tsp_local.py

+ 12 - 3
a2/tsp_local.py

@@ -5,10 +5,12 @@ from heapq import heappush, heappop
 from random import shuffle, sample, randint, SystemRandom
 import matplotlib.pyplot as plt
 
+# globals to contain reference solutions
 neos14 = [318, 324, 336, 319, 351, 311, 272, 361, 274, 322]
 neos15 = [313, 318, 281, 324, 378, 291, 348, 342, 353, 325]
 neos16 = [404, 353, 361, 349, 358, 344, 373, 355, 243, 330]
 neos36 = 464
+neos = {14: neos14, 15: neos15, 16: neos16, 36: neos36}
 
 # returns a shuffled version of the cities list while maintaining the first & last elements
 def rand_order(cities):
@@ -255,26 +257,33 @@ def main():
     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]
+                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)
             
     figure, axes = plt.subplots(3, 1, True)
             
@@ -283,8 +292,8 @@ def main():
         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 Cost/Distance", color = 'r')
-        axis_twin.set_ylabel("Distance in units", color = 'r')
+        axis_twin.plot(range(1, 11), solution_scores[i], label = "Solution Quality", color = 'r')
+        axis_twin.set_ylabel("Solution Quality (dist / reference)", color = 'r')
         #axes[i].legend()
         #axis_twin.legend()