First pass of refactor

This commit is contained in:
Dario Mantegazza 2020-09-25 11:15:15 +02:00
parent d66374479f
commit f0c6a6d181
8 changed files with 36 additions and 18 deletions

14
.idea/.gitignore vendored Normal file
View File

@ -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

11
run.py
View File

@ -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"

View File

@ -9,7 +9,7 @@ else:
from AI2019.src import *
class Solver_TSP:
class SolverTSP:
solution: ndarray
found_length: float

View File

@ -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

View File

@ -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

View File

@ -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:

View File

@ -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

View File

@ -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)