# Uncomment and/or move this to generate plots
	# get_plots(history, graph=None, best=bestpath, 
	#           best_dist=distance(graph, bestpath))

	graph = parse_xml_graph('resources/fri26.xml')

	# Square matrix
	assert graph.shape[0] == graph.shape[1]
	size = graph.shape[0]

	# Hardcoded best path to validate distance calculations, zero-indexed
	bestpath = np.asarray([x-1 for x in [1, 25, 24, 23, 26, 22, 21, 17, 18,
                                          20, 19, 16, 11, 12, 13, 15, 14, 10,
                                          9, 8, 7, 5, 6, 4, 3, 2]])
	print "Best path: " + str(bestpath)
	print "Best path length: " + str(distance(graph, bestpath)) + "\n"

	# Initial values, probably need to be tuned
	initial_path = np.random.permutation(size)
	print "Initial path: " + str(initial_path)
	print "Initial path length: " + str(distance(graph, initial_path)) + "\n"
	initial_temp = 2.
	cool = 0.9
	nbefore = 480
	nswaps = 1
	reheat = np.sqrt(5)

	# Run simulated annealing
	print "\nSimulated Annealing\n"
	for iterr in [10**x for x in [3, 4, 5]]:
		with Timer() as t:
	# Uncomment and/or move this to generate plots
	# get_plots(history, graph=None, best=bestpath, 
	#           best_dist=distance(graph, bestpath))

	graph = parse_xml_graph('resources/fri26.xml')

	# Square matrix
	assert graph.shape[0] == graph.shape[1]
	size = graph.shape[0]

	# Hardcoded best path to validate distance calculations, zero-indexed
	bestpath = np.asarray([x-1 for x in [1, 25, 24, 23, 26, 22, 21, 17, 18,
                                          20, 19, 16, 11, 12, 13, 15, 14, 10,
                                          9, 8, 7, 5, 6, 4, 3, 2]])
	print "Best path: " + str(bestpath)
	print "Best path length: " + str(distance(graph, bestpath)) + "\n"

	# Initial values, probably need to be tuned
	initial_path = np.random.permutation(size)
	print "Initial path: " + str(initial_path)
	print "Initial path length: " + str(distance(graph, initial_path)) + "\n"
	nbefore = 100
	nswaps = 3
		
	# Reuse copy of initial path for all trials
	nsystems = 4
	initial_paths = []
	for i in xrange(nsystems):
	    initial_paths.append(np.asarray(initial_path))
	initial_temps = [1., np.sqrt(5), np.sqrt(5)**2, np.sqrt(5)**3]
import numpy as np
import matplotlib.pyplot as plt

from annealing_helper_functions import distance
from utils.xml_parse import parse_xml_graph

#### This function is a wrapper used to plot data that was created in multiple runs to compare performance.  

graph = parse_xml_graph('resources/fri26.xml')
bestpath = np.asarray([x-1 for x in [1, 25, 24, 23, 26, 22, 21, 17, 18, 20, 19, 16, 11, 12, 13, 15, 14, 10, 9, 8, 7, 5, 6, 4, 3, 2]])
optimum = distance(graph, bestpath)

time_hist_ppt = np.load('../SavedResults/time_hist_ppt.npy')
time_hist_spt = np.load('../SavedResults/time_hist_spt.npy')
time_hist_sa  = np.load('../SavedResults/time_hist_sa.npy')

dist_hist_ppt = np.load('../SavedResults/dist_hist_ppt.npy')
dist_hist_spt = np.load('../SavedResults/dist_hist_spt.npy')
dist_hist_sa  = np.load('../SavedResults/dist_hist_sa.npy')

plt.plot(time_hist_ppt, dist_hist_ppt, color='b', label = "Parallel Parallel")
plt.plot(time_hist_spt, dist_hist_spt, color='g', label = "Serial Parallel")
plt.plot(time_hist_sa , dist_hist_sa,  color='r', label = "Simulated Annealing")

plt.xlim([0, 
	np.min([np.max(time_hist_sa), 
	np.max(time_hist_spt), 
	np.max(time_hist_ppt)])])
plt.ylim([optimum - 50, dist_hist_ppt[0]])

axis_font = {'fontname':'Arial', 'size':30}
if __name__ == '__main__':
	# Necessary to make multiprocessing work on Windows:
	freeze_support()

    # Load graph
	graph = parse_xml_graph('resources/fri26.xml')
	
	#Square matrix
	assert graph.shape[0] == graph.shape[1]
	size = graph.shape[0]

	# Hardcoded best path to validate distance calculations, zero-indexed
	bestpath = np.asarray([x-1 for x in [1, 25, 24, 23, 26, 22, 21, 17, 18, 20, 19, 16, 11, 12, 13, 15, 14, 10, 9, 8, 7, 5, 6, 4, 3, 2]])
	print "Best path: " + str(bestpath)
	print "Best path length: " + str(distance(graph, bestpath)) + "\n"

	#Initial values
	num_processes = 4
	initial_Xs = [np.random.permutation(size) for i in range(num_processes)]
	ratios = [np.sqrt(i) for i in range(1,7,1)] #Ratio of temperatures between processes
	iterr=10000
	nswaps = 3
	nbefore = 100

	# Number of runs per parameter combination:
	runs = 30

	with Timer() as t:

		# Find ratio of temperatures that gives the best average results
import numpy as np

from annealing_helper_functions import distance, changepath, simulated_annealing, parallel_tempering
from xml_parse import parse_xml_graph
from timer import Timer

graph = parse_xml_graph('fri26.xml')
#Square matrix
assert graph.shape[0] == graph.shape[1]
size = graph.shape[0]

# Hardcoded best path to validate distance calculations, zero-indexed
bestpath = np.asarray([x-1 for x in [1, 25, 24, 23, 26, 22, 21, 17, 18, 20, 19, 16, 11, 12, 13, 15, 14, 10, 9, 8, 7, 5, 6, 4, 3, 2]])
print "Best path: " + str(bestpath)
print "Best path length: " + str(distance(graph, bestpath))

#Initial values, probably need to be tuned
initial_path = np.random.permutation(size)
initial_temp = 2.
cool = 0.9
reanneal = 100
nswaps = 3

# Run simulated annealing
print "\nSimulated Annealing\n"
for iterr in [10**x for x in [3,4,5]]:
	with Timer() as t:
		solution, history = simulated_annealing(graph, distance, initial_path, 
												initial_temp, cool, reanneal, iterr, 
												changepath, nswaps)
	print "\nIterations: {:.2E}".format(iterr)