def test_helper(demand, paths): g = los_angeles(theta, 'Polynomial')[demand] for p in paths: g.add_path_from_nodes(p) P = path_solver.linkpath_incidence(g) g.visualize(general=True) l1 = ue.solver(g, update=True) d1 = sum([link.delay*link.flow for link in g.links.values()]) l2 = P*path_solver.solver(g, update=True) d2 = sum([p.delay*p.flow for p in g.paths.values()]) l3 = ue.solver(g, update=True, SO=True) d3 = sum([link.delay*link.flow for link in g.links.values()]) l4 = P*path_solver.solver(g, update=True, SO=True) d4 = sum([p.delay*p.flow for p in g.paths.values()]) return l1,l2,l3,l4,d1,d2,d3,d4
def compute_wp_flow(SO=False, demand=3, random=False, data=None, path=None): """Generate map of L.A., UE path_flow, waypoint trajectories 1. Generate map of L.A. and waypoints with generate_wp 2. Get used paths in UE/SO (see generate_paths module) 3. generate waypoints following the map of L.A. 4. For each path, find closest waypoints Parameters: ---------- SO: if True suppose vehicle behavior follows SO, UE o.w. demand: OD demand random: if True, generate random UE/SO paths data: waypoint density (N0, N1, scale, regions, res, margin) inputs of generate_wp Return value: ------------- g: Graph object of L.A. p_flow: vector of path flows path_wps: dictionary of all the paths with flow>tol and with a list of closest waypoints to it or associated wp trajectory {path_id: wp_ids} wp_trajs: list of waypoint trajectories with paths along this trajectory [(wp_traj, path_list, flow)] """ g, WP = generate_wp(demand, data, path=path) paths = find_UESOpaths(SO, path=path) # find the used paths in for p in paths: g.add_path_from_nodes(p) g.visualize(general=True) p_flow, l_flow = path_solver.solver(g, update=True, SO=SO, random=random) path_wps, wp_trajs = WP.get_wp_trajs(g, 20, True) return g, p_flow, path_wps, wp_trajs, l_flow
def get_paths(SO, K, demand, return_paths=True, ffdelays=False, path=None, save_data=False, savepath=None): """This experiment does the following tests: 1. compute the UE/SO link flows using node-link formulation 2. get the link delays for the UE/SO link flow 3. find the K-shortest paths for these delays/marginal delays (used ones under UE/SO) 4. add these paths to the network 5. compute the UE/SO path flows using link-path formulation 6. check if we get the same UE/SO link flow Parameters: ----------- SO: if False, compute the UE, if True, compute the SO K: number of shortest paths demand: choice of OD demand return_paths: if True, return paths ffdelays: if True the k-shortest paths are obtained from ff delays Return value: ------------ """ print 'generate graph with demand', demand g = los_angeles(theta, 'Polynomial', path=path)[demand] if ffdelays: paths = get_shortest_paths(g, K) print 'compute UE' l1 = ue.solver(g, update=True, SO=SO) d1 = sum([link.delay*link.flow for link in g.links.values()]) if SO: for link in g.links.values(): link.delay = link.ffdelay*(1+0.75*(link.flow*link.delayfunc.slope)**4) print 'get {} shortest paths'.format(K) if not ffdelays: paths = get_shortest_paths(g, K) if return_paths: return paths for p in paths: g.add_path_from_nodes(p) g.visualize(general=True) print 'Generate link-path incidence matrix' P = path_solver.linkpath_incidence(g) x_true = path_solver.solver(g, update=True, SO=SO)[0] l2 = P*x_true d2 = sum([p.delay*p.flow for p in g.paths.values()]) if save_data: U, f = path_solver.path_to_OD_simplex(g) if savepath is None: savepath = 'data.pkl' data = {} data['U'] = np.array(matrix(U)) data['f'] = np.array(f).flatten() data['A'] = np.array(matrix(P)) data['b'] = np.array(l2).flatten() data['x_true'] = np.array(x_true).flatten() pickle.dump( data, open(savepath, "wb" ) ) #for i in range(P2.shape[0]): # print np.sum(P2[i,:]) return d1,d2,paths
def test_feasible_pathflows(SO, demand, random=False): """Test function feasible_pathflows""" paths = find_UESOpaths(SO) g = los_angeles(theta, 'Polynomial')[demand] l1 = ue.solver(g, update=True, SO=SO) d1 = sum([link.delay*link.flow for link in g.links.values()]) for p in paths: g.add_path_from_nodes(p) g.visualize(general=True) P = linkpath_incidence(g) l2 = P*path_solver.solver(g, update=True, SO=SO, random=random) d2 = sum([p.delay*p.flow for p in g.paths.values()]) ind_obs, ls, ds = {}, [], [] ind_obs[0] = g.indlinks.keys() ind_obs[1] = [(36,37,1), (13,14,1), (17,8,1), (24,17,1), (28,22,1), (14,13,1), (17,24,1), (24,40,1), (14,21,1), (16,23,1)] ind_obs[2] = [(17,24,1), (24,40,1), (14,21,1), (16,23,1)] for i in range(len(ind_obs)): obs = [g.indlinks[id] for id in ind_obs[i]] obs = [int(i) for i in list(np.sort(obs))] ls.append(P*path_solver.feasible_pathflows(g, l1[obs], obs, True)) ds.append(sum([p.delay*p.flow for p in g.paths.values()])) print d1,d2,ds print np.linalg.norm(l1-l2), [np.linalg.norm(l1-ls[i]) for i in range(len(ind_obs))]
def find_UESOpaths(SO, return_paths=True, random=False, path=None): """ 1. take the union for all optimum shortest paths for UE/SO 2. compute UE/SO using node-link and link-path formulation for all demands 3. compare results Parameters: ----------- SO: if False, compute the UE, if True, compute the SO return_paths: if True, do only step 1 and return paths, if False, do steps 2 and 3 """ paths, ls, ds, ps = [], [], [], [] if SO: K = [0,0,0,10] #[2, 2, 4, 7] else: K = [0,0,0,5] #[2, 2, 2, 3] [5,5,5,5] for i in range(4): tmp = get_paths(SO, K[i], i, path=path) for p in tmp: if p not in paths: paths.append(p) if return_paths: return paths for i in range(4): g = los_angeles(theta, 'Polynomial')[i] for p in paths: g.add_path_from_nodes(p) P = linkpath_incidence(g) g.visualize(general=True) l1 = ue.solver(g, update=True, SO=SO) d1 = sum([link.delay*link.flow for link in g.links.values()]) p_flows = path_solver.solver(g, update=True, SO=SO, random=random) l2 = P*p_flows d2 = sum([p.delay*p.flow for p in g.paths.values()]) ls.append([l1,l2]) ds.append([d1,d2]) ps.append(p_flows) for i in range(4): print np.linalg.norm(ls[i][0] - ls[i][1]) print ds[i][0], ds[i][1] print len(paths)