import time from grid_digraph_generator import GridDigraphGenerator from networkx_graph_helper import NetworkXGraphHelper from dreyfus_based_woc import DreyfusBasedWOC if __name__ == '__main__': m = n = 10 gh = GridDigraphGenerator() graph = gh.generate(m, n, edge_weighted=False) terminals = [88, 66, 77, 5, 33, 53, 71] pois = [65, 12] db_woc = DreyfusBasedWOC(graph) start_time = time.clock() forest, cost = db_woc.steiner_forest(terminals, pois, 4, method="Voronoi") elapsed_time = time.clock() - start_time ngh = NetworkXGraphHelper(graph) ngh.draw_graph(special_nodes=[(pois, None, None), (terminals, None, None)], special_subgraphs=[(forest, "#FF0000")], title_1="Dreyfus-based", title_2="Cost: " + str(cost) + ", Elapsed time: " + str(elapsed_time), edge_labels=db_woc.congestion, print_edge_labels=True)
from grid_digraph_generator import GridDigraphGenerator from networkx_graph_helper import NetworkXGraphHelper if __name__ == '__main__': gh = GridDigraphGenerator() graph = gh.generate(10, 10) subgraph = graph.extract_node_induced_subgraph( [4, 8, 9, 10, 34, 38, 37, 66, 35, 65, 67]) ngh = NetworkXGraphHelper(graph) ngh.draw_graph(special_subgraphs=[(subgraph, None)], print_node_labels=True)
suitability_graph = SuitabilityGraph() suitability_graph.append_graph(node_weighted) suitability_graph.extend_suitable_regions(seed, generator) suitability_graph.extend_suitable_regions(seed, generator) regions = suitability_graph.get_suitable_regions(generator) start_time = time.clock() dr = DreyfusIMRV2(suitability_graph, terminals, contract_graph=True, within_convex_hull=False) steiner_tree = dr.steiner_tree(consider_terminals=False) elapsed_time = time.clock() - start_time cost, node_cost = steiner_tree.compute_total_weights(terminals) ngh = NetworkXGraphHelper(suitability_graph) ngh.draw_graph( nodes_2=terminals, subgraphs_1=[r for _, (r, _, _, _, _, _) in regions.iteritems()], subgraphs_2=[steiner_tree], node_weight_generator=generator, title_1="Extended Dreyfus, seed = " + str(seed), title_2="Cost: " + str(cost - node_cost) + ", elapsed time: " + str(elapsed_time), print_node_labels=False, node_size=15, print_edge_labels=False)
req_1 = ([22, 55, 43], [27, 99]) req_2 = ([63, 76], [35, 46]) req_3 = ([47, 68, 97], [42, 15, 90]) req_4 = ([64, 23], [56]) req_5 = ([25, 75], [33]) requests = [req_1, req_2, req_3, req_4, req_5] st = time.clock() MSTs = bpr.steiner_forest(requests) et = time.clock() - st print et special_subgraphs = [(MST, None) for _, (MST, cost) in MSTs.iteritems()] ngh = NetworkXGraphHelper(graph) ngh.draw_graph(special_nodes=[(req_1[0], '#000000', 50), (req_1[1], '#000000', 100), (req_2[0], '#0000FF', 50), (req_2[1], '#0000FF', 100), (req_3[0], '#13E853', 50), (req_3[1], '#13E853', 100), (req_4[0], '#FF0000', 50), (req_4[1], '#FF0000', 100), (req_5[0], '#E67E22', 50), (req_5[1], '#E67E22', 100)], special_subgraphs=special_subgraphs, print_edge_labels=True, edge_labels=bpr.congestion)
suitability_graph.append_graph(graph) suitability_graph.extend_suitable_regions(seed, generator) suitability_graph.extend_suitable_regions(seed, generator) regions = suitability_graph.get_suitable_regions(generator) # suitability_graph.contract_suitable_regions(generator) # paths = [] # for t in terminals: # _, paths_t = dijkstra(suitability_graph, t, [poi], consider_node_weights=False) # paths.append(paths_t[poi]) # _, paths_poi = suitability_graph.compute_shortest(poi, terminals) suitability_graph.compute_dist_paths(origins=[poi], destinations=terminals) paths = [ suitability_graph.paths[tuple(sorted([t, poi]))] for t in terminals ] tree = SuitabilityGraph() for p in paths: tree.append_path(p, suitability_graph) ngh = NetworkXGraphHelper(suitability_graph) ngh.draw_graph( special_nodes=[(terminals, None, None), ([poi], None, None)], # subgraphs_1=[r for _, (r, _, _, _, _, _) in regions.iteritems()], special_subgraphs=[(tree, None)], print_node_labels=False)
from grid_digraph_generator import GridDigraphGenerator from networkx_graph_helper import NetworkXGraphHelper from dreyfus import Dreyfus if __name__ == '__main__': m = n = 10 gh = GridDigraphGenerator() node_weighted = gh.generate(m, n, edge_weighted=False) df = Dreyfus(node_weighted) for v in range(m * n): ecc, inc = node_weighted.steiner_n_stats(3, v, df) print(ecc, inc) ngh = NetworkXGraphHelper(node_weighted) ngh.draw_graph(print_node_labels=True, node_size=15)
regions = suitability_graph.get_suitable_regions(generator) cap = 5 max_dr = sys.maxint last_mile = sys.maxint hb = HotspotBased(suitability_graph, terminals, pois) start_time = time.clock() forest, cost, gr, avg_dr, num_trees, avg_or = hb.steiner_forest( k=cap, max_dr=max_dr, max_wd=last_mile) elapsed_time = time.clock() - start_time # cost2, _ = forest.calculate_costs() ngh = NetworkXGraphHelper(suitability_graph) ngh.draw_graph(nodes_1=pois, nodes_2=terminals, node_weight_generator=generator, subgraphs_1=[ r for _, (r, _, _, _, _, _) in regions.iteritems() ], subgraphs_2=[forest], title_1="Hotspot-based, seed: " + str(seed) + ", Capacity: " + str(cap) + ", Max. Detour ratio: " + str(max_dr) + ", Last mile: " + str(last_mile), title_2="Cost: " + str(cost) + ", Gain ratio: " + str(gr) + ", Avg. detour ratio: " + str(avg_dr) + ", Num. trees: " + str(num_trees) + ", Avg. occ. rate: " + str(avg_or) + ", elapsed time: " + str(elapsed_time), print_node_labels=False,
if __name__ == '__main__': m = n = 30 gh = GridDigraphGenerator() graph = gh.generate(m, n, edge_weighted=False) np.random.seed(0) terminals = np.random.choice(a=m * n, size=35, replace=False) pois = terminals[:5] terminals = terminals[5:] vst_rs = VST_RS(graph) start_time = time.clock() forest, cost, _, _, _, _, _ = vst_rs.steiner_forest(terminals, pois, 4, 8, merge_users=False) elapsed_time = time.clock() - start_time ngh = NetworkXGraphHelper(graph) ngh.draw_graph(special_nodes=[(pois, None, None), (terminals, None, None)], special_subgraphs=[(forest, "#FF0000")], title_1="VST-RS", title_2="Cost: " + str(cost) + ", Elapsed, time: " + str(elapsed_time), edge_labels=vst_rs.load, print_edge_labels=True)
from grid_digraph_generator import GridDigraphGenerator from steiner_forest_lp_directed import SteinerForestLPDirected from networkx_graph_helper import NetworkXGraphHelper if __name__ == '__main__': gh = GridDigraphGenerator() # graph = gh.generate(10, 10) # requests = [[27, 22, 55], [35, 63, 76]] graph = gh.generate(5, 5, edge_weighted=False) # requests = [[3, 10, 22], [16, 6, 19]] requests = [[21, 11, 18], [4, 3, 6]] sfd = SteinerForestLPDirected(graph, requests) forest = sfd.steiner_forest() ngh = NetworkXGraphHelper(graph) ngh.draw_graph(special_nodes=[(requests[0], '#000000', 50)], special_subgraphs=[(forest, None)], print_node_labels=True)
gh = GridDigraphGenerator() graph = gh.generate(m, n, capacitated=True, capacities_range=(1, 100), edge_weighted=False, node_weighted=True, node_weight_generator=generator, seed=seed ) # terminals = np.random.choice(a=m * n, size=8, replace=False) terminals = [64, 75, 56, 7, 35] # pois = terminals[:3] pois = [20, 49] # terminals = terminals[3:] mz = VST_RS(graph) start_time = time.clock() forest, cost, _, _, _, _, sts, _ = mz.steiner_forest(terminals, pois, 5, 8) elapsed_time = time.clock() - start_time special_nodes = [(terminals, '#000000', 35), (pois, '#0000FF', 65)] ngh = NetworkXGraphHelper(graph) ngh.draw_graph(special_nodes=special_nodes, # special_subgraphs=[(forest, "#FF0000")], special_subgraphs=[(st[1], None) for st in sts], title_1="Mustafiz's algorithm, seed = " + str(seed), title_2="Cost: " + str(cost) + ", elapsed time: " + str(elapsed_time))
# node_weight_generator=generator, node_weights=node_weights, seed=seed) # pois = [634, 496, 310] # terminals = [696, 610, 518, 493, 583, 288, 320, 439, 187, 587, 590, 259, 200, 261] pois = [133] terminals = [220, 250, 190, 285, 461, 491] suitability_graph = SuitabilityGraph() suitability_graph.append_graph(node_weighted) hb = HotspotBased(suitability_graph, terminals, pois) start_time = time.clock() forest, cost, gr, avg_dr, num_trees, avg_or, avg_oc = hb.steiner_forest( k=5, get_lsv=False) elapsed_time = time.clock() - start_time # forest = SuitabilityDigraph() # cost = elapsed_time = 0 ngh = NetworkXGraphHelper(suitability_graph) ngh.draw_graph(special_nodes=[(terminals, None, None), (pois, None, None)], special_subgraphs=[(forest, None)], title_1="Hotspot-based, seed = " + str(seed), title_2="Cost: " + str(cost) + ", Gain ratio: " + str(gr) + ", Avg. detour ratio: " + str(avg_dr) + ", Num. trees: " + str(num_trees) + ", Avg. occ. rate: " + str(avg_or) + ", elapsed time: " + str(elapsed_time), print_node_labels=True)
where = set(range(m * n)).difference(occupied) terminals = np.random.choice(a=list(where), size=15, replace=False) pois_4 = terminals[:2] terminals_4 = terminals[2:] queries.append((terminals_4, pois_4)) vst_rs = VST_RS(graph) start_time = time.clock() plans, cost, warl, mwrl, mrl1, mrl2, entropy, ni = \ vst_rs.congestion_aware(queries, 4, 8, bpr, randomize=False, max_iter=20) elapsed_time = time.clock() - start_time print cost, warl, mwrl, elapsed_time ngh = NetworkXGraphHelper(graph) # labels = graph.get_capacities() # labels = {e: round(float(vst_rs.load[e]) / graph.get_capacities()[e], 2) for e in graph.get_edges()} labels = {e: vst_rs.load[e] for e in graph.get_edges()} ngh.draw_graph( special_subgraphs=[(plan, None) for _, plan in plans], special_nodes=[(terminals_1, '#000000', 25), (pois_1, '#000000', 65), (terminals_2, '#0000FF', 25), (pois_2, '#0000FF', 65), (terminals_3, '#13E853', 25), (pois_3, '#13E853', 65), (terminals_4, '#FF0000', 25), (pois_4, '#FF0000', 65) ], edge_labels=labels, print_edge_labels=True, print_node_labels=False, title_1="Congestion-aware",
# t_sn = list(terminals) # t_sn.append(sn) # t_sn_mc = suitability_graph.build_metric_closure(t_sn) # mst_t_sn_mc = Prim(t_sn_mc).spanning_tree() # closures_edges.update(t_sn_mc.get_edges()) # msts_edges.update(mst_t_sn_mc.get_edges()) # ee = closures_edges - msts_edges # excluded_edges.update(ee) suitability_mc = suitability_graph.build_suitability_metric_closure( generator, terminals, excluded_edges) dr = Dreyfus(suitability_mc) st, cost = dr.steiner_tree(terminals, consider_terminals=False) steiner_tree = suitability_graph.build_subgraph_from_metric_closure(st) elapsed_time = time.clock() - start_time cost, node_cost = steiner_tree.compute_total_weights(terminals) special_subgraphs = [(r, "#00FF00") for _, (r, _, _, _, _, _) in regions.iteritems()] special_subgraphs.append((steiner_tree, "#FF0000")) ngh = NetworkXGraphHelper(suitability_graph) ngh.draw_graph(special_nodes=[(terminals, None, None)], special_subgraphs=special_subgraphs, title_1="Dreyfus (metric closure), seed = " + str(seed), title_2="Cost: " + str(cost - node_cost) + ", elapsed time: " + str(elapsed_time), print_node_labels=False, print_edge_labels=False)
terminals = [870, 902, 776, 578, 585] suitability_graph = SuitabilityGraph() suitability_graph.append_graph(node_weighted) suitability_graph.extend_suitable_regions(seed, generator) suitability_graph.extend_suitable_regions(seed, generator) regions = suitability_graph.get_suitable_regions(generator) suitable_nodes = suitability_graph.get_suitable_nodes( generator, excluded_nodes=terminals) dist, _ = dijkstra(suitability_graph, terminals[0], terminals[1:]) upper_bound = sum([dist[t] for t in terminals[1:]]) c = [] for s in suitable_nodes: dist_s, _ = dijkstra(suitability_graph, s, terminals) total_s = sum([dist_s[t] for t in terminals]) if total_s <= upper_bound: c.append(s) ngh = NetworkXGraphHelper(suitability_graph) ngh.draw_graph(special_nodes=[(terminals, None, None), (c, None, None)], special_subgraphs=[(r, "#00FF00") for _, (r, _, _, _, _, _) in regions.iteritems()], print_node_labels=False)
from grid_digraph_generator import GridDigraphGenerator from steiner_tree_lp import SteinerTreeLP from networkx_graph_helper import NetworkXGraphHelper if __name__ == '__main__': gh = GridDigraphGenerator() graph = gh.generate(5, 5) terminals = [] poi = None splp = SteinerTreeLP(graph, terminals, poi) ngh = NetworkXGraphHelper(graph) ngh.draw_graph()
while set(suitability_graph.keys()).intersection(terminals) != set( terminals) or set(hotspots).intersection(terminals) != set(): terminals = np.random.choice(a=m * n, size=30, replace=False) pois = terminals[:3] terminals = terminals[3:] regions = suitability_graph.get_suitable_regions(generator) mz = VST_RS(suitability_graph) start_time = time.clock() forest, cost, gr, _, _, _, _ = mz.steiner_forest(terminals, pois, 5, 8, merge_users=False) elapsed_time = time.clock() - start_time # cost2, _ = forest.calculate_costs() special_nodes = [(terminals, '#000000', 35), (pois, '#0000FF', 65)] ngh = NetworkXGraphHelper(suitability_graph) ngh.draw_graph(special_nodes=special_nodes, special_subgraphs=[(forest, None)], title_1="Mustafiz's algorithm, seed = " + str(seed), title_2="Cost: " + str(cost) + ", Gain ratio: " + str(gr) + ", elapsed time: " + str(elapsed_time), print_node_labels=False, print_edge_labels=False)
suitability_graph.extend_suitable_regions(seed, generator) suitability_graph.extend_suitable_regions(seed, generator) suitability_graph.extend_suitable_regions(seed, generator) suitability_graph.extend_suitable_regions(seed, generator) hotspots = suitability_graph.get_suitable_nodes(generator) terminals = np.random.choice(a=m * n, size=60, replace=False) while set(suitability_graph.keys()).intersection(terminals) != set( terminals) or set(hotspots).intersection(terminals) != set(): terminals = np.random.choice(a=m * n, size=60, replace=False) pois = terminals[:15] terminals = terminals[15:] regions = suitability_graph.get_suitable_regions(generator) hb = HotspotBased(suitability_graph, terminals, pois) start_time = time.clock() forest, cost, _, _, _, _, _ = hb.steiner_forest(clone_hotspots=True, get_lsv=False) elapsed_time = time.clock() - start_time ngh = NetworkXGraphHelper(suitability_graph) ngh.draw_graph(special_nodes=[(pois, None, None), (terminals, None, None)], special_subgraphs=[(forest, None)], title_1="Hotspot-based, seed = " + str(seed), title_2="Cost: " + str(cost) + ", elapsed time: " + str(elapsed_time), print_node_labels=False)
graph.append_edge_2((15, 21), weight=1, capacity=1) graph.append_edge_2((16, 17), weight=1, capacity=1) graph.append_edge_2((16, 22), weight=1, capacity=1) graph.append_edge_2((17, 23), weight=1, capacity=1) graph.append_edge_2((18, 19), weight=1, capacity=1) graph.append_edge_2((19, 20), weight=1, capacity=1) graph.append_edge_2((20, 21), weight=0.5, capacity=1) graph.append_edge_2((21, 22), weight=0.5, capacity=1) graph.append_edge_2((22, 23), weight=1, capacity=1) queries = [([3, 8, 14, 15], [6, 12, 10]), ([20, 21], [1, 9]), ([0, 5, 23], [11])] vst_rs = VST_RS(graph) # plans, cost, weighted_avg_relative_load, max_relative_load = vst_rs.non_congestion_aware(queries, 4, 8, bpr_log) plans, cost, warl, mwrl, mrl1, mrl2, entropy, ni = \ vst_rs.congestion_aware(queries, 4, 8, bpr, log_history=True, randomize=False) ngh = NetworkXGraphHelper(graph) ngh.draw_graph(special_nodes=[([3, 8, 14, 15], '#000000', 35), ([6, 12, 10], '#000000', 65), ([20, 21], '#0000FF', 35), ([1, 9], '#0000FF', 65), ([0, 5, 23], '#13E853', 35), ([11], '#13E853', 65)], special_subgraphs=[(plan, None) for _, plan in plans], title_2="Cost (w. congestion): " + str(round(cost, 2)), edge_labels=vst_rs.load, print_node_labels=True, print_edge_labels=True)
merge_users = False max_iter = 50 alpha = 1.0 beta = 4.0 # NCA ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ vst_rs = VST_RS(graph) st = time.clock() plans, c, warl, mwrl, mrl1, mrl2, entropy = \ vst_rs.non_congestion_aware(queries_z, 4, 8, bpr, merge_users=merge_users, alpha=alpha, beta=beta, verbose=True) et = time.clock() - st print c, warl, mwrl, mrl1, mrl2, entropy, et ngh = NetworkXGraphHelper(graph) labels = { e: vst_rs.load[e] for e in graph.get_edges() if vst_rs.load[e] > 1 } ngh.draw_graph(special_subgraphs=[(plan, None) for _, plan, _ in plans], special_nodes=special, edge_labels=labels, print_edge_labels=True, title_2="Cost: {:.2f}".format(c)) # CA +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ vst_rs = VST_RS(graph) st = time.clock() plans, c, warl, mwrl, mrl1, mrl2, entropy, ni, list_plans = \ vst_rs.congestion_aware(queries_z, 4, 8, bpr, merge_users=merge_users, max_iter=max_iter, alpha=alpha,
from graph import Graph from grid_digraph_generator import GridDigraphGenerator from networkx_graph_helper import NetworkXGraphHelper if __name__ == '__main__': gh = GridDigraphGenerator() graph = gh.generate(5, 5, edge_weighted=False) graph[18][19] = graph[19][18] = 0.5 graph[1][6] = graph[6][1] = 0.5 graph.compute_dist_paths() subtree_1 = Graph() subtree_1.append_path(graph.paths[(0, 12)], graph) subtree_2 = Graph() subtree_2.append_path(graph.paths[(4, 12)], graph) subtree_3 = Graph() subtree_3.append_path(graph.paths[(12, 20)], graph) subtree_4 = Graph() subtree_4.append_path(graph.paths[(12, 24)], graph) ngh = NetworkXGraphHelper(graph) ngh.draw_graph(special_subgraphs=[(subtree_1, None), (subtree_2, None), (subtree_3, None), (subtree_4, None)], print_node_labels=True)
weights = {v: generator.weights["VERY_SUITABLE"][0] for v in suitability_graph} suitability_graph.update_node_weights(weights) b = Baltz(suitability_graph) requests = [[27, 22, 55, 43], [35, 63, 76], [42, 47, 68], [56, 64, 23], [33, 25, 75]] st = time.clock() MSTs = b.steiner_forest(requests) et = time.clock() - st print et special_subgraphs = [(MST, None) for _, (MST, cost) in MSTs.iteritems()] ngh = NetworkXGraphHelper(suitability_graph) ngh.draw_graph(special_subgraphs=special_subgraphs, print_node_labels=True) # mz = Mustafiz(suitability_graph, [63, 76], [35], 4, sys.maxint) # T0, l, _, _, _, _ = mz.steiner_forest() # special_subgraphs = [(T0, '#000000')] # ngh = NetworkXGraphHelper(suitability_graph) # ngh.draw_graph(special_subgraphs=special_subgraphs, node_labels=True) # # mz = Mustafiz(suitability_graph, [47, 68], [42], 4, sys.maxint) # T1, l, _, _, _, _ = mz.steiner_forest() # special_subgraphs = [(T1, '#0000FF')] # ngh = NetworkXGraphHelper(suitability_graph) # ngh.draw_graph(special_subgraphs=special_subgraphs, node_labels=True) # # mz = Mustafiz(suitability_graph, [64, 23], [56], 4, sys.maxint)
node_weight_generator=generator, seed=seed) terminals = [288, 315, 231, 312, 111, 609, 645, 434, 654, 469, 186] suitability_graph = SuitabilityGraph() suitability_graph.append_graph(node_weighted) suitability_graph.extend_suitable_regions(seed, generator) suitability_graph.extend_suitable_regions(seed, generator) regions = suitability_graph.get_suitable_regions(generator) start_time = time.clock() sp = SpidersV2(graph=suitability_graph, terminals=terminals) steiner_tree = sp.steiner_tree() elapsed_time = time.clock() - start_time cost, node_cost = steiner_tree.compute_total_weights(terminals) ngh = NetworkXGraphHelper(suitability_graph) ngh.draw_graph( special_nodes=[(terminals, None, None)], # special_subgraphs=[r for _, (r, _, _) in regions.iteritems()], special_subgraphs=[(steiner_tree, None)], title_1="Spiders V2, seed = " + str(seed), title_2="Cost: " + str(cost - node_cost) + ", elapsed time: " + str(elapsed_time), print_node_labels=False, print_edge_labels=False)
from grid_digraph_generator import GridDigraphGenerator from graph import Graph from networkx_graph_helper import NetworkXGraphHelper if __name__ == '__main__': # graph = {'a': {'c': 14, 'd': 9, 'e': 15}, # 'c': {'a': 14, 'f': 1}, # 'd': {'a': 9, 'f': 11, 'g': 5, 'e': 3}, # 'e': {'a': 15, 'd': 3, 'g': 15, 'h': 20}, # 'f': {'c': 1, 'd': 11, 'g': 20, 'i': 1}, # 'g': {'f': 20, 'd': 5, 'e': 15, 'h': 2, 'i': 30}, # 'h': {'e': 20, 'g': 2, 'b': 3}, # 'i': {'f': 1, 'g': 30, 'b': 1}, # 'b': {'i': 1, 'h': 3}} gh = GridDigraphGenerator() graph = gh.generate(30, 30) terminals = [123, 464] distances_dual = shortest_path_dual(graph, terminals[0]) distances, paths = shortest_path_primal(graph, terminals[0]) ngh = NetworkXGraphHelper(graph) graph_path = Graph() graph_path.append_path(paths[terminals[1]], graph) ngh.draw_graph(nodes_2=terminals, subgraphs_2=[graph_path], title_2="Primal: " + str(distances[terminals[1]]) + ", Dual: " + str(distances_dual[terminals[1]]))
if __name__ == '__main__': generator = SuitableNodeWeightGenerator() seed = 0 m = n = 40 gh = GridDigraphGenerator() node_weighted = gh.generate(m, n, edge_weighted=False, node_weighted=True, node_weight_generator=generator, seed=seed) terminals = [0, 90] poi = 55 terminals_poi = list(terminals) terminals_poi.append(poi) suitability_graph = SuitabilityGraph() suitability_graph.append_graph(node_weighted) exclusive = suitability_graph.build_suitability_metric_closure( generator, terminals_poi) ngh = NetworkXGraphHelper(exclusive) ngh.draw_graph(print_node_labels=True)
return stats_ if __name__ == '__main__': # Generate the graph. m = n = 10 gh = GridDigraphGenerator() graph = gh.generate(m, n, edge_weighted=False) # Setup the requests and vehicles. requests = [([(3, 1, 300), (27, 1, 300)], (38, 1, 300)), ([(12, 1, 300), (68, 1, 300), (63, 1, 300)], (55, 1, 300)), ([(3, 1, 300), (27, 1, 300)], (24, 1, 300))] vehicles = [((6, 1, 300), (29, 1, 300)), ((78, 1, 300), (54, 1, 300))] # CSDP-AP and plot objects. csdp_ap = CsdpAp(graph) ngh = NetworkXGraphHelper(graph) # ------------------------------------------------------------------------------------------------------------------ # MILP # ------------------------------------------------------------------------------------------------------------------ routes, cost = csdp_ap.solve(requests, vehicles) stats = compute_stats_per_driver_type(routes, graph) print stats ss = ngh.special_subgraphs_from_paths(routes) if routes is not None: ngh.draw_graph(special_nodes=[([3, 27, 12, 68, 63], None, 65), ([38, 55, 24], None, 65), ([6, 29, 78, 54], None, 65)], special_subgraphs=ss, title_1="MILP solution", title_2="Cost: %f" % cost,
from grid_digraph_generator import GridDigraphGenerator from rayward_smith import RaywardSmith from networkx_graph_helper import NetworkXGraphHelper if __name__ == '__main__': gh = GridDigraphGenerator() graph = gh.generate(30, 30, edge_weighted=False) terminals = [288, 315, 231, 312, 111, 609, 645, 434, 654, 469, 186] # terminals = [3, 7, 9, 15] rs = RaywardSmith(graph, terminals) # steiner_tree, cost, best_nodes = rs.steiner_tree() rs_st = rs.steiner_tree() rs_cost, node_cost = rs_st.compute_total_weights(terminals) ngh = NetworkXGraphHelper(graph) ngh.draw_graph(special_nodes=[(terminals, None, None)], special_subgraphs=[(rs_st, None)], title_1='Rayward-Smith algorithm (network graph)', title_2="Cost: " + str(rs_cost) + ", Nodes: " + str(node_cost) + ", Edges: " + str(rs_cost - node_cost), print_node_labels=False)
edge_weighted=False, node_weighted=True, # node_weight_generator=generator, node_weights=node_weights, seed=seed) terminals = [200, 760, 763, 766, 499] suitability_graph = SuitabilityGraph() suitability_graph.append_graph(node_weighted) dr = DreyfusIMR(suitability_graph, terminals, contract_graph=False, within_convex_hull=False) start_time = time.clock() steiner_tree = dr.steiner_tree(consider_terminals=False) elapsed_time = time.clock() - start_time cost, node_cost = steiner_tree.compute_total_weights(terminals) ngh = NetworkXGraphHelper(suitability_graph) ngh.draw_graph(nodes_1=terminals, subgraphs_2=[steiner_tree], node_weight_generator=generator, title_1="Extended Dreyfus, seed = " + str(seed), title_2="Cost: " + str(cost) + ", elapsed time: " + str(elapsed_time), print_node_labels=True, node_size=15)
# 'g': (1.1, {'f': 2, 'b': 4, 'c': 1, 'i': 3}), # 'h': (1.1, {'d': 1, 'i': 2}), # 'i': (1.4, {'f': 2, 'g': 3, 'd': 1, 'h': 2})} # graph = { # 'a': (1, {'b': 1, 'd': 1}), # 'b': (1, {'a': 1, 'c': 1, 'e': 1}), # 'c': (1, {'b': 1, 'f': 1}), # 'd': (1, {'a': 1, 'e': 1, 'g': 1}), # 'e': (1, {'d': 1, 'b': 1, 'f': 1, 'h': 1}), # 'f': (1, {'c': 1, 'e': 1, 'i': 1}), # 'g': (1, {'d': 1, 'h': 1}), # 'h': (1, {'g': 1, 'e': 1, 'i': 1}), # 'i': (1, {'h': 1, 'f': 1}) # } seed = 1 generator = SuitableNodeWeightGenerator() gh = GridDigraphGenerator() weighted_graph = gh.generate(10, 10, node_weighted=True, node_weight_generator=generator, seed=seed) ngh = NetworkXGraphHelper(weighted_graph) ngh.draw_graph() subgraph = ngh.get_node_induced_subgraph([12, 13, 63, 84]) print(subgraph.edges())
# 'g': (1.1, {'f': 2, 'b': 4, 'c': 1, 'i': 3}), # 'h': (1.1, {'d': 1, 'i': 2}), # 'i': (1.4, {'f': 2, 'g': 3, 'd': 1, 'h': 2})} # # terminals = ['b', 'c', 'e', 'h', 'i'] seed = 6 gh = GridDigraphGenerator() generator = SuitableNodeWeightGenerator() node_weighted = gh.generate(30, 30, node_weighted=True, node_weight_generator=generator, seed=seed) terminals = [123, 230, 310, 464, 588, 625, 700] kr = KleinRavi(node_weighted, terminals) kr_st = kr.steiner_tree() kr_cost, node_cost = kr_st.compute_total_weights(terminals) ngh = NetworkXGraphHelper(node_weighted) ngh.draw_graph( special_nodes=[(terminals, "#0000FF", None)], title_1="Klein-Ravi's algorithm (node-weighted network graph), seed = " + str(seed), title_2="Cost: " + str(kr_cost) + ", Nodes: " + str(node_cost) + ", Edges: " + str(kr_cost - node_cost), print_node_labels=False)
node_weighted=True, node_weight_generator=generator, seed=seed) terminals = [678, 359, 123, 456, 463, 897] suitability_graph = SuitabilityGraph() suitability_graph.append_graph(node_weighted) suitability_graph.extend_suitable_regions(seed, generator) suitability_graph.extend_suitable_regions(seed, generator) suitability_graph.extend_suitable_regions(seed, generator) suitability_graph.extend_suitable_regions(seed, generator) regions = suitability_graph.get_suitable_regions(generator) cb = ClusterBasedV2(suitability_graph, terminals) start_time = time.clock() steiner_tree = cb.steiner_tree() elapsed_time = time.clock() - start_time ngh = NetworkXGraphHelper(suitability_graph) ngh.draw_graph( nodes_1=terminals, node_weight_generator=generator, subgraphs_1=[r for _, (r, _, _, _, _, _) in regions.iteritems()], subgraphs_2=[steiner_tree], title_2="elapsed time: " + str(elapsed_time), print_node_labels=True, node_size=15)