def MATCHCOSTS(match, P, Q, roadnet): costs = [] for i, j in match: p = ROAD.RoadAddress(*P[i]) q = ROAD.RoadAddress(*Q[j]) d = ROAD.distance(roadnet, p, q, 'length') costs.append(d) return costs
def SPLITTOUR( tour, N, demands, roadmap ) : """ split the tour (seq. of indices) of demands (list of demands) on roadmap into k pieces """ tours = [] tourlen = CYCLECOST( tour, demands, roadmap ) fragtarget = float(tourlen) / N EDGES = zip( tour[-1:] + tour[:-1], tour ) # cycle; first "prev" is last x, y = [ ROAD.RoadAddress(None,None) for i in range(2) ] # storage for k in range(N) : frag = [] fraglen = 0. while fraglen < fragtarget : if not len( EDGES ) > 0 : break i, j = EDGES.pop(0) prev, curr = demands[i], demands[j] p, q, r = prev.delv, curr.pick, curr.delv # fetch x.init(*p) ; y.init(*q) exten = roadpaths.minpath( x, y, roadmap ) y.init(*r) exten = roadpaths.pathExtend( exten, y, roadmap ) extenlen = roadpaths.pathLength( exten ) frag.append(j) fraglen += roadpaths.pathLength( exten ) #print exten, extenlen, fraglen, fragtarget tours.append( frag ) return tours
def CYCLECOST( cycle, demands, roadnet, length_attr='length' ) : addr = lambda p : ROAD.RoadAddress( *p ) carry = [ ROAD.distance( roadnet, addr(dem.pick), addr(dem.delv), length_attr ) for dem in demands ] edges = zip( cycle, cycle[1:] + cycle[:1] ) #print edges edges = [ ( demands[i], demands[j] ) for i,j in edges ] empty = [ ROAD.distance( roadnet, addr(dem1.delv), addr(dem2.pick), length_attr ) for dem1,dem2 in edges ] return sum( carry ) + sum( empty )
def ASSIGNFRAGS( tours, agentLocs, demands, roadmap ) : """ assign the fragments to agents in accordance with a minimum matching from each agent (its position) to some origin on the tour """ # compute distance to tour (some pickup) from each agent graph = nx.Graph() x = ROAD.RoadAddress(None,None) for agent, agentLoc in agentLocs.iteritems() : t = token() t.agent = agent def cost( demidx ) : x.init( *demands[demidx].pick ) return ROAD.distance( roadmap, agentLoc, x, 'length' ) for touridx, tour in enumerate( tours ) : if len( tour ) > 0 : options = [ ( cost(demidx), k ) for k, demidx in enumerate(tour) ] else : options = [ ( 0., 0 ) ] # empty tour copt, kopt = min( options ) # negative weights for MIN weight graph.add_edge( t, touridx, weight = -copt, start_at = kopt ) # get optimal matching MATCH = nx.matching.max_weight_matching( graph, maxcardinality=True ) assign = {} for touridx, tour in enumerate( tours ) : u = MATCH[touridx] k = graph.get_edge_data( u, touridx ).get( 'start_at' ) agent = u.agent order = tour[k:] + tour[:k] assign[agent] = order return assign
pos[v] = np.array( (X[3],-k * band_height) ) r1, r2, r3 = [ fmt % k for fmt in [ 'LCONN%d', 'BAND%d', 'RCONN%d' ] ] roadmap.add_edge(1,u, r1 ) roadmap.add_edge(u,v, r2 ) roadmap.add_edge(v,4, r3 ) # give length annotations for u,v, road, data in roadmap.edges_iter( keys=True, data=True ) : #p = np.array( pos[u] ) #q = np.array( pos[v] ) data['length'] = np.linalg.norm( pos[v] - pos[u] ) # place the points Y = np.linspace(0,1,NUMPOINTS+2)[1:-1] S = [ ROAD.RoadAddress('start',y) for y in Y ] T = [ ROAD.RoadAddress('end',y) for y in Y ] SAMPLE_RANDOMLY = False """ congestion function """ #rho = lambda x : np.power( x, 2. ) # square law, why not! p = .07 rho = lambda x : np.power( abs(x), p ) # linear congestion? # separate instance? if SAMPLE_RANDOMLY :
DEMANDS = [ demand(p,q) for p,q in zip( PP, QQ ) ] fhk = RoadMapFHK( roadnet ) print fhk.cover tour = fhk( DEMANDS, demand.getTail, demand.getHead ) #tour = ROADSSPLICE( DEMANDS, roadnet ) from setiptah.vehrouting.stackercrane2 import WALKCOST tourlen = WALKCOST( tour, DEMANDS, demand.getTail, demand.getHead, fhk.distance ) #cost = CYCLECOST( tour, DEMANDS, roadnet ) N = 3 agentLocs = { 'agent%d' % i : ROAD.RoadAddress( *sampler.sample() ) for i in range(N) } assign_star = fhk.kSPLICE( DEMANDS, agentLocs, demand.getTail, demand.getHead ) assign = SPLITANDASSIGN( tour, agentLocs, DEMANDS, demand.getTail, demand.getHead, fhk.distance ) print assign == assign_star costs = { i : WALKCOST( walk, DEMANDS, demand.getTail, demand.getHead, fhk.distance ) for i, walk in assign_star.iteritems() } #order = ORDERPOINTS( PP, tour ) #point_tour = [ PP[i] for i in order ] #print point_tour
for road, Cz in objective_dict.iteritems(): cost = costWrapper(Cz) C = [cost(zz) for zz in z] plt.figure() plt.plot(z, C, '--', marker='x') match = ROADSBIPARTITEMATCH(PP, QQ, roadnet) costs = MATCHCOSTS(match, PP, QQ, roadnet) cost = ROADMATCHCOST(match, PP, QQ, roadnet) print match print costs print cost # compare to optimal matching if True and NUMPOINT <= 50: PR = [ROAD.RoadAddress(road, y) for road, y in PP] QR = [ROAD.RoadAddress(road, y) for road, y in QQ] class pointnode(): def __init__(self, point, idx): self.point = point self.idx = idx RED = [pointnode(p, i) for i, p in enumerate(PR)] BLU = [pointnode(q, j) for j, q in enumerate(QR)] graph = nx.Graph() match_mat = np.zeros((NUMPOINT, NUMPOINT)) for (i, r), (j, b) in itertools.product(enumerate(RED), enumerate(BLU)): w = ROAD.distance(roadnet, r.point, b.point, 'length') graph.add_edge(r, b, weight=-w)