for _,__,road, data in roadnet.edges_iter( keys=True, data=True ) :
            data['length'] = 5.
        roadset = [ road for _,__,road in roadnet.edges_iter( keys=True ) ]
        
        normrategraph = nx.DiGraph()
        # is it a "true zero" issue?
        if False :
            for road1, road2 in itertools.product( roadset, roadset ) :
                normrategraph.add_edge( road1, road2, rate=.001 )
        # add the meaningful rates
        normrategraph.add_edge( 'N', 'E', rate=1./5 )
        normrategraph.add_edge( 'W', 'E', rate=1./5 )
        normrategraph.add_edge( 'W', 'S', rate=3./5 )
    
    elif True :
        roadnet = roadprob.sampleroadnet()
        normrategraph = massprob.sample_rategraph( roadnet, normalize=True )
        
    else :
        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. )
        

    probe = RoadmapEMD()
    probe.horizon = 10000.
    
    complexity_computed = []
    complexity_estimated = []
示例#2
0
 def run(self) :
     """ setup """
     sim = Simulation()
     self.sim = sim
     
     clock = PoissonClock( self.rate )
     clock.join_sim( sim )
     
     # prepare geometry queries
     if True :
         ORIGIN = np.zeros(2)
         
         def samplepoint() :
             return np.random.rand(2)
             
         planner = EuclideanPlanner
         #scheduler = RoundRobinScheduler()
         
         # Euclidean instantiation
         getTail = lambda dem : dem.origin
         getHead = lambda dem : dem.destination
         distance = lambda x, y : np.linalg.norm( y - x )
         scheduler = kCraneScheduler( getTail, getHead, distance )
         
     else :
         import setiptah.roadgeometry.roadmap_basic as ROAD
         import setiptah.roadgeometry.probability as roadprob
         from setiptah.roadgeometry.roadmap_paths import RoadmapPlanner
         
         roadmap = roadprob.sampleroadnet()
         U = roadprob.UniformDist( roadmap )
         samplepoint = U.sample
         
         ORIGIN = samplepoint()
         
         distance = lambda x, y : ROAD.distance( roadmap, 
                                                 x, y, length='length' )
         planner = RoadmapPlanner( roadmap )
         
         if True :
             scheduler = RoundRobinScheduler()
         else :
             getTail = lambda dem : dem.origin
             getHead = lambda dem : dem.destination
             scheduler = kCraneScheduler( getTail, getHead, distance )
     
     # instantiate the gate
     gate = GatedTaxiDispatch()
     gate.setScheduler( scheduler )
     
     # instantiate the fleet
     TAXI = []
     for i in range( self.numveh ) :
         taxi = Taxi()
         TAXI.append( taxi )
         
         taxi.setPlanner( planner )
         taxi.setLocation( ORIGIN )
         taxi.setSpeed( self.vehspeed )
         
         taxi.join_sim( sim )
         
         gateIF = gate.newInterface()
         gate.add( gateIF )
         gateIF.output.connect( taxi.appendDemands )
         taxi.signalWakeup.connect( gateIF.input )
         taxi.signalIdle.connect( gateIF.input )
         
     gate.join_sim( sim )
     
     def tick() :
         print 'tick, %g' % sim.get_time()
         
         
     SIMULATION.DEMANDS = []
     def myarrival() :
         x = samplepoint()
         y = samplepoint()
         
         #print x, y
         time = sim.get_time()
         demand = Taxi.Demand( x, y, time )
         print 'demand generated ', x, y, time
         
         SIMULATION.DEMANDS.append( demand )
         # send the demand to the gate, not any one taxi
         gate.queueDemand( demand )
         
     EVER = dumbcounter()
     
     clock.source().connect( myarrival )
     clock.source().connect( EVER.increment )
         
     RESULTS.ever_tape = []
     RESULTS.alive_tape = []
     
     def record() :
         RESULTS.ever_tape.append( EVER.value() )
         
         total = len( gate._demandQ )
         for taxi in TAXI :
             total += len( taxi._demandQ )
             
         RESULTS.alive_tape.append( total )
         
     probe = UniformClock(.1)        # why not
     probe.join_sim( sim )
     probe.source().connect( record )
     
     
     """ run """
     if False :
         while sim.get_time() <= self.horizon :
             callback = sim.get_next_action()
             callback()
             
             frac = sim.get_time() / self.horizon
             perc = int( 100. * frac )
             #print perc
             
             self.timeElapsed.emit( perc )       # emit the custom signal?
             
         self.alarm()
             
     else :
         T0 = 100.
         alpha = 2.
         beta = 1.1
         gamma = 1.
         XTHRESH = 250
         
         # phase I --- simulate base time
         while sim.get_time() <= T0 :
             callback = sim.get_next_action()
             callback()
             
         self.alarm()
         
         
         # phase II
         T = [ T0 ]
         xmax = max( RESULTS.alive_tape )
         Tk = T0
         while True :
             Tk = alpha*Tk
             
             epoch = sim.get_time()
             while sim.get_time() - epoch <= Tk :
                 callback = sim.get_next_action()
                 callback()
                 
             T.append( Tk )
             self.alarm()
             
             newmax = max( RESULTS.alive_tape )
             if newmax > XTHRESH :
                 print 'TOO MUCH! BAILING!'
                 return
             if newmax <= beta * xmax : break
             
             xmax = newmax
             
             
         # phase III
         Tf = gamma * sum( T )
         epoch = sim.get_time()
         while sim.get_time() - epoch <= Tf :
             callback = sim.get_next_action()
             callback()
             
         RESULTS.finalT = Tf
         self.alarm()
         
         print 'SIMULATION DONE'
        self.roadmap = roadmap
        
    def __call__(self, orig, dest, length_attr='length' ) :
        path = minpath( orig, dest, self.roadmap, length_attr )
        return pathLength(path), RoadTrajectory(path)




