Beispiel #1
0
def central_bank_action(agent, **kwargs):
    """
    If exchanges are down "enough", distribute coupons!
    Enough is a parameter.
    """
    execution_key = get_exec_key(kwargs=kwargs)
    CB_interven_pts = get_env_attr("CB_interven_pts",
                                   execution_key=execution_key)
    set_env_attr("num_rounds",
                 get_env_attr("num_rounds", execution_key=execution_key) + 1,
                 execution_key=execution_key)
    co_op_mbrs = get_group(CO_OP_MEMBERS, execution_key=execution_key)
    unemp_rate = get_env_attr("last_per_unemp",
                              execution_key=execution_key,
                              default_value=0) / len(co_op_mbrs) * 100
    unemp_threshold = agent["percent_change"]
    if unemp_rate >= unemp_threshold:
        user_tell("Unemployment has risen to " + str(unemp_rate) +
                  " more than default value " + str(unemp_threshold) +
                  " CB Intervened")
        CB_interven_pts.append([
            get_env_attr("num_rounds", execution_key=execution_key),
            get_env_attr("last_per_exchg", execution_key=execution_key)
        ])
        distribute_coupons(agent, execution_key=execution_key)
    return True
Beispiel #2
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
Beispiel #3
0
def check_for_new_agents(agent):
    curr_x = agent.get_x()
    curr_y = agent.get_y()
    for y in ([-1, 0, 1]):
        for x in ([-1, 0, 1]):
            if (x != 0) or (y != 0):
                new_x = curr_x + x
                new_y = curr_y + y
                potential_new_agent = get_env().get_agent_at(new_x, new_y)
                if potential_new_agent is None:
                    if apply_dead_rules(new_x, new_y):
                        get_env_attr("to_come_alive").append((new_x, new_y))
Beispiel #4
0
def wolfram_action(wolf_env, **kwargs):
    """
    The action that will be taken every period.
    """
    execution_key = kwargs[EXEC_KEY]

    row_above_idx = get_env_attr("prev_row_idx", execution_key=execution_key)

    active_row_idx = wolf_env.height - wolf_env.get_periods()

    wolf_env.user.tell("Checking agents in row {} against the rule {}".format(
        active_row_idx, get_env_attr("rule_num", execution_key=execution_key)))

    if active_row_idx < 1:
        gone_far_enough()
        return DONT_MOVE
    else:
        next_row_idx = active_row_idx - 1

        next_row = wolf_env.get_row_hood(next_row_idx)

        left_color = \
            agent_color(x=0, y=active_row_idx, execution_key=execution_key)

        x = 0

        row_above = wolf_env.get_row_hood(row_above_idx)

        for agent_nm in row_above:
            if DEBUG:
                print("Checking agent at", row_above[agent_nm])
            if (x > 0) and (x < wolf_env.width - 1):
                middle_color = agent_color(agent_nm=agent_nm,
                                           execution_key=execution_key)
                right_color = agent_color(x=x + 1,
                                          y=active_row_idx,
                                          execution_key=execution_key)
                if DEBUG:
                    print("  Left: %d, middle: %d, right: %d" %
                          (left_color, middle_color, right_color))
                if next_color(
                        get_env_attr("rule_dict", execution_key=execution_key),
                        left_color, middle_color, right_color):
                    wolf_env.add_switch(
                        next_row[agent_nm_from_xy(x, next_row_idx)],
                        get_group(WHITE, execution_key),
                        get_group(BLACK, execution_key))
                left_color = middle_color
            x += 1

    set_env_attr("prev_row_idx", next_row_idx, execution_key=execution_key)
    return DONT_MOVE
Beispiel #5
0
def game_agent_action(agent, **kwargs):
    """
    The action that will be taken every period for the agents.
    Checks its Moore neighborhood and checks the number of neighbors to
        the rules of Game of Life.
    """

    if get_env_attr("reset_lists"):
        reset_env_attrs()

    check_for_new_agents(agent)
    if live_or_die(agent) == DIE:
        get_env_attr("to_die").append(agent)
    return True
