This commit is contained in:
UmbertoJr 2019-10-31 18:58:06 +01:00
parent ef19a8b0a4
commit 9b1694c153

35
run.py
View File

@ -1,11 +1,13 @@
from src import * from src import *
from concorde.tsp import TSPSolver import pandas as pd
from time import time as t
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 = ["eil76.tsp"]
methods = ["random", "nearest_neighbors", "best_nn"]
results = []
index = []
for name in names: for name in names:
print("\n\n#############################") print("\n\n#############################")
filename = f"problems/{name}" filename = f"problems/{name}"
@ -14,31 +16,24 @@ def run(show_plots=False):
if show_plots: if show_plots:
instance.plot_data() instance.plot_data()
solver = TSPSolver.from_data( for method in methods:
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"]:
solver = Solver_TSP(method) solver = Solver_TSP(method)
start = t()
solver(instance, return_value=False) solver(instance, return_value=False)
end = t()
print(f"the total length for the solution found is {solver.found_length}", 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")
index.append((name, method))
results.append([solver.found_length, instance.best_sol, solver.gap, start - end])
if show_plots: if show_plots:
solver.plot_solution() solver.plot_solution()
solver.method = "optimal" index = pd.MultiIndex.from_tuples(index, names=['problem', 'method'])
solver.solution = np.concatenate([tour_opt, [tour_opt[0]]])
solver.solved = True
solver.plot_solution()
print(solver.evaluate_solution(return_value=True))
return pd.DataFrame(results, index=index, columns=["tour length", "optimal solution", "gap", "time to solve"])
if __name__ == '__main__': if __name__ == '__main__':
run(show_plots=True) print(run(show_plots=True))