def _options_roads_neq( roadnet, road1, road2, length_attr ) : edge1, data1 = ROAD.obtain_edge( roadnet, road1, data_flag=True ) u1,v1,_ = edge1 ; roadlen1 = data1.get( length_attr, np.inf ) edge2, data2 = ROAD.obtain_edge( roadnet, road2, data_flag=True ) u2,v2,_ = edge2 ; roadlen2 = data2.get( length_attr, np.inf ) x = GLOBAL.x ; y = GLOBAL.y cost_dict = { '<-s' : x, 's->' : roadlen1 - x, '->t' : y, 't<-' : roadlen2 - y } options = [] # paths through endpoints WAYP1 = [ ( 's->', v1 ) ] if not data1.get( 'oneway', False ) : WAYP1.append( ( '<-s', u1 ) ) WAYP2 = [ ( '->t', u2 ) ] if not data2.get( 'oneway', False ) : WAYP2.append( ( 't<-', v2 ) ) for s,u in WAYP1 : p = ROAD.roadify( roadnet, u, length_attr ) for t,v in WAYP2 : q = ROAD.roadify( roadnet, v, length_attr ) dst = ROAD.distance( roadnet, p, q, length_attr ) cost = cost_dict[s] + dst + cost_dict[t] options.append( cost ) return options
def roadEd_conditional( roadnet, road1, road2, length_attr='length' ) : edge1, data1 = ROAD.obtain_edge( roadnet, road1, data_flag=True ) roadlen1 = data1.get( length_attr, np.inf ) edge2, data2 = ROAD.obtain_edge( roadnet, road2, data_flag=True ) roadlen2 = data2.get( length_attr, np.inf ) phases = _compute_phases( roadnet, road1, road2, length_attr='length' ) pglints = [ _phase_to_pglint( c, constr ) for c,constr in phases ] # somewhere we need to normalize! vals = [ pglint.integrate( c, d, A, b ) for c, d, A, b in pglints ] return sum( vals ) / roadlen1 / roadlen2
def _compute_phases( roadnet, road1, road2, length_attr ) : edge1, data1 = ROAD.obtain_edge( roadnet, road1, data_flag=True ) roadlen1 = data1.get( length_attr, np.inf ) edge2, data2 = ROAD.obtain_edge( roadnet, road2, data_flag=True ) roadlen2 = data2.get( length_attr, np.inf ) res = [] x = GLOBAL.x ; y = GLOBAL.y canvas = [ -x, # x >= 0 x - roadlen1, # x <= roadlen1 -y, # y >= 0 y - roadlen2 # y <= roadlen2 ] """ there are three "interesting" cases """ if road1 == road2 : # case 1: road1 == road2, y >= x options = _options_roads_eq_ygeqx( roadnet, road1, length_attr ) zones = _zonify_options( options ) for zone in zones : zone.extend( canvas ) zone.append( x - y ) phases = zip( options, zones ) res.extend( phases ) # case 2: road1 == road2, y <= x options = _options_roads_eq_yleqx( roadnet, road1, length_attr ) zones = _zonify_options( options ) for zone in zones : zone.extend( canvas ) zone.append( y - x ) phases = zip( options, zones ) res.extend( phases ) else : # case 3: road1 != road2 options = _options_roads_neq( roadnet, road1, road2, length_attr ) zones = _zonify_options( options ) for zone in zones : zone.extend( canvas ) phases = zip( options, zones ) res.extend( phases ) return res
def computeImbalance( roadmap, rategraph, rate_attr='rate', **kwargs ) : weight1_attr = kwargs.get( 'weight1_attr', 'weight1' ) weight2_attr = kwargs.get( 'weight2_attr', 'weight2' ) for road in rategraph.nodes_iter() : supply = rategraph.in_degree( road, rate_attr ) - rategraph.out_degree( road, rate_attr ) _, road_data = ROAD.obtain_edge( roadmap, road, True ) if supply > 0. : road_data[weight1_attr] = supply elif supply < 0. : road_data[weight2_attr] = -supply
def _options_roads_eq_yleqx( roadnet, road, length_attr ) : edge, data = ROAD.obtain_edge( roadnet, road, data_flag=True ) u,v,_ = edge ; roadlen = data.get( length_attr, np.inf ) p = ROAD.roadify( roadnet, u, length_attr ) q = ROAD.roadify( roadnet, v, length_attr ) x = GLOBAL.x ; y = GLOBAL.y sojourn_cost = roadlen - x + ROAD.distance( roadnet, q, p, length_attr ) + y # a path using the rest of the network options = [ sojourn_cost ] if not data.get( 'oneway', False ) : options.append( x - y ) return options
def samplepair( roadnet, distr1, distr2=None, length_attr='length' ) : """ assume distr sums to one; if not, no guaranteed behavior """ if distr2 is not None : raise 'not implemented yet' # choose appropriately, randomly... can probably do better x = np.random.rand() X = 0. for (road1,road2), prob in distr1.iteritems() : X += prob if X >= x : break _, data1 = ROAD.obtain_edge( roadnet, road1, data_flag=True ) _, data2 = ROAD.obtain_edge( roadnet, road2, data_flag=True ) roadlen1 = data1.get( 'length' ) roadlen2 = data2.get( 'length' ) x = roadlen1 * np.random.rand() y = roadlen2 * np.random.rand() p = ROAD.RoadAddress( road1, x ) q = ROAD.RoadAddress( road2, y ) return p, q
def position(address, roadmap, pos, length_attr='length'): """ get the Euclidean position of a street address, given roadmap and dictionary of vertex positions """ road, coord = address coord = float(coord) u, v, key = ROAD.obtain_edge(roadmap, road) assert key == road data = ROAD.get_road_data(road, roadmap) width = data.get( length_attr, 1 ) #ROAD.get_edge_data( ) x = pos[u] vec = pos[v] - x return x + vec * coord / width