This repository has been archived on 2021-10-31. You can view files and clone it, but cannot push or open issues or pull requests.
AICup/run.py

45 lines
1.3 KiB
Python
Raw Normal View History

2019-10-31 15:04:38 +00:00
from src import *
2019-10-31 17:33:22 +00:00
from concorde.tsp import TSPSolver
2019-10-23 19:07:20 +00:00
2019-10-23 19:19:38 +00:00
def run(show_plots=False):
2019-10-31 15:08:29 +00:00
# names = [name_ for name_ in os.listdir("./problems") if "tsp" in name_]
names = ["ch130.tsp"]
2019-10-31 15:17:47 +00:00
2019-10-23 19:07:20 +00:00
for name in names:
2019-10-23 19:19:38 +00:00
print("\n\n#############################")
2019-10-23 19:07:20 +00:00
filename = f"problems/{name}"
instance = Instance(filename)
instance.print_info()
2019-10-23 19:19:38 +00:00
if show_plots:
instance.plot_data()
2019-10-31 15:17:47 +00:00
2019-10-31 17:33:22 +00:00
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)
2019-10-31 15:32:43 +00:00
for method in ["random", "nearest_neighbors", "best_nn"]:
2019-10-31 15:17:47 +00:00
solver = Solver_TSP(method)
solver(instance, return_value=False)
print(f"the total length for the solution found is {solver.found_length}",
f"while the optimal length is {instance.best_sol}",
f"the gap is {solver.gap} %", sep="\n")
if show_plots:
solver.plot_solution()
2019-10-23 19:11:32 +00:00
2019-10-31 17:33:22 +00:00
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))
2019-10-23 19:11:32 +00:00
2019-10-23 19:07:20 +00:00
if __name__ == '__main__':
2019-10-31 15:08:29 +00:00
run(show_plots=True)