|
@@ -1,6 +1,7 @@
|
|
|
import sys
|
|
|
import copy
|
|
|
import math
|
|
|
+import time
|
|
|
from heapq import heappush, heappop
|
|
|
from random import shuffle, sample, randint, SystemRandom
|
|
|
import matplotlib.pyplot as plt
|
|
@@ -158,6 +159,7 @@ def tenzing_norgay(cities):
|
|
|
# returns the hill climbing solution w/ x random restarts for a given input
|
|
|
# if you want x restarts, call with x + 1
|
|
|
def reinhold_messner(cities, x):
|
|
|
+ start = time.process_time()
|
|
|
curcost = length(cities)
|
|
|
curpath = copy.deepcopy(cities)
|
|
|
bestcost = length(cities)
|
|
@@ -178,7 +180,8 @@ def reinhold_messner(cities, x):
|
|
|
curcost = length(curpath)
|
|
|
x -= 1
|
|
|
|
|
|
- return(bestpath, bestcost)
|
|
|
+ end = time.process_time()
|
|
|
+ return(bestpath, bestcost, end - start)
|
|
|
|
|
|
|
|
|
# returns true or false randomly, based on the distribution e^(delta / temp)
|
|
@@ -357,7 +360,7 @@ def sir_edmund_hillary_graph():
|
|
|
plt.close()
|
|
|
|
|
|
|
|
|
-# function to generate PDF results for basic hill climbing
|
|
|
+# function to generate PDF results for basic hill climbing w/ tabu and sideways moves
|
|
|
def tenzing_norgay_graph():
|
|
|
plt.ioff()
|
|
|
plt.switch_backend('agg')
|
|
@@ -459,10 +462,74 @@ def tenzing_norgay_graph():
|
|
|
pdf.savefig()
|
|
|
plt.close()
|
|
|
|
|
|
+
|
|
|
+# function to generate PDF results for basic hill climbing w/ random restarts
|
|
|
+def reinhold_messner_graph():
|
|
|
+ plt.ioff()
|
|
|
+ plt.switch_backend('agg')
|
|
|
+ solution_time = []
|
|
|
+ solution_scores = []
|
|
|
+
|
|
|
+ # num of cities
|
|
|
+ for i in range(14, 17):
|
|
|
+ times = []
|
|
|
+ solution_times.append(times)
|
|
|
+ scores = []
|
|
|
+ solution_scores.append(scores)
|
|
|
+
|
|
|
+ # num of instances
|
|
|
+ for j in range(1, 3):
|
|
|
+ filepath = "tsp_problems/" + str(i) + "/instance_" + str(j) + ".txt"
|
|
|
+ prob_times = []
|
|
|
+ times.append(prob_times)
|
|
|
+ prob_scores = []
|
|
|
+ scores.append(prob_scores)
|
|
|
+
|
|
|
+ # num of repeats
|
|
|
+ for k in range(1, 16):
|
|
|
+ ktimes = []
|
|
|
+ prob_times.append(ktimes)
|
|
|
+ kscores = []
|
|
|
+ prob_scores.append(kscores)
|
|
|
+
|
|
|
+ runtime = 0
|
|
|
+ qual = 0
|
|
|
+ # run problem 100 times
|
|
|
+ for j in range(0, 100):
|
|
|
+ # path, cost, runtime returned
|
|
|
+ result = solver(filepath, 3, k)
|
|
|
+ runtime += result[2]
|
|
|
+ qual += (result[1] / neos[i][j - 1])
|
|
|
+
|
|
|
+ ktimes.append(runtime / 100.0)
|
|
|
+ kscores.append(qual / 100.0)
|
|
|
+
|
|
|
+ with PdfPages("hill_climbing_restarts.pdf") as pdf:
|
|
|
+ figure, axes = plt.subplots(3, 2, True)
|
|
|
+ figure.suptitle("Hill Climbing - Restarts")
|
|
|
+
|
|
|
+ for i in range(0, 3):
|
|
|
+ for j in range(0, 2):
|
|
|
+ axes[i][j].plot(range(1, 16), solution_times[i][j], color = 'b')
|
|
|
+ axes[i][j].set_xlabel("Problem Instance w/ " + str(i + 14) + " cities")
|
|
|
+ axes[i][j].set_ylabel("Time Taken", color = 'b')
|
|
|
+ axis_twin = axes[i].twinx()
|
|
|
+ axis_twin.plot(range(1, 16), solution_scores[i][j], 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()
|
|
|
+
|
|
|
+
|
|
|
|
|
|
# main function that will call needed graphing function(s)
|
|
|
def main():
|
|
|
#sir_edmund_hillary_graph()
|
|
|
- tenzing_norgay_graph()
|
|
|
+ #tenzing_norgay_graph()
|
|
|
+ reinhold_messner_graph()
|
|
|
|
|
|
main()
|