2020-09-28 09:56:36 +00:00
|
|
|
import glob
|
2020-12-20 18:39:46 +00:00
|
|
|
import pandas as pd
|
2020-12-21 14:46:50 +00:00
|
|
|
from aco.io_tsp import ProblemInstance
|
|
|
|
from aco.TSP_solver import TSPSolver
|
|
|
|
import os
|
2020-12-21 15:52:19 +00:00
|
|
|
import sys
|
2019-12-02 08:11:15 +00:00
|
|
|
|
2019-11-09 15:52:13 +00:00
|
|
|
def run(show_plots=False, verbose=False):
|
2020-12-21 15:52:19 +00:00
|
|
|
os.system("rm -f " + " ".join(glob.glob("sol/*.sol") + glob.glob("c_prob/*.txt")))
|
|
|
|
if sys.argv[1] == "all":
|
|
|
|
os.system("c++ -O2 -lpthread --std=c++11 -o c_prob/aco aco.cc opt.cc")
|
|
|
|
problems = glob.glob('./problems/*.tsp') if sys.argv[1] == "all" else [f"./problems/{sys.argv[1]}.tsp"]
|
2020-11-29 21:17:17 +00:00
|
|
|
|
2019-10-31 17:58:06 +00:00
|
|
|
results = []
|
|
|
|
index = []
|
2020-09-28 07:30:21 +00:00
|
|
|
for problem_path in problems:
|
|
|
|
prob_instance = ProblemInstance(problem_path)
|
2019-11-09 15:52:13 +00:00
|
|
|
if verbose:
|
2020-09-28 07:30:21 +00:00
|
|
|
prob_instance.print_info()
|
2019-10-23 19:19:38 +00:00
|
|
|
if show_plots:
|
2020-09-28 07:30:21 +00:00
|
|
|
prob_instance.plot_data()
|
2019-10-31 15:17:47 +00:00
|
|
|
|
2020-12-21 14:46:50 +00:00
|
|
|
solver = TSPSolver(prob_instance)
|
|
|
|
solution = solver.compute_solution(verbose=verbose)
|
|
|
|
if verbose:
|
|
|
|
print(f"the total length for the solution found is {solver.found_length}",
|
|
|
|
f"while the optimal length is {solver.problem_instance.best_sol}",
|
|
|
|
f"the gap is {solver.gap}%",
|
|
|
|
f"the solution is found in {solver.duration} seconds", sep="\n")
|
|
|
|
index.append((problem_path, "'C++ ant colony optimization'"))
|
|
|
|
results.append([solver.found_length,
|
|
|
|
solver.problem_instance.best_sol,
|
|
|
|
solver.gap,
|
|
|
|
solver.duration])
|
|
|
|
|
|
|
|
with open("sol/" + prob_instance.name + ".sol", "w") as f:
|
|
|
|
print(solution, file=f)
|
2019-10-31 15:17:47 +00:00
|
|
|
|
2020-12-21 14:46:50 +00:00
|
|
|
if show_plots:
|
2019-11-04 05:43:54 +00:00
|
|
|
solver.plot_solution()
|
|
|
|
|
2020-12-21 14:46:50 +00:00
|
|
|
return pd.DataFrame(results, index=index,
|
|
|
|
columns=["tour length", "optimal solution",
|
|
|
|
"gap", "time to solve"])
|
2019-10-23 19:11:32 +00:00
|
|
|
|
2019-10-31 18:05:53 +00:00
|
|
|
|
2019-10-23 19:07:20 +00:00
|
|
|
if __name__ == '__main__':
|
2020-12-21 14:46:50 +00:00
|
|
|
df = run(show_plots=False, verbose=True)
|
2019-11-18 07:31:03 +00:00
|
|
|
df.to_csv("./results.csv")
|