def test_TSP_unweighted():
    G = nx.cycle_graph(9)
    path = nx_app.traveling_salesman_problem(G, nodes=[3, 6], cycle=False)
    assert path in ([3, 4, 5, 6], [6, 5, 4, 3])

    cycle = nx_app.traveling_salesman_problem(G, nodes=[3, 6])
    assert cycle in ([3, 4, 5, 6, 5, 4, 3], [6, 5, 4, 3, 4, 5, 6])
def test_TSP_incomplete_graph_short_path():
    G = nx.cycle_graph(9)
    G.add_edges_from([(4, 9), (9, 10), (10, 11), (11, 0)])
    G[4][5]["weight"] = 5

    cycle = nx_app.traveling_salesman_problem(G)
    print(cycle)
    assert len(cycle) == 17 and len(set(cycle)) == 12

    # make sure that cutting one edge out of complete graph formulation
    # cuts out many edges out of the path of the TSP
    path = nx_app.traveling_salesman_problem(G, cycle=False)
    print(path)
    assert len(path) == 13 and len(set(path)) == 12
def test_asadpour_integral_held_karp():
    """
    This test uses an integral held karp solution and the held karp function
    will return a graph rather than a dict, bypassing most of the asadpour
    algorithm.

    At first glance, this test probably doesn't look like it ensures that we
    skip the rest of the asadpour algorithm, but it does. We are not fixing a
    see for the random number generator, so if we sample any spanning trees
    the approximation would be different basically every time this test is
    executed but it is not since held karp is deterministic and we do not
    reach the portion of the code with the dependence on random numbers.
    """
    np = pytest.importorskip("numpy")

    G_array = np.array([
        [0, 26, 63, 59, 69, 31, 41],
        [62, 0, 91, 53, 75, 87, 47],
        [47, 82, 0, 90, 15, 9, 18],
        [68, 19, 5, 0, 58, 34, 93],
        [11, 58, 53, 55, 0, 61, 79],
        [88, 75, 13, 76, 98, 0, 40],
        [41, 61, 55, 88, 46, 45, 0],
    ])

    G = nx.from_numpy_array(G_array, create_using=nx.DiGraph)

    for _ in range(2):
        tour = nx_app.traveling_salesman_problem(G,
                                                 method=nx_app.asadpour_atsp)

        assert [1, 3, 2, 5, 2, 6, 4, 0, 1] == tour
def test_asadpour_tsp():
    """
    Test the complete asadpour tsp algorithm with the fractional, symmetric
    Held Karp solution. This test also uses an incomplete graph as input.
    """
    # This version of Figure 2 has all of the edge weights multiplied by 100
    # and the 0 weight edges have a weight of 1.
    pytest.importorskip("numpy")
    pytest.importorskip("scipy")

    edge_list = [
        (0, 1, 100),
        (0, 2, 100),
        (0, 5, 1),
        (1, 2, 100),
        (1, 4, 1),
        (2, 3, 1),
        (3, 4, 100),
        (3, 5, 100),
        (4, 5, 100),
        (1, 0, 100),
        (2, 0, 100),
        (5, 0, 1),
        (2, 1, 100),
        (4, 1, 1),
        (3, 2, 1),
        (4, 3, 100),
        (5, 3, 100),
        (5, 4, 100),
    ]

    G = nx.DiGraph()
    G.add_weighted_edges_from(edge_list)

    def fixed_asadpour(G, weight):
        return nx_app.asadpour_atsp(G, weight, 19)

    tour = nx_app.traveling_salesman_problem(G,
                                             weight="weight",
                                             method=fixed_asadpour)

    # Check that the returned list is a valid tour. Because this is an
    # incomplete graph, the conditions are not as strict. We need the tour to
    #
    #   Start and end at the same node
    #   Pass through every vertex at least once
    #   Have a total cost at most ln(6) / ln(ln(6)) = 3.0723 times the optimal
    #
    # For the second condition it is possible to have the tour pass through the
    # same vertex more then. Imagine that the tour on the complete version takes
    # an edge not in the original graph. In the output this is substituted with
    # the shortest path between those vertices, allowing vertices to appear more
    # than once.
    #
    # However, we are using a fixed random number generator so we know what the
    # expected tour is.
    expected_tours = [[1, 4, 5, 0, 2, 3, 2, 1], [3, 2, 0, 1, 4, 5, 3]]

    assert tour in expected_tours
Beispiel #5
0
def test_TSP_method():
    G = nx.cycle_graph(9)
    G[4][5]["weight"] = 10

    def my_tsp_method(G, weight):
        return nx_app.simulated_annealing_tsp(G, "greedy", weight, source=4, seed=1)

    path = nx_app.traveling_salesman_problem(G, method=my_tsp_method, cycle=False)
    print(path)
    assert path == [4, 3, 2, 1, 0, 8, 7, 6, 5]
def test_asadpour_real_world():
    """
    This test uses airline prices between the six largest cities in the US.

        * New York City -> JFK
        * Los Angeles -> LAX
        * Chicago -> ORD
        * Houston -> IAH
        * Phoenix -> PHX
        * Philadelphia -> PHL

    Flight prices from August 2021 using Delta or American airlines to get
    nonstop flight. The brute force solution found the optimal tour to cost $872

    This test also uses the `source` keyword argument to ensure that the tour
    always starts at city 0.
    """
    np = pytest.importorskip("numpy")
    pytest.importorskip("scipy")

    G_array = np.array([
        # JFK  LAX  ORD  IAH  PHX  PHL
        [0, 243, 199, 208, 169, 183],  # JFK
        [277, 0, 217, 123, 127, 252],  # LAX
        [297, 197, 0, 197, 123, 177],  # ORD
        [303, 169, 197, 0, 117, 117],  # IAH
        [257, 127, 160, 117, 0, 319],  # PHX
        [183, 332, 217, 117, 319, 0],  # PHL
    ])

    node_map = {0: "JFK", 1: "LAX", 2: "ORD", 3: "IAH", 4: "PHX", 5: "PHL"}

    expected_tours = [
        ["JFK", "LAX", "PHX", "ORD", "IAH", "PHL", "JFK"],
        ["JFK", "ORD", "PHX", "LAX", "IAH", "PHL", "JFK"],
    ]

    G = nx.from_numpy_array(G_array, create_using=nx.DiGraph)
    nx.relabel_nodes(G, node_map, copy=False)

    def fixed_asadpour(G, weight):
        return nx_app.asadpour_atsp(G, weight, 37, source="JFK")

    tour = nx_app.traveling_salesman_problem(G,
                                             weight="weight",
                                             method=fixed_asadpour)

    assert tour in expected_tours