From ef19a8b0a4c116b6df453fefbab7c05d82fff99a Mon Sep 17 00:00:00 2001 From: UmbertoJr Date: Thu, 31 Oct 2019 18:33:22 +0100 Subject: [PATCH] udpate --- run.py | 17 ++++++++++++++++- src/TSP_solver.py | 1 - src/io_tsp.py | 2 +- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/run.py b/run.py index b879e9e..0da35b8 100644 --- a/run.py +++ b/run.py @@ -1,5 +1,5 @@ from src import * -import os +from concorde.tsp import TSPSolver def run(show_plots=False): @@ -14,6 +14,15 @@ def run(show_plots=False): if show_plots: instance.plot_data() + solver = TSPSolver.from_data( + instance.points[:, 1]*100, + instance.points[:, 2]*100, + norm="EUC_2D" + ) + solution = solver.solve(verbose=False) + tour_opt = np.copy(solution.tour) + + for method in ["random", "nearest_neighbors", "best_nn"]: solver = Solver_TSP(method) solver(instance, return_value=False) @@ -24,6 +33,12 @@ def run(show_plots=False): if show_plots: solver.plot_solution() + solver.method = "optimal" + solver.solution = np.concatenate([tour_opt, [tour_opt[0]]]) + solver.solved = True + solver.plot_solution() + print(solver.evaluate_solution(return_value=True)) + if __name__ == '__main__': run(show_plots=True) diff --git a/src/TSP_solver.py b/src/TSP_solver.py index 0396fdb..f0a4ce0 100644 --- a/src/TSP_solver.py +++ b/src/TSP_solver.py @@ -64,7 +64,6 @@ class Solver_TSP: self.solved = True return self.solution - def plot_solution(self): assert self.solved, "You can't plot the solution, you need to solve it first!" plt.figure(figsize=(8, 8)) diff --git a/src/io_tsp.py b/src/io_tsp.py index ffb111c..052169d 100644 --- a/src/io_tsp.py +++ b/src/io_tsp.py @@ -51,7 +51,7 @@ class Instance: @staticmethod def distance_euc(zi, zj): xi, xj = zi[0], zj[0] - yi, yj = zi[0], zj[0] + yi, yj = zi[1], zj[1] return round(np.sqrt((xi - xj) ** 2 + (yi - yj) ** 2)) def create_dist_matrix(self):