Exemplo n.º 1
0
        def via_via(via):
            path1 = dijkstra_path(adjacency, weight, start, via)
            path2 = dijkstra_path(adjacency, weight, via, end)
            path = path1 + path2[1:]

            edges = []
            for i in range(len(path) - 1):
                u = path[i]
                v = path[i + 1]
                if v not in network.edge[u]:
                    u, v = v, u
                edges.append([u, v])

            vertexcolor = {}
            vertexcolor[start] = '#00ff00'
            vertexcolor[end] = '#00ff00'
            vertexcolor[via] = '#0000ff'

            plotter.clear_vertices()
            plotter.clear_edges()

            plotter.draw_vertices(text={key: key for key in (start, via, end)},
                                  textcolor={key: '#ffffff' for key in path[1:-1]},
                                  facecolor=vertexcolor,
                                  radius=0.15,
                                  picker=10)

            plotter.draw_edges(color={(u, v): '#ff0000' for u, v in edges},
                               width={(u, v): 4.0 for u, v in edges},
                               text={(u, v): '{:.1f}'.format(weight[(u, v)]) for u, v in network.edges()},
                               fontsize=4.0)
Exemplo n.º 2
0
def via_via(via):

    # compute the shortest path from start to via
    # and from via to end
    # combine the paths

    path1 = dijkstra_path(adjacency, weight, start, via)
    path2 = dijkstra_path(adjacency, weight, via, end)
    path = path1 + path2[1:]

    edges = []
    for i in range(len(path) - 1):
        u = path[i]
        v = path[i + 1]
        if v not in network.edge[u]:
            u, v = v, u
        edges.append([u, v])

    # update the plot

    vertexcolor = {}
    vertexcolor[start] = '#00ff00'
    vertexcolor[end] = '#00ff00'
    vertexcolor[via] = '#FF00F6'

    plotter.clear_vertices()
    plotter.clear_edges()

    plotter.draw_vertices(text={key: key
                                for key in (start, via, end)},
                          textcolor={key: '#ffffff'
                                     for key in path[1:-1]},
                          facecolor=vertexcolor,
                          radius=3,
                          picker=20)

    plotter.draw_edges(color={(u, v): '#ff0000'
                              for u, v in edges},
                       width={(u, v): 4.0
                              for u, v in edges})
Exemplo n.º 3
0
def network_order(start, structure, network):
    """ Extract node and element orders from a Network for a given start-point.

    Parameters
    ----------
    start : list
        Start point co-ordinates.
    structure : obj
        Structure object.
    network : obj
        Network object.

    Returns
    -------
    list
        Ordered nodes.
    list
        Ordered elements.
    list
        Cumulative lengths at element mid-points.
    float
        Total length.

    """

    gkey_key = network.gkey_key()
    start = gkey_key[geometric_key(start, '{0}f'.format(structure.tol))]
    leaves = network.leaves()
    leaves.remove(start)
    end = leaves[0]

    adjacency = {i: network.vertex_neighbors(i) for i in network.vertices()}
    weight = {(u, v): 1 for u, v in network.edges()}
    weight.update({(v, u): weight[(u, v)] for u, v in network.edges()})
    path = dijkstra_path(adjacency, weight, start, end)
    nodes = [
        structure.check_node_exists(network.vertex_coordinates(i))
        for i in path
    ]
    elements, arclengths, length = [], [], 0

    for i in range(len(nodes) - 1):
        sp = nodes[i]
        ep = nodes[i + 1]
        elements.append(structure.check_element_exists([sp, ep]))
        xyz_sp = structure.node_xyz(sp)
        xyz_ep = structure.node_xyz(ep)
        dL = distance_point_point(xyz_sp, xyz_ep)
        arclengths.append(length + dL / 2.)
        length += dL

    return nodes, elements, arclengths, length
Exemplo n.º 4
0
start = end = 0
while start == end:
    start = choice(leaves)
    end = choice(leaves)

# construc an adjacency dict
# add weight to the edges corresponding to their length
# compute the shortest path

adjacency = {key: network.vertex_neighbors(key) for key in network.vertices()}

weight = {(u, v): network.edge_length(u, v) for u, v in network.edges()}
weight.update({(v, u): weight[(u, v)] for u, v in network.edges()})

path = dijkstra_path(adjacency, weight, start, end)

# visualize the result

plotter = NetworkPlotter(network, figsize=(10, 8), fontsize=6)

edges = []
for u, v in pairwise(path):
    if v not in network.edge[u]:
        u, v = v, u
    edges.append([u, v])

plotter.draw_vertices(
    text={key: key
          for key in (start, end)},
    facecolor={key: '#ff0000'
Exemplo n.º 5
0
    artist = NetworkArtist(network, layer='new_lines')
    artist.draw_vertices(color=(255, 0, 0))
    artist.redraw()

    # select vertices
    vertices = network_select_vertices(network, "select a network vertices")

    print(vertices)

    adjacency = {
        key: network.vertex_neighbors(key)
        for key in network.vertices()
    }

    weight = {(u, v): network.edge_length(u, v) for u, v in network.edges()}
    weight.update({(v, u): weight[(u, v)] for u, v in network.edges()})

    path = dijkstra_path(adjacency, weight, vertices[0], vertices[1])

    print(path)

    edges = []
    for u, v in pairwise(path):
        if v not in network.edge[u]:
            u, v = v, u
        edges.append((u, v))

    artist.draw_edges(keys=edges, color=(255, 0, 0))
    artist.clear_vertices()
    artist.redraw()
Exemplo n.º 6
0
# add the same weight in both directions
weight.update({(v, u): weight[(u, v)] for u, v in network.edges()})

# set high weights for some specific edges
weight[(18, 1)] = 100
weight[(1, 18)] = 100
weight[(0, 18)] = 100
weight[(18, 0)] = 100

# specify start and end
start = 21
end = 19

# compute the shortest path taking into account the edge weights
path = dijkstra_path(network.adjacency, weight, start, end)

# convert the path to network edges
edges = [(v, u) if not network.has_edge(u, v) else (u, v)
         for u, v in pairwise(path)]

# make a plotter
plotter = NetworkPlotter(network, figsize=(8, 5))

# set default font sizes
plotter.defaults['vertex.fontsize'] = 6
plotter.defaults['edge.fontsize'] = 6

# draw the vertices
plotter.draw_vertices(
    text='key',