def define_edge_term(variable_dict, graph, edge, prefix="x", perturb=False, max_weight=None): """ Define a single edge term in the objective """ weight = graph_utils.get_edge_weight(graph, *edge) name = name_variable(edge, prefix=prefix) return compute_edge_term(weight, variable_dict[name])
def compute_f3_edge(graph, vertex_a, vertex_b, self_max=False): """ Compute feature f3 of Sun et al. for a specific edge""" out_weights = graph_utils.get_edge_weights(graph, vertex_a) edge_weight = graph_utils.get_edge_weight(graph, vertex_a, vertex_b) if not self_max: numerator = edge_weight - np.mean(out_weights) denominator = np.max(out_weights) - np.min(out_weights) else: numerator = edge_weight - np.partition(out_weights, -2)[:-1].mean() denominator = np.partition(out_weights, -2)[-2] - np.min(out_weights) return numerator / denominator
def compute_f2_edge(graph, vertex_a, vertex_b, self_max=False): """ Compute feature f2 of Sun et al. for a specific edge""" out_weights = graph_utils.get_edge_weights(graph, vertex_a) in_weights = graph_utils.get_edge_weights(graph, vertex_b, out=False) edge_weight = graph_utils.get_edge_weight(graph, vertex_a, vertex_b) numerator = edge_weight - np.min(out_weights) if not self_max: denominator = np.max(in_weights) - np.min(in_weights) else: denominator = np.partition(in_weights, -2)[-2] - np.min(in_weights) return numerator / denominator
def compute_f1_edge(graph, vertex_a, vertex_b, self_max=False): """ Compute feature f1 of Sun et al. for a specific edge""" """ If the self-weight is the largest weight, set self_max true """ out_weights = graph_utils.get_edge_weights(graph, vertex_a) edge_weight = graph_utils.get_edge_weight(graph, vertex_a, vertex_b) numerator = edge_weight - np.min(out_weights) if not self_max: denominator = np.max(out_weights) - np.min(out_weights) else: denominator = np.partition(out_weights, -2)[-2] - np.min(out_weights) return numerator / denominator
def get_path_lengths(graph, path): """ Get an array of edge lengths for a path """ edges = get_path_edges(path) return np.array( [graph_utils.get_edge_weight(graph, *edge) for edge in edges])
object = io_utils.optObject().read_from_file(file) graph = object.get_graph() graph = graph_utils.delete_self_weights(graph) graph = graph.to_undirected() solution = cc_solve.solution_from_path(file) edges = graph_utils.get_tour_edges(solution.tour) if np.min(solution.tour) < 1 and np.min(graph.nodes) > 0: edges += 1 edges = model.tuples_to_strings(edges) sparse_edges = model.run_sparsify(graph, 6) prune_edges = [] for item in sparse_edges: prune_edges += list(item) prune_weights = [graph_utils.get_edge_weight(graph, *edge) for edge in prune_edges] prune_bunches = [(*edge, {"weight": weight}) for (edge, weight) in zip(prune_edges, prune_weights)] g = nx.Graph() g.add_edges_from(prune_bunches) all_weights = [] for mun, vertex in enumerate(graph.nodes): edges = [edge for edge in graph.edges if vertex in edge] weights = [9999999] * len(edges) for pum, edge in enumerate(edges): if edge in prune_edges: index = prune_edges.index(edge) weights[pum] = prune_weights[index]