if __name__ == '__main__' :
    from setiptah.roadgeometry.roadmap_basic import distance
    
    import setiptah.roadgeometry.probability as roadprob
    
    if True :
        roadmap = roadprob.sampleroadnet()
        p = roadprob.sampleaddress( roadmap )
        q = roadprob.sampleaddress( roadmap )
    
    # give me a summary of the road network
    for u,v,road, data in roadmap.edges_iter( keys=True, data=True ) :
        print '%s: %s -> %s ; length=%f' % ( road, repr(u), repr(v), data.get('length',1) )
        
    print '...going from %s to %s' % ( repr(p), repr(q) )
    frwd = minpath( p, q, roadmap )
    frwdL = pathLength( frwd )
    frwdLRef = distance( roadmap, p, q, 'length' )
    print frwd
    print 'FRWD LENGTH: %f; survey says: %f' % ( frwdL, frwdLRef )
    
    
示例#4
0
 def run(self) :
     """ setup """
     sim = Simulation()
     
     clock = PoissonClock( self.rate )
     clock.join_sim( sim )
     
     # prepare geometry queries
     if True :
         ORIGIN = np.zeros(2)
         
         def samplepoint() :
             return np.random.rand(2)
             
         planner = EuclideanPlanner
         #scheduler = RoundRobinScheduler()
         
         # Euclidean instantiation
         getTail = lambda dem : dem.origin
         getHead = lambda dem : dem.destination
         distance = lambda x, y : np.linalg.norm( y - x )
         scheduler = kCraneScheduler( getTail, getHead, distance )
         
     else :
         import setiptah.roadgeometry.roadmap_basic as ROAD
         import setiptah.roadgeometry.probability as roadprob
         from setiptah.roadgeometry.roadmap_paths import RoadmapPlanner
         
         roadmap = roadprob.sampleroadnet()
         U = roadprob.UniformDist( roadmap )
         samplepoint = U.sample
         
         ORIGIN = samplepoint()
         
         distance = lambda x, y : ROAD.distance( roadmap, 
                                                 x, y, length='length' )
         planner = RoadmapPlanner( roadmap )
         
         if True :
             scheduler = RoundRobinScheduler()
         else :
             getTail = lambda dem : dem.origin
             getHead = lambda dem : dem.destination
             scheduler = kCraneScheduler( getTail, getHead, distance )
     
     # instantiate the gate
     gate = GatedTaxiDispatch()
     gate.setScheduler( scheduler )
     
     # instantiate the fleet
     TAXI = []
     for i in range( self.numveh ) :
         taxi = Taxi()
         TAXI.append( taxi )
         
         taxi.setPlanner( planner )
         taxi.setLocation( ORIGIN )
         taxi.setSpeed( self.vehspeed )
         
         taxi.join_sim( sim )
         
         gateIF = gate.newInterface()
         gate.add( gateIF )
         gateIF.output.connect( taxi.appendDemands )
         taxi.signalWakeup.connect( gateIF.input )
         taxi.signalIdle.connect( gateIF.input )
         
     gate.join_sim( sim )
     
     def tick() :
         print 'tick, %g' % sim.get_time()
         
     def myarrival() :
         x = samplepoint()
         y = samplepoint()
         
         #print x, y
         time = sim.get_time()
         demand = Taxi.Demand( x, y, time )
         print 'demand generated ', x, y, time
         
         # send the demand to the gate, not any one taxi
         gate.queueDemand( demand )
         
     EVER = dumbcounter()
     
     clock.source().connect( myarrival )
     clock.source().connect( EVER.increment )
         
     RESULTS.ever_tape = []
     RESULTS.alive_tape = []
     
     def record() :
         RESULTS.ever_tape.append( EVER.value() )
         
         total = len( gate._demandQ )
         for taxi in TAXI :
             total += len( taxi._demandQ )
             
         RESULTS.alive_tape.append( total )
         
     probe = UniformClock(.1)        # why not
     probe.join_sim( sim )
     probe.source().connect( record )
     
     
     """ run """
     #T = 50.
     while sim.get_time() <= self.horizon :
         callback = sim.get_next_action()
         callback()
         
         frac = sim.get_time() / self.horizon
         perc = int( 100. * frac )
         #print perc
         
         self.timeElapsed.emit( perc )       # emit the custom signal?
         
     self.simulateDone.emit()