def pickle_model(self, model): """ Creates and serializes the pickleLight object based on previously defined properties. Parameters ---------- model : Model An instance of the model on which the current simulation is based. """ start = time.time() model_lite = Model(5) if self.pickle_schedule: model_lite.schedule.agents = model.schedule.agents.union( model.schedule.agents_to_schedule) for a in model_lite.schedule.agents: if a.__class__.__name__ == "CancerCell": # Can't pickle functions a.reactToDrug_ = None model_lite.schedule.helpers = [h for h in model.schedule.helpers if h.__class__.__name__ not in "ExitConditionWatcher"] if self.pickle_envs: model_lite.environments = copy.deepcopy(model.environments) env_start = time.time() for k, v in model_lite.environments.iteritems(): v.grid = dict(v.grid) env_end = time.time() print("Cloning environments took %s seconds" % str( env_end - env_start)) model_lite.output = model.output model_lite.properties = model.properties # Can't pickle functions model_lite.properties["agents"]["cancerCells"][ "drugReactFunction"] = None model_lite.current_epoch = model.current_epoch if self.prefix is None: target = "%s/epoch_%s.pickle" % (self.out_dir, model.current_epoch) else: target = "%s/%s_epoch_%s.pickle" % ( self.out_dir, self.prefix, model.current_epoch) with open(target, "wb") as output_file: pickle.dump(model_lite, output_file) end = time.time() print("Pickler lite took %s seconds" % str(end - start))
def generate_model(properties, numEpochs): """ Generates and sets up a model object for warburg investigation. Includes assigning properties, instantiating agents and environments, etc. Parameters ---------- properties : dict A dictionary of model properties, as generated by the generate_properties function numEpochs : number Number of epochs the model should run for Returns ------- Model A configured instance of the model object. """ model = Model(numEpochs) model.properties = properties xsize = ysize = zsize = model.properties["envSize"] # Adding environments ObjectGrid3D(model.properties["envNames"]["agentEnvName"], xsize, ysize, zsize, model) NumericalGrid3D(model.properties["envNames"]["oxygenEnvName"], xsize, ysize, zsize, model) NumericalGrid3D(model.properties["envNames"]["vegfEnvName"], xsize, ysize, zsize, model) NumericalGrid3D(model.properties["envNames"]["glucoseEnvName"], xsize, ysize, zsize, model) NumericalGrid3D(model.properties["envNames"]["drugEnvName"], xsize, ysize, zsize, model) hc = 0 tc = 0 tic = 0 # Adding agents for x in range(xsize): for y in range(ysize): for z in range(zsize): if x % 2 == z % 2: agent = HealthyCell(model) hc += 1 agent.add_agent_to_grid( model.properties["envNames"]["agentEnvName"], (x, y, z), model) model.schedule.agents.add(agent) else: for _ in range(model.properties["initialAgentSetup"] ["numEndothelialCells"]): agent = TipCell(model) tic += 1 agent.add_agent_to_grid( model.properties["envNames"]["agentEnvName"], (x, y, z), model) model.schedule.agents.add(agent) print("hc %s tc %s tic %s" % (hc, tc, tic)) for _ in range(model.properties["initialAgentSetup"]["numCancerCells"]): for x in range(8, 12): for y in range(8, 12): for z in range(8, 12): c = CancerCell(model) state = ["G1", "S", "G2", "M"][randint(0, 3)] state_length = \ model.properties["agents"]["baseCellCycleLength"][ state] progress_in_state = randint(0, state_length - 1) c.current_state = state c.progress_in_state = progress_in_state c.add_agent_to_grid( model.properties["envNames"]["agentEnvName"], (x, y, z), model) model.schedule.agents.add(c) # Adding helpers model.schedule.helpers.append(HeartbeatHelper()) model.schedule.helpers.append(GlucoseDiffusionHelper(model)) odh = OxygenDiffusionHelper(model) model.schedule.helpers.append(odh) model.schedule.helpers.append(VegfDiffusionHelper(model)) snapshot_interval = 10 model.schedule.helpers.append(AgentCounter(model)) model.schedule.helpers.append( CancerCellWatcher(model, distributionInterval=snapshot_interval)) model.schedule.helpers.append(VegfStimulusWatcher(model)) model.schedule.helpers.append(TumourVolumeWatcher(model)) model.schedule.helpers.append( OxygenConcentrationWatcher(model, interval=snapshot_interval)) model.schedule.helpers.append( GlucoseConcentrationWatcher(model, interval=snapshot_interval)) model.schedule.helpers.append( DeathCauseWatcher(model, interval=snapshot_interval)) model.schedule.helpers.append( ModelPicklerLite(model.properties["outDir"], pickle_every=400)) def num_agents_exit_condition(model): return len([ a for a in model.schedule.agents if a.__class__.__name__ == "CancerCell" ]) > 400000 def no_cancer_cells(model): return len([ c for c in model.schedule.agents if c.__class__.__name__ == "CancerCell" and not c.dead ]) == 0 model.schedule.helpers.append( ExitConditionWatcher([num_agents_exit_condition, no_cancer_cells])) return model
def step_epilogue(self, model): model.output["best_fitness_in_epochs"] \ .append(model.properties["best_fitness"]) epochs = 50 model = Model(epochs) xsize = ysize = 500 ObjectGrid2D("agent_env", xsize, ysize, model) target_position = (12, 12) model.properties = { "best_fitness": 0, # setting global best to 0 "best_position": (0, 0), "target_position": target_position } model.output = {"best_fitness_in_epochs": []} num_agents = 20 for _ in range(num_agents): agent_position = (randint(0, xsize), randint(0, ysize)) agent = PSOAgent(agent_position) model.schedule.agents.add(agent) agent.add_agent_to_grid("agent_env", agent_position, model) model.schedule.helpers.append(FitnessTrackerHelper())