def test_prob_state_trans(self): global STATE_TRANS for i in range(REP_RAND_TESTS): new_state = prob_state_trans(0, STATE_TRANS) self.assertNotEqual(new_state, 2) self.assertNotEqual(new_state, 3) new_state = prob_state_trans(1, STATE_TRANS) self.assertEqual(new_state, 2) new_state = prob_state_trans(2, STATE_TRANS) self.assertNotEqual(new_state, 0) self.assertNotEqual(new_state, 1) new_state = prob_state_trans(3, STATE_TRANS) self.assertEqual(new_state, 0)
def tree_action(agent, **kwargs): """ This is what trees do each turn in the forest. """ execution_key = get_exec_key(kwargs=kwargs) old_state = agent["state"] if is_healthy(agent): if exists_neighbor(agent, pred=is_on_fire, execution_key=execution_key): if DEBUG2: user_log_notif("Setting nearby tree on fire!") agent["state"] = NF # if we didn't catch on fire above, do probabilistic transition: if old_state == agent["state"]: # we gotta do these str/int shenanigans with state cause # JSON only allows strings as dict keys agent["state"] = \ str(prob_state_trans(int(old_state), get_env_attr(TRANS_TABLE, execution_key=execution_key))) if DEBUG2: if agent["state"] == NF: user_log_notif("Tree spontaneously catching fire.") if old_state != agent["state"]: # if we entered a new state, then... env = get_env(execution_key=execution_key) group_map = get_env_attr(GROUP_MAP, execution_key=execution_key) if group_map is None: user_log_err("group_map is None!") return True agent.has_acted = True env.add_switch(agent, group_map[old_state], group_map[agent["state"]]) return True
def tree_action(agent): """ This is what trees do each turn in the forest. """ global on_fire old_state = agent["state"] if is_healthy(agent): nearby_fires = Composite(agent.name + "'s nearby fires") neighbors = agent.locator.get_moore_hood(agent) if neighbors is not None: nearby_fires = neighbors.subset(is_on_fire, agent) if len(nearby_fires) > 0: if DEBUG2: print("Setting nearby tree on fire!") agent["state"] = NF # if we didn't catch on fire above, do probabilistic transition: if old_state == agent["state"]: agent["state"] = prob_state_trans(old_state, STATE_TRANS) if old_state != agent["state"]: agent.has_acted = True agent.locator.add_switch(agent, group_map[old_state], group_map[agent["state"]]) return True
def test_prob_state_trans(self): STATE_TRANS = [ [.98, .02, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, .96, .04], [1.0, 0.0, 0.0, 0.0], ] for i in range(REP_RAND_TESTS): new_state = prob_state_trans(0, STATE_TRANS) self.assertNotEqual(new_state, 2) self.assertNotEqual(new_state, 3) new_state = prob_state_trans(1, STATE_TRANS) self.assertEqual(new_state, 2) new_state = prob_state_trans(2, STATE_TRANS) self.assertNotEqual(new_state, 0) self.assertNotEqual(new_state, 1) new_state = prob_state_trans(3, STATE_TRANS) self.assertEqual(new_state, 0)
def tree_action(agent): """ This is what trees do each turn in the forest. """ global on_fire old_state = agent["state"] if is_healthy(agent): nearby_fires = on_fire.subset(in_hood, agent, NEARBY) if len(nearby_fires) > 0: if DEBUG2: print("Setting nearby tree on fire!") agent["state"] = NF # if we didn't catch on fire above, do probabilistic transition: if old_state == agent["state"]: agent["state"] = prob_state_trans(old_state, STATE_TRANS) if old_state != agent["state"]: agent.locator.add_switch(agent, group_map[old_state], group_map[agent["state"]]) return True
def person_action(agent, **kwargs): """ This is what people do each turn in the epidemic. """ execution_key = get_exec_key(kwargs=kwargs) infec_dist = get_prop('infection_distance', DEF_INFEC_DIST, execution_key=execution_key) old_state = agent[STATE] if is_healthy(agent): distance_mod = 1 if agent["is_wearing_mask"]: distance_mod = 0.5 curr_reg = CircularRegion(center=agent.get_pos(), radius=(2 * infec_dist * distance_mod), execution_key=execution_key) sub_reg = curr_reg.create_sub_reg(center=agent.get_pos(), radius=(infec_dist * distance_mod), execution_key=execution_key) agent_list = sub_reg.get_agents(exclude_self=True) if (agent_list is not None and (len(agent_list) > 0)): if DEBUG2: user_log_notif("Exposing nearby people!") agent[STATE] = EX else: for curr_agent in curr_reg.get_agents(exclude_self=True): curr_distance = (distance(curr_agent, agent) - DEF_INFEC_DIST) if (curr_distance > infec_dist and curr_distance <= (infec_dist * 2)): inverse_square_val = ((1 / (curr_distance ** 2)) * distance_mod) if inverse_square_val > 0: r = random() if inverse_square_val / 100 > r: agent[STATE] = EX # if we didn't catch disease above, do probabilistic transition: if old_state == agent[STATE]: # we gotta do these str/int shenanigans with state cause # JSON only allows strings as dict keys agent[STATE] = str(prob_state_trans(int(old_state), STATE_TRANS)) if agent[STATE] == EX: user_log_notif("Person spontaneously catching virus.") if old_state != agent[STATE]: # if we entered a new state, then... group_map = get_env(execution_key=execution_key).get_attr(GROUP_MAP) if group_map is None: user_log_err("group_map is None!") return DONT_MOVE agent.has_acted = True get_env(execution_key=execution_key).add_switch(agent, group_map[old_state], group_map[ agent[STATE]]) if is_dead(agent): return DONT_MOVE if not is_isolated(agent, **kwargs): social_distancing(agent, **kwargs) return MOVE