From 00c1eb851160ed7bc26a2aa11f0611531094aedd Mon Sep 17 00:00:00 2001 From: UmbertoJr Date: Mon, 18 Nov 2019 08:15:29 +0100 Subject: [PATCH] solver --- run.py | 31 ++++++++++++++++--------------- src/TSP_solver.py | 8 ++++++-- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/run.py b/run.py index 7ff77ff..d5c528b 100644 --- a/run.py +++ b/run.py @@ -6,7 +6,8 @@ from time import time as t def run(show_plots=False, verbose=False): # names = [name_ for name_ in os.listdir("./problems") if "tsp" in name_] names = ["eil76.tsp", "kroA100.tsp"] - methods = Solver_TSP.available_methods.keys() + initializers = Solver_TSP.available_initializers.keys() + improvements = Solver_TSP.available_improvements.keys() results = [] index = [] for name in names: @@ -18,23 +19,23 @@ def run(show_plots=False, verbose=False): if show_plots: instance.plot_data() - for method in methods: - solver = Solver_TSP(method) - start = t() - solver(instance, return_value=False, verbose=verbose) - end = t() + for init in initializers: + solver = Solver_TSP(init) + for improve in improvements: + solver.bind(improve) + solver(instance, return_value=False, verbose=verbose) - if verbose: - print(f"the total length for the solution found is {solver.found_length}", - f"while the optimal length is {instance.best_sol}", - f"the gap is {solver.gap}%", - f"the solution is found in {np.round(end - start, 5)} seconds", sep="\n") + if verbose: + print(f"the total length for the solution found is {solver.found_length}", + f"while the optimal length is {instance.best_sol}", + f"the gap is {solver.gap}%", + f"the solution is found in {solver.time_to_solve} seconds", sep="\n") - index.append((name, method)) - results.append([solver.found_length, instance.best_sol, solver.gap, end - start]) + index.append((name, solver.name_method)) + results.append([solver.found_length, instance.best_sol, solver.gap, solver.time_to_solve]) - if show_plots: - solver.plot_solution() + if show_plots: + solver.plot_solution() if instance.exist_opt: solver.solution = np.concatenate([instance.optimal_tour, [instance.optimal_tour[0]]]) diff --git a/src/TSP_solver.py b/src/TSP_solver.py index 08d8c76..bca39de 100644 --- a/src/TSP_solver.py +++ b/src/TSP_solver.py @@ -1,6 +1,7 @@ from numpy.core._multiarray_umath import ndarray import os from time import time as t +import numpy as np if 'AI' in os.getcwd(): from src import * else: @@ -25,12 +26,14 @@ class Solver_TSP: # "best_nn": self.best_nn, "multi_fragment": self.mf} self.initializer = initializer self.methods = [initializer] + self.name_method = "initialize with " + initializer self.solved = False assert initializer in self.available_initializers, f"the {initializer} initializer is not available currently." def bind(self, local_or_meta): assert local_or_meta in self.available_improvements, f"the {local_or_meta} method is not available currently." self.methods.append(local_or_meta) + self.name_method = ", improve with " + local_or_meta def __call__(self, instance_, verbose=True, return_value=True): self.instance = instance_ @@ -45,10 +48,11 @@ class Solver_TSP: assert self.check_if_solution_is_valid(self.solution), "Error the solution is not valid" end = t() + self.time_to_solve = np.around(end - start,3) self.evaluate_solution() self._gap() if verbose: - print(f"### solution found with {self.gap} % gap in {np.around(end - start, 3)} seconds ####") + print(f"### solution found with {self.gap} % gap in {self.time_to_solve} seconds ####") if return_value: return self.solution @@ -56,7 +60,7 @@ class Solver_TSP: assert self.solved, "You can't plot the solution, you need to solve it first!" plt.figure(figsize=(8, 8)) self._gap() - plt.title(f"{self.instance.name} solved with {self.method} solver, gap {self.gap}") + plt.title(f"{self.instance.name} solved with {self.name_method} solver, gap {self.gap}") ordered_points = self.instance.points[self.solution] plt.plot(ordered_points[:, 1], ordered_points[:, 2], 'b-') plt.show()