def polyTree3(): graph = Graph() graph.addEdge(parents=[0, 1], children=[3]) graph.addEdge(parents=[1, 2], children=[4]) return graph
def graph8(): graph = Graph() graph.addEdge(parents=[0], children=[1, 2]) graph.addEdge(parents=[1, 2], children=[3]) # fbs = np.array( [ 2 ] ) # return graph, fbs return graph
def graph6(): graph = Graph() graph.addEdge(parents=[0, 1], children=[2]) graph.addEdge(parents=[2, 3], children=[0]) # fbs = np.array( [ 0 ] ) assert 0, 'This graph has a feedback cycle' # return graph, fbs return graph
def graph3(): graph = Graph() graph.addEdge(parents=[0, 1], children=[2, 3, 9]) graph.addEdge(parents=[2, 3], children=[4]) graph.addEdge(parents=[1, 2, 3, 8], children=[5, 6]) graph.addEdge(parents=[1, 2, 4, 5, 6, 9], children=[7, 10]) # fbs = np.array( [ 1, 2, 3, 4, 6 ] ) # return graph, fbs return graph
def graph16(): graph = Graph() graph.addEdge(parents=[0], children=[2, 3]) graph.addEdge(parents=[1], children=[4, 5]) graph.addEdge(parents=[2, 3], children=[6]) graph.addEdge(parents=[4, 5], children=[7]) graph.addEdge(parents=[6, 7], children=[8]) graph.addEdge(parents=[2, 8], children=[9]) graph.addEdge(parents=[5, 8], children=[10]) # fbs = np.array( [ 3, 4, 6, 7 ] ) # return graph, fbs return graph
def polytreeTest(): graphs = [ polyTree1(), polyTree2(), polyTree3(), polyTree4(), polyTree5(), polyTree6(), polyTree7() ] graph = graph9() edge_parents, edge_children = graph.toSparse() big_sparse = preprocessSparseGraphForTraversal( edge_parents.astype(np.int32), edge_children.astype(np.int32)) edge_parents, edge_children, node_meta, edge_meta, graph_meta = big_sparse sparses = [graph.toSparse() for graph in graphs] edge_parents, edge_children = Graph.combineSparse(sparses) if (False): big_graph = Graph.fromSparse(edge_parents, edge_children) big_graph.draw(output_folder='/app/host') start = time.time() big_sparse = preprocessSparseGraphForTraversal( edge_parents.astype(np.int32), edge_children.astype(np.int32)) edge_parents, edge_children, node_meta, edge_meta, graph_meta = big_sparse u_order, v_order, batch_sizes = polytreeMessagePassing(*big_sparse) end = time.time() print('Cython way took', end - start) print('There are', node_meta.shape, 'nodes') print('There are', edge_meta.shape, 'edges') u_deque = deque(u_order) v_deque = deque(v_order) for i, (u_batch_size, v_batch_size) in enumerate(batch_sizes): u_nodes = [u_deque.popleft() for _ in range(u_batch_size)] v_edges_and_nodes = [v_deque.popleft() for _ in range(v_batch_size)] print('\nbatch number', i) print('u_batch_size', u_batch_size) print('v_batch_size', v_batch_size) print('u_nodes', u_nodes) print('v_edges_and_nodes', v_edges_and_nodes)
def graph14(): graph = Graph() graph.addEdge(parents=[0], children=[1, 2]) graph.addEdge(parents=[1], children=[3]) graph.addEdge(parents=[2], children=[4]) graph.addEdge(parents=[3, 4], children=[5]) # fbs = np.array( [ 1 ] ) # return graph, fbs return graph
def graph15(): graph = Graph() graph.addEdge(parents=[0], children=[1, 2]) graph.addEdge(parents=[1], children=[3, 4]) graph.addEdge(parents=[2], children=[5]) graph.addEdge(parents=[3], children=[6]) graph.addEdge(parents=[4, 6], children=[7]) graph.addEdge(parents=[4, 5], children=[8]) # fbs = np.array( [ 4 ] ) # return graph, fbs return graph
def graph9(): graph = Graph() graph.addEdge(parents=[7, 8], children=[0]) graph.addEdge(parents=[0, 1], children=[3]) graph.addEdge(parents=[1, 2], children=[4]) graph.addEdge(parents=[2, 3, 4], children=[5, 6]) # fbs = np.array( [ 2, 4 ] ) # return graph, fbs return graph
def graph13(): graph = Graph() graph.addEdge(parents=[0, 1], children=[3]) graph.addEdge(parents=[1, 2], children=[4]) graph.addEdge(parents=[3], children=[5]) graph.addEdge(parents=[4], children=[7]) graph.addEdge(parents=[5, 6], children=[8]) graph.addEdge(parents=[6, 7], children=[9]) # fbs = np.array( [ 6 ] ) # return graph, fbs return graph
def polyTree4(): graph = Graph() graph.addEdge(parents=[0, 1, 2], children=[4, 5]) graph.addEdge(parents=[2, 3], children=[6]) return graph
def polyTree2(): graph = Graph() graph.addEdge(parents=[0, 1], children=[4]) graph.addEdge(parents=[2, 3], children=[5]) graph.addEdge(parents=[4, 5], children=[6, 7]) return graph
def graph1(): graph = Graph() graph.addEdge(parents=[0, 1], children=[2, 3]) graph.addEdge(parents=[2, 3], children=[4]) # fbs = np.array( [ 2 ] ) # return graph, fbs return graph
def graph10(): graph = Graph() graph.addEdge(parents=[0], children=[1, 2]) graph.addEdge(parents=[1, 2], children=[3]) graph.addEdge(parents=[3], children=[4, 5]) graph.addEdge(parents=[4, 5], children=[6]) # fbs = np.array( [ 1, 4 ] ) # return graph, fbs return graph
def graph12(): graph = Graph() graph.addEdge(parents=[0, 1, 2], children=[6, 7]) graph.addEdge(parents=[3], children=[8]) graph.addEdge(parents=[4, 5], children=[9]) graph.addEdge(parents=[5, 6], children=[10]) graph.addEdge(parents=[6], children=[11]) graph.addEdge(parents=[7, 8], children=[12]) graph.addEdge(parents=[11], children=[13]) graph.addEdge(parents=[11, 12], children=[14]) graph.addEdge(parents=[9, 10, 11], children=[15]) graph.addEdge(parents=[13, 14], children=[16]) graph.addEdge(parents=[8, 14], children=[17]) graph.addEdge(parents=[15, 16, 17], children=[18, 19, 20]) # fbs = np.array( [ 5, 6, 11, 14 ] ) # return graph, fbs return graph
def polyTree8(): graph = Graph() graph.addEdge(parents=[0], children=[1]) graph.addEdge(parents=[1], children=[2]) graph.addEdge(parents=[2], children=[3]) graph.addEdge(parents=[3], children=[4]) graph.addEdge(parents=[4], children=[5]) graph.addEdge(parents=[5], children=[6]) graph.addEdge(parents=[6], children=[7]) graph.addEdge(parents=[7], children=[8]) graph.addEdge(parents=[8], children=[9]) graph.addEdge(parents=[9], children=[10]) graph.addEdge(parents=[10], children=[11]) graph.addEdge(parents=[11], children=[12]) graph.addEdge(parents=[12], children=[13]) graph.addEdge(parents=[13], children=[14]) graph.addEdge(parents=[14], children=[15]) return graph
def polyTree6(): graph = Graph() graph.addEdge(parents=[0], children=[1]) return graph
def polyTree5(): graph = Graph() graph.addEdge(parents=[0, 1], children=[6, 7]) graph.addEdge(parents=[2], children=[8]) graph.addEdge(parents=[3, 4, 5], children=[9, 10, 11]) graph.addEdge(parents=[7, 8], children=[12]) graph.addEdge(parents=[6, 9], children=[13]) graph.addEdge(parents=[15, 16], children=[17]) graph.addEdge(parents=[13, 17], children=[14]) return graph
def graph5(): graph = Graph() graph.addEdge(parents=[0, 1], children=[2, 3, 9]) graph.addEdge(parents=[2, 3], children=[4]) graph.addEdge(parents=[1, 2, 3, 8], children=[5, 6]) graph.addEdge(parents=[1, 2, 4, 5, 6, 9], children=[7, 10]) graph.addEdge(parents=[7, 10], children=[0]) # fbs = np.array( [ 0, 1, 2, 3, 4, 6 ] ) assert 0, 'This graph has a feedback cycle' # return graph, fbs return graph
def polyTree1(): graph = Graph() graph.addEdge(parents=[0, 1], children=[2]) return graph
def graph11(): graph = Graph() graph.addEdge(parents=[0], children=[1]) graph.addEdge(parents=[1, 2], children=[3, 4, 5]) graph.addEdge(parents=[3, 4], children=[6]) graph.addEdge(parents=[6], children=[7]) graph.addEdge(parents=[5, 6], children=[8]) # fbs = np.array( [ 3, 5 ] ) # return graph, fbs return graph
def processGraphs(graphs): sparses = [graph.toSparse() for graph in graphs] edge_parents, edge_children = Graph.combineSparse(sparses) return preprocessSparseGraphForTraversal(edge_parents.astype(np.int32), edge_children.astype(np.int32))