def runN(self, periods=DEF_TIME): """ Run our model for N periods. Return the total number of actions taken. """ acts = 0 for i in range(periods): # before members act, give birth to new agents # we will have tuple of agent and group # do group += agent if self.womb is not None: for (agent, group) in self.womb: join(group, agent) del self.womb[:] if self.switches is not None: for (agent, grp1, grp2) in self.switches: switch(agent, grp1, grp2) del self.switches[:] for mbr in self.pop_hist.pops: if mbr in self.members and self.is_mbr_comp(mbr): self.pop_hist.record_pop(mbr, self.pop_count(mbr)) else: self.pop_hist.record_pop(mbr, 0) curr_acts = super().__call__() acts += curr_acts return acts
def test_switch_groups(self): """ This test is here rather than in agent because it requires Composite! """ switch(self.hardy, self.camb, self.calc) self.assertTrue(str(self.hardy) not in self.camb) self.assertTrue(str(self.hardy) in self.calc)
def test_switch_groups(self): """ Test switching groups. This test is here rather than in agent because it requires Composite! """ switch(self.hardy.name, self.camb.name, self.calc.name) self.assertIn(str(self.hardy), self.calc)
def now_switch(self, agent, from_grp, to_grp): """ Switches the groups of the agent now instead of at the end of period unlike add_switch. """ switch(agent.name, from_grp.name, to_grp.name) self.num_switches += 1
def test_change_color(self): """ Test changing an agent's color. """ change_color(self.test_follower, fshn.society, fshn.opp_group) self.assertEqual(len(fshn.society.switches), 1) (agent, from_grp, to_grp) = fshn.society.switches[0] self.assertEqual(str(agent), TEST_FNAME) self.assertEqual(str(from_grp), BLUE_FOLLOWERS) self.assertEqual(str(to_grp), RED_FOLLOWERS) switch(agent, from_grp, to_grp) self.assertEqual(agent.primary_group(), to_grp)
def set_up(props=None): """ A func to set up run that can also be used by test code. """ init_props(MODEL_NAME, props) execution_key = int(props[EXEC_KEY].val) \ if props is not None else CLI_EXEC_KEY width = get_prop('grid_width', DEF_WIDTH, execution_key=execution_key) height = (width // 2) + (width % 2) groups = [ Composite(WHITE, {"color": WHITE}, execution_key=execution_key), Composite(BLACK, { "color": BLACK, "marker": SQUARE }, execution_key=execution_key) ] for y in range(height): for x in range(width): groups[W] += create_wolf_cell(x, y, execution_key) wolfram_env = Env(MODEL_NAME, action=wolfram_action, height=height, width=width, members=groups, attrs={ "size": 50, "hide_grid_lines": True, "hide_legend": True }, random_placing=False, execution_key=execution_key) rule_num = get_prop('rule_number', DEF_RULE, execution_key=execution_key) wolfram_env.set_attr("rule_num", rule_num) wolfram_env.set_attr("rule_dict", get_rule(rule_num)) wolfram_env.exclude_menu_item("line_graph") ''' This switch needs to happen before the environment is executed. Using add switch doesn't process the switch until after the environment is executed which breaks the model. ''' top_center_agent = \ wolfram_env.get_agent_at(width // 2, top_row(execution_key)) switch(top_center_agent.name, WHITE, BLACK, execution_key=execution_key) # top row is the "previous" because we just processed it set_env_attr("prev_row_idx", top_row(execution_key), execution_key=execution_key)
def change_group(agent, sandpile, curr_group_idx, next_group_idx): # noqa F811 """ Change group from current group index passed in to the next group index passed in """ switch(agent, groups[curr_group_idx], groups[next_group_idx])
def handle_switches(self): if self.switches is not None: for (agent_nm, from_grp_nm, to_grp_nm) in self.switches: switch(agent_nm, from_grp_nm, to_grp_nm, self.execution_key) self.num_switches += 1 self.switches.clear()
def handle_switches(self): if self.switches is not None: for (agent_nm, from_grp_nm, to_grp_nm) in self.switches: switch(agent_nm, from_grp_nm, to_grp_nm) self.num_switches += 1 del self.switches[:]