2020-09-28 09:56:36 +00:00
|
|
|
import glob
|
2019-10-31 17:58:06 +00:00
|
|
|
import pandas as pd
|
2020-09-28 07:30:21 +00:00
|
|
|
from src.io_tsp import ProblemInstance
|
|
|
|
from src.TSP_solver import SolverTSP, available_improvers, available_solvers
|
2020-09-25 09:15:15 +00:00
|
|
|
import numpy as np
|
2019-11-18 07:16:45 +00:00
|
|
|
|
2019-10-23 19:07:20 +00:00
|
|
|
|
2020-09-28 09:07:19 +00:00
|
|
|
def use_solver_to_compute_solution(solver, improve, index, results, name, verbose, show_plots):
|
2019-12-02 08:11:15 +00:00
|
|
|
solver.bind(improve)
|
2020-09-28 07:30:21 +00:00
|
|
|
solver.compute_solution(return_value=False, verbose=verbose)
|
2019-12-02 08:11:15 +00:00
|
|
|
|
|
|
|
if verbose:
|
|
|
|
print(f"the total length for the solution found is {solver.found_length}",
|
2020-09-28 07:30:21 +00:00
|
|
|
f"while the optimal length is {solver.problem_instance.best_sol}",
|
2019-12-02 08:11:15 +00:00
|
|
|
f"the gap is {solver.gap}%",
|
2020-09-28 07:30:21 +00:00
|
|
|
f"the solution is found in {solver.duration} seconds", sep="\n")
|
2019-12-02 08:11:15 +00:00
|
|
|
|
|
|
|
index.append((name, solver.name_method))
|
2020-09-28 07:30:21 +00:00
|
|
|
results.append([solver.found_length, solver.problem_instance.best_sol, solver.gap, solver.duration])
|
2019-12-02 08:11:15 +00:00
|
|
|
|
|
|
|
if show_plots:
|
|
|
|
solver.plot_solution()
|
|
|
|
|
|
|
|
|
2019-11-09 15:52:13 +00:00
|
|
|
def run(show_plots=False, verbose=False):
|
2020-10-01 16:03:04 +00:00
|
|
|
# problems = glob.glob('./problems/*.tsp')
|
|
|
|
problems = ["./problems/eil76.tsp"]
|
2020-09-28 07:30:21 +00:00
|
|
|
solvers_names = available_solvers.keys()
|
|
|
|
improvers_names = available_improvers.keys()
|
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-09-28 07:30:21 +00:00
|
|
|
for solver_name in solvers_names:
|
|
|
|
for improve in improvers_names:
|
|
|
|
solver = SolverTSP(solver_name, prob_instance)
|
2020-09-28 09:07:19 +00:00
|
|
|
use_solver_to_compute_solution(solver, improve, index, results, problem_path, verbose, show_plots)
|
2020-09-28 10:13:53 +00:00
|
|
|
for improve2 in [j for j in improvers_names if j not in [improve]]:
|
|
|
|
use_solver_to_compute_solution(solver, improve2, index, results, problem_path, verbose, show_plots)
|
2019-10-31 15:17:47 +00:00
|
|
|
|
2020-09-28 10:13:53 +00:00
|
|
|
for improve3 in [j for j in improvers_names if j not in [improve, improve2]]:
|
|
|
|
use_solver_to_compute_solution(solver, improve3, index, results, problem_path, verbose,
|
|
|
|
show_plots)
|
|
|
|
solver.pop()
|
2019-11-09 15:52:13 +00:00
|
|
|
|
2020-09-28 10:13:53 +00:00
|
|
|
solver.pop()
|
2019-10-31 17:58:06 +00:00
|
|
|
|
2020-09-28 07:30:21 +00:00
|
|
|
if prob_instance.exist_opt and show_plots:
|
2020-09-28 09:22:14 +00:00
|
|
|
solver = SolverTSP("optimal", prob_instance)
|
2020-09-28 09:56:36 +00:00
|
|
|
solver.solved = True
|
2020-09-28 07:30:21 +00:00
|
|
|
solver.solution = np.concatenate([prob_instance.optimal_tour, [prob_instance.optimal_tour[0]]])
|
2019-11-04 05:43:54 +00:00
|
|
|
solver.plot_solution()
|
|
|
|
|
2019-10-31 17:58:06 +00:00
|
|
|
index = pd.MultiIndex.from_tuples(index, names=['problem', 'method'])
|
2019-10-31 17:33:22 +00:00
|
|
|
|
2019-10-31 17:58:06 +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__':
|
2019-11-18 07:31:03 +00:00
|
|
|
df = run(show_plots=False, verbose=True)
|
|
|
|
df.to_csv("./results.csv")
|