def load_solution(self, path=None): """ :param str path: (Optional) Path of solution file. If solution not in directory containing problem file. :return: A solution object. :rtype: tsp.models.Solution """ load_path = self.out_abs_path if path is None else path if self._solution is None: self._solution = tsp.load_solution(load_path) return self._solution
plt.title(title) plt.savefig(savename) plt.cla() if __name__ == "__main__": DATA_PATH = Path("data") assert DATA_PATH.exists() NAME = "berlin52" NAME = "ulysses16" PROBLEM_PATH = DATA_PATH / f"{NAME}.tsp" SOLUTION_PATH = DATA_PATH / f"{NAME}.opt.tour" p = tsp.load_problem(PROBLEM_PATH) s = tsp.load_solution(SOLUTION_PATH) print(f"Optimal tour: {s.tours}") print(f"Optimal tour: {p.trace_tours(s)}") g = p.get_graph() draw(g, s.tours[0]) #def get_colors( # graph, edges, default='b', highlight='r', #): # c = np.full(len(graph.edges), default) # w = np.ones(len(graph.edges)) # # for i, edge in enumerate(graph.edges): # if edge in edges: # c[i] = highlight
def parse_solution(res_path): """ Парсим файл решения """ solution = tsplib95.load_solution(res_path) return np.array(solution.tours[0])
import acopy import tsplib95 colony = acopy.ant.Colony(alpha=1, beta=3) problem = tsplib95.load_problem('./data/att48.tsp') G = problem.get_graph() solution = tsplib95.load_solution('./data/att48.opt.tour') print(problem.trace_tours(solution)) ants = 25 iterations = 100 deviation1 = [] solver = acopy.solvers.Solver(rho=0.2, q=1) for i in range(0, 9): G = problem.get_graph() colony = acopy.ant.Colony(alpha=1, beta=2) tour = solver.solve(G, colony, ants, iterations) print(tour.cost) deviation1.append(abs(33522 - tour.cost)) result1 = sum(deviation1) / len(deviation1) print("Ants: ", ants, "Iterations: ", iterations) print("Averave deviation: ", result1) print("nodes: ", tour.nodes) print("\n") ################################################################################
import acopy import tsplib95 from functions import distance_on_unit_sphere from plotSprawko import plot colony = acopy.ant.Colony(alpha=1, beta=3) problem = tsplib95.load_problem('./data/ulysses16.tsp', distance_on_unit_sphere) problem.special = distance_on_unit_sphere G = problem.get_graph() solution = tsplib95.load_solution('./data/ulysses16.opt.tour') # problem.trace_tours(solution) ants = 25 iterations = 100 deviation1 = [] solver = acopy.solvers.Solver(rho=0.03, q=1) optimal_solution = 6859 for i in range(0, 9): G = problem.get_graph() colony = acopy.ant.Colony(alpha=1, beta=3) tour = solver.solve(G, colony, ants, iterations) print(tour.cost) deviation1.append(abs(optimal_solution - tour.cost)) result1 = sum(deviation1) / len(deviation1)
import acopy import tsplib95 colony = acopy.ant.Colony(alpha=1, beta=3) problem = tsplib95.load_problem('./data/eil51.tsp') G = problem.get_graph() solution = tsplib95.load_solution('./data/eil101.opt.tour') # print(problem.trace_tours(solution)) # optimal : 426 ants = 200 iterations = 100 deviation1 = [] solver = acopy.solvers.Solver(rho=0.2, q=1) for i in range(0, 9): G = problem.get_graph() colony = acopy.ant.Colony(alpha=1, beta=2) tour = solver.solve(G, colony, ants, iterations) print(tour.cost) deviation1.append(abs(426-tour.cost)) result1 = sum(deviation1) / len(deviation1) print("Ants: ", ants, "Iterations: ", iterations) print("Averave deviation: ", result1) print("nodes: ", tour.nodes) print("\n")