From de3e3f0793c3249c30a44254a1fbb8e408359935 Mon Sep 17 00:00:00 2001 From: UmbertoJr Date: Mon, 4 Nov 2019 06:43:54 +0100 Subject: [PATCH] solver --- run.py | 4 ++++ src/TSP_solver.py | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/run.py b/run.py index 19af09b..c5b31f7 100644 --- a/run.py +++ b/run.py @@ -32,6 +32,10 @@ def run(show_plots=False): if show_plots: solver.plot_solution() + if instance.optimal_tour: + solver.solution = np.concatenate([instance.optimal_tour, [instance.optimal_tour[0]]]) + solver.plot_solution() + index = pd.MultiIndex.from_tuples(index, names=['problem', 'method']) return pd.DataFrame(results, index=index, columns=["tour length", "optimal solution", "gap", "time to solve"]) diff --git a/src/TSP_solver.py b/src/TSP_solver.py index f0a4ce0..ee6c2c7 100644 --- a/src/TSP_solver.py +++ b/src/TSP_solver.py @@ -17,6 +17,7 @@ class Solver_TSP: def __call__(self, instance_, verbose=True, return_value=True): self.instance = instance_ + self.solved = False if verbose: print(f"### solving with {self.method} ####") self.solution = self.available_methods[self.method](instance_) @@ -67,7 +68,8 @@ class Solver_TSP: def plot_solution(self): assert self.solved, "You can't plot the solution, you need to solve it first!" plt.figure(figsize=(8, 8)) - plt.title(f"{self.instance.name} with gap {self.gap}") + self._gap() + plt.title(f"{self.instance.name} solved with {self.method}, gap {self.gap}") ordered_points = self.instance.points[self.solution] plt.plot(ordered_points[:, 1], ordered_points[:, 2], 'b-') plt.show()