'avg detour': (total_detour / no_ad_hoc) if no_ad_hoc > 0 else 0, 'w avg detour': weighted_avg_detour, }, 'dedicated': { 'no': no_dedicated, 'total': total_dedicated, 'avg': (total_dedicated / no_dedicated) if no_dedicated > 0 else 0, } } 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)
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]]))
# node_weights[26] = generator.weights['VERY_SUITABLE'][0] # node_weights[721] = generator.weights['VERY_SUITABLE'][0] # node_weights[771] = generator.weights['VERY_SUITABLE'][0] # node_weights[722] = generator.weights['VERY_SUITABLE'][0] # node_weights[772] = generator.weights['VERY_SUITABLE'][0] # node_weights[2333] = generator.weights['VERY_SUITABLE'][0] # node_weights[2383] = generator.weights['VERY_SUITABLE'][0] # node_weights[1573] = generator.weights['VERY_SUITABLE'][0] # node_weights[1574] = generator.weights['VERY_SUITABLE'][0] # node_weights[1623] = generator.weights['VERY_SUITABLE'][0] # node_weights[1624] = generator.weights['VERY_SUITABLE'][0] # node_weights[1120] = generator.weights['VERY_SUITABLE'][0] # node_weights[931] = generator.weights['VERY_SUITABLE'][0] # node_weights[930] = generator.weights['VERY_SUITABLE'][0] gh = GridDigraphGenerator() graph = gh.generate(m, n, edge_weighted=True, edge_weights_range=(1, 2), node_weighted=True, node_weight_generator=generator, node_weights=node_weights, seed=seed) # terminals = [1226, 1265, 1134, 1482, 1721, 677, 1567, 814, 1879, 635, 838, 2077, 2227, 911] # poi = 1226 # terminals = [1222, 470, 388, 899, 1185, 750, 739, 487, 850, 1299] # poi = 1505 # terminals = [23, 45, 56, 289, 365]
import time from grid_digraph_generator import GridDigraphGenerator from networkx_graph_helper import NetworkXGraphHelper from bpr_based import BPRBased if __name__ == '__main__': m = n = 10 gg = GridDigraphGenerator() graph = gg.generate(m, n, capacitated=True, capacities_range=(1, 2)) bpr = BPRBased(graph) 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),
def main(argv): p_method = "pp" try: opts, args = getopt.getopt(argv, "hm:") except getopt.GetoptError as error: print(error) print_usage() sys.exit(2) for opt, arg in opts: if opt == "-h": print_usage() sys.exit(0) elif opt == "-m": p_method = arg break comm = None rank = MASTER_RANK if p_method == "mpi": comm = MPI.COMM_WORLD rank = comm.Get_rank() if rank != MASTER_RANK: while True: res = comm.recv(source=MASTER_RANK) print res num_samples = 5 num_queries = [16, 32] num_users_query = [16] prop_pois_users = 0.1 m = n = 30 N = m * n graph = GridDigraphGenerator().generate(m, n, edge_weighted=True) merge_users = False max_iter = 50 alpha = 1.0 beta = 4.0 results = [] for nq in num_queries: for nu in num_users_query: num_pois = max(int(prop_pois_users * nu), 1) graph.capacitated = True capacity = int(math.ceil((nu / 4.0 * nq) / 12.0)) graph.set_capacities({e: capacity for e in graph.get_edges()}) print "(nq, nu, np, cap):", (nq, nu, num_pois, capacity) for sample in range(num_samples): print "\tsample:", sample ppq = distribute_pois_in_queries((m, n), nq, num_pois, seed=0) queries_u = [] queries_z = [] # all_pois = [] for ps in ppq.values(): all_pois.extend(ps) free_nodes = set(range(m * n)).difference(all_pois) # occupied_t = set() occupied_p = set() for i, pois_z in ppq.iteritems(): np.random.seed(sample * i) # where_t = set(free_nodes).difference(occupied_t) terminals = np.random.choice(a=list(where_t), size=nu, replace=False) queries_z.append((terminals, pois_z)) occupied_t.update(terminals) occupied_p.update(terminals) # where_p = set(range(m * n)).difference(occupied_p) pois_u = np.random.choice(a=list(where_p), size=num_pois, replace=False) queries_u.append((terminals, pois_u)) occupied_p.update(pois_u) # # VST-NCA ********************************************************************************************** # POIs Zipfian distributed. vst_rs = VST_RS(graph) st = time.clock() _, c, warl, mwrl, mrl1, mrl2, entropy = \ vst_rs.non_congestion_aware(queries_z, 4, 8, bpr, merge_users=merge_users, alpha=alpha, beta=beta, p_method=p_method, verbose=False) et = time.clock() - st line = ["VST-NCA", "N/A", "zipfian", N, capacity, merge_users, sample, nq, nu, prop_pois_users, num_pois, c, warl, mwrl, mrl1, mrl2, 0, et, alpha, beta, entropy] print line results.append(line) # POIs Uniformly distributed. vst_rs = VST_RS(graph) st = time.clock() _, c, warl, mwrl, mrl1, mrl2, entropy = \ vst_rs.non_congestion_aware(queries_u, 4, 8, bpr, merge_users=merge_users, alpha=alpha, beta=beta, p_method=p_method, verbose=False) et = time.clock() - st line = ["VST-NCA", "N/A", "uniform", N, capacity, merge_users, sample, nq, nu, prop_pois_users, num_pois, c, warl, mwrl, mrl1, mrl2, 0, et, alpha, beta, entropy] print line results.append(line) # VST-NCA ********************************************************************************************** # VST-CA *********************************************************************************************** # MIXED # POIs Zipfian distributed. vst_rs = VST_RS(graph) st = time.clock() _, c, warl, mwrl, mrl1, mrl2, entropy, ni = \ vst_rs.congestion_aware(queries_z, 4, 8, bpr, merge_users=merge_users, max_iter=max_iter, alpha=alpha, beta=beta, verbose=False, randomize=True, p_method=p_method) et = time.clock() - st ni_ = str(ni) if ni == max_iter: ni_ += "(*)" line = ["VST-CA", "mixed", "zipfian", N, capacity, merge_users, sample, nq, nu, prop_pois_users, num_pois, c, warl, mwrl, mrl1, mrl2, ni_, et, alpha, beta, entropy] print line results.append(line) # POIs Uniformly distributed. vst_rs = VST_RS(graph) st = time.clock() _, c, warl, mwrl, mrl1, mrl2, entropy, ni = \ vst_rs.congestion_aware(queries_u, 4, 8, bpr, merge_users=merge_users, max_iter=max_iter, alpha=alpha, beta=beta, verbose=False, randomize=True, p_method=p_method) et = time.clock() - st ni_ = str(ni) if ni == max_iter: ni_ += "(*)" line = ["VST-CA", "mixed", "uniform", N, capacity, merge_users, sample, nq, nu, prop_pois_users, num_pois, c, warl, mwrl, mrl1, mrl2, ni_, et, alpha, beta, entropy] print line results.append(line) # PURE # POIs Zipfian distributed. vst_rs = VST_RS(graph) st = time.clock() _, c, warl, mwrl, mrl1, mrl2, entropy, ni = \ vst_rs.congestion_aware(queries_z, 4, 8, bpr, merge_users=merge_users, max_iter=max_iter, alpha=alpha, beta=beta, verbose=False, randomize=False, p_method=p_method) et = time.clock() - st ni_ = str(ni) if ni == max_iter: ni_ += "(*)" line = ["VST-CA", "pure", "zipfian", N, capacity, merge_users, sample, nq, nu, prop_pois_users, num_pois, c, warl, mwrl, mrl1, mrl2, ni_, et, alpha, beta, entropy] print line results.append(line) # POIs Uniformly distributed. vst_rs = VST_RS(graph) st = time.clock() _, c, warl, mwrl, mrl1, mrl2, entropy, ni = \ vst_rs.congestion_aware(queries_u, 4, 8, bpr, merge_users=merge_users, max_iter=max_iter, alpha=alpha, beta=beta, verbose=False, randomize=False, p_method=p_method) et = time.clock() - st ni_ = str(ni) if ni == max_iter: ni_ += "(*)" line = ["VST-CA", "pure", "uniform", N, capacity, merge_users, sample, nq, nu, prop_pois_users, num_pois, c, warl, mwrl, mrl1, mrl2, ni_, et, alpha, beta, entropy] print line results.append(line) # VST-CA *********************************************************************************************** result_file = open("files/vstca_vstnca_2_" + time.strftime("%d%b%Y_%H%M%S") + ".csv", 'wb') wr = csv.writer(result_file) wr.writerows(results)
num_seeds = 1 num_samples = 3 sizes = [90] nums_terminals = [7] generator = SuitableNodeWeightGenerator() results = [] try: for seed in range(num_seeds): for size in sizes: graph = GridDigraphGenerator().generate( size, size, node_weighted=True, node_weight_generator=generator, seed=seed) suitability_graph = SuitabilityGraph() suitability_graph.append_graph(graph) total_num_suitable_nodes = len( suitability_graph.get_suitable_nodes(generator)) for num_terminals in nums_terminals: for sample in range(num_samples): line = [ seed, size * size, total_num_suitable_nodes,
# node_weights[720] = generator.weights['VERY_SUITABLE'][0] # node_weights[721] = generator.weights['VERY_SUITABLE'][0] # node_weights[771] = generator.weights['VERY_SUITABLE'][0] # node_weights[722] = generator.weights['VERY_SUITABLE'][0] # node_weights[772] = generator.weights['VERY_SUITABLE'][0] # node_weights[2333] = generator.weights['VERY_SUITABLE'][0] # node_weights[2383] = generator.weights['VERY_SUITABLE'][0] # node_weights[1573] = generator.weights['VERY_SUITABLE'][0] # node_weights[1574] = generator.weights['VERY_SUITABLE'][0] # node_weights[1623] = generator.weights['VERY_SUITABLE'][0] # node_weights[1624] = generator.weights['VERY_SUITABLE'][0] # node_weights[1120] = generator.weights['VERY_SUITABLE'][0] # node_weights[931] = generator.weights['VERY_SUITABLE'][0] # node_weights[930] = generator.weights['VERY_SUITABLE'][0] gh = GridDigraphGenerator() graph = gh.generate(20, 20, edge_weighted=False, node_weighted=True, node_weight_generator=generator, node_weights=node_weights, seed=seed) suitability_graph = SuitabilityGraph() suitability_graph.append_graph(graph) terminals = [82, 182] poi = 173 terminals_poi = list(terminals) terminals_poi.append(poi)
import time import numpy as np from grid_digraph_generator import GridDigraphGenerator from vst_rs import VST_RS from link_performance import bpr if __name__ == '__main__': m = n = 50 gh = GridDigraphGenerator() graph = gh.generate(m, n, edge_weighted=True, capacitated=True, capacities_range=(30, 40)) no_queries = 100 np.random.seed(0) no_users = np.random.choice(a=range(2, 8), size=no_queries, replace=True) no_pois = np.random.choice(a=range(1, 5), size=no_queries, replace=True) queries = [] occupied = [] for i in range(no_queries): np.random.seed(i) where = set(range(m * n)).difference(occupied) nodes = np.random.choice(a=list(where), size=no_users[i] + no_pois[i], replace=False)
# nums_pois = [5, 6, 7, 8, 9, 10] nums_pois = [80] # num_extensions = 1 capacity = [4] results = [] generator = SuitableNodeWeightGenerator() # try: for seed in seeds: print("seed:", seed) for msns in range(len(ms)): print("nodes:", ms[msns] * ns[msns]) graph = GridDigraphGenerator().generate( ms[msns], ns[msns], node_weighted=True, node_weight_generator=generator, seed=seed) suitability_graph = SuitabilityGraph() suitability_graph.append_graph(graph) hotspots = suitability_graph.get_suitable_nodes(generator) start_time = time.clock() suitability_graph.compute_dist_paths(origins=hotspots, destinations=hotspots, compute_paths=False) print "compute", time.clock() - start_time, "# hotspots:", len( hotspots) for num_seats in capacity: # suitability_graph.extend_suitable_regions(seed, generator)
# '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())
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()
from grid_digraph_generator import GridDigraphGenerator if __name__ == '__main__': gh = GridDigraphGenerator() graph = gh.generate(5, 5, capacitated=True) print graph.get_capacities()
import numpy as np from grid_digraph_generator import GridDigraphGenerator from suitability import SuitableNodeWeightGenerator from networkx_graph_helper import NetworkXGraphHelper from vst_rs import VST_RS if __name__ == '__main__': generator = SuitableNodeWeightGenerator() seed = 15 m = n = 10 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:]
from grid_digraph_generator import GridDigraphGenerator from vst_rs import VST_RS from networkx_graph_helper import NetworkXGraphHelper from suitability import SuitabilityGraph, SuitableNodeWeightGenerator if __name__ == '__main__': generator = SuitableNodeWeightGenerator() seed = 1 height = width = 30 padding = 5 # Generate graph gh = GridDigraphGenerator() graph = gh.generate(height, width, edge_weighted=False, node_weighted=True, node_weight_generator=generator, seed=seed) # Transform graph into a suitability-graph because algorithm needs to know which the suitable nodes are. suitability_graph = SuitabilityGraph() suitability_graph.append_graph(graph) # # Compute shortest distances between every pair of nodes. # suitability_graph.compute_dist_paths(compute_paths=False) # As our goal is to show how Mustafizur's algorithm works, terminals can meet anywhere, thus original suitable nodes
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)
from grid_digraph_generator import GridDigraphGenerator from networkx_graph_helper import NetworkXGraphHelper from vst_rs import VST_RS from link_performance import bpr from utils import distribute_pois_in_queries if __name__ == '__main__': m = n = 10 capacities = (2, 4) nq = 2 npq = 1 nuq = 6 gh = GridDigraphGenerator() graph = GridDigraphGenerator().generate(m, n, edge_weighted=False, capacitated=True, capacities_range=capacities) ppq = distribute_pois_in_queries((m, n), nq, npq, seed=2) # queries_u = [] queries_z = [] # all_pois = [] for ps in ppq.values(): all_pois.extend(ps) free_nodes = set(range(m * n)).difference(all_pois) #
from suitability import SuitableNodeWeightGenerator, SuitabilityGraph from networkx_graph_helper import NetworkXGraphHelper from graph import dijkstra from dreyfus_imr import DreyfusIMR if __name__ == '__main__': generator = SuitableNodeWeightGenerator() seed = 0 m = n = 40 # node_weights = [generator.weights['WARNING'][0] for _ in range(m * n)] # node_weights[1221] = generator.weights['VERY_SUITABLE'][0] gh = GridDigraphGenerator() node_weighted = gh.generate( m, n, edge_weighted=True, node_weighted=True, node_weight_generator=generator, # node_weights=node_weights, seed=seed) terminals = [742, 870, 776, 578] suitability_graph = SuitabilityGraph() suitability_graph.append_graph(node_weighted)
import time from baltz import Baltz from grid_digraph_generator import GridDigraphGenerator from networkx_graph_helper import NetworkXGraphHelper from suitability import SuitabilityGraph, SuitableNodeWeightGenerator if __name__ == '__main__': generator = SuitableNodeWeightGenerator() m = n = 10 gh = GridDigraphGenerator() graph = gh.generate(m, n, edge_weighted=False, node_weighted=True, node_weight_generator=generator) suitability_graph = SuitabilityGraph() suitability_graph.append_graph(graph) 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
if __name__ == '__main__': # graph = {'a': (1.2, {'b': 4, 'd': 3}), # 'b': (1.4, {'a': 4, 'd': 5, 'e': 3, 'f': 3, 'g': 4, 'c': 2}), # 'c': (1.1, {'b': 2, 'g': 1}), # 'd': (1.7, {'a': 3, 'b': 5, 'e': 1, 'f': 2, 'i': 1, 'h': 1}), # 'e': (1.2, {'d': 1, 'b': 3, 'f': 1}), # 'f': (1.8, {'e': 1, 'd': 2, 'b': 3, 'g': 2, 'i': 2}), # '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)
def setUp(self): generator = GridDigraphGenerator() self.graph = generator.generate(5, 5, edge_weighted=False)