def populate_board(patterns, pattern_num, execution_key=CLI_EXEC_KEY): agent_locs = patterns[pattern_num] b = get_group(BLACK, execution_key) for loc in agent_locs: agent = create_game_cell(loc[X], loc[Y]) b += agent get_env().place_member(agent, xy=loc)
def check_for_new_agents(agent): global to_come_alive if DEBUG: print("checking_for_new_agents!") 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 DEBUG: print("=================") print("Check for new agents at: ", (new_x, new_y)) print("apply_dead_rules check: ", apply_dead_rules(new_x, new_y)) print("Checking (16,16): ", get_env().get_agent_at(16, 16)) if (get_env().get_agent_at(16, 16) is not None): print("Checkgin (16,16) group: ", get_env().get_agent_at(16, 16) .primary_group()) print("=================") if apply_dead_rules(new_x, new_y): to_come_alive.append((new_x, new_y)) if DEBUG2: print("to come alive being append to:(x,y)", (new_x, new_y)) return to_come_alive
def main(): set_up() run_notice(MODEL_NAME) # get_env() returns a callable object: get_env()() return 0
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 populate_board_tumbler(width, height): center = [width // 2, height // 2] # this code is clearly awful: we must re-code this # per the mathematical pattern underlying this crap b = get_group(BLACK) agent_loc = [(center[0] - 1, center[1]), (center[0] - 2, center[1]), (center[0] + 1, center[1]), (center[0] + 2, center[1]), (center[0] - 1, center[1] - 1), (center[0] - 2, center[1] - 1), (center[0] + 1, center[1] - 1), (center[0] + 2, center[1] - 1), (center[0] - 1, center[1] - 2), (center[0] - 1, center[1] - 3), (center[0] - 1, center[1] - 4), (center[0] + 1, center[1] - 2), (center[0] + 1, center[1] - 3), (center[0] + 1, center[1] - 4), (center[0] - 3, center[1] - 3), (center[0] - 3, center[1] - 4), (center[0] - 3, center[1] - 5), (center[0] - 2, center[1] - 5), (center[0] + 3, center[1] - 3), (center[0] + 3, center[1] - 4), (center[0] + 3, center[1] - 5), (center[0] + 2, center[1] - 5)] for loc in agent_loc: agent = create_game_cell(loc[0], loc[1]) b += agent get_env().place_member(agent, xy=loc)
def gone_far_enough(): get_env().user.error_message["run"] = (' '.join([ "You have reached the ", "maximum height ", "and cannot run the model ", "for more periods.\n", "Please pick one of the ", "other options." ])) get_env().exclude_menu_item("run")
def populate_board_small_exploder(width, height, execution_key=CLI_EXEC_KEY): center = [width // 2, height // 2] agent_loc = get_agent_rules("small_exploder", center) b = get_group(BLACK, execution_key) for loc in agent_loc: agent = create_game_cell(loc[X], loc[Y]) b += agent get_env().place_member(agent, xy=loc)
def main(): set_up() if DEBUG.debug: get_env().user.tell(get_env().__repr__()) get_env()() return 0
def record_amt(pop_hist, execution_key=CLI_EXEC_KEY): """ This is our hook into the env to record the number of trades each period. """ get_env(execution_key=execution_key) for good in natures_goods: if natures_goods[good]["is_allocated"] is True: pop_hist.record_pop(good, natures_goods[good][TRADE_COUNT])
def main(): set_up() run_notice(MODEL_NAME) try: get_env()() except (NoSheep, SpaceFull) as excp: user_tell(excp) user_tell("Halting model.") return 0
def reproduce(agent, create_func, group, **kwargs): """ Agents reproduce when TIME_TO_REPR reaches 0 """ execution_key = get_exec_key(kwargs=kwargs) if DEBUG: user_debug(str(agent.name) + " is having a baby!") get_env(execution_key=execution_key).add_child(group) agent[TIME_TO_REPR] = agent["orig_repr_time"]
def populate_board_lightweight_spaceship(width, height, execution_key=CLI_EXEC_KEY): center = [width // 2, height // 2] agent_loc = get_agent_rules("spaceship", center) b = get_group(BLACK, execution_key) for loc in agent_loc: agent = create_game_cell(loc[0], loc[1]) b += agent get_env().place_member(agent, xy=loc)
def main(): global wine_group global cheese_group global max_util (cheese_group, wine_group, max_util) = set_up() # `get_env()` returns an env, which itself is a callable object get_env()() return 0
def populate_board_glider(width, height): b = get_group(BLACK) center = [width // 2, height // 2] agent_loc = [(center[0], center[1]), (center[0] - 1, center[1] + 1), (center[0] + 1, center[1] + 1), (center[0] + 1, center[1]), (center[0], center[1] - 1)] for loc in agent_loc: agent = create_game_cell(loc[0], loc[1]) b += agent get_env().place_member(agent, xy=loc)
def social_distancing(agent, **kwargs): """ This function sets a new angle for the agent's movement. """ execution_key = get_exec_key(kwargs=kwargs) curr_region = CircularRegion(get_env(execution_key=execution_key), agent.get_pos(), DEF_PERSON_MOVE * 2) agents_in_range = curr_region.get_agents( get_env(execution_key=execution_key), pred=None) new_angle = get_move_angle(agent, agents_in_range) agent["angle"] = new_angle
def main(): global resource_holders global entrepreneurs (blue_group, red_group) = set_up() if DEBUG.debug2: get_env().user.tell(get_env().__repr__()) get_env()() return 0
def setUp(self, props=None): set_up() self.wolf = create_wolf(TEST_WNAME, TEST_WNUM) self.sheep = create_sheep(TEST_SNAME, TEST_SNUM) self.wolves = get_group(WOLF_GROUP) self.wolves += self.wolf # make a new plural for sheep! self.sheeps = get_group(SHEEP_GROUP) self.sheeps += self.sheep get_env().place_member(self.sheep, None) get_env().place_member(self.wolf, None)
def populate_board_exploder(width, height, execution_key=CLI_EXEC_KEY): center = [width // 2, height // 2] agent_loc = get_agent_rules("exploder", center) b = get_group(BLACK, execution_key) for i in range(0, 5): agent_loc.append((center[X] - 2, center[Y] - i)) agent_loc.append((center[X] + 2, center[Y] - i)) for loc in agent_loc: agent = create_game_cell(loc[0], loc[1]) b += agent get_env().place_member(agent, xy=loc)
def main(): global trader_group global max_utility (trader_group, max_utility) = set_up() if DEBUG.debug2: user_log_notif(get_env().__repr__()) get_env()() return 0
def populate_board_exploder(width, height): center = [width // 2, height // 2] agent_loc = [(center[0], center[1]), (center[0], center[1] - 4)] b = get_group(BLACK) for i in range(0, 5): agent_loc.append((center[0] - 2, center[1] - i)) agent_loc.append((center[0] + 2, center[1] - i)) for loc in agent_loc: agent = create_game_cell(loc[0], loc[1]) b += agent get_env().place_member(agent, xy=loc)
def populate_board_tumbler(width, height, execution_key=CLI_EXEC_KEY): """ Tumbler is a classic GOL pattern. But this must be recoded to eliminate all the hard-coding of positions. """ center = [width // 2, height // 2] b = get_group(BLACK, execution_key) agent_loc = get_agent_rules("tumbler", center) for loc in agent_loc: agent = create_game_cell(loc[0], loc[1]) b += agent get_env().place_member(agent, xy=loc)
def rholder_action(agent, **kwargs): if agent["resources"]: get_env().user.tell("I'm " + agent.name + " and I've got resources. I have " + str(agent["cash"]) + " dollors now." + " I have " + str(agent["resources"]) + ".") else: get_env().user.tell("I'm " + agent.name + " and I've got resources. I have " + str(agent["cash"]) + " dollors now." + " I ran out of resources!") # resource holder dont move return True
def populate_board_n_horizontal_row(width, height, n=10): center = [width // 2, height // 2] agent_loc = [] right = (n // 2) + (n % 2) left = n // 2 b = get_group(BLACK) for r in range(right): agent_loc.append((center[0] + r, center[1])) for le in range(1, left): agent_loc.append((center[0] - le, center[1])) for loc in agent_loc: agent = create_game_cell(loc[0], loc[1]) b += agent get_env().place_member(agent, xy=loc)
def populate_board_n_vertical_row(width, height, n=10): center = [width // 2, height // 2] agent_loc = [] top = (n // 2) + (n % 2) bottom = n // 2 b = get_group(BLACK) for t in range(top): agent_loc.append((center[0], center[1] + t)) for b in range(1, bottom): agent_loc.append((center[0], center[1] - b)) for loc in agent_loc: agent = create_game_cell(loc[0], loc[1]) b += agent get_env().place_member(agent, xy=loc)
def populate_board_lightweight_spaceship(width, height): center = [width // 2, height // 2] agent_loc = [(center[0], center[1]), (center[0] - 1, center[1]), (center[0] - 2, center[1]), (center[0] - 3, center[1]), (center[0], center[1] - 1), (center[0], center[1] - 2), (center[0] - 4, center[1] - 1), (center[0] - 1, center[1] - 3), (center[0] - 4, center[1] - 3)] b = get_group(BLACK) for loc in agent_loc: agent = create_game_cell(loc[0], loc[1]) b += agent get_env().place_member(agent, xy=loc)
def seek_a_trade(agent, comp=False, **kwargs): execution_key = get_exec_key(kwargs=kwargs) nearby_agent = get_env( execution_key=execution_key).get_closest_agent(agent) if nearby_agent is not None: negotiate(agent, nearby_agent, comp) return MOVE
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 record_price(pop_hist, execution_key=CLI_EXEC_KEY): """ This is our hook into the env to record the number of exchanges each period. """ market_maker = get_env(execution_key=execution_key)[MARKET_MAKER] pop_hist.record_pop(ASSET_PRICE, market_maker["asset_price"])
def env(self): """ This is the env property. We use `registry.get_env()` to return whatever the registry has. """ return get_env(execution_key=self.execution_key)
def __init__(self, space=None, NW=None, NE=None, SW=None, SE=None, center=None, size=None, agents_move=True, **kwargs): # alternate structure? # self.corners[NW] = nw self.execution_key = CLI_EXEC_KEY if EXEC_KEY in kwargs: self.execution_key = kwargs[EXEC_KEY] self.name = gen_region_name(NW, NE, SW, SE, center, size) if (space is None): space = get_env(self.execution_key) self.space = space if (center is not None and size is not None): self.NW = (center[X] - size, center[Y] + size) self.NE = (center[X] + size + 1, center[Y] + size) self.SW = (center[X] - size, center[Y] - size - 1) self.SE = (center[X] + size + 1, center[Y] - size - 1) self.center = center self.size = size else: self.NW = NW self.NE = NE self.SW = SW self.SE = SE self.center = None self.size = None self.check_bounds() self.width = abs(self.NW[X] - self.NE[X]) self.height = abs(self.NW[Y] - self.SW[Y]) self.agents_move = agents_move self.my_agents = [] self.my_sub_regs = [] if not self.agents_move: self._load_agents()