Exemple #1
0
def test_astar_lightest_path():
    g = Graph()
    for i in range(4):
        g.add_node(i)
    g.add_edge(0, 1)
    g.add_edge(0, 2)
    g.add_edge(1, 3)
    g.add_edge(2, 3)
    weights = {
        (0, 1): 1,
        (0, 2): 1,
        (1, 3): 2,
        (2, 3): 1,
    }
    heuristic = {i: 1 for i in range(4)}
    path = astar_lightest_path(g.adjacency, weights, heuristic, 0, 3)
    assert path == [0, 2, 3]
Exemple #2
0
def test_graph_networkx_conversion():
    if compas.IPY:
        return

    g = Graph()
    g.attributes['name'] = 'DiGraph'
    g.attributes['val'] = (0, 0, 0)
    g.add_node(0)
    g.add_node(1, weight=1.2, height='test')
    g.add_node(2, x=1, y=1, z=0)

    g.add_edge(0, 1, attr_value=10)
    g.add_edge(1, 2)

    nxg = g.to_networkx()

    assert nxg.graph['name'] == 'DiGraph', "Graph attributes must be preserved"
    assert nxg.graph['val'] == (0, 0, 0), "Graph attributes must be preserved"
    assert set(nxg.nodes()) == set(g.nodes()), "Node sets must match"
    assert nxg.nodes[1]['weight'] == 1.2, "Node attributes must be preserved"
    assert nxg.nodes[1]['height'] == "test", "Node attributes must be preserved"
    assert nxg.nodes[2]['x'] == 1, "Node attributes must be preserved"

    assert set(nxg.edges()) == set(((0, 1), (1, 2))), "Edge sets must match"
    assert nxg.edges[0, 1]['attr_value'] == 10, "Edge attributes must be preserved"

    g2 = Graph.from_networkx(nxg)

    assert g.number_of_nodes() == g2.number_of_nodes()
    assert g.number_of_edges() == g2.number_of_edges()
    assert g2.edge_attribute((0, 1), 'attr_value') == 10
    assert g2.attributes['name'] == 'DiGraph', "Graph attributes must be preserved"
    assert g2.attributes['val'] == (0, 0, 0), "Graph attributes must be preserved"