예제 #1
0
def solve(G):
    """
    Args:
        G: networkx.Graph

    Returns:
        T: networkx.Graph
    """

    g = GraphSolver(G)
    start = g.find_leaf_path()
    if start is None:
        start = first_heuristic(g)
    graph_choices = []
    for v in g.neighbors(start):
        newG = G.copy()
        new_g = GraphSolver(newG)
        new_g.visit(start)
        new_g.visit(v, (start, v))
        graph_choices.append(new_g)
    minT = G
    minCost = float('inf')
    for g_option in graph_choices:
        g_option.dijkstra_solve_graph(start, calculate_heuristic,
                                      first_heuristic)
        optimize_sorted(g_option, g_option.T, cycle_killer_fn=kill_cycle)
        cost = average_pairwise_distance(g_option.T)
        if cost < minCost:
            minCost = cost
            minT = g_option.T
    return minT
예제 #2
0
def solve(G):
    """
    Args:
        G: networkx.Graph

    Returns:
        T: networkx.Graph
    """

    g = GraphSolver(G)
    start = g.find_leaf_path()
    T = g.dijkstra_solve_graph(start, calculate_heuristic, first_heuristic)
    optimize_sorted(g, T)
    rebuilder = Rebuilder(g)
    min_T = T.copy()
    min_cost = average_pairwise_distance(T)
    # print('*', min_cost)
    for _ in range(50):
        if rebuilder.rebuild():
            g = GraphSolver(G)
            for v in min_T.nodes:
                g.visit(v)
            for e in min_T.edges:
                g.add_edge(e)
            # print('reset')
        g.dijkstra_solve_graph(start, calculate_heuristic, first_heuristic)
        optimize_sorted(g, g.T)

        cost = average_pairwise_distance(g.T)
        # print(cost)
        if cost < min_cost:
            min_cost = cost
            min_T = g.T.copy()
    return min_T
예제 #3
0
def solve(G):
    """
    Args:
        G: networkx.Graph

    Returns:
        T: networkx.Graph
    """

    g = GraphSolver(G)
    start = g.find_leaf_path()
    T = g.dijkstra_solve_graph(start, calculate_heuristic, first_heuristic)
    return T
def solve(G):
    """
    Args:
        G: networkx.Graph

    Returns:
        T: networkx.Graph
    """

    g = GraphSolver(G)
    start = g.find_leaf_path()
    T = g.dijkstra_solve_graph(start, calculate_heuristic, first_heuristic)
    optimize_sorted(g, T, kill_cycle_all_paths)
    return T
예제 #5
0
def solve(G):
    """
    Args:
        G: networkx.Graph

    Returns:
        T: networkx.Graph
    """

    g = GraphSolver(G)
    start = g.find_leaf_path()
    T = g.dijkstra_solve_graph(start, calculate_heuristic, first_heuristic)
    if average_pairwise_distance(T) == 0:
        return T
    optimize_sorted(g, T, cycle_killer_fn=kill_cycle)
    return T