def set_up(props=None): # testcase??? """ A func to set up run that can also be used by test code. """ init_props(MODEL_NAME, props, model_dir="ml") group = [] group.append( Composite(DEALER_GRP, {"color": BLUE}, member_creator=generate_dealer, num_members=get_prop('num_dealers', DEF_NUM_DEALER))) # can we put the testing period in to composite too? group.append( Composite(AVA_BUYER_GRP, {"color": RED}, member_creator=ava_cb, num_members=get_prop('num_buyers', DEF_NUM_BUYER))) group.append( Composite(SELINA_BUYER_GRP, {"color": RED}, member_creator=selina_cb, num_members=get_prop('num_buyers', DEF_NUM_BUYER))) Env(MODEL_NAME, height=get_prop('grid_height', DEF_HEIGHT), width=get_prop('grid_width', DEF_WIDTH), members=group)
def set_up(props=None): """ A func to set up run that can also be used by test code. """ global resource_holders global entrepreneurs pa = init_props(MODEL_NAME, props, model_dir="capital") entrepreneurs = Composite("Entrepreneurs", {"color": BLUE}, member_creator=create_entr, props=pa, num_members=pa.get('num_entr', DEF_NUM_ENTR)) resource_holders = Composite("Resource_holders", {"color": RED}, member_creator=create_rholder, props=pa, num_members=pa.get('num_rholder', DEF_NUM_RHOLDER)) Env("neighborhood", height=get_prop('grid_height', DEF_HEIGHT), width=get_prop('grid_width', DEF_WIDTH), members=[resource_holders, entrepreneurs]) return resource_holders, entrepreneurs
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 = get_prop('grid_height', DEF_HEIGHT, execution_key=execution_key) groups = [] for i in range(NUM_GROUPS): groups.append( Composite(str(i), {"marker": CIRCLE}, execution_key=execution_key)) for y in range(height): for x in range(width): groups[0] += create_grain(x, y, execution_key=execution_key) sandpile_env = Env(MODEL_NAME, action=sandpile_action, height=height, width=width, members=groups, attrs={ "size": 65, "hide_axes": True, "hide_legend": True }, random_placing=False, execution_key=execution_key) # these settings must be re-done every API re-load: set_env_attrs(execution_key) return sandpile_env, groups
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 groups = [] groups.append( Composite(BLUE_TSETTERS, {"color": NAVY}, execution_key=execution_key)) groups.append(Composite(RED_TSETTERS, {"color": DARKRED}, member_creator=create_tsetter, num_members=get_prop('num_tsetters', NUM_TSETTERS, execution_key=execution_key), execution_key=execution_key)) groups.append( Composite(RED_FOLLOWERS, {"color": RED}, execution_key=execution_key)) groups.append(Composite(BLUE_FOLLOWERS, {"color": BLUE}, member_creator=create_follower, num_members=get_prop('num_followers', NUM_FOLLOWERS, execution_key=execution_key), execution_key=execution_key)) Env(MODEL_NAME, members=groups, attrs={OPP_GROUP: opp_group}, execution_key=execution_key)
def set_up(props=None): """ A func to set up run that can also be used by test code. """ pa = init_props(MODEL_NAME, props, model_dir="capital") execution_key = int(props[EXEC_KEY].val) \ if props is not None else CLI_EXEC_KEY traders = Composite("Traders", member_creator=create_trader, props=pa, num_members=get_prop('num_traders', DEF_NUM_TRADERS, execution_key=execution_key), execution_key=execution_key) check_props(execution_key=execution_key) nature_to_traders(traders, natures_goods) 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=[traders], attrs={"goods": natures_goods}, pop_hist_setup=initial_amt, execution_key=execution_key) set_env_attrs(execution_key=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) exec_key = init_exec_key(props) groups = [] groups.append( Composite("value_investors", {"color": BLUE}, member_creator=create_value_investor, num_members=get_prop("value_investors", DEF_NUM_VALUE_INVESTOR, execution_key=exec_key), execution_key=exec_key)) groups.append( Composite("trend_followers", {"color": RED}, member_creator=create_trend_follower, num_members=get_prop("trend_followers", DEF_NUM_TREND_FOLLOWER, execution_key=exec_key), execution_key=exec_key), ) groups.append(create_market_maker(MARKET_MAKER)) Env(MODEL_NAME, members=groups, width=UNLIMITED, height=UNLIMITED, pop_hist_setup=initial_price, execution_key=exec_key) get_env(execution_key=exec_key).exclude_menu_item("scatter_plot") set_env_attrs(execution_key=exec_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 create_resident(name, i, group=BLUE, **kwargs): """ Creates agent of specified color type """ execution_key = get_exec_key(kwargs=kwargs) if group == BLUE: grp_idx = BLUE_GRP_IDX mean_tol = get_prop('mean_tol', DEF_TOLERANCE, execution_key=execution_key) else: grp_idx = RED_GRP_IDX mean_tol = -get_prop( 'mean_tol', DEF_TOLERANCE, execution_key=execution_key) dev = get_prop('deviation', DEF_SIGMA, execution_key=execution_key) this_tolerance = get_tolerance(mean_tol, dev) return Agent(name + str(i), action=seg_agent_action, attrs={ TOLERANCE: this_tolerance, GRP_INDEX: grp_idx, "hood_changed": True, "just_moved": False, "hood_size": get_prop('hood_size', DEF_HOOD_SIZE, execution_key=execution_key) }, execution_key=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 blue_group = Composite("Blues", {"color": BLUE}, member_creator=create_agent, num_members=get_prop('num_blue', DEF_NUM_BLUE, execution_key=execution_key), execution_key=execution_key) red_group = Composite("Reds", {"color": RED}, member_creator=create_agent, num_members=get_prop('num_red', DEF_NUM_RED, execution_key=execution_key), 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=[blue_group, red_group], execution_key=execution_key)
def create_rholder(name, i, props=None): """ Create an agent. """ k_price = DEF_K_PRICE resources = copy.deepcopy(DEF_CAP_WANTED) num_resources = len(resources) price_list = copy.deepcopy(DEF_EACH_CAP_PRICE) if props is not None: k_price = props.get('cap_price', DEF_K_PRICE) for k in price_list.keys(): price_list[k] = float("{0:.2f}".format( float(k_price * random.uniform(0.5, 1.5)))) starting_cash = DEF_RHOLDER_CASH if props is not None: starting_cash = get_prop('rholder_starting_cash', DEF_RHOLDER_CASH) if props is not None: total_resources = get_prop('rholder_starting_resource_total', DEF_TOTAL_RESOURCES_RHOLDER_HAVE) for k in resources.keys(): resources[k] = int( (total_resources * 2) * (random.random() / num_resources)) return Agent(name + str(i), action=rholder_action, attrs={ "cash": starting_cash, "resources": resources, "price": price_list })
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 blue_agents = Composite(group_names[BLUE_GRP_IDX], {"color": BLUE}, member_creator=create_resident, num_members=get_prop('num_blue', NUM_BLUE, execution_key=execution_key), group=BLUE, execution_key=execution_key) red_agents = Composite(group_names[RED_GRP_IDX], {"color": RED}, member_creator=create_resident, num_members=get_prop('num_red', NUM_RED, execution_key=execution_key), group=RED, execution_key=execution_key) city = Env(MODEL_NAME, members=[blue_agents, red_agents], 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) city.exclude_menu_item("line_graph")
def set_up(props=None): """ A func to set up run that can also be used by test code. """ init_props(MODEL_NAME, props) height = get_prop("grid_height", DEF_HEIGHT) width = get_prop("grid_width", DEF_WIDTH) simulation = get_prop("simulation", 0) black = Composite("Black", {"color": BLACK, "marker": SQUARE}) groups = [black] Env("Game of Life", action=gameoflife_action, height=height, width=width, members=groups, attrs={"size": 100, "change_grid_spacing": (0.5, 1), "hide_xy_ticks": True, "hide_legend": True}, random_placing=False) populate_board_dict[simulation](width, height) return groups
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 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 toxins = Composite(TOXINS, {"color": RED}, member_creator=create_toxin, num_members=get_prop('num_toxins', NUM_TOXINS, execution_key=execution_key), execution_key=execution_key) nutrients = Composite(NUTRIENTS, {"color": GREEN}, member_creator=create_nutrient, num_members=get_prop('num_nutrients', NUM_TOXINS, execution_key=execution_key), execution_key=execution_key) bacteria = Composite(BACTERIA, {"color": BLUE}, member_creator=create_bacterium, num_members=get_prop('num_toxins', DEF_NUM_BACT, execution_key=execution_key), 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=[toxins, nutrients, bacteria], execution_key=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) exec_key = init_exec_key(props) members = [] members.append( Composite(WOLF_GROUP, attrs={"color": TAN}, member_creator=create_wolf, num_members=get_prop('num_wolves', NUM_WOLVES, execution_key=exec_key), execution_key=exec_key)) members.append( Composite(SHEEP_GROUP, attrs={"color": GRAY}, member_creator=create_sheep, num_members=get_prop('num_sheep', NUM_SHEEP, execution_key=exec_key), execution_key=exec_key)) Env(MODEL_NAME, members=members, attrs={ "prey_dist": get_prop("prey_dist", PREY_DIST, execution_key=exec_key) }, height=get_prop('grid_height', MEADOW_HEIGHT, execution_key=exec_key), width=get_prop('grid_width', MEADOW_WIDTH, execution_key=exec_key), execution_key=exec_key)
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_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 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 num_members = get_prop('num_babysitter', DEF_BABYSITTER, execution_key=execution_key) co_op_members = Composite(CO_OP_MEMBERS, {"color": RED}, num_members=num_members, member_creator=create_babysitter, execution_key=execution_key) central_bank = Composite("central_bank", {"color": GREEN}, num_members=1, member_creator=create_central_bank, execution_key=execution_key) Env(MODEL_NAME, members=[co_op_members, central_bank], action=coop_action, width=UNLIMITED, height=UNLIMITED, pop_hist_setup=initial_exchanges, attrs={ "last_per_exchg": 0, "last_per_unemp": 0, "num_rounds": 0, "CB_interven_pts": [] }, execution_key=execution_key) set_env_attrs(execution_key=execution_key)
def check_props(**kwargs): """ A func to delete properties of goods in nature_goods dictionary if the user want to disable them. """ execution_key = get_exec_key(kwargs=kwargs) div = get_prop('divisibility', execution_key=execution_key) dua = get_prop('durability', execution_key=execution_key) trans = get_prop('transportability', execution_key=execution_key) for goods in natures_goods: if div == 0 and "divisibility" in natures_goods[goods]: del natures_goods[goods]["divisibility"] if dua == 0 and DUR in natures_goods[goods]: del natures_goods[goods][DUR] if trans == 0 and "transportability" in natures_goods[goods]: del natures_goods[goods]["transportability"]
def create_babysitter(name, i, **kwargs): """ Create a babysitter. """ execution_key = get_exec_key(kwargs=kwargs) babysitter = Agent(name + str(i), action=babysitter_action, execution_key=execution_key) mean_coupons = get_prop("average_coupons", DEF_COUPON, execution_key=execution_key) dev = get_prop("deviation", DEF_SIGMA, execution_key=execution_key) babysitter["goal"] = None babysitter['coupons'] = int(gaussian(mean_coupons, dev)) babysitter['desired_cash'] = get_prop("desired_cash", DEF_DESIRED_CASH_BAL, execution_key=execution_key) return babysitter
def is_isolated(agent, **kwargs): ''' Checks if agent is maintaining distancing. ''' execution_key = get_exec_key(kwargs=kwargs) return not exists_neighbor(agent, size=get_prop('distancing', DEF_DISTANCING, execution_key=execution_key), execution_key=execution_key)
def create_central_bank(name, i, **kwargs): """ Create the central bank to distribute the coupons """ execution_key = get_exec_key(kwargs=kwargs) central_bank = Agent(name, action=central_bank_action, execution_key=execution_key) central_bank["percent_change"] = get_prop("percent_change", DEF_PERCENT, execution_key=execution_key) central_bank["extra_coupons"] = get_prop("extra_coupons", DEF_COUPON, execution_key=execution_key) central_bank["extra_dev"] = get_prop("extra_deviation", DEF_SIGMA, execution_key=execution_key) return central_bank
def create_geneng(name, i, **kwargs): """ Create an agent. """ dur = get_prop("lifespan", DEF_DURATION) return Agent(name + str(i), action=geneng_action, duration=dur, attrs={"height": START_HEIGHT})
def create_person(name, i, state=HE, **kwargs): """ Create a new person! By default, they start out healthy. """ execution_key = get_exec_key(kwargs=kwargs) mask_chance = random() mask_bool = False if mask_chance < get_prop("mask_rate", DEF_MASK_RATE, execution_key=execution_key): mask_bool = True person = Agent(name + str(i), action=person_action, attrs={STATE: state, "angle": None, "max_move": get_prop('person_move', DEF_PERSON_MOVE, execution_key=execution_key), "is_wearing_mask": mask_bool}, execution_key=execution_key) return person
def create_boyfriend(name, i, props=None): """ Create an agent. """ return Agent(name + str(i), action=boyfriend_action, attrs={ "cycle": get_prop('cycle', DEF_CYCLE), "going": True })
def create_trend_follower(name, i, **kwargs): """ Create a trend follower. """ execution_key = get_exec_key(kwargs=kwargs) average_period = get_prop("average_period", DEF_PERIODS, execution_key=execution_key) dev = get_prop("deviation_follower", DEF_SIGMA, execution_key=execution_key) trend_follower = Agent(name + str(i), action=trend_follower_action, execution_key=execution_key) trend_follower["change_period"] = gaussian(average_period, dev) trend_follower["capital"] = DEF_CAPITAL trend_follower["num_stock"] = 0 return trend_follower
def set_up(props=None): """ A func to set up run that can also be used by test code. """ global max_utility pa = init_props(MODEL_NAME, props, model_dir="capital") trader_group = Composite("trader", {"color": BLUE}, member_creator=create_trader, props=pa, num_members=get_prop('num_traders', DEF_NUM_TRADER)) Env("env", height=get_prop('grid_height', DEF_HEIGHT), width=get_prop('grid_width', DEF_WIDTH), members=[trader_group]) for trader in trader_group: allocate_resources(trader_group[trader], GOODS) user_log_notif(trader_group[trader]["goods"]) return (trader_group, max_utility)
def set_up(props=None): """ A func to set up run that can also be used by test code. """ init_props(MODEL_NAME, props) geneng_group = Composite(GENENG, {"color": BLUE}, member_creator=create_geneng, num_members=get_prop('num_geneng', DEF_NUM_GENENG)) natural_group = Composite(NATURAL, {"color": RED}, member_creator=create_natural, num_members=get_prop('num_natural', DEF_NUM_NATURAL)) Env(MODEL_NAME, height=get_prop('grid_height', DEF_HEIGHT), width=get_prop('grid_width', DEF_WIDTH), members=[geneng_group, natural_group]) set_env_attrs()
def create_natural(name, i, **kwargs): """ Create an agent. """ dur = get_prop("lifespan", DEF_DURATION) height = gauss(START_HEIGHT, DEF_SIGMA) return Agent(name + str(i), action=natural_action, duration=dur, attrs={"height": height})
def create_nutrient(name, i, **kwargs): """ Create a nutrient. """ execution_key = get_exec_key(kwargs=kwargs) nutrient = Agent(name + str(i), action=nutrient_action, execution_key=execution_key) nutrient["max_move"] = get_prop("nutrient_move", DEF_NUTRIENT_MOVE, execution_key=execution_key) return nutrient