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/code/run.py

52 lines
1.9 KiB
Python

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):
if sys.argv[1] == "all":
os.system("rm -f " + " ".join(glob.glob("sol/*.sol") + glob.glob("c_prob/*.txt")))
os.system("c++ -O2 -lpthread --std=c++11 -o c_prob/aco aco.cc opt.cc")
else:
os.system("rm -f " + " ".join(glob.glob("c_prob/*.txt")))
problems = glob.glob('./problems/*.tsp') if sys.argv[1] == "all" else ["./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(list(map(lambda x: x + 1, 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")