Ejemplo n.º 1
0
 recorder = UniformPoller( record_interval )
 unserviced_query = lambda : len( gate.demands ) + len( dispatch.demands )
 recorder.set_query( unserviced_query )
 recorder.join_sim( sim )
 
 def display() :
     n = len( recorder.tape )
     T = record_interval * np.arange( n )
     plt.plot( T, recorder.tape )
     plt.xlabel( 'Time elapsed' )
     plt.ylabel( 'Number of Demands Waiting' )
 
 
 """ run the simulation """
 while sim.get_time() < horizon :
     call = sim.get_next_action()
     call()
     
     
     
     
     
 """ COMPUTE STATISTICS """
 total_demands = len( ALL_DEMANDS )
 nleft = len( dispatch.demands )
 print 'arrival rate (simulated, observed): %f, %f' % ( arrivalrate, total_demands / horizon )
 
 #overrate_est = float( nleft - preload ) / horizon
 take_only_last = int( .25 * horizon )
 overrate_est = float( recorder.tape[-1] - recorder.tape[-take_only_last] ) / take_only_last
 overrate_est = float( recorder.tape[-1] ) / horizon
Ejemplo n.º 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'
Ejemplo n.º 3
0
 def run(self, experiment ) :
     """ setup """
     sim = Simulation()
     self.sim = sim
     
     clock = PoissonClock( experiment.arrivalrate )
     clock.join_sim( sim )
     
     # prepare domain planning
     distr = distribs.distributions[ experiment.distrib_key ]
     
     # prepare domain Stacker Crane scheduling --- could be made dynamic
     # cuz i'm dumb...
     getTail = lambda dem : dem.origin
     getHead = lambda dem : dem.destination
     
     
     
     # instantiate planner
     if isinstance( distr, EuclideanDistribution ) :
         planner = EuclideanPlanner
         
     elif isinstance( distr, RoadmapDistribution ) :
         planner = RoadmapPlanner( distr.roadmap )
         
     else :
         raise NotImplementedError('unrecognized distribution')
     
     # instantiate scheduler
     if False or isinstance( distr, EuclideanDistribution ) :
         scheduler = kCraneScheduler( getTail, getHead, distr.distance )
         
     elif isinstance( distr, RoadmapDistribution ) :
         from setiptah.taxitheory.roadmap.simulation import RoadMap_kCraneScheduler
         scheduler = RoadMap_kCraneScheduler( getTail, getHead, distr.roadmap )
         
     else :
         raise NotImplementedError('unrecognized distribution')
             
     
     
     # instantiate the gate
     gate = GatedTaxiDispatch()
     gate.setScheduler( scheduler )
     
     # instantiate the fleet
     ORIGIN = distr.origin()
     
     TAXI = []
     for i in range( experiment.numveh ) :
         taxi = Taxi()
         TAXI.append( taxi )
         
         taxi.setPlanner( planner )
         taxi.setLocation( ORIGIN )
         taxi.setSpeed( experiment.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()
         
         
     self.DEMANDS = []
     def myarrival() :
         demand = distr.sample()
         x = distr.getTail( demand )
         y = distr.getHead( demand )
         
         time = sim.get_time()
         demand = Taxi.Demand( x, y, time )
         print 'demand generated ', x, y, time
         
         self.DEMANDS.append( demand )
         # send the demand to the gate, not any one taxi
         gate.queueDemand( demand )
         
     clock.source().connect( myarrival )
     #clock.source().connect( EVER.increment )
     
     # should do post-hoc analysis... ignore this stuff
     # EDIT: do just for plotting
     self.ever_tape = []
     self.alive_tape = []
     
     def record() :
         self.ever_tape.append( len( self.DEMANDS ) )
         
         total = len( gate._demandQ )
         for taxi in TAXI :
             total += len( taxi._demandQ )
             
         self.alive_tape.append( total )
         
     probe = UniformClock(.1)        # why not
     probe.join_sim( sim )
     probe.source().connect( record )
     
     
     """ SIMULATE """
     if False :
         plt.close('all')
         self.fig = plt.figure()
         plt.show()
         
     self.batch_indices = []
     self.simUpdate()
     
     FIRSTBATCH = 50.
     MINBATCH = 20.
     CYCLESPERBATCH = 10.
     CEILINGRATIO = 1.01
     #
     ALPHA = .01
     QUOTA = 20
     # just *assume* we will not simulate above stability!!!
     # XTHRESH = 200       # this needs to be added!!!
     
     # phase I --- simulate base time
     # first, try to get approximately EN0 demands
     T0 = FIRSTBATCH / experiment.arrivalrate
     
     while sim.get_time() < T0 :
         callback = sim.get_next_action()
         callback()
     self.simUpdate()
     
     # phase II
     T = [ T0 ]
     
     quota = QUOTA
     batchmean = globalmean = np.mean( self.alive_tape )
     while quota > 0 :
         # try to get a "replacement", but never go for less that MINBATCH
         #target = max( batchmean, MINBATCH )
         target = max( CYCLESPERBATCH * globalmean, MINBATCH )
         Tk = target / experiment.arrivalrate
         
         epoch = sim.get_time()
         while sim.get_time() - epoch < Tk :
             callback = sim.get_next_action()
             callback()
             
         T.append( Tk )
         self.simUpdate()
         
         if False :
             # branch based on batchmean
             bstart = self.batch_indices[-2]
             batchmean = np.mean( self.alive_tape[bstart:] )
             if batchmean <= CEILINGRATIO * globalmean :
                 quota -= 1
             else :
                 pass
                 #quota = QUOTA
                 
         else :
             # branch based on globalmean, but quota in a row
             globalmean_next = np.mean( self.alive_tape )
             #if globalmean_next <= CEILINGRATIO * globalmean :
             if np.abs( globalmean_next - globalmean ) <= ALPHA * globalmean :
                 quota -= 1
             else :
                 quota = QUOTA
                 
             globalmean = globalmean_next
             
         
     # phase III
     # Tf = gamma * sum( T )
     Tf = 0.     # currently, no phase three
     epoch = sim.get_time()
     while sim.get_time() - epoch < Tf :
         callback = sim.get_next_action()
         callback()
         
     self.finalT = Tf
     self.simUpdate()
     
     
     print 'SIMULATION DONE'
Ejemplo n.º 4
0
        total = len( gate._demandQ )
        for taxi in TAXI :
            total += len( taxi._demandQ )
            
        alive_tape.append( total )
    
    probe = UniformClock(.1)
    probe.join_sim( sim )
    probe.source().connect( record )
    
    
    
    
    
    
    """ run """
    T = 50.
    while sim.get_time() <= T :
        callback = sim.get_next_action()
        callback()
    
    
    tocs = len( ever_tape )        # for example
    time = np.linspace(0,T, tocs )
    plt.step( time, ever_tape, time, alive_tape )
    plt.show()
    
    
    
    
Ejemplo n.º 5
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()