def step(self, flush=True): self.sim_init() self.stepnum += 1 log.debug("Step: %d" % self.stepnum) # protocol.send(("step", self.stepnum)) sim = self.sim sim.before_step(self.stepnum) for step_method in self.steps: log.debug('calling steppable %r' % step_method) for steppable in self.steppables[step_method.im_class](): # log.debug('calling %r on %r' % (step_method, steppable)) if step_method.im_self is not None: if isinstance(step_method.im_self, sim.__class__): step_method() else: step_method(sim) elif isinstance(steppable, step_method.im_class): step_method(steppable, sim) sim.after_step(self.stepnum) if flush: protocol.flush(lambda x: ['frame', self.stepnum, x])
def run(self, steps=100): """ Run the simulation for N steps. Set to 0 to run endlessly. """ self.sim_init() log.info("Running simulation %s for %d steps..." % (self.sim.__class__.__name__, steps)) for n in zrange(steps): # TODO: get messages and stop if requested, otherwise: self.step(flush=False) protocol.flush(lambda x: ['frame', self.stepnum, x])
def do_sim(self, s): 'Access simulation parameters' field, _, value = s.partition(' ') sim = self.worker.sim if not field: protocol.push({k: getattr(sim, k) for k in sim._fields.keys()}) elif _hasattr(self.worker.sim, field): if value: _setattr(self.worker.sim, field, value) protocol.flush() else: attr = _getattr(self.worker.sim, field) if not hasattr(attr, '__call__'): protocol.push(attr) else: protocol.push(attr())
def __init__(self, simclass, simmodule=None): self.is_setup = False self.module = simmodule or sys.modules[simclass.__module__].__file__ self.simclass = simclass self.sim = simclass() log.info("Loading simulation %s" % simclass.__name__) # protocol.active = False protocol.active = True protocol.greet() # dirty hack to test the concept: protocol.send('sim name %s' % simclass.__name__) if simclass.__doc__: protocol.send(['sim', 'doc', simclass.__doc__]) protocol.flush(lambda x: ['preamble', x])
def sim_init(self, force=False): if force or not self.is_setup: self.sim.__init__() self.sim.setup() self.is_setup = True self.steppables = {} self.steps = [] self.stepnum = 0 # initialize steps if not self.sim.steps: raise Exception("Must specify simulation steps") else: for step in self.sim.steps: if isinstance(step, (list, tuple)): step, iterfun = step else: if inspect.ismethod(step) and step.im_self is not None: # a bounded method self.steppables[step.im_class] = [step.im_self].__iter__ self.steps.append(step) continue # raise Exception("Only unbounded methods can be steps") if issubclass(step.im_class, Agent): # multiple agent types supported, step() will filter iterfun = self.sim.agents.__iter__ elif issubclass(step.im_class, Cell): iterfun = self.sim.space.cells else: raise Exception('Unknown step class, must provide an iterator') self.steppables[step.im_class] = iterfun self.steps.append(step) log.debug("Steps: %s", self.steps) if self.sim.space: protocol.send('sim space grid'.split() + [self.sim.space.dimensions], ) protocol.flush(lambda x: ['preamble', x])