def test_all_or_nothing(self): # load the data graph=np.loadtxt('data/braess_graph.csv', delimiter=',', skiprows=1) g = construct_igraph(graph) od=np.loadtxt('data/braess_od.csv', delimiter=',', skiprows=1) od={int(od[0]): ([int(od[1])],[od[2]])} # all or nothing assignment L = all_or_nothing(g, od) self.assertTrue(np.linalg.norm(L - np.array([2., 0., 2., 0., 2.])) < eps) g.es["weight"] = [1., 1., 1000., 1.0, 0.0] # modify graph L = all_or_nothing(g, od) self.assertTrue(np.linalg.norm(L - np.array([0., 2., 0., 0., 2.])) < eps)
def LA_free_flow_costs(thres, cog_costs): ''' study aiming at comparing the OD costs of all-or-nothing assignment between costs = travel times, and costs with multiplicative cognitive costs ''' net, demand, node, geom = load_LA_2() g = construct_igraph(net) g2 = construct_igraph(net) od = construct_od(demand) print np.array(g.es["weight"]).dot(all_or_nothing(g, od))/ (np.sum(demand[:,2])*60.) for K in cog_costs: net2, small_capacity = multiply_cognitive_cost(net, geom, thres, K) g2.es["weight"] = net2[:,3] print np.array(g.es["weight"]).dot(all_or_nothing(g2, od))/ (np.sum(demand[:,2])*60.)
def all_or_nothing_assignment(cost, net, demand): # given vector of edge costs, graph, and demand, computes the AoN # assignment g = construct_igraph(net) od = construct_od(demand) g.es["weight"] = cost.tolist() return all_or_nothing(g, od)
def test_all_or_nothing(self): # load the data graph = np.loadtxt('data/braess_graph.csv', delimiter=',', skiprows=1) g = construct_igraph(graph) od = np.loadtxt('data/braess_od.csv', delimiter=',', skiprows=1) od = {int(od[0]): ([int(od[1])], [od[2]])} # all or nothing assignment L = all_or_nothing(g, od) self.assertTrue( np.linalg.norm(L - np.array([2., 0., 2., 0., 2.])) < eps) g.es["weight"] = [1., 1., 1000., 1.0, 0.0] # modify graph L = all_or_nothing(g, od) self.assertTrue( np.linalg.norm(L - np.array([0., 2., 0., 0., 2.])) < eps)
def search_direction(f, graph, g, od): # computes the Frank-Wolfe step # g is just a canvas containing the link information and to be updated with # the most recent edge costs x = np.power(f.reshape((f.shape[0], 1)), np.array([0, 1, 2, 3, 4])) grad = np.einsum('ij,ij->i', x, graph[:, 3:]) # print x.shape, graph[:,3:].shape, grad.shape # print x[0], graph[:,3:][0], grad[0] g.es['weight'] = grad.tolist() #start timer #start_time1 = timeit.default_timer() L, path_flows = all_or_nothing(g, od) # print len(path_flows) # for k in path_flows: # print k, path_flows[k] # exit(1) #end of timer #elapsed1 = timeit.default_timer() - start_time1 #print ('all_or_nothing took %s seconds' % elapsed1) return L, grad, path_flows
def LA_free_flow_costs(thres, cog_costs): ''' study aiming at comparing the OD costs of all-or-nothing assignment between costs = travel times, and costs with multiplicative cognitive costs ''' net, demand, node, geom = load_LA_2() g = construct_igraph(net) g2 = construct_igraph(net) od = construct_od(demand) print np.array(g.es["weight"]).dot(all_or_nothing( g, od)) / (np.sum(demand[:, 2]) * 60.) for K in cog_costs: net2, small_capacity = multiply_cognitive_cost(net, geom, thres, K) g2.es["weight"] = net2[:, 3] print np.array(g.es["weight"]).dot(all_or_nothing( g2, od)) / (np.sum(demand[:, 2]) * 60.)
def test_all_or_nothing_2(self): # load the data graph=np.loadtxt('data/graph_test.csv', delimiter=',') g = construct_igraph(graph) # print graph od=np.loadtxt('data/od_test.csv', delimiter=',') od={int(od[0]): ([int(od[1])],[od[2]])} L = all_or_nothing(g, od)
def search_direction(f, graph, g, od): # computes the Frank-Wolfe step # g is just a canvas containing the link information and to be updated with # the most recent edge costs x = np.power(f.reshape((f.shape[0], 1)), np.array([0, 1, 2, 3, 4])) grad = np.einsum('ij,ij->i', x, graph[:, 3:]) g.es["weight"] = grad.tolist() return all_or_nothing(g, od), grad
def test_all_or_nothing_2(self): # load the data graph = np.loadtxt('data/graph_test.csv', delimiter=',') g = construct_igraph(graph) # print graph od = np.loadtxt('data/od_test.csv', delimiter=',') od = {int(od[0]): ([int(od[1])], [od[2]])} L = all_or_nothing(g, od)
def search_direction(f, graph, g, od): # computes the Frank-Wolfe step # g is just a canvas containing the link information and to be updated with # the most recent edge costs x = np.power(f.reshape((f.shape[0],1)), np.array([0,1,2,3,4])) grad = np.einsum('ij,ij->i', x, graph[:,3:]) g.es["weight"] = grad.tolist() return all_or_nothing(g, od), grad
def residual(graphs, demands, g, ods, fs): f = np.sum(fs, axis=1) x = np.power(f.reshape((graphs[0].shape[0],1)), np.array([0,1,2,3,4])) r = 0. for i, (graph, demand, od) in enumerate(zip(graphs, demands, ods)): grad = np.einsum('ij,ij->i', x, graph[:,3:]) g.es["weight"] = grad.tolist() L = all_or_nothing(g, od) r = r + grad.dot(fs[:,i] - L) return r
def residual(graphs, demands, g, ods, fs): f = np.sum(fs, axis=1) x = np.power(f.reshape((graphs[0].shape[0], 1)), np.array([0, 1, 2, 3, 4])) r = 0. for i, (graph, demand, od) in enumerate(zip(graphs, demands, ods)): grad = np.einsum('ij,ij->i', x, graph[:, 3:]) g.es["weight"] = grad.tolist() L = all_or_nothing(g, od) r = r + grad.dot(fs[:, i] - L) return r
def search_direction(f, graph, g, od, features): # computes the Frank-Wolfe step # g is just a canvas containing the link information and to be updated with # the most recent edge costs x = np.power(f.reshape((f.shape[0], 1)), np.array([0, 1, 2, 3, 4])) grad = np.einsum('ij,ij->i', x, graph[:, 3:]) g.es["weight"] = grad.tolist() #start timer #start_time1 = timeit.default_timer() L = all_or_nothing(g, od) #end of timer #elapsed1 = timeit.default_timer() - start_time1 #print ("all_or_nothing took %s seconds" % elapsed1) return L, grad
def search_direction_social_optimum(f, graph, g, od): # computes the Frank-Wolfe step # g is just a canvas containing the link information and to be updated with # the most recent edge costs x = np.power(f.reshape((f.shape[0], 1)), np.array([0, 1, 2, 3, 4])) #import pdb; pdb.set_trace() #When we add the price of anarchy, the cost function becomes a0+ 2*a2*x+ 3*a3*x^2+ 4*a4*x^3+ 5*a5*x^4 #Matrix coefficients saves the 1, 2, 3, 4, and 5 coefficients in front of the ai*x terms coefficients = np.array([1, 2, 3, 4, 5]) onesArray = np.ones((f.shape[0], 5)) coefficients = onesArray * coefficients.transpose() #y stands for the flow multiplied by the coefficients #import pdb; pdb.set_trace() y = np.einsum('ij, ij->ij', x, coefficients) grad = np.einsum('ij,ij->i', y, graph[:, 3:]) g.es["weight"] = grad.tolist() #Calculating All_or_nothing L = all_or_nothing(g, od) return L, grad
def total_free_flow_cost(g, od): return np.array(g.es["weight"]).dot(all_or_nothing(g, od))
def all_or_nothing_assignment(cost, net, demand): # given vector of edge costs, graph, and demand, computes the AoN assignment g = construct_igraph(net) od = construct_od(demand) g.es["weight"] = cost.tolist() return all_or_nothing(g, od)
def total_ff_costs_heterogeneous(graphs, g, ods): cost = 0. for graph, od in zip(graphs, ods): g.es["weight"] = graph[:, 3].tolist() cost = cost + graph[:, 3].dot(all_or_nothing(g, od)) return cost
def total_ff_costs_heterogeneous(graphs, g, ods): cost = 0. for graph, od in zip(graphs, ods): g.es["weight"] = graph[:,3].tolist() cost = cost + graph[:,3].dot(all_or_nothing(g, od)) return cost