{names} {subclusters} {' '*tab}}} """ return template def _vis_nodes(node): s = "\n" for nd in node.children: s = s + f" {id(node)} -> {id(nd)};\n" s = s + _vis_nodes(nd) return s if __name__ == "__main__": parser = argparse.ArgumentParser(description='Visualise graphs') parser.add_argument('graph_id', help='name of the query graph') args = parser.parse_args() g = parse.get_graph(args.graph_id) fig = plt.figure() ax = fig.add_subplot(121) visualise_graph(g, ax) ax2 = fig.add_subplot(122) visualise_sec_tree(q18.sectree, ax) # fig.show() # input("press") sectree.get_SEC(g, 1)
def main(): """ type of graph """ if GRAPH_TYPE is 'scale_free': n = GRAPH_SIZE graph = nx.scale_free_graph(n) graph = nx.Graph(graph) elif GRAPH_TYPE is 'isp': nodes, edges = parse.get_graph('../../speedy/data/visualizations/sample_topologies/BtNorthAmerica.gv') graph = nx.Graph() graph.add_nodes_from(nodes) graph.add_edges_from(edges) n = len(graph.nodes()) else: print "Error! Graph type invalid." adj_mat = nx.adjacency_matrix(graph) """ type of demand matrix """ if SRC_TYPE is 'uniform': """ uniform load """ demand_mat = np.ones([n, n]) np.fill_diagonal(demand_mat, 0.0) demand_mat = demand_mat/np.sum(demand_mat) demand_mat = demand_mat * 1000 * TXN_VALUE elif SRC_TYPE is 'skew': """ skewed load """ exp_load = np.exp(np.arange(0, -n, -1) * SKEW_RATE) exp_load = exp_load.reshape([n, 1]) demand_mat = exp_load * np.ones([1, n]) np.fill_diagonal(demand_mat, 0.0) demand_mat = demand_mat/np.sum(demand_mat) demand_mat = demand_mat * 1000 * TXN_VALUE else: print "Error! Source type invalid." """ credits on links, delay and number of paths to consider """ credit_mat = np.ones([n, n]) * CREDIT_AMT credit_mat = credit_mat / 2. credit_mat = adj_mat.multiply(credit_mat).todense() delay = .5 max_num_paths = MAX_NUM_PATHS """ initialize payment network """ network = payment_network(graph, demand_mat, credit_mat, delay, max_num_paths) """ initialize logs """ total_flow_values = np.zeros([1, NUM_ITERATIONS]) cs_err_l = np.zeros([1, NUM_ITERATIONS]) cs_err_y = np.zeros([1, NUM_ITERATIONS]) cs_err_z = np.zeros([1, NUM_ITERATIONS]) pc_err_d = np.zeros([1, NUM_ITERATIONS]) pc_err_c = np.zeros([1, NUM_ITERATIONS]) pc_err_b = np.zeros([1, NUM_ITERATIONS]) """ run distributed algorithm """ for step in range(NUM_ITERATIONS): network.update_flows() total_flow_values[0, step] = network.print_total_flow_value() network.update_prices() err_l, err_y, err_z, err_d, err_c, err_b = network.print_errors() cs_err_l[0, step] = err_l cs_err_y[0, step] = err_y cs_err_z[0, step] = err_z pc_err_d[0, step] = err_d pc_err_c[0, step] = err_c pc_err_b[0, step] = err_b print total_flow_values/np.sum(demand_mat) # network.print_link_prices() # network.print_flows() # network.print_path_prices() """ save logs """ np.save('./total_flow_values.npy', total_flow_values) np.save('./cs_err_l.npy', cs_err_l) np.save('./cs_err_y.npy', cs_err_y) np.save('./cs_err_z.npy', cs_err_z) np.save('./pc_err_d.npy', pc_err_d) np.save('./pc_err_c.npy', pc_err_c) np.save('./pc_err_b.npy', pc_err_b) np.save('./demand_mat.npy', demand_mat)
def main(): """ read credit amount """ credit_amt = CREDIT_AMT """ construct graph """ if GRAPH_TYPE is 'test': graph = nx.Graph() graph.add_nodes_from([0, 1, 2, 3]) graph.add_edges_from([(0, 1), (1, 2), (2, 3)]) n = len(graph.nodes()) elif GRAPH_TYPE is 'scale_free': n = GRAPH_SIZE graph = nx.scale_free_graph(n, seed=RAND_SEED) graph = nx.Graph(graph) graph.remove_edges_from(graph.selfloop_edges()) elif GRAPH_TYPE is 'erdos_renyi': n = GRAPH_SIZE graph = nx.fast_gnp_random_graph(n, 0.2, seed=RAND_SEED) elif GRAPH_TYPE is 'isp': nodes, edges = parse.get_graph( '../../speedy/data/visualizations/sample_topologies/BtNorthAmerica.gv' ) graph = nx.Graph() graph.add_nodes_from(nodes) graph.add_edges_from(edges) n = len(graph.nodes()) else: print "Error! Graph type invalid." assert nx.is_connected(graph) """ construct demand matrix """ if SRC_TYPE is 'test': """ test load """ demand_mat = np.zeros([n, n]) demand_mat[0, 1] = 1. demand_mat[1, 0] = 1. demand_mat[1, 3] = 1. demand_mat[3, 1] = 1. demand_mat = demand_mat / np.sum(demand_mat) demand_mat = demand_mat * 1000 * TXN_VALUE elif SRC_TYPE is 'uniform': """ uniform load """ demand_mat = np.ones([n, n]) np.fill_diagonal(demand_mat, 0.0) demand_mat = demand_mat / np.sum(demand_mat) demand_mat = demand_mat * 1000 * TXN_VALUE elif SRC_TYPE is 'skew': """ skewed load """ exp_load = np.exp(np.arange(0, -n, -1) * SKEW_RATE) exp_load = exp_load.reshape([n, 1]) demand_mat = exp_load * np.ones([1, n]) np.fill_diagonal(demand_mat, 0.0) demand_mat = demand_mat / np.sum(demand_mat) demand_mat = demand_mat * 1000 * TXN_VALUE else: print "Error! Source type invalid." "" """ construct credit matrix """ if CREDIT_TYPE is 'uniform': credit_mat = np.ones([n, n]) * credit_amt elif CREDIT_TYPE is 'random': np.random.seed(RAND_SEED) credit_mat = np.triu(np.random.rand(n, n), 1) * 2 * credit_amt credit_mat += credit_mat.transpose() else: print "Error! Credit matrix type invalid." delay = DELAY total_flow_skew_list = [0.] # np.linspace(0, 2, 20) throughput = np.zeros(len(total_flow_skew_list)) solver = global_optimal_flows(graph, demand_mat, credit_mat, delay) for i, total_flow_skew in enumerate(total_flow_skew_list): throughput[i] = solver.compute_lp_solution(total_flow_skew) # solver.print_lp_solution() print throughput / np.sum(demand_mat) np.save('./throughput.npy', throughput) np.save('./total_flow_skew.npy', total_flow_skew_list)
def main(): """ construct graph """ if GRAPH_TYPE == 'test': graph = nx.Graph() graph.add_nodes_from([0, 1, 2, 3]) graph.add_edges_from([(0, 1), (1, 2), (2, 3)]) n = len(graph.nodes()) elif GRAPH_TYPE == 'scale_free': n = GRAPH_SIZE graph = nx.scale_free_graph(n, seed=RAND_SEED) graph = nx.Graph(graph) graph.remove_edges_from(graph.selfloop_edges()) elif GRAPH_TYPE == 'small_world': n = GRAPH_SIZE graph = nx.watts_strogatz_graph(n, k=8, p=0.01, seed=RAND_SEED) elif GRAPH_TYPE == 'erdos_renyi': n = GRAPH_SIZE graph = nx.fast_gnp_random_graph(n, 0.2, seed=RAND_SEED) elif GRAPH_TYPE == 'isp': nodes, edges = parse.get_graph( '../../speedy/data/visualizations/sample_topologies/BtNorthAmerica.gv' ) graph = nx.Graph() graph.add_nodes_from(nodes) graph.add_edges_from(edges) n = len(graph.nodes()) elif GRAPH_TYPE == 'lnd': graph = nx.read_edgelist( "../oblivious_routing/lnd_dec4_2018_reducedsize.edgelist") rename_dict = {v: int(str(v)) for v in graph.nodes()} graph = nx.relabel_nodes(graph, rename_dict) for e in graph.edges(): graph.edges[e]['capacity'] = int(str(graph.edges[e]['capacity'])) graph = nx.Graph(graph) graph.remove_edges_from(graph.selfloop_edges()) n = nx.number_of_nodes(graph) else: print "Error! Graph type invalid." assert nx.is_connected(graph) """ construct demand matrix """ if SRC_TYPE == 'test': """ test load """ demand_mat = np.zeros([n, n]) demand_mat[0, 1] = 1. demand_mat[1, 0] = 1. demand_mat[1, 3] = 1. demand_mat[3, 1] = 1. demand_mat = demand_mat / np.sum(demand_mat) demand_mat = demand_mat * 1000 * TXN_VALUE elif SRC_TYPE == 'uniform': """ uniform load """ demand_mat = np.ones([n, n]) np.fill_diagonal(demand_mat, 0.0) demand_mat = demand_mat / np.sum(demand_mat) demand_mat = demand_mat * 1000 * TXN_VALUE elif SRC_TYPE == 'skew': """ skewed load """ exp_load = np.exp(np.arange(0, -n, -1) * SKEW_RATE) exp_load = exp_load.reshape([n, 1]) demand_mat = exp_load * np.ones([1, n]) np.fill_diagonal(demand_mat, 0.0) demand_mat = demand_mat / np.sum(demand_mat) demand_mat = demand_mat * 1000 * TXN_VALUE elif SRC_TYPE == 'lnd': """ lnd load """ with open('./lnd_demand.pkl', 'rb') as input: demand_dict = pickle.load(input) demand_mat = np.zeros([n, n]) for (i, j) in demand_dict.keys(): demand_mat[i, j] = demand_dict[i, j] demand_mat = demand_mat * 10 else: print "Error! Source type invalid." "" """ construct credit matrix """ if CREDIT_TYPE == 'uniform': credit_mat = np.ones([n, n]) * 10 elif CREDIT_TYPE == 'random': np.random.seed(RAND_SEED) credit_mat = np.triu(np.random.rand(n, n), 1) * 2 credit_mat += credit_mat.transpose() credit_mat = credit_mat.astype(int) elif CREDIT_TYPE == 'lnd': credit_mat = np.zeros([n, n]) for e in graph.edges(): credit_mat[e[0], e[1]] = graph.edges[e]['capacity'] / 1000 credit_mat[e[1], e[0]] = graph.edges[e]['capacity'] / 1000 else: print "Error! Credit matrix type invalid." total_flow_skew_list = [0.] # np.linspace(0, 2, 20) throughput = np.zeros(len(total_flow_skew_list)) solver = global_optimal_flows(graph, demand_mat, credit_mat, MAX_NUM_PATHS) for i, total_flow_skew in enumerate(total_flow_skew_list): throughput[i] = solver.compute_lp_solution(total_flow_skew) # solver.print_lp_solution() # capacity_slack, capacity = solver.compute_capacity_slack() # for key in capacity_slack.keys(): # print key, capacity[key], round(capacity_slack[key], 2) # solver.draw_flow_graph() print throughput / np.sum(demand_mat) np.save('./throughput.npy', throughput) np.save('./total_flow_skew.npy', total_flow_skew_list)
def main(): """ construct graph """ if GRAPH_TYPE is 'test': graph = nx.Graph() graph.add_nodes_from([0, 1, 2, 3]) graph.add_edges_from([(0, 1), (1, 2), (2, 3)]) n = len(graph.nodes()) elif GRAPH_TYPE == 'scale_free': n = GRAPH_SIZE graph = nx.scale_free_graph(n, seed=RAND_SEED) graph = nx.Graph(graph) graph.remove_edges_from(graph.selfloop_edges()) elif GRAPH_TYPE == 'small_world': n = GRAPH_SIZE graph = nx.watts_strogatz_graph(n, 8, 0.25, seed=RAND_SEED) graph = nx.Graph(graph) graph.remove_edges_from(graph.selfloop_edges()) elif GRAPH_TYPE is 'isp': nodes, edges = parse.get_graph( '../../speedy/data/visualizations/sample_topologies/BtNorthAmerica.gv' ) graph = nx.Graph() graph.add_nodes_from(nodes) graph.add_edges_from(edges) n = len(graph.nodes()) else: print "Error! Graph type invalid." """ construct demand matrix """ if SRC_TYPE is 'test': """ test load """ demand_mat = np.zeros([n, n]) demand_mat[0, 1] = 1. demand_mat[1, 0] = 1. demand_mat[1, 3] = 1. demand_mat[3, 1] = 1. np.fill_diagonal(demand_mat, 0.0) demand_mat = demand_mat / np.sum(demand_mat) demand_mat = demand_mat * 1000 * TXN_VALUE elif SRC_TYPE is 'uniform': """ uniform load """ demand_mat = np.ones([n, n]) np.fill_diagonal(demand_mat, 0.0) demand_mat = demand_mat / np.sum(demand_mat) demand_mat = demand_mat * 1000 * TXN_VALUE elif SRC_TYPE is 'skew': """ skewed load """ exp_load = np.exp(np.arange(0, -n, -1) * SKEW_RATE) exp_load = exp_load.reshape([n, 1]) demand_mat = exp_load * np.ones([1, n]) np.fill_diagonal(demand_mat, 0.0) demand_mat = demand_mat / np.sum(demand_mat) demand_mat = demand_mat * 1000 * TXN_VALUE elif SRC_TYPE == 'pickle': """ pickle load """ with open('./demands/sw_10_routers_circ0_demand3_demand.pkl', 'rb') as input: demand_dict = pickle.load(input) demand_mat = np.zeros([n, n]) for (i, j) in demand_dict.keys(): demand_mat[i, j] = demand_dict[i, j] demand_mat = demand_mat * 88 * 3 else: print "Error! Source type invalid." "" """ construct credit matrix """ if CREDIT_TYPE is 'uniform': credit_mat = np.ones([n, n]) * 1200 else: print "Error! Credit matrix type invalid." delay = SINGLE_HOP_DELAY total_flow_skew_list = [0.] # np.linspace(0, 200000, 20) throughput = np.zeros(len(total_flow_skew_list)) solver = global_optimal_flows(graph, demand_mat, credit_mat, delay, MAX_NUM_PATHS, GRAPH_TYPE) for i, total_flow_skew in enumerate(total_flow_skew_list): throughput[i] = solver.compute_lp_solution(total_flow_skew) solver.print_lp_solution() # solver.draw_flow_graph() print throughput / np.sum(demand_mat) print solver.problem.status np.save('./throughput.npy', throughput) np.save('./total_flow_skew.npy', total_flow_skew_list)