import glob import pandas as pd from aco.io_tsp import ProblemInstance from aco.TSP_solver import TSPSolver import os import sys def run(show_plots=False, verbose=False): 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"] results = [] index = [] for problem_path in problems: prob_instance = ProblemInstance(problem_path) if verbose: prob_instance.print_info() if show_plots: prob_instance.plot_data() 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) if show_plots: solver.plot_solution() return pd.DataFrame(results, index=index, columns=["tour length", "optimal solution", "gap", "time to solve"]) if __name__ == '__main__': df = run(show_plots=False, verbose=True) df.to_csv("./results.csv")