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 _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 balance_cost2( demands, roadnet ) : distfunc = lambda p, q : ROAD.distance( roadnet, p, q, 'length' ) graph = nx.Graph() for dem1, dem2 in itertools.product( demands, demands ) : dist = distfunc( dem1.delv, dem2.pick ) graph.add_edge( (dem1,0), (dem2,1), weight = -dist ) # about to do MAX_weight_matching mate = nx.matching.max_weight_matching( graph, maxcardinality=True ) balances = [ -graph.get_edge_data( (dem1,0), (dem2,1) ).get('weight') for ( (dem1,side1), (dem2,side2) ) in mate.iteritems() if side1 == 0 ] total_balance = sum( balances ) return total_balance
def enroute_cost( demands, roadnet ) : distfunc = lambda p, q : ROAD.distance( roadnet, p, q, 'length' ) enroutes = [ distfunc( dem.pick, dem.delv ) for dem in demands ] total_enroute = sum( enroutes ) return total_enroute
def distance(self, p, q ) : return ROAD.distance( self.roadnet, p, q, 'length' )
roadnet = nx.MultiDiGraph() roadnet.add_edge( 0,1, 'LEFT', length=1. ) roadnet.add_edge( 1,2, 'RIGHT', length=1. ) normrategraph = nx.DiGraph() normrategraph.add_edge( 'LEFT', 'RIGHT', rate=1. ) numveh = 1 # DEBUG: for some reason, multiple vehicles is a no-no?!? vehspeed = 1. horizon = 10000. """ end parameters """ """ derived parameters """ # the function which will be used by various components to compute distances f_dist = lambda p, q : ROAD.distance( roadnet, p, q, 'length' ) # Note: rates sum to 1. # compute necessary "service velocity" demvel_enroute = road_complexity.demand_enroute_velocity( roadnet, normrategraph ) demvel_balance = road_complexity.demand_balance_velocity( roadnet, normrategraph ) # MM = road_complexity.MoversComplexity( roadnet, normrategraph ) max_rate = numveh * vehspeed / MM #arrivalrate = max_rate * 1. + 1. #arrivalrate = max_rate * .99 + 0. arrivalrate = max_rate + 0.1 #* 1.02 rategraph = nx.DiGraph() for r1, r2, data in normrategraph.edges_iter( data=True ) : rategraph.add_edge( r1, r2, rate=arrivalrate * data.get('rate') )
label, length = road_iter.next() roadnet.add_edge( u, v, label, length=length, oneway=True ) if True : distr = {} distr[('W','E')] = 1./5 distr[('N','E')] = 1./5 distr[('W','S')] = 3./5 Ed = roadEd( roadnet, distr, length_attr='length' ) print 'Ed computed %f' % Ed pairs = [ ROADRAND.samplepair( roadnet, distr ) for i in range(20000) ] dst = [ ROAD.distance( roadnet, p, q, 'length' ) for p,q in pairs ] Ed_emp = np.mean( dst ) print 'Ed empirical %f' % Ed_emp else : ROADS = ['N', 'S', 'E', 'W' ] #PAIRS = itertools.product( ROADS, ROADS ) #PAIRS = [ ('E','E') ] edges = [ name for _,__,name in roadnet.edges( keys=True ) ] #PAIRS = [ ( random.choice(edges), random.choice(edges) ) for i in range(5) ] for road1, road2 in PAIRS : Ed_cond = roadEd_conditional( roadnet, road1, road2 ) # pairs = [ sample( roadnet, { (road1, road2) : 1.} ) for i in range(20000) ] dst = [ ROAD.distance( roadnet, p, q, 'length' ) for p,q in pairs ]
def measurenx_to_approxnx( roadnet, epsilon, length='length', weight1='weight1', weight2='weight2' ) : """ input: a road network, with weights on its elements output: returns a graph summarizing the network optimization problem instance; roadnets are multi-digraph, where edge 'keys' are assumed to be unique, i.e., road names; and should be different from node labels too; """ digraph = nx.DiGraph() #digraph.add_node('s') #digraph.add_node('t') SUPPLY = [] DEMAND = [] """ insert supply and demand of roads """ for u,v, road, data in roadnet.edges_iter( keys=True, data=True ) : roadlen = float( data.get( length, 1 ) ) # float() just in case assert roadlen >= 0. """ split the road into equal-length segments; create a node for each segment; record boundary points, and mass contained """ N = int( np.ceil( roadlen / epsilon ) ) eps = roadlen / N surplus = float( data.get( weight1, 0. ) ) - data.get( weight2, 0. ) deficit = -surplus bd = np.linspace( 0, roadlen, N+1 ) bd = [ roadmaps.RoadAddress( road, x ) for x in bd ] for i, boundary in enumerate( zip( bd[:-1], bd[1:] ) ) : if surplus > 0. : node = (road,i,'supply') digraph.add_node( node, boundary=boundary ) digraph.add_edge( 's', node, flow=cvxpy.variable(), minflow=0., maxflow=surplus/N ) SUPPLY.append( node ) if deficit > 0. : node = (road,i,'demand') digraph.add_node( node, boundary=boundary ) digraph.add_edge( node, 't', flow=cvxpy.variable(), minflow=0., maxflow=deficit/N ) DEMAND.append( node ) """ ...and nodes """ for u, data in roadnet.nodes_iter( data=True ) : surplus = data.get( weight1, 0. ) - data.get( weight2, 0. ) deficit = -surplus if surplus > 0. : boundary = [ roadmaps.roadify( roadnet, u, weight=length ) ] node = (u,'supply') digraph.add_node( node, boundary=boundary ) digraph.add_edge( 's', node, flow=cvxpy.variable(), minflow=0., maxflow=surplus ) SUPPLY.append( node ) if deficit > 0. : boundary = [ roadmaps.roadify( roadnet, v, weight=length ) ] node = (u,'demand') digraph.add_node( node, boundary=boundary ) digraph.add_edge( node, 't', flow=cvxpy.variable(), minflow=0., maxflow=deficit ) DEMAND.append( node ) """ generate bipartite graph b/w SUPPLY and DEMAND """ for u, v in itertools.product( SUPPLY, DEMAND ) : bd_u = digraph.node[u]['boundary'] bd_v = digraph.node[v]['boundary'] options = [ pair for pair in itertools.product( bd_u, bd_v ) ] options = [ roadmaps.distance( roadnet, p, q, weight=length ) for p,q in options ] #options = [ np.inf ] w = min( options ) W = max( options ) flowvar = cvxpy.variable() digraph.add_edge( u, v, flow=flowvar, minflow=0., w=w, W=W, cost_lo = w * flowvar, cost_hi = W * flowvar ) nxopt.attach_flownx_constraints( digraph ) return digraph # a flow network