def __init__(self, initial_time=0, factor=1.0, strict=True): Environment.__init__(self, initial_time) self.env_start = initial_time self.real_start = time() self._factor = factor self._strict = strict
def __init__(self, initial_time=0, factor=1.0, strict=True): Environment.__init__(self, initial_time) self.env_start = initial_time self.real_start = time() self.factor = factor """Scaling factor of the real-time.""" self.strict = strict """Running mode of the environment. :meth:`step()` will raise a
def step(self): """Waits until enough real-time has passed for the next event to happen. The delay is scaled according to the real-time :attr:`factor`. If the events of a time step are processed too slowly for the given :attr:`factor` and if :attr:`strict` is enabled, a :exc:`RuntimeError` is raised. """ evt_time = self.peek() if evt_time is Infinity: raise EmptySchedule() sim_delta = evt_time - self.env_start real_delta = time() - self.real_start delay = sim_delta * self.factor - real_delta if delay > 0: sleep(delay) elif self.strict and -delay > self.factor: # Events scheduled for time *t* may take just up to *t+1* # for their computation, before an error is raised. raise RuntimeError( 'Simulation too slow for real time (%.3fs).' % -delay) return Environment.step(self)
def step(self): """Process the next event after enough real-time has passed for the event to happen. The delay is scaled according to the real-time :attr:`factor`. With :attr:`strict` mode enabled, a :exc:`RuntimeError` will be raised, if the event is processed too slowly. """ evt_time = self.peek() if evt_time is Infinity: raise EmptySchedule() real_time = self.real_start + (evt_time - self.env_start) * self.factor if self.strict and time() - real_time > self.factor: # Events scheduled for time *t* may take just up to *t+1* # for their computation, before an error is raised. raise RuntimeError('Simulation too slow for real time (%.3fs).' % ( time() - real_time)) # Sleep in a loop to fix inaccuracies of windows (see # http://stackoverflow.com/a/15967564 for details) and to ignore # interrupts. while True: delta = real_time - time() if delta <= 0: break sleep(delta) return Environment.step(self)
def main(commuters=10, sim_max_time=700, train_capacity=800): env = Environment() data=[] train = Train(train_capacity, env) train.start_monitor(data) station_objs = convert_to_station_obj(TUBELINES['Jubilee'], env) train.initialise(station_objs.values()) commuters, stats = initialise_commuters( commuters, station_objs, env ) env.process(train.run()) for station in station_objs.itervalues(): env.process(station.train_arrives(train)) for commuter in commuters: env.process(commuter.request_train_space(train)) env.run(until=sim_max_time) print '****DATA*****', data