diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..51a6ea7 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,14 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/../../../../:\code\AI2020BsC\.idea/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ +/AI2020BsC.iml +/misc.xml +/modules.xml +/other.xml +/inspectionProfiles/profiles_settings.xml +/vcs.xml diff --git a/run.py b/run.py index db82057..06984d4 100644 --- a/run.py +++ b/run.py @@ -1,5 +1,7 @@ -from src import * import pandas as pd +from src.io_tsp import Instance +from src.TSP_solver import SolverTSP +import numpy as np def add(solver, instance, improve, index, results, name, verbose, show_plots): @@ -22,8 +24,8 @@ def add(solver, instance, improve, index, results, name, verbose, show_plots): def run(show_plots=False, verbose=False): # names = [name_ for name_ in os.listdir("./problems") if "tsp" in name_] names = ["eil76.tsp"] - initializers = Solver_TSP.available_initializers.keys() - improvements = Solver_TSP.available_improvements.keys() + initializers = SolverTSP.available_initializers.keys() + improvements = SolverTSP.available_improvements.keys() results = [] index = [] for name in names: @@ -37,7 +39,7 @@ def run(show_plots=False, verbose=False): for init in initializers: for improve in improvements: - solver = Solver_TSP(init) + solver = SolverTSP(init) add(solver, instance, improve, index, results, name, verbose, show_plots) for improve2 in [j for j in improvements if j not in [improve]]: add(solver, instance, improve2, index, results, name, verbose, show_plots) @@ -48,7 +50,6 @@ def run(show_plots=False, verbose=False): solver.pop() - if instance.exist_opt and show_plots: solver.solution = np.concatenate([instance.optimal_tour, [instance.optimal_tour[0]]]) solver.method = "optimal" diff --git a/src/TSP_solver.py b/src/TSP_solver.py index 530cac3..44e0596 100644 --- a/src/TSP_solver.py +++ b/src/TSP_solver.py @@ -9,7 +9,7 @@ else: from AI2019.src import * -class Solver_TSP: +class SolverTSP: solution: ndarray found_length: float diff --git a/src/constructive_algorithms.py b/src/constructive_algorithms.py index 1ea453d..2d2bdc3 100644 --- a/src/constructive_algorithms.py +++ b/src/constructive_algorithms.py @@ -36,7 +36,7 @@ class nearest_neighbor: for start in range(instance_.nPoints): new_solution = nearest_neighbor.nn(instance_, starting_node=start) solutions.append(new_solution) - lens.append(compute_lenght(new_solution, instance_.dist_matrix)) + lens.append(compute_length(new_solution, instance_.dist_matrix)) solution = solutions[np.argmin(lens)] return solution diff --git a/src/io_tsp.py b/src/io_tsp.py index d4b41fd..b86e622 100644 --- a/src/io_tsp.py +++ b/src/io_tsp.py @@ -3,6 +3,8 @@ from typing import List from matplotlib import pyplot as plt from numpy.core._multiarray_umath import ndarray +from src.utils import distance_euc + class Instance: nPoints: int @@ -14,6 +16,7 @@ class Instance: def __init__(self, name_tsp): self.read_instance(name_tsp) + self.exist_opt = None # TODO determine default value def read_instance(self, name_tsp): # read raw data @@ -63,17 +66,13 @@ class Instance: plt.annotate(txt, (self.points[i, 1], self.points[i, 2])) plt.show() - @staticmethod - def distance_euc(zi, zj): - xi, xj = zi[0], zj[0] - yi, yj = zi[1], zj[1] - return round(np.sqrt((xi - xj) ** 2 + (yi - yj) ** 2), 0) + def create_dist_matrix(self): self.dist_matrix = np.zeros((self.nPoints, self.nPoints)) for i in range(self.nPoints): for j in range(i, self.nPoints): - self.dist_matrix[i, j] = self.distance_euc(self.points[i][1:3], self.points[j][1:3]) + self.dist_matrix[i, j] = distance_euc(self.points[i][1:3], self.points[j][1:3]) self.dist_matrix += self.dist_matrix.T diff --git a/src/local_search.py b/src/local_search.py index 62be9a9..74a5cdc 100644 --- a/src/local_search.py +++ b/src/local_search.py @@ -41,7 +41,7 @@ class TwoOpt: @staticmethod def loop2opt(solution, instance, max_num_of_uncrosses=10000): matrix_dist = instance.dist_matrix - new_len = compute_lenght(solution, matrix_dist) + new_len = compute_length(solution, matrix_dist) new_tsp_sequence = np.copy(np.array(solution)) uncross = 0 while uncross < max_num_of_uncrosses: @@ -119,7 +119,7 @@ class TwoDotFiveOpt: @staticmethod def loop2dot5opt(solution, instance, max_num_of_changes=10000): matrix_dist = instance.dist_matrix - actual_len = compute_lenght(solution, matrix_dist) + actual_len = compute_length(solution, matrix_dist) new_tsp_sequence = np.copy(np.array(solution)) uncross = 0 while uncross < max_num_of_changes: diff --git a/src/meta_heuristics.py b/src/meta_heuristics.py index 1f554b3..7cc1baf 100644 --- a/src/meta_heuristics.py +++ b/src/meta_heuristics.py @@ -14,7 +14,7 @@ class Simulated_Annealing: # initial setup temperature = instance.best_sol / np.sqrt(instance.nPoints) current_sol = np.array(solution) - current_len = compute_lenght(solution, instance.dist_matrix) + current_len = compute_length(solution, instance.dist_matrix) best_sol = np.array(solution) best_len = current_len diff --git a/src/utils.py b/src/utils.py index 613ff65..4368e98 100644 --- a/src/utils.py +++ b/src/utils.py @@ -1,5 +1,4 @@ - -def compute_lenght(solution, dist_matrix): +def compute_length(solution, dist_matrix): total_length = 0 starting_node = solution[0] from_node = starting_node @@ -8,3 +7,8 @@ def compute_lenght(solution, dist_matrix): from_node = node return total_length + +def distance_euc(zi, zj): + xi, xj = zi[0], zj[0] + yi, yj = zi[1], zj[1] + return round(np.sqrt((xi - xj) ** 2 + (yi - yj) ** 2), 0)