Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

## Draft: connecting components without taking the same path #13

Open
samuelduchesne opened this issue Jul 8, 2019 · 0 comments
Open

Comments

@samuelduchesne
Copy link
Owner

Draft: connecting components without taking the same path

import random

import matplotlib.pyplot as plt
import networkx as nx
import tqdm


def chunks(l, n):
    """Yield successive n-sized chunks from l."""
    for i in range(0, len(l), n):
        yield l[i:i + n]


if __name__ == '__main__':
    G = nx.grid_2d_graph(100, 100)
    H = G.copy()
    I = nx.Graph()
    paths = {}
    for i in tqdm.tnrange(10):
        node_list = [n for n in G.nodes()]
        chuncks = [c for c in chunks(node_list, int(len(node_list) / 4))]
        u = random.choice(chuncks[0])
        v = random.choice(chuncks[3])

        paths[i] = nx.shortest_path(G, u, v)
        edges = list(zip(paths[i], paths[i][1:] + paths[i][:1]))
        # G.remove_edges_from(edges)
        G.remove_nodes_from(paths[i])
        I.add_edges_from(edges[:-1])

    pos_h = {n: n for n in H.nodes}
    pso_i = {n: n for n in I.nodes}

    fig, ax = plt.subplots(1, 1, figsize=(20, 20))
    nx.draw_networkx_nodes(H, pos_h, arrows=False, with_labels=False,
                           node_size=1)
    nx.draw_networkx_edges(I, pso_i, edge_color='r')
    plt.show()

Imagine that at the end of those lines is a TRNSYS component. The point grid is there to show the available paths.:

test-shortestpath

Originally posted by @samuelduchesne in #11 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant