lab 2 ready

This commit is contained in:
Dario Mantegazza 2020-11-01 19:24:26 +01:00
parent 6947aaacb1
commit 4598f90ed7
4 changed files with 906 additions and 121 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -52,10 +52,17 @@ class TSPSolver:
print(f"### solving with {self.algorithms} ####")
start_time = t()
self.solution = self.available_solvers[self.algorithms[0]](self.problem_instance)
assert self.check_if_solution_is_valid(), "Error the solution is not valid"
if self.check_if_solution_is_valid():
print(f"Error the solution of {self.algorithm_name} for problem {self.problem_instance.name} is not valid")
if return_value:
return False
for i in range(1, len(self.algorithms)):
self.solution = self.available_improvers[self.algorithms[i]](self.solution, self.problem_instance)
assert self.check_if_solution_is_valid(), "Error the solution is not valid"
improver = self.algorithms[i]
self.solution = self.available_improvers[improver](self.solution, self.problem_instance)
if self.check_if_solution_is_valid():
print(f"Error the solution of {self.algorithm_name} with {improver} for problem {self.problem_instance.name} is not valid")
if return_value:
return False
end_time = t()
self.duration = np.around(end_time - start_time, 3)
@ -75,17 +82,17 @@ class TSPSolver:
plt.show()
def check_if_solution_is_valid(self):
# rights_values = np.sum(
# [self.check_validation(i, solution[:-1]) for i in np.arange(self.problem_instance.nPoints)])
rights_values = np.sum(
[1 if np.sum(self.solution[:-1] == i) == 1 else 0 for i in np.arange(self.problem_instance.nPoints)])
return rights_values == self.problem_instance.nPoints
[self.check_validation(i, self.solution[:-1]) for i in np.arange(self.problem_instance.nPoints)])
# rights_values = np.sum(
# [1 if np.sum(self.solution[:-1] == i) == 1 else 0 for i in np.arange(self.problem_instance.nPoints)])
# return rights_values == self.problem_instance.nPoints
# def check_validation(self, node, solution):
# if np.sum(solution == node) == 1:
# return 1
# else:
# return 0
def check_validation(self, node, solution):
if np.sum(solution == node) == 1:
return 1
else:
return 0
def evaluate_solution(self, return_value=False):
total_length = 0

View File

@ -12,7 +12,7 @@ def compute_length(solution, dist_matrix):
def distance_euc(point_i, point_j):
rounding=0
rounding = 0
x_i, y_i = point_i[0], point_i[1]
x_j, y_j = point_j[0], point_j[1]
distance = np.sqrt((x_i - x_j) ** 2 + (y_i - y_j) ** 2)