def time_testing( n, path, data_numbers): results = {} number_of_instances = len(data_numbers) sumBB_simple = [0 for i in range(number_of_instances)] sumBB_sym = [0 for i in range(number_of_instances)] sumBB_asym = [0 for i in range(number_of_instances)] sum_BF = [0 for i in range(number_of_instances)] sum_dp = [0 for i in range(number_of_instances)] results["Problem size"]= [] results["Brute Force"]= [] results["Branch and bound(MST)"] = [] results["Branch and bound(two minimum edges)"] = [] results["Simple branch and bound"] = [] results["Dynamic programming"] =[] sum_index = 0 for z in data_numbers: ts = t.TSP() ts.read_from_file(path+'data{}.txt'.format(z)) # for i in ts.graph_matrix: # print(i) branch_and_bound = b.BB(ts) dp = DP.DP(ts) for i in range(n): start1 = time.time() branch_and_bound.simple_BB() end1 = time.time() sumBB_simple[sum_index]+= end1-start1 start = time.time() branch_and_bound.MST_BB_symmetric() end = time.time() sumBB_sym[sum_index]+=(end-start) start = time.time() branch_and_bound.BB_asymmetric() end = time.time() sumBB_asym[sum_index] += (end-start) start = time.time() length_dp , route_dp= dp.dp_method() end = time.time() sum_dp[sum_index]+=(end-start) if z<13: start = time.time() ts.DFS_based() end = time.time() sum_BF[sum_index]+=(end-start) results["Problem size"].append(z) if z<13: results["Brute Force"].append(sum_BF[sum_index]/n) else: results["Brute Force"].append(None) results["Branch and bound(MST)"].append(sumBB_sym[sum_index]/n) results["Branch and bound(two minimum edges)"].append(sumBB_asym[sum_index]/n) results["Simple branch and bound"].append(sumBB_simple[sum_index]/n) results["Dynamic programming"].append(sum_dp[sum_index]/n) sum_index+=1 return results
import unittest import numpy as np from classes import TSP as t from classes import GA tsp = t.TSP() tsp.read_from_file('data/dataEtap2/data16.txt') gen = GA.GeneticAlgorithm(tsp) population_number = 200 gen.PMX_alg(3, population_number, 0.8, 0.1, "inversion", "roulette", 3, 10, "hybrid") a = [1, 2, 3] b = [1, 0, 3] c = [2, 3, 4] d = [a, b, c] class MyTestCase(unittest.TestCase): def test_parents_len(self): for parent in gen.parents: self.assertEqual(len(parent), gen.graph_size) def test_parents_0idx(self): for parent in gen.parents: self.assertEqual(parent[0], 0) def test_children_0idx(self): for newone in gen.children: self.assertEqual(newone[0], 0) def test_children_pop(self):
def temp_testing(n,path,data_numbers,opt): results = {} number_of_instances = len(data_numbers) sumSA_5000_t = [0 for i in range(number_of_instances)] sumSA_10000_t= [0 for i in range(number_of_instances)] sumSA_20000_t = [0 for i in range(number_of_instances)] sumSA_30000_t= [0 for i in range(number_of_instances)] sumSA_40000_t = [0 for i in range(number_of_instances)] sumSA_50000_t = [0 for i in range(number_of_instances)] sumSA_5000_e = [0 for i in range(number_of_instances)] sumSA_10000_e= [0 for i in range(number_of_instances)] sumSA_20000_e = [0 for i in range(number_of_instances)] sumSA_30000_e= [0 for i in range(number_of_instances)] sumSA_40000_e = [0 for i in range(number_of_instances)] sumSA_50000_e = [0 for i in range(number_of_instances)] results["Problem size"] = [] results["Simulated annealing - 5000 time"] = [] results["Simulated annealing - 10000 time"] = [] results["Simulated annealing - 20000 time"] = [] results["Simulated annealing - 30000 time"] = [] results["Simulated annealing - 40000 time"] = [] results["Simulated annealing - 50000 time"] = [] results["Simulated annealing - 5000 error"] = [] results["Simulated annealing - 10000 error"] = [] results["Simulated annealing - 20000 error"] = [] results["Simulated annealing - 30000 error"] = [] results["Simulated annealing - 40000 error"] = [] results["Simulated annealing - 50000 error"] = [] sum_index = 0 for z in data_numbers: tsp = t.TSP() tsp.read_from_file(path+'data{}.txt'.format(z)) sim_annealing = SA.SA(tsp) for i in range(n): start1 = time.time() dist , rt = sim_annealing.start_annealing(5000,20000,"insert","random","lin",2,6) end1 = time.time() sumSA_5000_t[sum_index]+= end1-start1 sumSA_5000_e[sum_index] += dist start1 = time.time() dist , rt = sim_annealing.start_annealing(10000,20000,"insert","random","lin",2,6) end1 = time.time() sumSA_10000_t[sum_index]+= end1-start1 sumSA_10000_e[sum_index] += dist start1 = time.time() dist , rt = sim_annealing.start_annealing(20000,20000,"insert","random","lin",2,6) end1 = time.time() sumSA_20000_t[sum_index]+= end1-start1 sumSA_20000_e[sum_index] += dist start1 = time.time() dist , rt = sim_annealing.start_annealing(30000,20000,"insert","random","lin",2,6) end1 = time.time() sumSA_30000_t[sum_index]+= end1-start1 sumSA_30000_e[sum_index] += dist start1 = time.time() dist , rt = sim_annealing.start_annealing(40000,20000,"insert","random","lin",2,6) end1 = time.time() sumSA_40000_t[sum_index]+= end1-start1 sumSA_40000_e[sum_index] += dist start1 = time.time() dist , rt = sim_annealing.start_annealing(50000,20000,"insert","random","lin",2,6) end1 = time.time() sumSA_50000_t[sum_index]+= end1-start1 sumSA_50000_e[sum_index] += dist results["Problem size"].append(z) results["Simulated annealing - 5000 time"].append(sumSA_5000_t[sum_index]/n) results["Simulated annealing - 10000 time"].append(sumSA_10000_t[sum_index]/n) results["Simulated annealing - 20000 time"].append(sumSA_20000_t[sum_index]/n) results["Simulated annealing - 30000 time"].append(sumSA_30000_t[sum_index]/n) results["Simulated annealing - 40000 time"].append(sumSA_40000_t[sum_index]/n) results["Simulated annealing - 50000 time"].append(sumSA_50000_t[sum_index]/n) results["Simulated annealing - 5000 error"].append((((sumSA_5000_e[sum_index]/n)-opt[sum_index])/opt[sum_index])*100) results["Simulated annealing - 10000 error"].append((((sumSA_10000_e[sum_index]/n)-opt[sum_index])/opt[sum_index])*100) results["Simulated annealing - 20000 error"].append((((sumSA_20000_e[sum_index]/n)-opt[sum_index])/opt[sum_index])*100) results["Simulated annealing - 30000 error"].append((((sumSA_30000_e[sum_index]/n)-opt[sum_index])/opt[sum_index])*100) results["Simulated annealing - 40000 error"].append((((sumSA_40000_e[sum_index]/n)-opt[sum_index])/opt[sum_index])*100) results["Simulated annealing - 50000 error"].append((((sumSA_50000_e[sum_index]/n)-opt[sum_index])/opt[sum_index])*100) sum_index+=1 return results
def cooling_testing(n,path,data_numbers,opt): results = {} number_of_instances = len(data_numbers) sumSA_geo_t = [0 for i in range(number_of_instances)] sumSA_lin_t= [0 for i in range(number_of_instances)] sumSA_log_t = [0 for i in range(number_of_instances)] sumSA_geo_e = [0 for i in range(number_of_instances)] sumSA_lin_e = [0 for i in range(number_of_instances)] sumSA_log_e = [0 for i in range(number_of_instances)] results["Problem size"] = [] results["Simulated annealing - logarithmic time"] = [] results["Simulated annealing - linear time"] = [] results["Simulated annealing - geometric time"] = [] results["Simulated annealing - logarithmic error"] = [] results["Simulated annealing - linear error"] = [] results["Simulated annealing - geometric error"] = [] sum_index = 0 for z in data_numbers: tsp = t.TSP() tsp.read_from_file(path+'data{}.txt'.format(z)) sim_annealing = SA.SA(tsp) for i in range(n): start1 = time.time() dist , rt = sim_annealing.start_annealing(7000,20000,"insert","random","geo",0.99,6) end1 = time.time() sumSA_geo_t[sum_index]+= end1-start1 sumSA_geo_e[sum_index] += dist start1 = time.time() dist , rt = sim_annealing.start_annealing(7000,20000,"insert","random","lin",2,6) end1 = time.time() sumSA_lin_t[sum_index]+= end1-start1 sumSA_lin_e[sum_index] += dist start1 = time.time() dist , rt = sim_annealing.start_annealing(7000,20000,"insert","random","log",2,6) end1 = time.time() sumSA_log_t[sum_index]+= end1-start1 sumSA_log_e[sum_index] += dist results["Problem size"].append(z) results["Simulated annealing - geometric time"].append(sumSA_geo_t[sum_index]/n) results["Simulated annealing - linear time"].append(sumSA_lin_t[sum_index]/n) results["Simulated annealing - logarithmic time"].append(sumSA_log_t[sum_index]/n) results["Simulated annealing - geometric error"].append((((sumSA_geo_e[sum_index]/n)-opt[sum_index])/opt[sum_index])*100) results["Simulated annealing - linear error"].append((((sumSA_lin_e[sum_index]/n)-opt[sum_index])/opt[sum_index])*100) results["Simulated annealing - logarithmic error"].append((((sumSA_log_e[sum_index]/n)-opt[sum_index])/opt[sum_index])*100) sum_index+=1 return results
def tabuTS_testing( n, path, data_numbers,opt): results = {} number_of_instances = len(data_numbers) sumTS_2_t = [0 for i in range(number_of_instances)] sumTS_2_e = [0 for i in range(number_of_instances)] sumTS_3_t = [0 for i in range(number_of_instances)] sumTS_3_e = [0 for i in range(number_of_instances)] sumTS_4_t = [0 for i in range(number_of_instances)] sumTS_4_e = [0 for i in range(number_of_instances)] sumTS_5_t = [0 for i in range(number_of_instances)] sumTS_5_e = [0 for i in range(number_of_instances)] results["Problem size"] = [] results["Tabu search - 2 time"] = [] results["Tabu search - 2 error"] = [] results["Tabu search - 3 error"] = [] results["Tabu search - 3 time"] = [] results["Tabu search - 4 error"] = [] results["Tabu search - 4 time"] = [] results["Tabu search - 5 error"] = [] results["Tabu search - 5 time"] = [] sum_index = 0 for z in data_numbers: tsp = t.TSP() tsp.read_from_file(path+'data{}.txt'.format(z)) tabu_s = TS.TS(tsp) for i in range(n): tabu_s = TS.TS(tsp) start1 = time.time() dist , rt = tabu_s.start_search_matrix(3000,"greedy","insert",2,80) end1 = time.time() sumTS_2_t[sum_index]+= end1-start1 sumTS_2_e[sum_index] += dist tabu_s = TS.TS(tsp) start1 = time.time() dist , rt = tabu_s.start_search_matrix(3000,"greedy","insert",3,80) end1 = time.time() sumTS_3_t[sum_index]+= end1-start1 sumTS_3_e[sum_index] += dist tabu_s = TS.TS(tsp) start1 = time.time() dist , rt = tabu_s.start_search_matrix(3000,"greedy","insert",4,80) end1 = time.time() sumTS_4_t[sum_index]+= end1-start1 sumTS_4_e[sum_index] += dist tabu_s = TS.TS(tsp) start1 = time.time() dist , rt = tabu_s.start_search_matrix(3000,"greedy","insert",5,80) end1 = time.time() sumTS_5_t[sum_index]+= end1-start1 sumTS_5_e[sum_index] += dist results["Problem size"].append(z) results["Tabu search - 2 time"].append(sumTS_2_t[sum_index]/n) results["Tabu search - 2 error"].append((((sumTS_2_e[sum_index]/n)-opt[sum_index])/opt[sum_index])*100) results["Tabu search - 3 error"].append((((sumTS_3_e[sum_index]/n)-opt[sum_index])/opt[sum_index])*100) results["Tabu search - 3 time"].append(sumTS_3_t[sum_index]/n) results["Tabu search - 4 error"].append((((sumTS_4_e[sum_index]/n)-opt[sum_index])/opt[sum_index])*100) results["Tabu search - 4 time"].append(sumTS_4_t[sum_index]/n) results["Tabu search - 5 error"].append((((sumTS_5_e[sum_index]/n)-opt[sum_index])/opt[sum_index])*100) results["Tabu search - 5 time"].append(sumTS_5_t[sum_index]/n) sum_index+=1 if(sum_index%4 == 0): df = pd.DataFrame(results) df.to_csv("backup.csv") return results
def time_testing( n, path, data_numbers,opt): results = {} number_of_instances = len(data_numbers) sumSA_ivrt_t = [0 for i in range(number_of_instances)] sumSA_ins_t= [0 for i in range(number_of_instances)] sumSA_sw_t = [0 for i in range(number_of_instances)] sumSA_ivrt_e = [0 for i in range(number_of_instances)] sumSA_ins_e = [0 for i in range(number_of_instances)] sumSA_sw_e = [0 for i in range(number_of_instances)] sumTS_mat_ins_t = [0 for i in range(number_of_instances)] sumTS_list_t = [0 for i in range(number_of_instances)] sumTS_mat_ins_e = [0 for i in range(number_of_instances)] sumTS_list_e = [0 for i in range(number_of_instances)] sumTS_mat_inv_t = [0 for i in range(number_of_instances)] sumTS_mat_inv_e = [0 for i in range(number_of_instances)] sumTS_mat_sw_t = [0 for i in range(number_of_instances)] sumTS_mat_sw_e = [0 for i in range(number_of_instances)] results["Problem size"] = [] results["Simulated annealing - invert time"] = [] results["Simulated annealing - insert time"] = [] results["Simulated annealing - swap time"] = [] results["Simulated annealing - invert error"] = [] results["Simulated annealing - insert error"] = [] results["Simulated annealing - swap error"] = [] results["Tabu search - list time"] = [] results["Tabu search - list error"] = [] results["Tabu search - matrix insert error"] = [] results["Tabu search - matrix insert time"] = [] results["Tabu search - matrix invert error"] = [] results["Tabu search - matrix invert time"] = [] results["Tabu search - matrix swap error"] = [] results["Tabu search - matrix swap time"] = [] sum_index = 0 for z in data_numbers: tsp = t.TSP() tsp.read_from_file(path+'data{}.txt'.format(z)) sim_annealing = SA.SA(tsp) tabu_s = TS.TS(tsp) for i in range(n): start1 = time.time() dist , rt = sim_annealing.start_annealing(7000,20000,"invert","random","lin",2,6) end1 = time.time() sumSA_ivrt_t[sum_index]+= end1-start1 sumSA_ivrt_e[sum_index] += dist start1 = time.time() dist , rt = sim_annealing.start_annealing(7000,20000,"insert","random","lin",2,6) end1 = time.time() sumSA_ins_t[sum_index]+= end1-start1 sumSA_ins_e[sum_index] += dist start1 = time.time() dist , rt = sim_annealing.start_annealing(7000,20000,"swap","random","lin",2,6) end1 = time.time() sumSA_sw_t[sum_index]+= end1-start1 sumSA_sw_e[sum_index] += dist tabu_s = TS.TS(tsp) start1 = time.time() dist , rt = tabu_s.start_search(3000,"greedy","insert",3,80) end1 = time.time() sumTS_list_t[sum_index]+= end1-start1 sumTS_list_e[sum_index] += dist tabu_s = TS.TS(tsp) start1 = time.time() dist , rt = tabu_s.start_search_matrix(3000,"greedy","insert",3,80) end1 = time.time() sumTS_mat_ins_t[sum_index]+= end1-start1 sumTS_mat_ins_e[sum_index] += dist tabu_s = TS.TS(tsp) start1 = time.time() dist , rt = tabu_s.start_search_matrix(3000,"greedy","invert",3,80) end1 = time.time() sumTS_mat_inv_t[sum_index]+= end1-start1 sumTS_mat_inv_e[sum_index] += dist tabu_s = TS.TS(tsp) start1 = time.time() dist , rt = tabu_s.start_search_matrix(3000,"greedy","swap",3,80) end1 = time.time() sumTS_mat_sw_t[sum_index]+= end1-start1 sumTS_mat_sw_e[sum_index] += dist results["Problem size"].append(z) results["Simulated annealing - invert time"].append(sumSA_ivrt_t[sum_index]/n) results["Simulated annealing - insert time"].append(sumSA_ins_t[sum_index]/n) results["Simulated annealing - swap time"].append(sumSA_sw_t[sum_index]/n) results["Simulated annealing - invert error"].append((((sumSA_ivrt_e[sum_index]/n)-opt[sum_index])/opt[sum_index])*100) results["Simulated annealing - insert error"].append((((sumSA_ins_e[sum_index]/n)-opt[sum_index])/opt[sum_index])*100) results["Simulated annealing - swap error"].append((((sumSA_sw_e[sum_index]/n)-opt[sum_index])/opt[sum_index])*100) results["Tabu search - list time"].append(sumTS_list_t[sum_index]/n) results["Tabu search - list error"].append((((sumTS_list_e[sum_index]/n)-opt[sum_index])/opt[sum_index])*100) results["Tabu search - matrix insert error"].append((((sumTS_mat_ins_e[sum_index]/n)-opt[sum_index])/opt[sum_index])*100) results["Tabu search - matrix insert time"].append(sumTS_mat_ins_t[sum_index]/n) results["Tabu search - matrix invert error"].append((((sumTS_mat_inv_e[sum_index]/n)-opt[sum_index])/opt[sum_index])*100) results["Tabu search - matrix invert time"].append(sumTS_mat_inv_t[sum_index]/n) results["Tabu search - matrix swap error"].append((((sumTS_mat_sw_e[sum_index]/n)-opt[sum_index])/opt[sum_index])*100) results["Tabu search - matrix swap time"].append(sumTS_mat_sw_t[sum_index]/n) sum_index+=1 if(sum_index%4 == 0): df = pd.DataFrame(results) df.to_csv("backup.csv") return results
mode='markers+lines', name='Tabu search - matrix swap', marker = dict(size = 12) )) pio.write_html(figure, file='test_plot.html', auto_open=True) figure.show() x = t.TSP() x.read_from_file('data/dataEtap2/data13.txt') # sim_ann = SA.SA(x) # start = time.time() # opt, route = sim_ann.start_annealing(7000,20000,"invert","greedy","lin",2,5) # end = time.time() # res = end-start # print(opt, res, route) # # tabu_s = TS.TS(x) # start = time.time() # optTS, routeTS = tabu_s.start_search(3000,"greedy","invert",3,80) # end = time.time() # res = end-start # print(optTS, res,routeTS)