Example #1
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
Example #2
0
 def wrapper(*args, **kwargs):
     # args[0] is self!
     if args[0].pos is None:
         user_log_err("Using the pos of an unlocated agent: " +
                      args[0].name + " in function " + fn.__name__)
         return 0
     return fn(*args, **kwargs)
Example #3
0
def split(agent1, agent2):
    """
    Break connection between agent1 and agent2.
    """
    if not is_composite(agent1):
        user_log_err("Attempt to remove " + str(agent2) + " from non-group " +
                     str(agent1))
        return False
    else:
        agent1.del_member(agent2)
        agent2.del_group(agent1)
        return True
Example #4
0
def join(agent1, agent2):
    """
    Create connection between agent1 and agent2.
    """
    if not is_composite(agent1):
        user_log_err("Attempt to place " + str(agent2) + " in non-group " +
                     str(agent1))
        return False
    else:
        if not agent1.add_member(agent2):
            user_log_notif("Could not add mbr " + str(agent2) + " to " +
                           str(agent1))
        if not agent2.add_group(agent1):
            user_log_notif("Could not add grp " + str(agent2) + " to " +
                           str(agent1))
        return True
Example #5
0
def user_log_err(msg):
    return reg.user_log_err(msg)
Example #6
0
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