lab 2 ready
This commit is contained in:
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
|
@ -52,10 +52,17 @@ class TSPSolver:
|
||||||
print(f"### solving with {self.algorithms} ####")
|
print(f"### solving with {self.algorithms} ####")
|
||||||
start_time = t()
|
start_time = t()
|
||||||
self.solution = self.available_solvers[self.algorithms[0]](self.problem_instance)
|
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)):
|
for i in range(1, len(self.algorithms)):
|
||||||
self.solution = self.available_improvers[self.algorithms[i]](self.solution, self.problem_instance)
|
improver = self.algorithms[i]
|
||||||
assert self.check_if_solution_is_valid(), "Error the solution is not valid"
|
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()
|
end_time = t()
|
||||||
self.duration = np.around(end_time - start_time, 3)
|
self.duration = np.around(end_time - start_time, 3)
|
||||||
|
@ -75,17 +82,17 @@ class TSPSolver:
|
||||||
plt.show()
|
plt.show()
|
||||||
|
|
||||||
def check_if_solution_is_valid(self):
|
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(
|
rights_values = np.sum(
|
||||||
[1 if np.sum(self.solution[:-1] == i) == 1 else 0 for i in np.arange(self.problem_instance.nPoints)])
|
[self.check_validation(i, self.solution[:-1]) for i in np.arange(self.problem_instance.nPoints)])
|
||||||
return rights_values == 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):
|
def check_validation(self, node, solution):
|
||||||
# if np.sum(solution == node) == 1:
|
if np.sum(solution == node) == 1:
|
||||||
# return 1
|
return 1
|
||||||
# else:
|
else:
|
||||||
# return 0
|
return 0
|
||||||
|
|
||||||
def evaluate_solution(self, return_value=False):
|
def evaluate_solution(self, return_value=False):
|
||||||
total_length = 0
|
total_length = 0
|
||||||
|
|
|
@ -12,7 +12,7 @@ def compute_length(solution, dist_matrix):
|
||||||
|
|
||||||
|
|
||||||
def distance_euc(point_i, point_j):
|
def distance_euc(point_i, point_j):
|
||||||
rounding=0
|
rounding = 0
|
||||||
x_i, y_i = point_i[0], point_i[1]
|
x_i, y_i = point_i[0], point_i[1]
|
||||||
x_j, y_j = point_j[0], point_j[1]
|
x_j, y_j = point_j[0], point_j[1]
|
||||||
distance = np.sqrt((x_i - x_j) ** 2 + (y_i - y_j) ** 2)
|
distance = np.sqrt((x_i - x_j) ** 2 + (y_i - y_j) ** 2)
|
||||||
|
|
Reference in a new issue