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 )
""" simulation setup """ # construct the simulation blocks from simulation import Simulation from sources import RoadnetDemandSource from queues import GatedQueue, BatchNNeighDispatcher from servers import Vehicle sim = Simulation() source = RoadnetDemandSource( roadnet, rategraph ) 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 ) :