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 )
addr = ROAD.RoadAddress( road, roadlen ) subres = self._expand_address( addr, roadnet, length ) res.extend( subres ) return res if __name__ == '__main__' : import roadgeometry.probability as roadprob roadnet = roadprob.sampleroadnet() n = 100 points = [ roadprob.sampleaddress( roadnet ) for i in range(n) ] pset = PointSet() for p in points : pset.insert( p ) def find_nearest( addr ) : dist_to = lambda q : ROAD.distance( roadnet, addr, q, 'length' ) trips = [ ( dist_to(q), q ) for q in points ] return min( trips )[1] def sidebyside( addr ) : by_pset = pset.find_nearest( addr, roadnet ) by_naive = find_nearest( addr )
""" 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 ALL_DEMANDS = [] def record_arrival( dem ) : ALL_DEMANDS.append( dem ) def give_to_dispatch( dem ) : p, q = dem loc = p #dispatch.demand_arrived( dem, loc ) gate.demand_arrived( dem )