Exemplo n.º 1
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 )
     
     
     # policy setup
     #import setiptah.roadgeometry.probability as roadprob
     #roadmap = roadprob.sampleroadnet()
     #U = roadprob.UniformDist( roadmap )
     #samplepoint = U.sample
     # instantiate the gate
     gate = GatedTaxiDispatch()
     gate.setScheduler( RoadmapFHKScheduler( self.roadnet ) )
     self.gate = gate
     
     # source -> gate
     
     # fleet setup
     vehicles = {}
     self.vehicles = vehicles
     
     # fleet parameters
     from setiptah.roadgeometry.roadmap_paths import RoadmapPlanner
     planner = RoadmapPlanner( self.roadnet ) 
     roads = [ road for _,__,road in self.roadnet.edges(data=True) ]
     road = random.choice( roads )
     ORIGIN = ROAD.RoadAddress(road,0.)      # just... somewhere
     
     for i in range( self.numveh ) :
         taxi = Taxi()
         
         taxi.setPlanner( planner )
         randpoint = roadprob.sampleaddress( self.roadnet, 'length' )
         taxi.setLocation( randpoint )
         taxi.setSpeed( 1. )
         
         taxi.join_sim( sim )
         
         gateIF = gate.newInterface()
         gate.add( gateIF )
         
         vehicles[taxi] = gateIF
         gateIF.output.connect( taxi.appendDemands )
         
         taxi.signalWakeup.connect( gateIF.input )
         taxi.signalIdle.connect( gateIF.input )
         
     gate.join_sim( sim )
     
     
     # record number of unassigned demands
     self.tape = []
     def record_unassigned() :
         # first, count the demands waiting in the gate
         unassg = len( self.gate._demandQ )  #+ len( self.dispatch.demands )
         # then, count the demands in vehicle queues (minus possibly 1 being worked on)
         for veh in self.vehicles :
             temp = len( veh._demandQ )
             temp = max( temp, 1 ) - 1
             unassg += temp
         # recorder the number
         self.tape.append( unassg )
         
     recorder = UniformClock( 1. )
     recorder.join_sim( sim )
     recorder.output.connect( record_unassigned )
     self.recorder = recorder
     
     # report demand arrivals to several places
     self.DEMANDS = []
     def record_arrival( dem ) :
         self.DEMANDS.append( dem )
     
     def give_to_dispatch( dem ) :
         #p, q = dem
         taxidem = Taxi.Demand( *dem )
         self.gate.queueDemand( taxidem )
         
     source_out = source.source()
     #source_out.connect( counting.increment )
     source_out.connect( record_arrival )
     source_out.connect( give_to_dispatch )
     #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.requestRelease )
     #dispatch.request_batch.connect( gimme )
     #gate.output.connect( dispatch.batch_arrived )
     #gate_if.batch_out.connect( dispatch.batch_arrived )
     
     def hello( *args, **kwargs ) :
         print 'vehicle is ready!'
Exemplo 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() # because nearest neighbor "cheats" if the arrival rate is too high
     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 )
     
     # record number of unassigned demands
     self.tape = []
     def record_unassigned() :
         unassg = len( self.gate._queue ) + len( self.dispatch.demands )
         self.tape.append( unassg )
     recorder = UniformClock( 1. )
     recorder.join_sim( sim )
     recorder.output.connect( record_unassigned )
     self.recorder = recorder
     
     # 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.arrival( 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.requestRelease )
     dispatch.request_batch.connect( gimme )
     gate.output.connect( dispatch.batch_arrived )
     #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 )