def solve(cities): # print(cities) N = len(cities) dist = [[0] * N for i in range(N)] # print(dist) for i in range(N): for j in range(i, N): dist[i][j] = dist[j][i] = distance(cities[i], cities[j]) # print(dist) # current_city = random.randint(0, N-1) # unvisited_cities = [] # for i in range(N): # if i != current_city: # unvisited_cities.append(i) current_city = 0 unvisited_cities = set(range(1, N)) # always starting with the zero-th city tour = [current_city] while unvisited_cities: next_city = choose_a_random_neighbor(dist, cities, current_city, unvisited_cities) unvisited_cities.remove(next_city) tour.append(next_city) current_city = next_city # print(tour) return two_opt.two_opt(cities, tour)
def solve(cities): N = len(cities) dist = [[0] * N for i in range(N)] for i in range(N): for j in range(i, N): dist[i][j] = dist[j][i] = distance(cities[i], cities[j]) # print(dist) current_city = 0 unvisited_cities = list(range(1, N)) # always starting with the zero-th city tour = [current_city] # compare current node with each node in the tour # pick the node which is closest to current node # connect it to the closest node while unvisited_cities: # print(unvisited_cities) if len(tour) == 1: next_city = min(unvisited_cities, key=lambda city: dist[current_city][city]) unvisited_cities.remove(next_city) tour.append(next_city) else: current_node = unvisited_cities[0] closest_node = min(tour, key=lambda city: dist[current_node][city]) tour.insert((tour.index(closest_node)) + 1, current_node) unvisited_cities.remove(current_node) # print(path_length(tour, cities)) # print(tour) return two_opt.two_opt(cities, tour)
def solver(argv): for i,x in enumerate(argv): if argv[i]=='-alg': algo=argv[i+1] elif argv[i]=='-inst': fileName=argv[i+1] elif argv[i]=='-time': cutoff=int(argv[i+1]) elif argv[i]=='-seed': seed=int(argv[i+1]) # fileName='ulysses16.tsp' G, optimalCost=createGraph(fileName) print optimalCost if algo=='BnB': with open(fileName[:-4]+'_BnB_'+str(cutoff)+'.trace','w') as ftrace: tour,foundCost=branchAndBound(G,cutoff,ftrace) elif algo=='Approx': tour,foundCost=mst_approx(G) elif algo=='Heur': foundCost=greedy_approx(G) elif algo=='LS1': with open(fileName[:-4]+'_LS1_'+str(seed)+'_'+str(cutoff)+'.trace','w') as ftrace: tour,foundCost=simulated_annealiing(G,seed,cutoff,ftrace) elif algo=='LS2': with open(fileName[:-4]+'_LS2_'+str(seed)+'_'+str(cutoff)+'.trace','w') as ftrace: tour,foundCost=two_opt(G,cutoff,seed,ftrace) print tour,foundCost
def solve(cities, k, dist): N = len(cities) # attempt farthest insertion # step 1: start with a subtour which contains arbitrary starting node # step 2: find the farthest node from sub tour current_city = k unvisited_cities = [] for i in range(N): if i != k: unvisited_cities.append(i) sub_tour = [current_city] next_city = min(unvisited_cities, key=lambda city: dist[current_city][city]) # print(next_city) sub_tour.append(next_city) unvisited_cities.remove(next_city) sub_tour.append(current_city) # print(sub_tour) # here, sub_tour = [A, B, A] # find node k not in the subtour which is farthest from any node in the sub-tour while unvisited_cities: # selection step curr_node_r = min(unvisited_cities, key=lambda city: dist[city][sub_tour[0]]) for i in range(1, len(sub_tour)): new_node_r = min(unvisited_cities, key=lambda city: dist[city][i]) if new_node_r < curr_node_r: curr_node_r = new_node_r # print(curr_node_r) # insertion step # Find the edge (i, j) in the subtour which minimizes cir + crj - cij f = distance(cities[sub_tour[0]], cities[curr_node_r]) + distance(cities[curr_node_r], cities[sub_tour[0 + 1]]) - distance(cities[sub_tour[0]], cities[sub_tour[0 + 1]]) insert_index = 0 for i in range(len(sub_tour) - 1): if i == N - i - 1: continue new_f = distance(cities[sub_tour[i]], cities[curr_node_r]) + distance(cities[curr_node_r], cities[sub_tour[i + 1]]) - distance(cities[sub_tour[i]], cities[sub_tour[i + 1]]) if new_f < f: f = new_f insert_index = i unvisited_cities.remove(curr_node_r) sub_tour.insert(insert_index + 1, curr_node_r) # print(sub_tour) tour = sub_tour[:-1] return two_opt.two_opt(cities, tour)
def main(argv): alpha = float(argv[1]) coords, distances = tsplib.load(argv[2]) count = len(distances) solution, best_cost = nearest_tour(distances, len(distances)) plot = plotting.TspPlot(coords, draw_guess=True) print best_cost penalties = numpy.copy(distances) penalties[:] = 0.0 def calc_cost(solution): cost = tsplib.calc_cost(solution, distances) sol_pens = penalties[solution[:-1], solution[1:]] gls_cost = (cost + alpha * (best_cost/count+1) * (sol_pens.sum() + penalties[solution[-1], solution[0]])) return gls_cost def penalise(solution): sol_dists = distances[solution[0:-1], solution[1:]] sol_dists = numpy.append(sol_dists, (distances[solution[0], solution[-1]],)) sol_pens = penalties[solution[0:-1], solution[1:]] sol_pens = numpy.append(sol_pens, (penalties[solution[0], solution[-1]],)) utility = sol_dists/(sol_pens+1) index_a = numpy.argmax(utility) index_b = index_a+1 if index_a != count-1 else 0 a = solution[index_a] b = solution[index_b] penalties[a,b] += 1.0 penalties[b,a] += 1.0 def redraw_guess(solution, cost): cost = tsplib.calc_cost(solution, distances) # ugh plot.redraw_guess(solution, cost) gls_cost = cost = best_cost best_solution = numpy.copy(solution) plot.redraw_best(best_solution, cost) while 1: solution, gls_cost = two_opt(solution, gls_cost, len(distances), calc_cost, redraw_guess) cost = tsplib.calc_cost(solution, distances) if cost < best_cost: print cost best_cost = cost best_solution[:] = solution plot.redraw_best(best_solution, cost) penalise(solution)
def solve(cities, k, dist): N = len(cities) current_city = k unvisited_cities = [] for i in range(N): if i != k: unvisited_cities.append(i) tour = [current_city] while unvisited_cities: next_city = min(unvisited_cities, key=lambda city: dist[current_city][city]) unvisited_cities.remove(next_city) tour.append(next_city) current_city = next_city # print(tour) return two_opt.two_opt(cities, tour)
def solver(argv): for i, x in enumerate(argv): if argv[i] == '-alg': algo = argv[i + 1] elif argv[i] == '-inst': fileName = argv[i + 1] elif argv[i] == '-time': cutoff = int(argv[i + 1]) elif argv[i] == '-seed': seed = int(argv[i + 1]) # fileName='ulysses16.tsp' G, optimalCost = createGraph(fileName) print optimalCost if algo == 'BnB': with open(fileName[:-4] + '_BnB_' + str(cutoff) + '.trace', 'w') as ftrace: tour, foundCost = branchAndBound(G, cutoff, ftrace) elif algo == 'Approx': tour, foundCost = mst_approx(G) elif algo == 'Heur': foundCost = greedy_approx(G) elif algo == 'LS1': with open( fileName[:-4] + '_LS1_' + str(seed) + '_' + str(cutoff) + '.trace', 'w') as ftrace: tour, foundCost = simulated_annealiing(G, seed, cutoff, ftrace) elif algo == 'LS2': with open( fileName[:-4] + '_LS2_' + str(seed) + '_' + str(cutoff) + '.trace', 'w') as ftrace: tour, foundCost = two_opt(G, cutoff, seed, ftrace) print tour, foundCost
def solver(argv): for i,x in enumerate(argv): if argv[i]=='-alg': algo=argv[i+1] elif argv[i]=='-inst': fileName=argv[i+1] elif argv[i]=='-time': cutoff=int(argv[i+1]) elif argv[i]=='-seed': seed=int(argv[i+1]) G, optimalCost=createGraph(fileName) if algo=='BnB': with open(fileName[:-4]+'_BnB_'+str(cutoff)+'.trace','w') as ftrace: tour,foundCost=branchAndBound(G,cutoff,ftrace) elif algo=='Approx': tour,foundCost=mst_approx(G) elif algo=='Heur': tour,foundCost=greedy_approx(G) elif algo=='LS1': with open(fileName[:-4]+'_LS1_'+str(cutoff)+'_'+str(seed)+'.trace','w') as ftrace: tour,foundCost=simulated_annealiing(G,seed,cutoff,ftrace) elif algo=='LS2': with open(fileName[:-4]+'_LS2_'+str(cutoff)+'_'+str(seed)+'.trace','w') as ftrace: tour,foundCost=two_opt(G,seed,cutoff,ftrace) if algo=='LS1' or algo=='LS2': solutionFIleName=fileName[:-4]+'_'+algo+'_'+str(cutoff)+'_'+str(seed)+'.sol' else: solutionFIleName=fileName[:-4]+'_'+algo+'_'+str(cutoff)+'.sol' with open(solutionFIleName,'w') as fsol: if foundCost!=-1: fsol.write(str(foundCost)+'\n') tourString=','.join(list(map(str,tour)))+','+str(tour[0])+'\n' fsol.write(tourString)
def two_opt(self): improved_tour = two_opt(list(self.tour.numpy()), self.tsp_problem.distances) self.tour = torch.tensor(improved_tour)