def exact(filename, max_seconds=None): original_problem_graph = TSPProblemParser().parseDirected(filename) solution, cost = tsp_mip_cutting_planes.TSP().load_problem( original_problem_graph).build().solve(max_seconds) # print(f'solution = {solution}, cost = {cost}') return solution, cost
def local_search_augmented(filename, batch_size=8, n_iters=30, max_attemps=4, max_seconds=None): original_problem_graph = TSPProblemParser().parseDirected(filename) solution, cost = LocalSearchAugmented().solve(original_problem_graph, batch_size, n_iters, max_attemps, max_seconds) print(f'solution = {solution}, cost = {cost}') return solution, cost
def reduced_edges_aproximation(filename, max_seconds=None): original_problem_graph = TSPProblemParser().parse(filename) solution_mst, cost = MST().solve(original_problem_graph) solution_nn, cost = NN().solve_complete(original_problem_graph) reduced_graph = nx.operators.compose( get_reduced_graph(solution_mst, original_problem_graph, True), get_reduced_graph(solution_nn, original_problem_graph, True)) graph = convert_to_digraph(original_problem_graph) solution, cost = tsp_mip_cutting_planes.TSP().load_problem( reduced_graph).build().solve(max_seconds) # print(f'solution = {solution}, cost = {cost}') return solution, cost
def planar_aproximation_2(filename, max_seconds=None): original_problem_graph = TSPProblemParser().parse(filename) planar_graph = PMFG().get_reduced_graph(original_problem_graph) nn_algo = NN() solution_nn, cost = nn_algo.solve_complete(original_problem_graph) all_nn_graph = get_reduced_graph(solution_nn, original_problem_graph, True) for s in nn_algo.solutions: all_nn_graph = nx.operators.compose( all_nn_graph, get_reduced_graph(s, original_problem_graph, True)) planar_graph = convert_to_digraph(planar_graph) graph = convert_to_digraph(original_problem_graph) reduced_graph = nx.operators.compose(planar_graph, all_nn_graph) solution, cost = tsp_mip_cutting_planes.TSP().load_problem( reduced_graph).build().solve(max_seconds) # print(f'solution = {solution}, cost = {cost}') return solution, cost
solution = [] cost = 0 mst = self.get_mst(problem_graph) odd_graph = self.build_graph_with_odd_vertex(problem_graph, mst) matching_edges = nx.algorithms.matching.max_weight_matching(odd_graph) mst_extended = self.join_mst_and_matching_edges( problem_graph, mst, matching_edges) walk = list(nx.eulerian_circuit(mst_extended)) solution = self.fast_hamiltonian_circuit_from_euler_circuit(walk) for i in range(len(solution)): u = solution[i] v = solution[(i + 1) % len(solution)] weight = problem_graph[u][v]['weight'] cost += weight return solution, cost if __name__ == '__main__': filename = 'examples/ulysses16.json' original_problem_graph = TSPProblemParser().parse(filename) solution, cost = Christofides().solve(original_problem_graph) print(cost)
def nn_aproximation(filename): original_problem_graph = TSPProblemParser().parse(filename) solution, cost = NN().solve_complete(original_problem_graph) # print(f'solution = {solution}, cost = {cost}') return solution, cost
def christofides_aproximation(filename): original_problem_graph = TSPProblemParser().parse(filename) solution, cost = Christofides().solve(original_problem_graph) # print(f'solution = {solution}, cost = {cost}') return solution, cost