def frank_wolfe_on_I210(): ''' Frank-Wolfe on I210 ''' graph = np.loadtxt('data/I210_attack_net.csv', delimiter=',', skiprows=1) demand = np.loadtxt('data/I210_od.csv', delimiter=',', skiprows=1) demand[:,2] = 1. * demand[:,2] / 4000 # run solver f = solver_3(graph, demand, max_iter=1000, q=50, display=1, stop=1e-2) # display cost for a,b in zip(cost(f, graph), f*4000): print a,b # visualization node = np.loadtxt('data/I210_node.csv', delimiter=',', skiprows=1) # extract features: 'capacity', 'length', 'fftt' feat = extract_features('data/I210_attack_Sketch_net.csv') ratio = cost_ratio(f, graph) # merge features with the cost ratios features = np.zeros((feat.shape[0],4)) features[:,:3] = feat features[:,3] = ratio # join features with (lat1,lon1,lat2,lon2) links = process_links(graph, node, features) color = features[:,3] # we choose the costs names = ['capacity', 'length', 'fftt', 'tt_over_fftt'] geojson_link(links, names, color)
def load_network_data(name): #import pdb; pdb.set_trace() #The folder locations of all the input files graphLocation = 'data/' + name + '_net.csv' demandLocation = 'data/' + name + '_od.csv' nodeLocation = 'data/' + name + '_node.csv' featureLocation = 'data/' + name + '_net.txt' graph = np.loadtxt(graphLocation, delimiter=',', skiprows=1) demand = np.loadtxt(demandLocation, delimiter=',', skiprows=1) features = extract_features(featureLocation) import pdb pdb.set_trace() #LA network has a different way of processing the nodes file if (name == "LA"): node = np.loadtxt(nodeLocation, delimiter=',') features[10787, 0] = features[10787, 0] * 1.5 graph[10787, -1] = graph[10787, -1] / (1.5**4) features[3348, :] = features[3348, :] * 1.2 graph[3348, -1] = graph[3348, -1] / (1.2**4) else: node = np.loadtxt(nodeLocation, delimiter=',', skiprows=1) return graph, demand, node, features
def reduce_demand(): net, demand, node = load_LA() features = extract_features('data/LA_net.txt') f = np.loadtxt('data/LA/LA_output_3.csv', delimiter=',', skiprows=0) cr = cost_ratio(f, net) for row in range(net.shape[0]): if cr[row] >= 10.: out = [] for i in range(demand.shape[0]): if int(demand[i,0]) == int(net[row,1]): out.append(demand[i,2]) demand[i,2] = demand[i,2] / 10. if len(out) > 0: out = np.array(out) print '\nratio:', cr[row] print 'origin: {}\nflow: {}'.format(int(demand[i,0]), np.sum(out)) print np.sort(out).tolist() for row in range(net.shape[0]): if cr[row] >= 10.: out = [] for i in range(demand.shape[0]): if int(demand[i,1]) == int(net[row,2]): out.append(demand[i,2]) demand[i,2] = demand[i,2] / 10. if len(out) > 0: out = np.array(out) print '\nratio:', cr[row] print 'destination: {}\nflow: {}'.format(int(demand[i,0]), np.sum(out)) print np.sort(out).tolist()
def reduce_demand(): net, demand, node = load_LA() features = extract_features('data/LA_net.txt') f = np.loadtxt('data/LA/LA_output_3.csv', delimiter=',', skiprows=0) cr = cost_ratio(f, net) for row in range(net.shape[0]): if cr[row] >= 10.: out = [] for i in range(demand.shape[0]): if int(demand[i, 0]) == int(net[row, 1]): out.append(demand[i, 2]) demand[i, 2] = demand[i, 2] / 10. if len(out) > 0: out = np.array(out) print '\nratio:', cr[row] print 'origin: {}\nflow: {}'.format(int(demand[i, 0]), np.sum(out)) print np.sort(out).tolist() for row in range(net.shape[0]): if cr[row] >= 10.: out = [] for i in range(demand.shape[0]): if int(demand[i, 1]) == int(net[row, 2]): out.append(demand[i, 2]) demand[i, 2] = demand[i, 2] / 10. if len(out) > 0: out = np.array(out) print '\nratio:', cr[row] print 'destination: {}\nflow: {}'.format( int(demand[i, 0]), np.sum(out)) print np.sort(out).tolist()
def load_I210(): net = np.loadtxt('data/I210_attack_net.csv', delimiter=',', skiprows=1) demand = np.loadtxt('data/I210_od.csv', delimiter=',', skiprows=1) node = np.loadtxt('data/I210_node.csv', delimiter=',', skiprows=1) geometry = extract_features('data/I210_attack_Sketch_net.csv') demand = np.reshape(demand, (1, 3)) return net, demand, node, geometry
def frank_wolfe_on_I210(): ''' Frank-Wolfe on I210 ''' graph = np.loadtxt('data/I210_attack_net.csv', delimiter=',', skiprows=1) demand = np.loadtxt('data/I210_od.csv', delimiter=',', skiprows=1) demand[:, 2] = 1. * demand[:, 2] / 4000 # run solver f = solver_3(graph, demand, max_iter=1000, q=50, display=1, stop=1e-2) # display cost for a, b in zip(cost(f, graph), f * 4000): print a, b # visualization node = np.loadtxt('data/I210_node.csv', delimiter=',', skiprows=1) # extract features: 'capacity', 'length', 'fftt' feat = extract_features('data/I210_attack_Sketch_net.csv') ratio = cost_ratio(f, graph) # merge features with the cost ratios features = np.zeros((feat.shape[0], 4)) features[:, :3] = feat features[:, 3] = ratio # join features with (lat1,lon1,lat2,lon2) links = process_links(graph, node, features) color = features[:, 3] # we choose the costs names = ['capacity', 'length', 'fftt', 'tt_over_fftt'] geojson_link(links, names, color)
def load_LA_3(): # graph is an array of the network #It is a 28376 by 8 array, where the strucuture is [LINK id, node 1 (of link), node 2 (of link), a0,a1,a2,a3,a4 # where the ai are factors for the link cost functions graph = np.loadtxt('data/LA_net.csv', delimiter=',', skiprows=1) #demand is an array with num-OD by 3, where num-OD is the number of OD pairs #Entry in array is formatted as [origin, destination, demand] (still have to add the units of demands) demand = np.loadtxt('data/LA_od_3.csv', delimiter=',', skiprows=1) #node is an num-nod by 3 array, where num-node is the number of nodes #each entry has format [node id, ...] node = np.loadtxt('data/LA_node.csv', delimiter=',') # features = table in the format [[capacity, length, FreeFlowTime]] features = extract_features('data/LA_net.txt') # increase capacities of these two links because they have a travel time # in equilibrium that that is too big features[10787, 0] = features[10787, 0] * 1.5 graph[10787, -1] = graph[10787, -1] / (1.5**4) features[3348, :] = features[3348, :] * 1.2 graph[3348, -1] = graph[3348, -1] / (1.2**4) # divide demand going to node 106 by 10 because too large for i in range(demand.shape[0]): if demand[i, 1] == 106.: demand[i, 2] = demand[i, 2] / 10. return graph, demand, node, features
def load_I210(): net = np.loadtxt('data/I210_attack_net.csv', delimiter=',', skiprows=1) demand = np.loadtxt('data/I210_od.csv', delimiter=',', skiprows=1) node = np.loadtxt('data/I210_node.csv', delimiter=',', skiprows=1) geometry = extract_features('data/I210_attack_Sketch_net.csv') demand = np.reshape(demand, (1,3)) return net, demand, node, geometry
def load_LA_2(): #graph = np.loadtxt('data/LA_net.csv', delimiter=',', skiprows=1) #demand = np.loadtxt('data/LA_od_2.csv', delimiter=',', skiprows=1) #node = np.loadtxt('data/LA_node.csv', delimiter=',') #Changing everything to Chicago regional graph = np.loadtxt('data/ChicagoRegional_net.csv', delimiter=',', skiprows=1) demand = np.loadtxt('data/ChicagoRegional_od.csv', delimiter=',', skiprows=1) node = np.loadtxt('data/ChicagoRegional_node.csv', delimiter=',', skiprows=1) # features = table in the format [[capacity, length, FreeFlowTime]] #features = extract_features('data/LA_net.txt') features = extract_features('data/ChicagoRegional_net.txt') # increase capacities of these two links because they have a travel time # in equilibrium that that is too big #features[10787,0] = features[10787,0] * 1.5 #graph[10787,-1] = graph[10787,-1] / (1.5**4) #features[3348,:] = features[3348,:] * 1.2 #graph[3348,-1] = graph[3348,-1] / (1.2**4) return graph, demand, node, features
def visualize_result_ratio_study(fileName, ratio, name, mode): net, demand, node, features = load_network_data(name) #Loading the flow per link resulting from frank-wolfe f = np.loadtxt(fileName, delimiter=',', skiprows=0) #Location of the features featureLocation = 'data/' + name + '_net.txt' features = np.zeros((f.shape[0], 4)) features[:, :3] = extract_features(featureLocation) #Multiply the flow obtained by 4000 since we initially divided by 4000 before frank-wolfs f = np.divide(f * 4000, features[:, 0]) features[:, 3] = f links = process_links(net, node, features, in_order=True) #creates color array used to visulized the links #values useful in differenciating links based of flow on links color = 2.0 * f + 1.0 #Keeping track of the percentage of congestion links_congested = len(color[np.where(color >= 3)]) percentage_of_congestion = float(links_congested) / float(len(color)) print("congestion is at %3f " % percentage_of_congestion) geojson_link_Scenario_Study( ratio, links, ['capacity', 'length', 'fftt', 'flow_over_capacity'], color, name, mode)
def load_I210_modified(): net = np.loadtxt('data/I210_net.csv', delimiter=',', skiprows=1) demand = np.loadtxt('data/I210_od.csv', delimiter=',', skiprows=1) node = np.loadtxt('data/I210_node.csv', delimiter=',', skiprows=1) geometry = extract_features('data/I210Sketch_net.csv') demand = np.reshape(demand, (1, 3)) # modify link 21 net[21, -1] = net[21, -1] * (4**4) geometry[21, 0] = geometry[21, 0] / 4. return net, demand, node, geometry
def visualize_LA_capacity(): graph, demand, node = load_LA() features = extract_features('data/LA_net.txt') links = process_links(graph, node, features, in_order=True) color = features[:, 0] # we choose to color by the capacities names = ['capacity', 'length', 'fftt'] # color = 2.1 * features[:,0] / 2000. color = 2. * (features[:, 0] <= 900.) + 5. * (features[:, 0] > 900.) weight = (features[:, 0] <= 900.) + 3. * (features[:, 0] > 900.) geojson_link(links, names, color, weight)
def load_I210_modified(): net = np.loadtxt('data/I210_net.csv', delimiter=',', skiprows=1) demand = np.loadtxt('data/I210_od.csv', delimiter=',', skiprows=1) node = np.loadtxt('data/I210_node.csv', delimiter=',', skiprows=1) geometry = extract_features('data/I210Sketch_net.csv') demand = np.reshape(demand, (1,3)) # modify link 21 net[21,-1] = net[21,-1] * (4**4) geometry[21,0] = geometry[21,0] / 4. return net, demand, node, geometry
def visualize_LA_result(): net, demand, node = load_LA() f = np.loadtxt('data/LA_output.csv', delimiter=',', skiprows=0) features = np.zeros((f.shape[0], 4)) features[:,:3] = extract_features('data/LA_net.txt') f = np.divide(f, features[:,0]) features[:,3] = f links = process_links(net, node, features, in_order=True) color = 2.0*f + 1.0 geojson_link(links, ['capacity', 'length', 'fftt', 'flow_over_capacity'], color)
def visualize_LA_capacity(): graph, demand, node = load_LA() features = extract_features('data/LA_net.txt') links = process_links(graph, node, features, in_order=True) color = features[:,0] # we choose to color by the capacities names = ['capacity', 'length', 'fftt'] # color = 2.1 * features[:,0] / 2000. color = 2.*(features[:,0] <= 900.) + 5.*(features[:,0] > 900.) weight = (features[:,0] <= 900.) + 3.*(features[:,0] > 900.) geojson_link(links, names, color, weight)
def visualize_LA_result(): net, demand, node = load_LA() f = np.loadtxt('data/LA_output.csv', delimiter=',', skiprows=0) features = np.zeros((f.shape[0], 4)) features[:, :3] = extract_features('data/LA_net.txt') f = np.divide(f, features[:, 0]) features[:, 3] = f links = process_links(net, node, features, in_order=True) color = 2.0 * f + 1.0 geojson_link(links, ['capacity', 'length', 'fftt', 'flow_over_capacity'], color)
def load_LA_2(): graph = np.loadtxt('data/LA_net.csv', delimiter=',', skiprows=1) demand = np.loadtxt('data/LA_od_2.csv', delimiter=',', skiprows=1) node = np.loadtxt('data/LA_node.csv', delimiter=',') # features = table in the format [[capacity, length, FreeFlowTime]] features = extract_features('data/LA_net.txt') # increase capacities of these two links because they have a travel time # in equilibrium that that is too big features[10787,0] = features[10787,0] * 1.5 graph[10787,-1] = graph[10787,-1] / (1.5**4) features[3348,:] = features[3348,:] * 1.2 graph[3348,-1] = graph[3348,-1] / (1.2**4) return graph, demand, node, features
def visualize_LA(): net, demand, node = load_LA() f = np.loadtxt('data/la/LA_Cython.csv', delimiter=',', skiprows=0) features = np.zeros((f.shape[0], 4)) features[:, :3] = extract_features('data/LA_net.txt') #import pdb; pdb.set_trace() f = np.divide(f * 4000, features[:, 0]) features[:, 3] = f links = process_links(net, node, features, in_order=True) #creates color array used to visulized the links #values useful in differenciating links based of flow on links color = 2.0 * f + 1.0 #congestion = f/features[:,0] #determines the congestion levels of links geojson_link(links, ['capacity', 'length', 'fftt', 'flow_over_capacity'], color)
def LAfix_heterogeneous(demand_r, capacity): #demand_nr=1-demand_r '''generate heterogeneous game on LAfix network demand_r is the ratio of routed users capacity is the max capacity for which a road is considered low-capacity''' g2 = np.loadtxt('data/LAfix_net.csv', delimiter=',', skiprows=1) g1 = np.copy(g2) features = extract_features('data/LAfix_net.txt') for i in range(0, len(features)): if features[i][0] <= capacity: g1[i, 3:7] *= 3000 d1 = np.loadtxt('data/LA_od.csv', delimiter=',', skiprows=1) d2 = np.copy(d1) d1[:, 2] *= (1 - demand_r) d2[:, 2] *= demand_r return g1, g2, d1, d2
def I210_ratio_r_total(): ''' study the test_*.csv files generated by I210_parametric_study() in particular, visualize the ratio each type of users on each link ''' fs = np.loadtxt('data/test_50.csv', delimiter=',', skiprows=0) ratio = np.divide(fs[:,1], np.maximum(np.sum(fs, axis=1), 1e-8)) net = np.loadtxt('data/I210_net.csv', delimiter=',', skiprows=1) node = np.loadtxt('data/I210_node.csv', delimiter=',', skiprows=1) geometry = extract_features('data/I210Sketch_net.csv') features = np.zeros((fs.shape[0], 4)) features[:,:3] = geometry features[:,3] = ratio links = process_links(net, node, features) color = 2 * ratio # we choose the ratios of nr over r+nr geojson_link(links, ['capacity', 'length', 'fftt', 'r_routed'], color)
def I210_ratio_r_total(): ''' study the test_*.csv files generated by I210_parametric_study() in particular, visualize the ratio each type of users on each link ''' fs = np.loadtxt('data/test_50.csv', delimiter=',', skiprows=0) ratio = np.divide(fs[:, 1], np.maximum(np.sum(fs, axis=1), 1e-8)) net = np.loadtxt('data/I210_net.csv', delimiter=',', skiprows=1) node = np.loadtxt('data/I210_node.csv', delimiter=',', skiprows=1) geometry = extract_features('data/I210Sketch_net.csv') features = np.zeros((fs.shape[0], 4)) features[:, :3] = geometry features[:, 3] = ratio links = process_links(net, node, features) color = 2 * ratio # we choose the ratios of nr over r+nr geojson_link(links, ['capacity', 'length', 'fftt', 'r_routed'], color)
def main(): for alpha in [.75]: # for alpha in np.linspace(0, .49, 50): # for alpha in np.linspace(.5, .99, 50): print "ALPHA:", alpha start_time2 = timeit.default_timer() graph = np.loadtxt('data/LA_net.csv', delimiter=',', skiprows=1) demand = np.loadtxt('data/LA_od_2.csv', delimiter=',', skiprows=1) graph[10787, -1] = graph[10787, -1] / (1.5**4) graph[3348, -1] = graph[3348, -1] / (1.2**4) node = np.loadtxt('data/LA_node.csv', delimiter=',') features = extract_features('data/LA_net.txt') # graph = np.loadtxt('data/Chicago_net.csv', delimiter=',', skiprows=1) # demand = np.loadtxt('data/Chicago_od.csv', delimiter=',', skiprows=1) # node = np.loadtxt('data/Chicago_node.csv', delimiter=',', skiprows=1) # features = extract_features('data/ChicagoSketch_net.txt') # features = table in the format [[capacity, length, FreeFlowTime]] # alpha = .2 # also known as r thres = 1000. cog_cost = 3000. demand[:, 2] = 0.5 * demand[:, 2] / 4000 g_nr, small_capacity = multiply_cognitive_cost(graph, features, thres, cog_cost) d_nr, d_r = heterogeneous_demand(demand, alpha) fs, hs, n_d = fw_heterogeneous_1([graph, g_nr], [d_r, d_nr], alpha, max_iter=30, display=1) print n_d output = {'f': fs, 'h': hs, 'n_d': n_d} with open('graph_stuff/LA_net_od_2_alpha_{}.txt'.format(alpha), 'w') as outfile: # with open('graph_stuff/Chicago_net_od_2_alpha_{}.txt'.format(alpha), 'w') as outfile: outfile.write(pickle.dumps(output)) #end of timer elapsed2 = timeit.default_timer() - start_time2 print("Execution took %s seconds" % elapsed2)
def load_LA_3(): graph = np.loadtxt('data/LA_net.csv', delimiter=',', skiprows=1) demand = np.loadtxt('data/LA_od_3.csv', delimiter=',', skiprows=1) node = np.loadtxt('data/LA_node.csv', delimiter=',') # features = table in the format [[capacity, length, FreeFlowTime]] features = extract_features('data/LA_net.txt') # increase capacities of these two links because they have a travel time # in equilibrium that that is too big features[10787, 0] = features[10787, 0] * 1.5 graph[10787, -1] = graph[10787, -1] / (1.5**4) features[3348, :] = features[3348, :] * 1.2 graph[3348, -1] = graph[3348, -1] / (1.2**4) # divide demand going to node 106 by 10 because too large for i in range(demand.shape[0]): if demand[i, 1] == 106.: demand[i, 2] = demand[i, 2] / 10. return graph, demand, node, features
def load_LA_3(): graph = np.loadtxt('data/LA_net.csv', delimiter=',', skiprows=1) demand = np.loadtxt('data/LA_od_3.csv', delimiter=',', skiprows=1) node = np.loadtxt('data/LA_node.csv', delimiter=',') # features = table in the format [[capacity, length, FreeFlowTime]] features = extract_features('data/LA_net.txt') # increase capacities of these two links because they have a travel time # in equilibrium that that is too big features[10787,0] = features[10787,0] * 1.5 graph[10787,-1] = graph[10787,-1] / (1.5**4) features[3348,:] = features[3348,:] * 1.2 graph[3348,-1] = graph[3348,-1] / (1.2**4) # divide demand going to node 106 by 10 because too large for i in range(demand.shape[0]): if demand[i,1] == 106.: demand[i,2] = demand[i,2] / 10. return graph, demand, node, features
def visualize_LA_result_Scenario_Study(fileName, ratio): net, demand, node = load_LA() f = np.loadtxt(fileName, delimiter=',', skiprows=0) features = np.zeros((f.shape[0], 4)) features[:, :3] = extract_features('data/LA_net.txt') #features[:,:3] = extract_features('data/ChicagoRegional_net.txt') f = np.divide(f * 4000, features[:, 0]) features[:, 3] = f links = process_links(net, node, features, in_order=True) #creates color array used to visulized the links #values useful in differenciating links based of flow on links color = 2.0 * f + 1.0 #Keeping track of the percentage of congestion links_congested = len(color[np.where(color >= 3)]) percentage_of_congestion = float(links_congested) / float(len(color)) print("congestion is at %3f " % percentage_of_congestion) geojson_link_Scenario_Study( ratio, links, ['capacity', 'length', 'fftt', 'flow_over_capacity'], color)
spnd[i] = nodes[i] np.savetxt('data/iod_node.csv', spnd, delimiter=',', header='node,lat,lon', comments='') #Node ID is equal to node index: the file is sparse np.savetxt('data/iod_node_dense.csv', ondes, delimiter=',', header='node,lat,lon', comments='') #The file is densified and there are no blank lines nodes = [el[0] for el in ondes] #Select Links links = np.loadtxt('data/LA_net.csv', delimiter=',', skiprows=1) features = extract_features('data/LA_net.txt') lniks = [] faetures = [] for i in range(0, len(links)): if links[i][1] in nodes and links[i][ 2] in nodes: #Check if both nodes of the link are in the zone lniks.append(links[i]) arr = np.concatenate((np.array([0, 0]), features[i])) faetures.append(arr) np.savetxt('data/iod_net.csv', lniks, delimiter=',', header='LINK,O,D,a0,a1,a2,a3,a4', comments='') np.savetxt('data/iod_net.txt', faetures,
def load_chicago(): net = np.loadtxt('data/Chicago_net.csv', delimiter=',', skiprows=1) demand = np.loadtxt('data/Chicago_od.csv', delimiter=',', skiprows=1) node = np.loadtxt('data/Chicago_node.csv', delimiter=',', skiprows=1) geometry = extract_features('data/ChicagoSketch_net.txt') return net, demand, node, geometry
demand = np.loadtxt('data/Chicago_od.csv', delimiter=',', skiprows=1) demand[:, 2] = 0.5 * demand[:, 2] / 4000 od = construct_od(demand) # make link to lat/lng coordinate json file if make_link2coord: with open( '{}/{}_net_od_2_alpha_0.{:02d}.txt'.format(dash_data, prefix, 50), 'r') as infile: run = pickle.loads(infile.read()) fs = run['f'] f = np.array([l[0] + l[1] for l in fs]) if net == 'la': node = np.loadtxt('data/LA_node.csv', delimiter=',') features = np.zeros((f.shape[0], 4)) features[:, :3] = extract_features('data/LA_net.txt') elif net == 'chicago': node = np.loadtxt('data/Chicago_node.csv', delimiter=',', skiprows=1) features = np.zeros((f.shape[0], 4)) features[:, :3] = extract_features('data/ChicagoSketch_net.txt') f = np.divide(f * 4000, features[:, 0]) features[:, 3] = f links = process_links(graph, node, features, in_order=True) link2coord = {} for i in range(len(links)): link2coord[i] = { 'origin': [links[i, 0], links[i, 1]], 'destination': [links[i, 2], links[i, 3]], } with open('{}/link_coord.json'.format(out_data), 'w') as f:
def load_chicago(): net = np.loadtxt("data/Chicago_net.csv", delimiter=",", skiprows=1) demand = np.loadtxt("data/Chicago_od.csv", delimiter=",", skiprows=1) node = np.loadtxt("data/Chicago_node.csv", delimiter=",", skiprows=1) geometry = extract_features("data/ChicagoSketch_net.txt") return net, demand, node, geometry