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]]))
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
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),
# 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) dist_paths_node_node = {n: dijkstra(suitability_graph, n) for n in suitable_nodes} for e in terminals_poi: dist_paths_node_node[e] = dijkstra(suitability_graph, e)
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)
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 suitability import SuitabilityGraph, SuitableNodeWeightGenerator from networkx_graph_helper import NetworkXGraphHelper from dreyfus_imr_v2 import DreyfusIMRV2 if __name__ == '__main__': generator = SuitableNodeWeightGenerator() seed = 5 m = n = 30 # m = n = 10 gh = GridDigraphGenerator() node_weighted = gh.generate(m, n, edge_weighted=True, node_weighted=True, node_weight_generator=generator, seed=seed) terminals = [288, 315, 231, 312, 111, 609, 645, 434, 654, 469, 186] # terminals = [36, 78, 28, 11] 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()
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)
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)
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:] mz = VST_RS(graph) start_time = time.clock() forest, cost, _, _, _, _, sts, _ = mz.steiner_forest(terminals, pois, 5, 8) elapsed_time = time.clock() - start_time
from grid_digraph_generator import GridDigraphGenerator if __name__ == '__main__': gh = GridDigraphGenerator() graph = gh.generate(5, 5, capacitated=True) print graph.get_capacities()
import sys import time from baltz_based import BaltzBased from vst_rs import VST_RS from grid_digraph_generator import GridDigraphGenerator from networkx_graph_helper import NetworkXGraphHelper if __name__ == '__main__': m = n = 10 gh = GridDigraphGenerator() graph = gh.generate(m, n) b = BaltzBased(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 = b.steiner_forest(requests) et = time.clock() - st print et special_subgraphs = [(MST, None) for _, (MST, cost) in MSTs.iteritems()]
import time import numpy as np from grid_digraph_generator import GridDigraphGenerator from networkx_graph_helper import NetworkXGraphHelper from vst_rs import VST_RS from link_performance import bpr if __name__ == '__main__': m = n = 20 gh = GridDigraphGenerator() graph = gh.generate(m, n, edge_weighted=False, capacitated=True, capacities_range=(1, 10)) queries = [] np.random.seed(0) terminals = np.random.choice(a=m * n, size=15, replace=False) pois_1 = terminals[:2] terminals_1 = terminals[2:] queries.append((terminals_1, pois_1)) occupied = list(terminals_1) occupied.extend(pois_1) np.random.seed(1) where = set(range(m * n)).difference(occupied) terminals = np.random.choice(a=list(where), size=15, replace=False) pois_2 = terminals[:2]
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 # are reset and any node in the graph becomes a suitable node (except terminals and POIs). # Reset suitable nodes. hotspots = suitability_graph.get_suitable_nodes(generator)
# 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] terminals = [23, 17, 65] # terminals = [703, 858, 668, 171, 628, 886, 240, 383, 268, 686] # terminals = [3381, 2580, 2655, 3622, 2161, 5247, 5073, 871, 4946, 1017] # terminals = [23, 45, 56, 289, 365]
# '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())
'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) print stats
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()
generator = SuitableNodeWeightGenerator() seed = 0 m = n = 30 node_weights = [generator.weights['WARNING'][0] for _ in range(m * n)] node_weights[644] = generator.weights['VERY_SUITABLE'][0] # node_weights[401] = generator.weights['VERY_SUITABLE'][0] node_weights[405] = generator.weights['VERY_SUITABLE'][0] gh = GridDigraphGenerator() node_weighted = gh.generate( m, n, 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)
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)
# '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) 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) +
def setUp(self): generator = GridDigraphGenerator() self.graph = generator.generate(5, 5, edge_weighted=False)