def first_test(): way = 153 matrix = [ [999, 32, 26, 86, 11, 55, 29, 7, 89, 43, 21, 41, 48, 61, 93], [85, 999, 29, 70, 12, 71, 5, 78, 32, 60, 25, 34, 76, 70, 48], [15, 99, 999, 15, 79, 8, 10, 95, 84, 20, 41, 99, 42, 1, 54], [88, 21, 35, 999, 5, 26, 78, 64, 16, 38, 29, 47, 71, 89, 91], [48, 50, 79, 27, 999, 45, 24, 10, 85, 71, 98, 72, 5, 27, 25], [21, 96, 61, 6, 63, 999, 90, 27, 63, 82, 88, 51, 60, 94, 56], [57, 15, 11, 99, 82, 62, 999, 71, 80, 47, 31, 76, 35, 88, 0], [69, 98, 15, 13, 38, 1, 5, 999, 32, 37, 11, 62, 43, 26, 43], [75, 18, 58, 24, 12, 95, 72, 60, 999, 70, 58, 98, 77, 24, 33], [48, 90, 67, 24, 2, 61, 41, 67, 46, 999, 41, 49, 20, 33, 38], [49, 97, 37, 33, 5, 58, 67, 61, 39, 3, 999, 91, 9, 41, 0], [95, 44, 74, 46, 60, 62, 80, 4, 77, 54, 76, 999, 13, 8, 37], [75, 13, 37, 56, 12, 7, 66, 37, 28, 96, 38, 81, 999, 96, 45], [87, 78, 37, 81, 46, 80, 59, 59, 68, 27, 24, 21, 10, 999, 57], [28, 22, 23, 9, 93, 47, 69, 13, 20, 89, 70, 78, 7, 50, 999] ] print('\nДлина пути должна быть равна ', way) class_tsp = tsp() class_tsp.start(matrix) if class_tsp.way_price == way: print('Первый тест пройден') else: print('Первый тест завален')
def opt_shortest_path(points): t = tsp() t.nodes = points t.order = range(len(t.nodes)) t.dismatrix() # 2-opt法找到最短路径 count = 0 while(count < 100000): former_sum = t.sum(tsp=False) opted_order = t.order[:] # 随机生成两个交换的元素,起点终点不交换 left, right = random.randint( 1, len(t.order) - 2), random.randint(1, len(t.order) - 2) # 防止左边界比右边界大 if left > right: left, right = right, left # 左右边界之间的序列反转 temp = opted_order[left:right + 1] temp.reverse() opted_order[left:right + 1] = temp # 对新序列求和 if t.sum(opted_order, tsp=False) < former_sum: t.order = opted_order count = 0 else: count += 1 return t.order
def seven_test(): way = 1 matrix = [[999, 0, 0], [1, 999, 1], [1, 0, 999]] print('\nДлина пути должна быть равна ', way) class_tsp = tsp() class_tsp.start(matrix) if class_tsp.way_price == way: print('Седьмой тест пройден') else: print('Седьмой тест завален \n')
def five_test(): way = 2 matrix = [[999, 0, 1], [0, 999, 1], [1, 1, 999]] print('\nДлина пути должна быть равна ', way) class_tsp = tsp() class_tsp.start(matrix) if class_tsp.way_price == way: print('Пятый тест пройден') else: print('Пятый тест завален')
def eight_test(): way = 0 matrix = [[999, 0, 0], [0, 999, 0], [0, 0, 999]] print('\nДлина пути должна быть равна ', way) class_tsp = tsp() class_tsp.start(matrix) if class_tsp.way_price == way: print('Восьмой тест пройден') else: print('Восьмой тест завален \n')
def six_test(): way = 30 matrix = [[999, 5, 11, 9], [20, 999, 8, 7], [7, 14, 999, 8], [12, 6, 15, 999]] print('\nДлина пути должна быть равна ', way) class_tsp = tsp() class_tsp.start(matrix) if class_tsp.way_price == way: print('Шестой тест пройден') else: print('Шестой тест завален')
def solve(list_of_locations, list_of_homes, starting_car_location, adjacency_matrix, params=[]): """ Write your algorithm here. Input: list_of_locations: A list of locations such that node i of the graph corresponds to name at index i of the list list_of_homes: A list of homes starting_car_location: The name of the starting location for the car adjacency_matrix: The adjacency matrix from the input file Output: A list of locations representing the car path A dictionary mapping drop-off location to a list of homes of TAs that got off at that particular location NOTE: both outputs should be in terms of indices not the names of the locations themselves """ # This will be the map for index name to real location name locationMap = {} # This is the index of the starting location startingIndex = -1 # This is the list of homes as indexes homes = [] for i in range(len(list_of_locations)): locationMap[i] = list_of_locations[i] if list_of_locations[i] == starting_car_location: startingIndex = i if list_of_locations[i] in list_of_homes: homes.append(i) # Create clusters distanceMatrix, pathMatrix = shortestDistance(adjacency_matrix) min_cost = math.inf for k in range(2, len(distanceMatrix) // 2): clusters = cluster(list(locationMap.keys()), homes, distanceMatrix, k) raw_pathway = tsp(startingIndex, clusters, distanceMatrix) real_pathway = realPath(raw_pathway, pathMatrix) dropOffs = smart_dropOff(real_pathway, homes, distanceMatrix) c = cost(real_pathway, dropOffs, distanceMatrix) if c < min_cost: min_cost = c min_pathway = real_pathway min_dropOffs = dropOffs print("Smart Cost : " + str(min_cost)) print("Naive Cost : " + str(naive_cost(startingIndex, homes, distanceMatrix))) return min_pathway, min_dropOffs
def three_test(): way = 41 matrix = [[999, 20, 18, 12, 8], [5, 999, 14, 7, 11], [12, 18, 999, 6, 11], [11, 17, 11, 999, 12], [5, 5, 5, 5, 999]] print('\nДлина пути должна быть равна ', way) class_tsp = tsp() class_tsp.start(matrix) if class_tsp.way_price == way: print('Третий тест пройден') else: print('Третий тест завален')
def second_test(): way = 49 matrix = [[999, 7, 16, 21, 2, 17], [13, 999, 21, 15, 43, 23], [25, 3, 999, 31, 17, 9], [13, 10, 27, 999, 33, 12], [9, 2, 19, 14, 999, 51], [42, 17, 5, 9, 23, 999]] print('\nДлина пути должна быть равна ', way) class_tsp = tsp() class_tsp.start(matrix) if class_tsp.way_price == way: print('Второй тест пройден') else: print('Второй тест завален')
def four_test(): way = 138 matrix = [[999, 41, 40, 48, 40, 42, 6], [48, 999, 41, 49, 42, 46, 4], [22, 22, 999, 23, 24, 19, 2], [15, 17, 11, 999, 10, 14, 1], [47, 43, 18, 42, 999, 52, 0], [34, 39, 30, 39, 32, 999, 2], [34, 39, 30, 39, 32, 5, 999]] print('\nДлина пути должна быть равна ', way) class_tsp = tsp() class_tsp.start(matrix) if class_tsp.way_price == way: print('Четвертый тест пройден') else: print('Четвертый тест завален')
import tsp t = tsp([(0, 0), (0, 1), (1, 0), (1, 1)]) t.solve() print(t.result)
# coding:UTF-8 # Drawing import matplotlib.pyplot as plt import convexhull as ch from tsp import * # 测试TSP 凸包算法 test = tsp() test.readfile('TSPlib/oliver30.tsp') test.greedy() test.draw() plt.savefig("TSPcomparison/01_greedySolvedTSP.png", dpi=256) plt.show() test.opt(100000) test.draw() plt.savefig("TSPcomparison/02_2-opted_greedySolvedTSP.png", dpi=256) plt.show() gs = ch.DrawConvexHull(test.nodes) plt.show() test.convexhull() test.draw() plt.savefig("TSPcomparison/03_chSolvedTSP.png", dpi=256) plt.show() test.opt(100000)
def test_hw(self): print "\nStarting HW question:" cities = loadFile('tsp.txt') print "\n\nThe answer to the HW question is: %s\n" % tsp(cities)
def test_magic(self): cities = loadFile('18_cities.txt') self.assertAlmostEqual(3.501, tsp(cities), places=3)
def test_rectangle(self): cities = loadFile('rectangle.txt') self.assertEqual(10, tsp(cities))
def test_line(self): cities = loadFile('line.txt') self.assertEqual(12, tsp(cities))