udpate
This commit is contained in:
parent
4269a1b45a
commit
ef19a8b0a4
3 changed files with 17 additions and 3 deletions
17
run.py
17
run.py
|
@ -1,5 +1,5 @@
|
||||||
from src import *
|
from src import *
|
||||||
import os
|
from concorde.tsp import TSPSolver
|
||||||
|
|
||||||
|
|
||||||
def run(show_plots=False):
|
def run(show_plots=False):
|
||||||
|
@ -14,6 +14,15 @@ def run(show_plots=False):
|
||||||
if show_plots:
|
if show_plots:
|
||||||
instance.plot_data()
|
instance.plot_data()
|
||||||
|
|
||||||
|
solver = TSPSolver.from_data(
|
||||||
|
instance.points[:, 1]*100,
|
||||||
|
instance.points[:, 2]*100,
|
||||||
|
norm="EUC_2D"
|
||||||
|
)
|
||||||
|
solution = solver.solve(verbose=False)
|
||||||
|
tour_opt = np.copy(solution.tour)
|
||||||
|
|
||||||
|
|
||||||
for method in ["random", "nearest_neighbors", "best_nn"]:
|
for method in ["random", "nearest_neighbors", "best_nn"]:
|
||||||
solver = Solver_TSP(method)
|
solver = Solver_TSP(method)
|
||||||
solver(instance, return_value=False)
|
solver(instance, return_value=False)
|
||||||
|
@ -24,6 +33,12 @@ def run(show_plots=False):
|
||||||
if show_plots:
|
if show_plots:
|
||||||
solver.plot_solution()
|
solver.plot_solution()
|
||||||
|
|
||||||
|
solver.method = "optimal"
|
||||||
|
solver.solution = np.concatenate([tour_opt, [tour_opt[0]]])
|
||||||
|
solver.solved = True
|
||||||
|
solver.plot_solution()
|
||||||
|
print(solver.evaluate_solution(return_value=True))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
run(show_plots=True)
|
run(show_plots=True)
|
||||||
|
|
|
@ -64,7 +64,6 @@ class Solver_TSP:
|
||||||
self.solved = True
|
self.solved = True
|
||||||
return self.solution
|
return self.solution
|
||||||
|
|
||||||
|
|
||||||
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))
|
||||||
|
|
|
@ -51,7 +51,7 @@ class Instance:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def distance_euc(zi, zj):
|
def distance_euc(zi, zj):
|
||||||
xi, xj = zi[0], zj[0]
|
xi, xj = zi[0], zj[0]
|
||||||
yi, yj = zi[0], zj[0]
|
yi, yj = zi[1], zj[1]
|
||||||
return round(np.sqrt((xi - xj) ** 2 + (yi - yj) ** 2))
|
return round(np.sqrt((xi - xj) ** 2 + (yi - yj) ** 2))
|
||||||
|
|
||||||
def create_dist_matrix(self):
|
def create_dist_matrix(self):
|
||||||
|
|
Reference in a new issue