def common_action(agent, others_red, others_blue, op1, op2): changed = False num_others_red = max(len(others_red.subset(in_hood, agent, HOOD_SIZE)), NOT_ZERO) # prevent div by zero! num_others_blue = max(len(others_blue.subset(in_hood, agent, HOOD_SIZE)), NOT_ZERO) # prevent div by zero! total_others = num_others_red + num_others_blue if total_others <= 0: return False env_color = ratio_to_sin(num_others_red / total_others) agent[COLOR_PREF] = new_color_pref(agent[COLOR_PREF], env_color) if env_unfavorable(agent[DISPLAY_COLOR], agent[COLOR_PREF], op1, op2): changed = True agent[DISPLAY_COLOR] = not agent[DISPLAY_COLOR] change_color(agent, society, opp_group) return changed
def common_action(agent, others_red, others_blue, op1, op2, **kwargs): """ The actions for both followers and trendsetters """ execution_key = get_exec_key(kwargs=kwargs) num_others_red = len(others_red.subset(in_hood, agent, HOOD_SIZE)) num_others_blue = len(others_blue.subset(in_hood, agent, HOOD_SIZE)) total_others = num_others_red + num_others_blue if total_others <= 0: return False env_color = ratio_to_sin(num_others_red / total_others) agent[COLOR_PREF] = new_color_pref(agent[COLOR_PREF], env_color) if env_unfavorable(agent[DISPLAY_COLOR], agent[COLOR_PREF], op1, op2): change_color(agent, get_env(execution_key=execution_key), opp_group) return True else: return False
def test_ratio_to_sin(self): self.assertEqual(ratio_to_sin(0), 0) self.assertEqual(ratio_to_sin(1), 1) self.assertAlmostEqual(ratio_to_sin(.5), NEUTRAL)