2019-10-31 17:58:06 +00:00
|
|
|
import pandas as pd
|
2020-09-25 09:15:15 +00:00
|
|
|
from src.io_tsp import Instance
|
|
|
|
from src.TSP_solver import SolverTSP
|
|
|
|
import numpy as np
|
2019-11-18 07:16:45 +00:00
|
|
|
|
2019-10-23 19:07:20 +00:00
|
|
|
|
2019-12-02 08:11:15 +00:00
|
|
|
def add(solver, instance, improve, index, results, name, verbose, show_plots):
|
|
|
|
solver.bind(improve)
|
|
|
|
solver(instance, return_value=False, verbose=verbose)
|
|
|
|
|
|
|
|
if verbose:
|
|
|
|
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}%",
|
|
|
|
f"the solution is found in {solver.time_to_solve} seconds", sep="\n")
|
|
|
|
|
|
|
|
index.append((name, solver.name_method))
|
|
|
|
results.append([solver.found_length, instance.best_sol, solver.gap, solver.time_to_solve])
|
|
|
|
|
|
|
|
if show_plots:
|
|
|
|
solver.plot_solution()
|
|
|
|
|
|
|
|
|
2019-11-09 15:52:13 +00:00
|
|
|
def run(show_plots=False, verbose=False):
|
2019-10-31 15:08:29 +00:00
|
|
|
# names = [name_ for name_ in os.listdir("./problems") if "tsp" in name_]
|
2019-12-02 07:26:52 +00:00
|
|
|
names = ["eil76.tsp"]
|
2020-09-25 09:15:15 +00:00
|
|
|
initializers = SolverTSP.available_initializers.keys()
|
|
|
|
improvements = SolverTSP.available_improvements.keys()
|
2019-10-31 17:58:06 +00:00
|
|
|
results = []
|
|
|
|
index = []
|
2019-10-23 19:07:20 +00:00
|
|
|
for name in names:
|
|
|
|
filename = f"problems/{name}"
|
|
|
|
instance = Instance(filename)
|
2019-11-09 15:52:13 +00:00
|
|
|
if verbose:
|
|
|
|
print("\n\n#############################")
|
|
|
|
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-11-18 07:15:29 +00:00
|
|
|
for init in initializers:
|
|
|
|
for improve in improvements:
|
2020-09-25 09:15:15 +00:00
|
|
|
solver = SolverTSP(init)
|
2019-12-02 08:11:15 +00:00
|
|
|
add(solver, instance, improve, index, results, name, verbose, show_plots)
|
|
|
|
for improve2 in [j for j in improvements if j not in [improve]]:
|
|
|
|
add(solver, instance, improve2, index, results, name, verbose, show_plots)
|
2019-10-31 15:17:47 +00:00
|
|
|
|
2019-12-02 08:11:15 +00:00
|
|
|
for improve3 in [j for j in improvements if j not in [improve, improve2]]:
|
|
|
|
add(solver, instance, improve3, index, results, name, verbose, show_plots)
|
|
|
|
solver.pop()
|
2019-11-09 15:52:13 +00:00
|
|
|
|
2019-12-02 08:11:15 +00:00
|
|
|
solver.pop()
|
2019-10-31 17:58:06 +00:00
|
|
|
|
2019-11-18 07:31:47 +00:00
|
|
|
if instance.exist_opt and show_plots:
|
2019-11-04 05:43:54 +00:00
|
|
|
solver.solution = np.concatenate([instance.optimal_tour, [instance.optimal_tour[0]]])
|
2019-11-04 05:44:32 +00:00
|
|
|
solver.method = "optimal"
|
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")
|