Beispiel #6
0
def create_bb(name):
    """
    Create a big box store.
    """
    bb_book = {"expense": bb_expense,
               "capital": get_env_attr("bb_capital")}
    return Agent(name=name, attrs=bb_book, action=bb_action)
Beispiel #7
0
def get_util(store):
    """
    Get utility depending on the store type.
    """
    if store.primary_group() == get_group(BIG_BOX):
        return calc_util(store)
    else:
        return calc_util(store) + get_env_attr("mp_pref")
Beispiel #8
0
def record_exchanges(pop_hist, **kwargs):
    """
    This is our hook into the env to record the number of exchanges each
    period.
    """
    execution_key = get_exec_key(kwargs=kwargs)
    pop_hist.record_pop(
        "Exchanges", get_env_attr("last_per_exchg",
                                  execution_key=execution_key))
Beispiel #9
0
def get_prey(agent, sheep, **kwargs):
    """
        Wolves eat active sheep from the neighbourhood
    """
    exec_key = get_exec_key(kwargs=kwargs)
    if len(get_group(SHEEP_GROUP, exec_key)) <= 0:
        raise NoSheep("All out of sheep!")

    return get_env(execution_key=exec_key) \
        .get_neighbor_of_groupX(agent,
                                SHEEP_GROUP,
                                hood_size=get_env_attr(
                                    "prey_dist",
                                    execution_key=exec_key))
Beispiel #10
0
def gameoflife_action(biosphere, **kwargs):
    """
    The action that will be taken every period for the enviornment.
    Loops through the list of agents that has to come alive and die
        and carries out the corresponding action.
    """
    b = get_group(BLACK, get_exec_key(kwargs))
    # for the next loop, why not use the womb?
    for agent_pos in get_env_attr("to_come_alive"):
        if DEBUG:
            print("Agent at", agent_pos, "will come alive")
        if biosphere.get_agent_at(agent_pos[X], agent_pos[Y]) is None:
            agent = create_game_cell(agent_pos[X], agent_pos[Y])
            b += agent
            biosphere.place_member(agent, xy=(agent_pos[X], agent_pos[Y]))
    for agent in get_env_attr("to_die"):
        if not isinstance(agent, tuple):
            if DEBUG:
                print("Agent at", get_env_attr("to_die[agent]"), "will die")
            agent.die()
            # we shouldn't need to do the following:
            b.del_member(agent)
    return True
Beispiel #11
0
def drinker_action(agent, **kwargs):
    execution_key = get_exec_key(kwargs=kwargs)

    drinkers = get_group(DRINKERS, execution_key=execution_key)
    non_drinkers = get_group(NON_DRINKERS, execution_key=execution_key)

    changed = True
    decision = get_decision(agent)
    bar = get_env(execution_key=execution_key)
    bar.attrs[AGENTS_DECIDED] += 1
    attendance = get_env_attr(key=ATTENDANCE, execution_key=execution_key,
                              default_value=0)
    opt_occupancy = get_env_attr(OPT_OCCUPANCY, execution_key=execution_key)
    agents_decided = get_env_attr(AGENTS_DECIDED, execution_key=execution_key)
    if agents_decided == get_env_attr(POPULATION, execution_key=execution_key,
                                      default_value=0):
        if attendance > opt_occupancy:
            extras = attendance - opt_occupancy
            discourage(extras, **kwargs)
        set_env_attr(AGENTS_DECIDED, 0, execution_key=execution_key)
        set_env_attr(ATTENDANCE, 0, execution_key=execution_key)

    if decision:
        set_env_attr(ATTENDANCE, attendance + 1, execution_key=execution_key)
        if agent.primary_group() == non_drinkers:
            changed = False
            get_env(execution_key=execution_key).add_switch(agent,
                                                            non_drinkers,
                                                            drinkers)
    else:
        if agent.primary_group() == drinkers:
            changed = False
            get_env(execution_key=execution_key).add_switch(agent, drinkers,
                                                            non_drinkers)

    # return False means to move
    return changed