more refactoring and code cleaning
This commit is contained in:
parent
c75ecc3d5b
commit
bba6d0f428
8 changed files with 13 additions and 45 deletions
15
run.py
15
run.py
|
@ -32,7 +32,6 @@ def run(show_plots=False, verbose=False):
|
||||||
for problem_path in problems:
|
for problem_path in problems:
|
||||||
prob_instance = ProblemInstance(problem_path)
|
prob_instance = ProblemInstance(problem_path)
|
||||||
if verbose:
|
if verbose:
|
||||||
print("\n\n#############################")
|
|
||||||
prob_instance.print_info()
|
prob_instance.print_info()
|
||||||
if show_plots:
|
if show_plots:
|
||||||
prob_instance.plot_data()
|
prob_instance.plot_data()
|
||||||
|
@ -41,15 +40,15 @@ def run(show_plots=False, verbose=False):
|
||||||
for improve in improvers_names:
|
for improve in improvers_names:
|
||||||
solver = SolverTSP(solver_name, prob_instance)
|
solver = SolverTSP(solver_name, prob_instance)
|
||||||
use_solver_to_compute_solution(solver, improve, index, results, problem_path, verbose, show_plots)
|
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]]:
|
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)
|
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]]:
|
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,
|
use_solver_to_compute_solution(solver, improve3, index, results, problem_path, verbose,
|
||||||
show_plots)
|
show_plots)
|
||||||
solver.pop()
|
solver.pop()
|
||||||
|
|
||||||
solver.pop()
|
solver.pop()
|
||||||
|
|
||||||
if prob_instance.exist_opt and show_plots:
|
if prob_instance.exist_opt and show_plots:
|
||||||
solver = SolverTSP("optimal", prob_instance)
|
solver = SolverTSP("optimal", prob_instance)
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
from numpy.core._multiarray_umath import ndarray
|
|
||||||
from time import time as t
|
from time import time as t
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
|
@ -20,12 +19,11 @@ available_improvers = {"2-opt": loop2opt,
|
||||||
|
|
||||||
|
|
||||||
class SolverTSP:
|
class SolverTSP:
|
||||||
solution: ndarray
|
|
||||||
found_length: float
|
|
||||||
|
|
||||||
def __init__(self, algorithm_name, problem_instance):
|
def __init__(self, algorithm_name, problem_instance):
|
||||||
# assert algorithm_name in available_solvers, f"the {algorithm_name} initializer is not available currently."
|
# assert algorithm_name in available_solvers, f"the {algorithm_name} initializer is not available currently."
|
||||||
self.duration = np.inf
|
self.duration = np.inf
|
||||||
|
self.found_length = np.inf
|
||||||
self.algorithm_name = algorithm_name
|
self.algorithm_name = algorithm_name
|
||||||
self.algorithms = [algorithm_name]
|
self.algorithms = [algorithm_name]
|
||||||
self.name_method = "initialized with " + algorithm_name
|
self.name_method = "initialized with " + algorithm_name
|
||||||
|
|
|
@ -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 *
|
|
|
@ -1,25 +1,15 @@
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from typing import List
|
|
||||||
from matplotlib import pyplot as plt
|
from matplotlib import pyplot as plt
|
||||||
from numpy.core._multiarray_umath import ndarray
|
|
||||||
|
|
||||||
from src.utils import distance_euc
|
from src.utils import distance_euc
|
||||||
|
|
||||||
|
|
||||||
class ProblemInstance:
|
class ProblemInstance:
|
||||||
nPoints: int
|
|
||||||
best_sol: int
|
|
||||||
name: str
|
|
||||||
lines: List[str]
|
|
||||||
dist_matrix: ndarray
|
|
||||||
points: ndarray
|
|
||||||
|
|
||||||
def __init__(self, name_tsp):
|
def __init__(self, name_tsp):
|
||||||
self.exist_opt = False
|
self.exist_opt = False
|
||||||
self.optimal_tour = None
|
self.optimal_tour = None
|
||||||
self.read_instance(name_tsp)
|
self.dist_matrix = None
|
||||||
|
|
||||||
def read_instance(self, name_tsp):
|
|
||||||
# read raw data
|
# read raw data
|
||||||
file_object = open(name_tsp)
|
file_object = open(name_tsp)
|
||||||
data = file_object.read()
|
data = file_object.read()
|
||||||
|
@ -54,6 +44,7 @@ class ProblemInstance:
|
||||||
self.optimal_tour[i] = int(line_i[0]) - 1
|
self.optimal_tour[i] = int(line_i[0]) - 1
|
||||||
|
|
||||||
def print_info(self):
|
def print_info(self):
|
||||||
|
print("\n\n#############################")
|
||||||
print('name: ' + self.name)
|
print('name: ' + self.name)
|
||||||
print('nPoints: ' + str(self.nPoints))
|
print('nPoints: ' + str(self.nPoints))
|
||||||
print('best_sol: ' + str(self.best_sol))
|
print('best_sol: ' + str(self.best_sol))
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
class Iterated_Local_Search:
|
class IteratedLocalSearch:
|
||||||
|
|
||||||
def __call__(self):
|
def __call__(self):
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import numpy as np
|
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):
|
def sa(solution, instance, constant_temperature=0.95, iterations_for_each_temp=100):
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
from src import compute_length
|
from src.utils import compute_length
|
||||||
|
|
||||||
|
|
||||||
def step2opt(solution, matrix_dist, distance):
|
def step2opt(solution, matrix_dist, distance):
|
||||||
|
|
Reference in a new issue