def make_path(X, D): tsp = TSP() # Using the data matrix tsp.read_data(X) # Using the distance matrix tsp.read_mat(D) from tspy.solvers import TwoOpt_solver two_opt = TwoOpt_solver(initial_tour='NN', iter_num=10000) two_opt_tour = tsp.get_approx_solution(two_opt) #tsp.plot_solution('TwoOpt_solver') best_tour = tsp.get_best_solution() return best_tour
def solve(self): self.FloydWarshall() tsp = TSP() tsp.read_mat(np.array(self.dist, dtype=np.float64)) two_opt = TwoOpt_solver(initial_tour='NN', iter_num=1000000) tsp.get_approx_solution(two_opt) path = tsp.get_best_solution() path = path[1:] # Trim the start vertex start_idx = path.index(self.start) # Make sure the path starts and ends at Soda path = path[start_idx:] + path[:start_idx] + [self.start] changed = True # Not the most efficient, but oh well while changed: changed = False for i in range(len(path) - 1): u, v = path[i], path[i + 1] if self.adj[u][v] == Solver.INF: # This isn't an edge in the graph, so splice in the shortest path path[i:i + 2] = self.path[u][v] changed = True break # check all cycles along path to see if compression is better path = self.compressCycles(path) # try to remove dropoff locations path = self.removeDropoffLocations(path) # try to compress dropoff pairs path = self.compressDropoffPairs(path) self.finalPath = path # calculate final energy _, totalDist = self.calcDropoffsAndDist(path) return totalDist
from tspy import TSP from tspy.lower_bounds import Simple_LP_bound from tspy.lower_bounds import Connected_LP_bound from tspy.lower_bounds import MinCut_LP_bound from tspy.solvers import TwoOpt_solver from tspy.solvers import NN_solver import numpy as np a = TSP() N = 20 print(N) a.read_data(np.random.rand(N, 2)) a.get_approx_solution(TwoOpt_solver('NN')) a.get_approx_solution(NN_solver()) bounds = [Simple_LP_bound(), Connected_LP_bound(), MinCut_LP_bound()] for b in bounds: a.get_lower_bound(b) print(a.lower_bounds) a.get_best_solution() a.get_best_lower_bound()