while improved: improved = False for si in range(t.tour.n): for sj in range(si + 2, t.tour.n): t.tour.insert_new_node(t.tour.midpoint(si, sj)) t.optimize() t.tour.remove_last_xy() new_length = t.tour.tour_length() if new_length < best_length: t.optimize() best_length = t.tour.tour_length() best = t.tour.node_ids[:] print("best length: " + str(best_length)) improved = True else: t.tour.reset(best) if __name__ == "__main__": xy = reader.read_xy("input/berlin52.tsp") t = TwoOpt(xy) t.optimize() t.tour.plot() inserter(t) t.tour.validate() print("final length: " + str(t.tour.tour_length())) t.tour.plot() t.tour.show()
def perturbed_hill_climb_naive(xy, tour): tries = 0 success = 0 best_length = tour_util.length(xy, tour) while True: new_tour, naive_new_length = two_opt.optimize(xy, tour_util.double_bridge(tour)) # double bridge #test_tour = tour[:] #random.shuffle(test_tour) #new_tour, naive_new_length = two_opt.optimize(xy, test_tour) # random restart naive_gain = best_length - naive_new_length if naive_gain > 0: tour = new_tour best_length = naive_new_length success += 1 tries += 1 current_length = basic.tour_length(xy, tour) assert(best_length == current_length) if current_length <= TARGET_LENGTH: break print('current best: {} (iteration {}), improvement rate: {}'.format(best_length, tries, success / tries)) if __name__ == "__main__": print('stopping at target length {}'.format(TARGET_LENGTH)) problem_name = 'xqf131' xy = reader.read_xy("problems/{}.tsp".format(problem_name)) tour = tour_util.default(xy) tour, improvement = two_opt.optimize(xy, tour) perturbed_hill_climb(xy, tour)
#!/usr/bin/env python3 import reader from two_opt import TwoOpt xy = reader.read_xy("input/berlin52.tsp") xy = reader.read_xy("input/xqf131.tsp") t = TwoOpt(xy) t.optimize() print("local optimum: " + str(t.tour.tour_length())) best_length = t.tour.tour_length() best = t.tour.node_ids[:] for i in range(50): print(i) t.tour.randomize() t.optimize() new_length = t.tour.tour_length() if new_length < best_length: best = t.tour.node_ids[:] best_length = new_length print("best length: " + str(best_length)) else: t.tour.reset(best)
print("trial tour length: " + str(trial_length)) old_edges, new_edges = tour_diff(best_order, trial_order) basic.write_edges(old_edges, "output/old_edges_test.txt") basic.write_edges(new_edges, "output/new_edges_test.txt") best_order = merge(xy, best_order, trial_order) new_length = basic.tour_length(xy, best_order) print("new length: " + str(new_length)) assert (trial_length >= new_length) assert (len(best_order) == original_length) return best_order if __name__ == "__main__": xy = reader.read_xy("../data/xqf131.tsp") order = [i for i in range(len(xy))] random.shuffle(order) order = opt2(order) print("initial tour length: " + str(basic.tour_length(xy, order))) for i in range(1000): basic.write_edges_from_order(order, "output/order_test.txt") print("merge iteration: " + str(i)) premerge_length = basic.tour_length(xy, order) #order = merge_nn(xy, order, i) order = merge_double_bridge(xy, order, i) postmerge_length = basic.tour_length(xy, order) if postmerge_length != premerge_length: order = opt2(order) postmerge_climb_length = basic.tour_length(xy, order)
edges2 = set(edges2) same = [] for edge in edges1: if edge in edges2: same.append(edge) return same if __name__ == '__main__': problem_name = 'xqf131' lengths = [589, 592] lengths = [589, 596] edges1 = read_tour('solutions/{}_{}.tour'.format(problem_name, lengths[0])) edges2 = read_tour('solutions/{}_{}.tour'.format(problem_name, lengths[1])) same = common(edges1, edges2) xy = reader.read_xy('problems/{}.tsp'.format(problem_name)) diff1, diff2 = difference(edges1, edges2) for edge in diff1: a = xy[edge[0]] b = xy[edge[1]] plt.plot([a[0], b[0]], [a[1], b[1]], 'x-b') for edge in diff2: a = xy[edge[0]] b = xy[edge[1]] plt.plot([a[0], b[0]], [a[1], b[1]], 'x-r') for edge in same: a = xy[edge[0]] b = xy[edge[1]] plt.plot([a[0], b[0]], [a[1], b[1]], 'k:') plt.show()