solver
This commit is contained in:
parent
c2aaa9e5f7
commit
f5b513f75c
1 changed files with 72 additions and 70 deletions
|
@ -3,24 +3,26 @@ from matplotlib import pyplot as plt
|
||||||
from numpy.core._multiarray_umath import ndarray
|
from numpy.core._multiarray_umath import ndarray
|
||||||
import os
|
import os
|
||||||
if 'AI' in os.getcwd():
|
if 'AI' in os.getcwd():
|
||||||
from src.utils import *
|
from src import *
|
||||||
else:
|
else:
|
||||||
from AI2019.src.utils import *
|
from AI2019.src import *
|
||||||
|
|
||||||
|
|
||||||
class Solver_TSP:
|
class Solver_TSP:
|
||||||
|
|
||||||
solution: ndarray
|
solution: ndarray
|
||||||
found_length: float
|
found_length: float
|
||||||
available_methods = {"random": lambda x: x, "nearest_neighbors": lambda x: x,
|
available_initializers = {"random": random_initialier.random_method,
|
||||||
"best_nn": lambda x: x, "multi_fragment": lambda x: x}
|
"nearest_neighbors": nearest_neighbor.nn,
|
||||||
|
"best_nn": nearest_neighbor.best_nn,
|
||||||
|
"multi_fragment": multi_fragment.mf}
|
||||||
|
|
||||||
def __init__(self, method):
|
def __init__(self, method):
|
||||||
self.available_methods = {"random": self.random_method, "nearest_neighbors": self.nn,
|
# self.available_methods = {"random": self.random_method, "nearest_neighbors": self.nn,
|
||||||
"best_nn": self.best_nn, "multi_fragment": self.mf}
|
# "best_nn": self.best_nn, "multi_fragment": self.mf}
|
||||||
self.method = method
|
self.method = method
|
||||||
self.solved = False
|
self.solved = False
|
||||||
assert method in self.available_methods, f"the {method} method is not available currently."
|
assert method in self.available_initializers, f"the {method} method is not available currently."
|
||||||
|
|
||||||
def __call__(self, instance_, verbose=True, return_value=True):
|
def __call__(self, instance_, verbose=True, return_value=True):
|
||||||
self.instance = instance_
|
self.instance = instance_
|
||||||
|
@ -37,69 +39,69 @@ class Solver_TSP:
|
||||||
if return_value:
|
if return_value:
|
||||||
return self.solution
|
return self.solution
|
||||||
|
|
||||||
def random_method(self, instance_):
|
# def random_method(self, instance_):
|
||||||
n = int(instance_.nPoints)
|
# n = int(instance_.nPoints)
|
||||||
solution = np.random.choice(np.arange(n), size=n, replace=False)
|
# solution = np.random.choice(np.arange(n), size=n, replace=False)
|
||||||
self.solution = np.concatenate([solution, [solution[0]]])
|
# self.solution = np.concatenate([solution, [solution[0]]])
|
||||||
self.solved = True
|
# self.solved = True
|
||||||
return self.solution
|
# return self.solution
|
||||||
|
#
|
||||||
def nn(self, instance_, starting_node=0):
|
# def nn(self, instance_, starting_node=0):
|
||||||
dist_matrix = np.copy(instance_.dist_matrix)
|
# dist_matrix = np.copy(instance_.dist_matrix)
|
||||||
n = int(instance_.nPoints)
|
# n = int(instance_.nPoints)
|
||||||
node = starting_node
|
# node = starting_node
|
||||||
tour = [node]
|
# tour = [node]
|
||||||
for _ in range(n - 1):
|
# for _ in range(n - 1):
|
||||||
for new_node in np.argsort(dist_matrix[node]):
|
# for new_node in np.argsort(dist_matrix[node]):
|
||||||
if new_node not in tour:
|
# if new_node not in tour:
|
||||||
tour.append(new_node)
|
# tour.append(new_node)
|
||||||
node = new_node
|
# node = new_node
|
||||||
break
|
# break
|
||||||
tour.append(starting_node)
|
# tour.append(starting_node)
|
||||||
self.solution = np.array(tour)
|
# self.solution = np.array(tour)
|
||||||
self.solved = True
|
# self.solved = True
|
||||||
return self.solution
|
# return self.solution
|
||||||
|
#
|
||||||
def best_nn(self, instance_):
|
# def best_nn(self, instance_):
|
||||||
solutions, lens = [], []
|
# solutions, lens = [], []
|
||||||
for start in range(self.instance.nPoints):
|
# for start in range(self.instance.nPoints):
|
||||||
new_solution = self.nn(instance_, starting_node=start)
|
# new_solution = self.nn(instance_, starting_node=start)
|
||||||
solutions.append(new_solution)
|
# solutions.append(new_solution)
|
||||||
assert self.check_if_solution_is_valid(new_solution), "error on best_nn method"
|
# assert self.check_if_solution_is_valid(new_solution), "error on best_nn method"
|
||||||
lens.append(self.evaluate_solution(return_value=True))
|
# lens.append(self.evaluate_solution(return_value=True))
|
||||||
|
#
|
||||||
self.solution = solutions[np.argmin(lens)]
|
# self.solution = solutions[np.argmin(lens)]
|
||||||
self.solved = True
|
# self.solved = True
|
||||||
return self.solution
|
# return self.solution
|
||||||
|
#
|
||||||
def mf(self, instance):
|
# def mf(self, instance):
|
||||||
mat = np.copy(instance.dist_matrix)
|
# mat = np.copy(instance.dist_matrix)
|
||||||
mat = np.triu(mat)
|
# mat = np.triu(mat)
|
||||||
mat[mat == 0] = 100000
|
# mat[mat == 0] = 100000
|
||||||
solution = {str(i): [] for i in range(instance.nPoints)}
|
# solution = {str(i): [] for i in range(instance.nPoints)}
|
||||||
start_list = [i for i in range(instance.nPoints)]
|
# start_list = [i for i in range(instance.nPoints)]
|
||||||
inside = 0
|
# inside = 0
|
||||||
for el in np.argsort(mat.flatten()):
|
# for el in np.argsort(mat.flatten()):
|
||||||
node1, node2 = el // instance.nPoints, el % instance.nPoints
|
# node1, node2 = el // instance.nPoints, el % instance.nPoints
|
||||||
possible_edge = [node1, node2]
|
# possible_edge = [node1, node2]
|
||||||
if multi_fragment.check_if_available(node1, node2, solution):
|
# if multi_fragment.check_if_available(node1, node2, solution):
|
||||||
if multi_fragment.check_if_not_close(possible_edge, solution):
|
# if multi_fragment.check_if_not_close(possible_edge, solution):
|
||||||
# print("entrato", inside)
|
# # print("entrato", inside)
|
||||||
solution[str(node1)].append(node2)
|
# solution[str(node1)].append(node2)
|
||||||
solution[str(node2)].append(node1)
|
# solution[str(node2)].append(node1)
|
||||||
if len(solution[str(node1)]) == 2:
|
# if len(solution[str(node1)]) == 2:
|
||||||
start_list.remove(node1)
|
# start_list.remove(node1)
|
||||||
if len(solution[str(node2)]) == 2:
|
# if len(solution[str(node2)]) == 2:
|
||||||
start_list.remove(node2)
|
# start_list.remove(node2)
|
||||||
inside += 1
|
# inside += 1
|
||||||
# print(node1, node2, inside)
|
# # print(node1, node2, inside)
|
||||||
if inside == instance.nPoints - 1:
|
# if inside == instance.nPoints - 1:
|
||||||
# print(f"ricostruire la solutione da {start_list}",
|
# # print(f"ricostruire la solutione da {start_list}",
|
||||||
# f"vicini di questi due nodi {[solution[str(i)] for i in start_list]}")
|
# # f"vicini di questi due nodi {[solution[str(i)] for i in start_list]}")
|
||||||
solution = multi_fragment.create_solution(start_list, solution)
|
# solution = multi_fragment.create_solution(start_list, solution)
|
||||||
self.solution = solution
|
# self.solution = solution
|
||||||
self.solved = True
|
# self.solved = True
|
||||||
return self.solution
|
# return self.solution
|
||||||
|
|
||||||
def plot_solution(self):
|
def plot_solution(self):
|
||||||
assert self.solved, "You can't plot the solution, you need to solve it first!"
|
assert self.solved, "You can't plot the solution, you need to solve it first!"
|
||||||
|
|
Reference in a new issue