This commit is contained in:
UmbertoJr 2019-11-10 10:42:13 +01:00
parent 0e0dca1b9b
commit da5d4ca8ef
1 changed files with 50 additions and 9 deletions

View File

@ -1,3 +1,53 @@
import numpy as np
def compute_lenght(solution, dist_matrix):
total_length = 0
starting_node = solution[0]
from_node = starting_node
for node in solution[1:]:
total_length += dist_matrix[from_node, node]
from_node = node
return total_length
class random_initialier:
@staticmethod
def random_method(instance_):
n = int(instance_.nPoints)
solution = np.random.choice(np.arange(n), size=n, replace=False)
return np.concatenate([solution, [solution[0]]])
class nearest_neighbor:
@staticmethod
def nn(instance_, starting_node=0):
dist_matrix = np.copy(instance_.dist_matrix)
n = int(instance_.nPoints)
node = starting_node
tour = [node]
for _ in range(n - 1):
for new_node in np.argsort(dist_matrix[node]):
if new_node not in tour:
tour.append(new_node)
node = new_node
break
tour.append(starting_node)
return np.array(tour)
@staticmethod
def best_nn(self, instance_):
solutions, lens = [], []
for start in range(self.instance.nPoints):
new_solution = self.nn(instance_, starting_node=start)
solutions.append(new_solution)
assert self.check_if_solution_is_valid(new_solution), "error on best_nn method"
lens.append(self.evaluate_solution(return_value=True))
self.solution = solutions[np.argmin(lens)]
self.solved = True
return self.solution
class multi_fragment:
@staticmethod
@ -63,12 +113,3 @@ class multi_fragment:
sol_list.append(n1)
return sol_list
def compute_lenght(solution, dist_matrix):
total_length = 0
starting_node = solution[0]
from_node = starting_node
for node in solution[1:]:
total_length += dist_matrix[from_node, node]
from_node = node
return total_length