solver
This commit is contained in:
parent
2923872054
commit
00c1eb8511
2 changed files with 22 additions and 17 deletions
31
run.py
31
run.py
|
@ -6,7 +6,8 @@ from time import time as t
|
||||||
def run(show_plots=False, verbose=False):
|
def run(show_plots=False, verbose=False):
|
||||||
# names = [name_ for name_ in os.listdir("./problems") if "tsp" in name_]
|
# names = [name_ for name_ in os.listdir("./problems") if "tsp" in name_]
|
||||||
names = ["eil76.tsp", "kroA100.tsp"]
|
names = ["eil76.tsp", "kroA100.tsp"]
|
||||||
methods = Solver_TSP.available_methods.keys()
|
initializers = Solver_TSP.available_initializers.keys()
|
||||||
|
improvements = Solver_TSP.available_improvements.keys()
|
||||||
results = []
|
results = []
|
||||||
index = []
|
index = []
|
||||||
for name in names:
|
for name in names:
|
||||||
|
@ -18,23 +19,23 @@ def run(show_plots=False, verbose=False):
|
||||||
if show_plots:
|
if show_plots:
|
||||||
instance.plot_data()
|
instance.plot_data()
|
||||||
|
|
||||||
for method in methods:
|
for init in initializers:
|
||||||
solver = Solver_TSP(method)
|
solver = Solver_TSP(init)
|
||||||
start = t()
|
for improve in improvements:
|
||||||
solver(instance, return_value=False, verbose=verbose)
|
solver.bind(improve)
|
||||||
end = t()
|
solver(instance, return_value=False, verbose=verbose)
|
||||||
|
|
||||||
if verbose:
|
if verbose:
|
||||||
print(f"the total length for the solution found is {solver.found_length}",
|
print(f"the total length for the solution found is {solver.found_length}",
|
||||||
f"while the optimal length is {instance.best_sol}",
|
f"while the optimal length is {instance.best_sol}",
|
||||||
f"the gap is {solver.gap}%",
|
f"the gap is {solver.gap}%",
|
||||||
f"the solution is found in {np.round(end - start, 5)} seconds", sep="\n")
|
f"the solution is found in {solver.time_to_solve} seconds", sep="\n")
|
||||||
|
|
||||||
index.append((name, method))
|
index.append((name, solver.name_method))
|
||||||
results.append([solver.found_length, instance.best_sol, solver.gap, end - start])
|
results.append([solver.found_length, instance.best_sol, solver.gap, solver.time_to_solve])
|
||||||
|
|
||||||
if show_plots:
|
if show_plots:
|
||||||
solver.plot_solution()
|
solver.plot_solution()
|
||||||
|
|
||||||
if instance.exist_opt:
|
if instance.exist_opt:
|
||||||
solver.solution = np.concatenate([instance.optimal_tour, [instance.optimal_tour[0]]])
|
solver.solution = np.concatenate([instance.optimal_tour, [instance.optimal_tour[0]]])
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
from numpy.core._multiarray_umath import ndarray
|
from numpy.core._multiarray_umath import ndarray
|
||||||
import os
|
import os
|
||||||
from time import time as t
|
from time import time as t
|
||||||
|
import numpy as np
|
||||||
if 'AI' in os.getcwd():
|
if 'AI' in os.getcwd():
|
||||||
from src import *
|
from src import *
|
||||||
else:
|
else:
|
||||||
|
@ -25,12 +26,14 @@ class Solver_TSP:
|
||||||
# "best_nn": self.best_nn, "multi_fragment": self.mf}
|
# "best_nn": self.best_nn, "multi_fragment": self.mf}
|
||||||
self.initializer = initializer
|
self.initializer = initializer
|
||||||
self.methods = [initializer]
|
self.methods = [initializer]
|
||||||
|
self.name_method = "initialize with " + initializer
|
||||||
self.solved = False
|
self.solved = False
|
||||||
assert initializer in self.available_initializers, f"the {initializer} initializer is not available currently."
|
assert initializer in self.available_initializers, f"the {initializer} initializer is not available currently."
|
||||||
|
|
||||||
def bind(self, local_or_meta):
|
def bind(self, local_or_meta):
|
||||||
assert local_or_meta in self.available_improvements, f"the {local_or_meta} method is not available currently."
|
assert local_or_meta in self.available_improvements, f"the {local_or_meta} method is not available currently."
|
||||||
self.methods.append(local_or_meta)
|
self.methods.append(local_or_meta)
|
||||||
|
self.name_method = ", improve with " + local_or_meta
|
||||||
|
|
||||||
def __call__(self, instance_, verbose=True, return_value=True):
|
def __call__(self, instance_, verbose=True, return_value=True):
|
||||||
self.instance = instance_
|
self.instance = instance_
|
||||||
|
@ -45,10 +48,11 @@ class Solver_TSP:
|
||||||
assert self.check_if_solution_is_valid(self.solution), "Error the solution is not valid"
|
assert self.check_if_solution_is_valid(self.solution), "Error the solution is not valid"
|
||||||
|
|
||||||
end = t()
|
end = t()
|
||||||
|
self.time_to_solve = np.around(end - start,3)
|
||||||
self.evaluate_solution()
|
self.evaluate_solution()
|
||||||
self._gap()
|
self._gap()
|
||||||
if verbose:
|
if verbose:
|
||||||
print(f"### solution found with {self.gap} % gap in {np.around(end - start, 3)} seconds ####")
|
print(f"### solution found with {self.gap} % gap in {self.time_to_solve} seconds ####")
|
||||||
if return_value:
|
if return_value:
|
||||||
return self.solution
|
return self.solution
|
||||||
|
|
||||||
|
@ -56,7 +60,7 @@ class Solver_TSP:
|
||||||
assert self.solved, "You can't plot the solution, you need to solve it first!"
|
assert self.solved, "You can't plot the solution, you need to solve it first!"
|
||||||
plt.figure(figsize=(8, 8))
|
plt.figure(figsize=(8, 8))
|
||||||
self._gap()
|
self._gap()
|
||||||
plt.title(f"{self.instance.name} solved with {self.method} solver, gap {self.gap}")
|
plt.title(f"{self.instance.name} solved with {self.name_method} solver, gap {self.gap}")
|
||||||
ordered_points = self.instance.points[self.solution]
|
ordered_points = self.instance.points[self.solution]
|
||||||
plt.plot(ordered_points[:, 1], ordered_points[:, 2], 'b-')
|
plt.plot(ordered_points[:, 1], ordered_points[:, 2], 'b-')
|
||||||
plt.show()
|
plt.show()
|
||||||
|
|
Reference in a new issue