def testGraphasc(file_location): with open(file_location) as f: G = graph_io.load_graph(f) asc = sortAsc(G) greedyG, chromaticnr, time = Greedy(G, asc) writedotgraph(greedyG) print("chromaticnumber ascending order =" + str(chromaticnr) + " time elapsed: " + str(time)) return chromaticnr, time
def testGraphrndm(file_location): with open(file_location) as f: G = graph_io.load_graph(f) random = shuffle(G) greedyG, chromaticnr, time = Greedy(G, random) print("chromaticnumber random order =" + str(chromaticnr) + " time elapsed: " + str(time)) writedotgraph(greedyG) return chromaticnr, time
def testGraphdesc(file_location): with open(file_location) as f: G = graph_io.load_graph(f) print("Graph " + str(file_location)) desc = sortDesc(G) greedyG, chromaticnr, time = Greedy(G, desc) print("chromaticnumber descending order =" + str(chromaticnr) + " time elapsed: " + str(time)) return chromaticnr, time
def testfile(filename): """Check if results for the given file are correct""" with open(PATH + "/" + filename) as f: graphs = load_graph(f, read_list=True) graphs = graphs[0] results = [] for i in range(len(graphs)): for j in range(len(graphs)): if j > i: result = tree_isomorphism(graphs[i], graphs[j]) expected = get_expected_result(filename, graphs[i].name, graphs[j].name) message = "Expected " + str(expected) + " for " + graphs[ i].name + " and " + graphs[j].name + " in " + filename results.append([expected, result, message]) return results
for FileName in TestInstances: # Tuple arguments below: # First: printable string # Second: Boolean value # Third: Function # Fourth: Filename # Fifth: Whether output should be directed for testalg in [ ("Bellman-Ford, undirected", TEST_BELLMAN_FORD_UNDIRECTED, bellman_ford_undirected, "BellmanFordUndirected", False), ("Bellman-Ford, directed", TEST_BELLMAN_FORD_DIRECTED, bellman_ford_directed, "BellmanFordDirected", True), ("Dijkstra, undirected", TEST_DIJKSTRA_UNDIRECTED, dijkstra_undirected, "DijkstraUndirected", False), ("Dijkstra, directed", TEST_DIJKSTRA_DIRECTED, dijkstra_directed, "DijkstraDirected", True) ]: if USE_UNSAFE_GRAPH: print("\n\nLoading graph", FileName, "(Fast graph)") with open(os.path.join(os.getcwd(), FileName)) as f: G = load_graph(f, graph.Graph) else: print("\n\nLoading graph", FileName) with open(os.path.join(os.getcwd(), FileName)) as f: G = load_graph(f) for i, vertex in enumerate(list(G.vertices)): vertex.colornum = i do_testalg(testalg, G)
def run(proc_id, n_gpus, devices): th.manual_seed(1234) np.random.seed(1234) th.cuda.manual_seed_all(1234) dev_id = devices[proc_id] if n_gpus > 1: dist_init_method = 'tcp://{master_ip}:{master_port}'.format( master_ip='127.0.0.1', master_port='12345') world_size = n_gpus th.distributed.init_process_group(backend="nccl", init_method=dist_init_method, world_size=world_size, rank=dev_id) csr = load_graph('er100000') g = dgl.DGLGraph(csr, readonly=True) if proc_id == 0: print(g) in_feats = 300 n_classes = 10 features = th.rand(g.number_of_nodes(), in_feats).to(dev_id) labels = th.randint(n_classes, (g.number_of_nodes(), )).to(dev_id) # number of GCN layers L = 2 # number of hidden units of a fully connected layer n_hidden = 64 # dropout probability dropout = 0.2 # batch size batch_size = 1000 # number of neighbors to sample num_neighbors = 4 # number of epochs num_epochs = 10 model = GraphSAGE(in_feats, n_hidden, n_classes, L, F.relu, dropout, 'gcn') model = model.to(dev_id) loss_fcn = nn.CrossEntropyLoss() loss_fcn = loss_fcn.to(dev_id) optimizer = optim.Adam(model.parameters(), lr=0.03) th.cuda.synchronize() avg = 0 for epoch in range(num_epochs): i = 0 elapsed_time = 0 tic = time.time() if n_gpus > 1: th.distributed.barrier() # forward pred = model(g, features) # compute loss loss = loss_fcn(pred, labels) # backward optimizer.zero_grad() loss.backward() if n_gpus > 1: for param in model.parameters(): if param.requires_grad and param.grad is not None: th.distributed.all_reduce(param.grad.data, op=th.distributed.ReduceOp.SUM) param.grad.data /= n_gpus optimizer.step() toc = time.time() elapsed_time += toc - tic if n_gpus > 1: th.distributed.barrier() if proc_id == 0: print('epoch time: ', elapsed_time) if epoch >= 5: avg += elapsed_time if n_gpus > 1: th.distributed.barrier() if proc_id == 0: print('avg time: ', avg / (epoch - 4))
else: return MAYBE def verify_colors(G, H): """ Verify the colors of graph G and H :param G: The graph G :param H: The graph H :return: Whether the resulting coloring is a bijection or not """ for G_v in G.vertices: for H_v in H.vertices: if G_v.colornum == H_v.colornum: if not get_neighbourhood_colors(G_v) == get_neighbourhood_colors(H_v): return False return True if __name__ == "__main__": """ Main function :param 1: The .grl-file """ start = time.time() with open(sys.argv[1]) as f: graph_list = load_graph(f, read_list=True) refine_all(graph_list) end = time.time() print("Elapsed time (total): " + str(int((end - start) * 1000)) + " ms")
def load_graph_from_file(filename): """Check if results for the given file are correct""" with open(PATH + "/" + filename) as f: graphs = load_graph(f, read_list=True) return graphs[0][0]
def get_graphs_from_file(file: str) -> List[Graph]: with open(file) as f: graphs = load_graph(f, read_list=True) return graphs[0]
from graph_io import load_graph, write_dot def print_dot(filename, G): """ Print a dot file with filename and graph :param filename: The file :param G: The graph """ with open(filename, 'w') as f: write_dot(G, f) def print_all(basename, l): for i in range(len(l)): print_dot(basename + str(i) + ".dot", l[0][i]) if __name__ == "__main__": """ Main function :param 1: The .grl-file """ with open(sys.argv[1]) as f: if (str(sys.argv[1]).endswith(".grl")): graph_list = load_graph(f, read_list=True) print_all(str(sys.argv[1]).split('.grl')[0], graph_list) else: graph = load_graph(f, read_list=False) print_dot(str(sys.argv[1]).split('.gr')[0] + ".dot", graph)