def find_best(old_path, new_path, graph_num, print_output=False): """ This method finds the best of the two input paths for the graph with number graph_num, and returns it. :param: old_path - our current answer for this graph :param: new_path - the new path to check :param: graph_num - the graph file number. For example, for file 121.in, pass in 121 :param: print_output - boolean that reflects whether you want this function to print information :return: the best path of the input paths """ graph = Graph(open('instances/{0}.in'.format(graph_num)).read()) new_path_cost = graph.path_cost(new_path) old_path_cost = graph.path_cost(old_path) if print_output: print "New Path: ", new_path, " with cost: ", new_path_cost print "Old Path: ", old_path, " with cost: ", old_path_cost if not graph.is_valid_hamiltonian(new_path): if print_output: print "New path is not a valid hamiltonian" return old_path elif new_path_cost < old_path_cost: if print_output: print "New path selected" return new_path else: print "Old path selected" return old_path
def test_ten_nodes2_valid_hamiltonian(self): graph = Graph(open(TEST_PATH + 'ten_nodes2.in').read()) actual_path, actual_dist = kruskalsSolver(graph) self._check(graph.is_valid_hamiltonian(actual_path), True)
elif heuristic == Graph.BINOCULARS: mapping[g.path_cost(tuple(path))] += ["Binoculars" + t] print "path with cost {0} found by {1}".format(g.path_cost(tuple(path)), "Binoculars" + t) else: print "{0} found no valid path".format(heuristic) except ValueError as e: print "\t\tERROR in {0}".format(heuristic) continue # # Trying other algorithms: # # Kruskals kruskals_path, kruskals_cost = kruskalsSolver(g) if kruskals_path == None or not g.is_valid_hamiltonian(kruskals_path): print "\t\tERROR in KRUSKALS" else: results.append(kruskals_path) # instantiate a list for an empty path try: a = mapping[g.path_cost(tuple(kruskals_path))] except KeyError: mapping[g.path_cost(tuple(kruskals_path))] = [] mapping[g.path_cost(tuple(kruskals_path))] += ["Kruskals"] # Direct Dynamic Solver (if applicable) if g.num_nodes <= DYNAMIC_THRESHOLD: print "USING DYNAMIC SOLVER for " + str(i) dynamic_path, dynamic_cost = dynamicSolver(g)