Ejemplo n.º 1
0
 source.join_sim( sim )
 
 gate = GatedQueue()
 gate.join_sim( sim )
 
 dispatch = BatchNNeighDispatcher()
 # dispatch = NNeighDispatcher()
 dispatch.set_environment( roadnet )
 dispatch.join_sim( sim )
 
 """ add some demands to jump-start the simulation """
 preload = 0
 distr = {}
 for road1, road2, rate_data in normrategraph.edges_iter( data=True ) :
     distr[(road1,road2)] = rate_data.get( 'rate', 0. )
 bonus_demands = [ roadprob.samplepair( roadnet, distr ) for i in range(preload) ]
 for p, q in bonus_demands : gate.demand_arrived( (p,q) )
 """ end cheats """
 
 vehicles = {}
 for k in range( numveh ) :
     veh = Vehicle() ; vehicles[ veh ] = None
     veh.set_environment( f_dist )
     veh.set_speed( vehspeed )
     randpoint = roadprob.sampleaddress( roadnet, 'length' )
     veh.set_location( randpoint )
     veh.join_sim( sim )
 
 # connect components in simulation schematic
 """ source output connections """    
 # report demand arrivals to several places
Ejemplo n.º 2
0
 def sim_init( self ) :
     """ simulation setup """
     # construct the simulation blocks
     sim = Simulation()
     self.sim = sim
     
     if False :
         script = sample_demands( self.horizon, self.rategraph, self.roadnet )
         source = ScriptSource( script )
         print 'obtained %d demands' % len( source.script )
         debug_input()
     else :
         source = RoadnetDemandSource( self.roadnet, self.rategraph )
         
     self.source = source
     source.join_sim( sim )
     
     gate = GatedQueue()
     self.gate = gate
     gate.join_sim( sim )
     
     dispatch = BatchNNeighDispatcher()
     self.dispatch = dispatch
     dispatch.set_environment( roadnet )
     dispatch.join_sim( sim )
     
     """ add some demands to jump-start the simulation """
     preload = 0
     distr = {}
     for road1, road2, rate_data in normrategraph.edges_iter( data=True ) :
         distr[(road1,road2)] = rate_data.get( 'rate', 0. )
     bonus_demands = [ roadprob.samplepair( roadnet, distr ) for i in range(preload) ]
     for p, q in bonus_demands : gate.demand_arrived( (p,q) )
     """ end cheats """
     
     vehicles = {}
     self.vehicles = vehicles
     for k in range( self.numveh ) :
         veh = Vehicle() ; vehicles[ veh ] = None
         veh.set_environment( self.distance )
         veh.set_speed( self.vehspeed )
         randpoint = roadprob.sampleaddress( roadnet, 'length' )
         veh.set_location( randpoint )
         veh.join_sim( sim )
     
     # make a recorder
     recorder = UniformPoller( 1. )
     self.recorder = recorder
     def unassigned_query() :
         return len( self.gate.demands ) + len( self.dispatch.demands )
     recorder.set_query( unassigned_query )
     recorder.join_sim( sim )
     
     # report demand arrivals to several places
     self.DEMANDS = []
     def record_arrival( dem ) :
         self.DEMANDS.append( dem )
     
     def give_to_dispatch( dem ) :
         p, q = dem
         loc = p
         #dispatch.demand_arrived( dem, loc )
         self.gate.demand_arrived( dem )
         
     source_out = source.source()
     #source_out.connect( counting.increment )
     source_out.connect( give_to_dispatch )
     source_out.connect( record_arrival )
     #source.source().connect( give_to_dispatch )
     
     self.timer = data()
     self.timer.last_time = sim.time
     def say( dem ) :
         p, q = dem
         new_time = self.sim.time
         elapsed = new_time - self.timer.last_time
         print 'tick, %f: %s, %s' % ( elapsed, repr(p), repr(q) )
         self.timer.last_time = new_time
         
     source_out.connect( say )
     
     
     def gimme( *args, **kwargs ) :
         print "need a batch!!"
         
     # creates an interface from gate to interactive dispatcher
     gate_if = gate.spawn_interface()
     self.gate_if = gate_if
     dispatch.request_batch.connect( gate_if.request_in )
     dispatch.request_batch.connect( gimme )
     gate_if.batch_out.connect( dispatch.batch_arrived )
     
     def hello( *args, **kwargs ) :
         print 'vehicle is ready!'
     
     # vehicle signal connections
     for veh in vehicles :
         vehconn = dispatch.spawn_interface() ; vehicles[veh] = vehconn
         veh.ready.connect( vehconn.request_in )
         veh.ready.connect( hello )
         #veh.ready.connect( vehconn.request_in )
         vehconn.demand_out.connect( veh.receive_demand )
Ejemplo n.º 3
0
         v = random.choice( nodes )
         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) ]