From bba6d0f42811d396c4c96883889480a2e7490b22 Mon Sep 17 00:00:00 2001 From: Dario Mantegazza Date: Mon, 28 Sep 2020 12:13:53 +0200 Subject: [PATCH] more refactoring and code cleaning --- run.py | 15 +++++++-------- src/TSP_solver.py | 4 +--- src/__init__.py | 17 ----------------- src/io_tsp.py | 13 ++----------- src/iterated_local_search.py | 2 +- src/local_search.py | 3 --- src/simulated_annealing.py | 2 +- src/two_opt.py | 2 +- 8 files changed, 13 insertions(+), 45 deletions(-) delete mode 100644 src/local_search.py diff --git a/run.py b/run.py index 5403f1a..3537dd8 100644 --- a/run.py +++ b/run.py @@ -32,7 +32,6 @@ def run(show_plots=False, verbose=False): for problem_path in problems: prob_instance = ProblemInstance(problem_path) if verbose: - print("\n\n#############################") prob_instance.print_info() if show_plots: prob_instance.plot_data() @@ -41,15 +40,15 @@ def run(show_plots=False, verbose=False): for improve in improvers_names: solver = SolverTSP(solver_name, prob_instance) use_solver_to_compute_solution(solver, improve, index, results, problem_path, verbose, show_plots) - for improve2 in [j for j in improvers_names if j not in [improve]]: - use_solver_to_compute_solution(solver, improve2, index, results, problem_path, verbose, show_plots) + for improve2 in [j for j in improvers_names if j not in [improve]]: + use_solver_to_compute_solution(solver, improve2, index, results, problem_path, verbose, show_plots) - for improve3 in [j for j in improvers_names if j not in [improve, improve2]]: - use_solver_to_compute_solution(solver, improve3, index, results, problem_path, verbose, - show_plots) - solver.pop() + for improve3 in [j for j in improvers_names if j not in [improve, improve2]]: + use_solver_to_compute_solution(solver, improve3, index, results, problem_path, verbose, + show_plots) + solver.pop() - solver.pop() + solver.pop() if prob_instance.exist_opt and show_plots: solver = SolverTSP("optimal", prob_instance) diff --git a/src/TSP_solver.py b/src/TSP_solver.py index 70261fa..ef37313 100644 --- a/src/TSP_solver.py +++ b/src/TSP_solver.py @@ -1,4 +1,3 @@ -from numpy.core._multiarray_umath import ndarray from time import time as t import numpy as np import matplotlib.pyplot as plt @@ -20,12 +19,11 @@ available_improvers = {"2-opt": loop2opt, class SolverTSP: - solution: ndarray - found_length: float def __init__(self, algorithm_name, problem_instance): # assert algorithm_name in available_solvers, f"the {algorithm_name} initializer is not available currently." self.duration = np.inf + self.found_length = np.inf self.algorithm_name = algorithm_name self.algorithms = [algorithm_name] self.name_method = "initialized with " + algorithm_name diff --git a/src/__init__.py b/src/__init__.py index 540f723..e69de29 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -1,17 +0,0 @@ -import os - -if 'AI' in os.getcwd(): - from src.utils import * - from src.constructive_algorithms import * - from src.local_search import * - from src.iterated_local_search import * - from src.TSP_solver import * - from src.io_tsp import * - -else: - from AI2019.src.utils import * - from AI2019.src.constructive_algorithms import * - from AI2019.src.local_search import * - from AI2019.src.meta_heuristics import * - from AI2019.src.TSP_solver import * - from AI2019.src.io_tsp import * diff --git a/src/io_tsp.py b/src/io_tsp.py index 776f6a4..c1256cc 100644 --- a/src/io_tsp.py +++ b/src/io_tsp.py @@ -1,25 +1,15 @@ import numpy as np -from typing import List from matplotlib import pyplot as plt -from numpy.core._multiarray_umath import ndarray from src.utils import distance_euc class ProblemInstance: - nPoints: int - best_sol: int - name: str - lines: List[str] - dist_matrix: ndarray - points: ndarray def __init__(self, name_tsp): self.exist_opt = False self.optimal_tour = None - self.read_instance(name_tsp) - - def read_instance(self, name_tsp): + self.dist_matrix = None # read raw data file_object = open(name_tsp) data = file_object.read() @@ -54,6 +44,7 @@ class ProblemInstance: self.optimal_tour[i] = int(line_i[0]) - 1 def print_info(self): + print("\n\n#############################") print('name: ' + self.name) print('nPoints: ' + str(self.nPoints)) print('best_sol: ' + str(self.best_sol)) diff --git a/src/iterated_local_search.py b/src/iterated_local_search.py index 9d31562..9dbb7d9 100644 --- a/src/iterated_local_search.py +++ b/src/iterated_local_search.py @@ -1,4 +1,4 @@ -class Iterated_Local_Search: +class IteratedLocalSearch: def __call__(self): pass diff --git a/src/local_search.py b/src/local_search.py deleted file mode 100644 index b28b04f..0000000 --- a/src/local_search.py +++ /dev/null @@ -1,3 +0,0 @@ - - - diff --git a/src/simulated_annealing.py b/src/simulated_annealing.py index 6751fcd..134ab86 100644 --- a/src/simulated_annealing.py +++ b/src/simulated_annealing.py @@ -1,6 +1,6 @@ import numpy as np -from src import compute_length +from src.utils import compute_length def sa(solution, instance, constant_temperature=0.95, iterations_for_each_temp=100): diff --git a/src/two_opt.py b/src/two_opt.py index 4a03ff3..1268423 100644 --- a/src/two_opt.py +++ b/src/two_opt.py @@ -1,6 +1,6 @@ import numpy as np -from src import compute_length +from src.utils import compute_length def step2opt(solution, matrix_dist, distance):