This commit is contained in:
UmbertoJr 2019-10-31 16:17:47 +01:00
parent d4a251006d
commit 0af91eac3c
2 changed files with 17 additions and 12 deletions

9
run.py
View file

@ -5,20 +5,23 @@ import os
def run(show_plots=False): def run(show_plots=False):
# names = [name_ for name_ in os.listdir("./problems") if "tsp" in name_] # names = [name_ for name_ in os.listdir("./problems") if "tsp" in name_]
names = ["ch130.tsp"] names = ["ch130.tsp"]
for name in names: for name in names:
print("\n\n#############################") print("\n\n#############################")
filename = f"problems/{name}" filename = f"problems/{name}"
instance = Instance(filename) instance = Instance(filename)
instance.print_info() instance.print_info()
if show_plots:
instance.plot_data()
solver = Solver_TSP('random') for method in ["random", "nearest_neighbors"]:
solver = Solver_TSP(method)
solver(instance, return_value=False) solver(instance, return_value=False)
print(f"the total length for the solution found is {solver.evaluate_solution()}", print(f"the total length for the solution found is {solver.found_length}",
f"while the optimal length is {instance.best_sol}", f"while the optimal length is {instance.best_sol}",
f"the gap is {solver.gap} %", sep="\n") f"the gap is {solver.gap} %", sep="\n")
if show_plots: if show_plots:
instance.plot_data()
solver.plot_solution() solver.plot_solution()

View file

@ -17,11 +17,13 @@ class Solver_TSP:
def __call__(self, instance_, verbose=True, return_value=True): def __call__(self, instance_, verbose=True, return_value=True):
self.instance = instance_ self.instance = instance_
if verbose: if verbose:
print("### solving ####") print(f"### solving with {self.method}####")
self.solution = self.available_methods[self.method](instance_) self.solution = self.available_methods[self.method](instance_)
assert self.check_if_solution_is_valid(self.solution), "Error the solution is not valid" assert self.check_if_solution_is_valid(self.solution), "Error the solution is not valid"
self.evaluate_solution()
self._gap()
if verbose: if verbose:
print("### solution found ####") print(f"### solution found with {self.gap} ####")
self._gap() self._gap()
if return_value: if return_value:
return self.solution return self.solution
@ -50,7 +52,7 @@ class Solver_TSP:
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!"
plt.figure(figsize=(8, 8)) plt.figure(figsize=(8, 8))
plt.title(self.instance.name) plt.title(f"{self.instance.name} with gap {self.gap}")
ordered_points = self.instance.points[self.solution] ordered_points = self.instance.points[self.solution]
plt.plot(ordered_points[:, 1], ordered_points[:, 2], 'b-') plt.plot(ordered_points[:, 1], ordered_points[:, 2], 'b-')
plt.show() plt.show()
@ -68,7 +70,7 @@ class Solver_TSP:
else: else:
return 0 return 0
def evaluate_solution(self, return_value=True): def evaluate_solution(self, return_value=False):
total_length = 0 total_length = 0
starting_node = self.solution[0] starting_node = self.solution[0]
from_node = starting_node from_node = starting_node