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 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 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 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 check_LA_result(): net, demand, node, features = load_LA_2() demand[:, 2] = demand[:, 2] / 4000. f = np.loadtxt('data/LA/LA_output_4.csv', delimiter=',', skiprows=0) costs = cost(f, net) cr = cost_ratio(f, net) print np.sort(cr)[-20:] for row in range(net.shape[0]): if cr[row] >= 10.: print cr[row] print net[row, :3], features[row, :] L = all_or_nothing_assignment(costs, net, demand) print costs.dot(L) / np.sum(demand[:, 2])
def check_LA_result(): net, demand, node, features = load_LA_2() demand[:,2] = demand[:,2] / 4000. f = np.loadtxt('data/LA/LA_output_4.csv', delimiter=',', skiprows=0) costs = cost(f, net) cr = cost_ratio(f, net) print np.sort(cr)[-20:] for row in range(net.shape[0]): if cr[row] >= 10.: print cr[row] print net[row,:3], features[row,:] L = all_or_nothing_assignment(costs, net, demand) print costs.dot(L) / np.sum(demand[:,2])
def visualize_equilibrium_in_chicago(): """ visualize costs in the network of Chicago """ net, demand, node, f = load_chicago() flow = np.loadtxt("data/Chicago_results_2.csv", delimiter=",") # flow = np.loadtxt('data/Chicago_results.csv', delimiter=',', skiprows=1)[:,2] print average_cost(flow / 4000.0, net) costs = cost_ratio(flow / 4000.0, net) features = np.zeros((f.shape[0], 4)) features[:, :3] = f features[:, 3] = costs links = process_links(net, node, features) color = features[:, 3] - 1.0 # we choose the costs geojson_link(links, ["capacity", "length", "fftt", "tt_over_fftt"], color)
def visualize_equilibrium_in_chicago(): ''' visualize costs in the network of Chicago ''' net, demand, node, f = load_chicago() flow = np.loadtxt('data/Chicago_results_2.csv', delimiter=',') # flow = np.loadtxt('data/Chicago_results.csv', delimiter=',', skiprows=1)[:,2] print average_cost(flow/4000., net, demand) costs = cost_ratio(flow/4000., net) features = np.zeros((f.shape[0],4)) features[:,:3] = f features[:,3] = costs links = process_links(net, node, features) color = features[:,3] - 1.0 # we choose the costs geojson_link(links, ['capacity', 'length', 'fftt', 'tt_over_fftt'], color)
def chicago_tt_over_fftt(): """ study the test_*.csv files generated by chicago_parametric_study() visualize tt/fftt for each edge """ net, demand, node, geometry = load_chicago() g_nr, small_capacity = add_cognitive_cost(net, geometry, 2000.0, 1000.0) # f = np.loadtxt('data/test_0.csv', delimiter=',', skiprows=0) alpha = 0.01 fs = np.loadtxt("data/test_{}.csv".format(int(100.0 * alpha)), delimiter=",", skiprows=0) # f = np.loadtxt('data/test_100.csv', delimiter=',', skiprows=0) f = np.sum(fs, axis=1) costs = cost_ratio(f, net) features = np.zeros((f.shape[0], 4)) features[:, :3] = geometry features[:, 3] = costs links = process_links(net, node, features) color = (costs - 1.0) * 2.0 + 1.0 geojson_link(links, ["capacity", "length", "fftt", "tt_over_fftt"], color)
def chicago_tt_over_fftt(): ''' study the test_*.csv files generated by chicago_parametric_study() visualize tt/fftt for each edge ''' net, demand, node, geometry = load_chicago() g_nr, small_capacity = add_cognitive_cost(net, geometry, 2000., 1000.) # f = np.loadtxt('data/test_0.csv', delimiter=',', skiprows=0) alpha = 0.01 fs = np.loadtxt('data/test_{}.csv'.format(int(100.*alpha)), delimiter=',', skiprows=0) # f = np.loadtxt('data/test_100.csv', delimiter=',', skiprows=0) f = np.sum(fs, axis=1) costs = cost_ratio(f, net) features = np.zeros((f.shape[0], 4)) features[:,:3] = geometry features[:,3] = costs links = process_links(net, node, features) color = (costs - 1.0) * 2.0 + 1.0 geojson_link(links, ['capacity', 'length', 'fftt', 'tt_over_fftt'], color)
def test_cost_ratio(self): net = np.loadtxt('data/braess_net.csv', delimiter=',', skiprows=1) net[:,5] = np.array([2.]*5) flow = np.array([0., 1., 2., 3., 4.]) result = np.array([1.0, 3.0, 1e8, 19, 1e8]) self.assertTrue(np.linalg.norm(result - cost_ratio(flow, net)) < 1e-8)
def check_results(): # check highest ration of tt / fftt (should be less than 5) net, d, node, features = load_I210() f = np.loadtxt('data/I210/test_0.csv', delimiter=',') print np.max(cost_ratio(f, net))
def increase_capacity(): net, demand, node = load_LA() f = np.loadtxt('data/LA/LA_output_3.csv', delimiter=',', skiprows=0) cr = cost_ratio(f, net)
def test_cost_ratio(self): net = np.loadtxt('data/braess_net.csv', delimiter=',', skiprows=1) net[:, 5] = np.array([2.] * 5) flow = np.array([0., 1., 2., 3., 4.]) result = np.array([1.0, 3.0, 1e8, 19, 1e8]) self.assertTrue(np.linalg.norm(result - cost_ratio(flow, net)) < 1e-8)