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 calm = Composite(CALM, {"color": BLUE}, member_creator=create_resident, num_members=get_prop('num_people', NUM_CALM_AGENTS, execution_key=execution_key), group=BLUE, execution_key=execution_key) panic = Composite(PANIC, {"color": RED}, member_creator=create_resident, num_members=NUM_PANIC_AGENTS, group=RED, execution_key=execution_key) city = Env(MODEL_NAME, members=[calm, panic], height=get_prop('grid_height', DEF_CITY_DIM, execution_key=execution_key), width=get_prop('grid_width', DEF_CITY_DIM, execution_key=execution_key), execution_key=execution_key) set_env_attr(STATES, [CALM, PANIC], execution_key=execution_key) city.exclude_menu_item("line_graph")
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
def set_up(props=None): """ A func to set up a 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 forest_height = get_prop('grid_height', DEF_DIM, execution_key=execution_key) forest_width = get_prop('grid_width', DEF_DIM, execution_key=execution_key) new_fire = get_prop('new_fire', DEF_NEW_FIRE, execution_key=execution_key) set_trans(state_trans, HE, NF, float(new_fire), HE) forest_density = get_prop('density', DEF_DENSITY, execution_key=execution_key) tree_cnt = int(forest_height * forest_width * forest_density) groups = [] groups.append( Composite(HEALTHY, { "color": GREEN, "marker": TREE }, member_creator=plant_tree, num_members=tree_cnt, execution_key=execution_key)) groups.append( Composite(NEW_FIRE, { "color": TOMATO, "marker": TREE }, execution_key=execution_key)) groups.append( Composite(ON_FIRE, { "color": RED, "marker": TREE }, execution_key=execution_key)) groups.append( Composite(BURNED_OUT, { "color": BLACK, "marker": TREE }, execution_key=execution_key)) groups \ .append(Composite(NEW_GROWTH, {"color": SPRINGGREEN, "marker": TREE}, execution_key=execution_key)) Env(MODEL_NAME, height=forest_height, width=forest_width, members=groups, execution_key=execution_key) # the next set should just be done once: set_env_attr(TRANS_TABLE, state_trans, execution_key=execution_key) # whereas these settings must be re-done every API re-load: set_env_attrs(execution_key=execution_key)
def set_env_attrs(execution_key=CLI_EXEC_KEY): user_log_notif("Setting env attrs for " + MODEL_NAME) set_env_attr(GROUP_MAP, {HE: HEALTHY, EX: EXPOSED, IN: INFECTED, CN: CONTAGIOUS, DE: DEAD, IM: IMMUNE}, execution_key) set_env_attr("census_func", epidemic_report, execution_key)
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 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
def set_env_attrs(execution_key=CLI_EXEC_KEY): """ I actually don't think we need to do this here! It can be done once in set_up(). """ user_log_notif("Setting env attrs for forest fire.") set_env_attr(GROUP_MAP, { HE: HEALTHY, NF: NEW_FIRE, OF: ON_FIRE, BO: BURNED_OUT, NG: NEW_GROWTH }, execution_key)
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 population_prop = \ get_prop('population', DEF_POPULATION, execution_key=execution_key) drinkers = Composite(DRINKERS, {"color": RED}, member_creator=create_drinker, num_members=population_prop // 2, execution_key=execution_key) non_drinkers = Composite(NON_DRINKERS, {"color": BLUE}, member_creator=create_non_drinker, num_members=population_prop // 2, execution_key=execution_key) Env(MODEL_NAME, height=get_prop('grid_height', DEF_HEIGHT, execution_key=execution_key), width=get_prop('grid_width', DEF_WIDTH, execution_key=execution_key), members=[drinkers, non_drinkers], pop_hist_setup=setup_attendance, execution_key=execution_key) population = len(drinkers) + len(non_drinkers) set_env_attr(POPULATION, population, execution_key=execution_key) set_env_attr(OPT_OCCUPANCY, int(population * DEF_MOTIV), execution_key=execution_key) set_env_attr(AGENTS_DECIDED, 0, execution_key=execution_key) set_env_attr(ATTENDANCE, 0, execution_key=execution_key) set_env_attrs(execution_key=execution_key)
def coop_action(coop_env, **kwargs): execution_key = get_exec_key(kwargs=kwargs) sitters = get_sitters(get_group(CO_OP_MEMBERS, execution_key=execution_key)) going_out = get_going_out( get_group(CO_OP_MEMBERS, execution_key=execution_key)) exchanges = min(len(sitters), len(going_out)) sitter_agents = [agent for agent in sitters] going_out_agents = [agent for agent in going_out] for i in range(exchanges): sitter, outer = sitter_agents[i], going_out_agents[i] sitters[sitter]['coupons'] += 1 going_out[outer]['coupons'] -= 1 set_env_attr("last_per_exchg", exchanges, execution_key=execution_key) set_env_attr("last_per_unemp", max(len(sitters), len(going_out)) - exchanges, execution_key=execution_key) return True
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
def set_env_attrs(execution_key=CLI_EXEC_KEY): user_log_notif("Setting env attrs for " + MODEL_NAME) set_env_attr("pop_hist_func", record_price, execution_key=execution_key) set_env_attr("census_func", market_report, execution_key=execution_key)
def set_env_attrs(): user_log_notif("Setting env attrs for " + MODEL_NAME) set_env_attr("census_func", height_rpt)
def set_env_attrs(execution_key=CLI_EXEC_KEY): user_log_notif("Setting env attrs for " + MODEL_NAME) set_env_attr("pop_hist_func", attendance, execution_key) set_env_attr("census_func", attendance_report, execution_key)
def set_env_attrs(execution_key=CLI_EXEC_KEY): set_env_attr("pop_hist_func", record_amt, execution_key) set_env_attr("census_func", trade_report, execution_key) tu.max_utils = MONEY_MAX_UTIL
def reset_env_attrs(): set_env_attr("to_come_alive", []) set_env_attr("to_die", []) set_env_attr("reset_lists", False)