This commit is contained in:
UmbertoJr 2019-11-04 06:43:54 +01:00
parent 677e5f7656
commit de3e3f0793
2 changed files with 7 additions and 1 deletions

4
run.py
View file

@ -32,6 +32,10 @@ def run(show_plots=False):
if show_plots: if show_plots:
solver.plot_solution() solver.plot_solution()
if instance.optimal_tour:
solver.solution = np.concatenate([instance.optimal_tour, [instance.optimal_tour[0]]])
solver.plot_solution()
index = pd.MultiIndex.from_tuples(index, names=['problem', 'method']) index = pd.MultiIndex.from_tuples(index, names=['problem', 'method'])
return pd.DataFrame(results, index=index, columns=["tour length", "optimal solution", "gap", "time to solve"]) return pd.DataFrame(results, index=index, columns=["tour length", "optimal solution", "gap", "time to solve"])

View file

@ -17,6 +17,7 @@ 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_
self.solved = False
if verbose: if verbose:
print(f"### solving with {self.method} ####") print(f"### solving with {self.method} ####")
self.solution = self.available_methods[self.method](instance_) self.solution = self.available_methods[self.method](instance_)
@ -67,7 +68,8 @@ 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(f"{self.instance.name} with gap {self.gap}") self._gap()
plt.title(f"{self.instance.name} solved with {self.method}, 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()