def switch(agent_nm, grp1_nm, grp2_nm): """ Move agent from grp1 to grp2. We first must recover agent objects from the registry. """ agent = get_registration(agent_nm) grp1 = get_registration(grp1_nm) grp2 = get_registration(grp2_nm) if agent is None or grp1 is None or grp2 is None: print("Could not find an agent for a switch", agent_nm, grp1_nm, grp2_nm) split(grp1, agent) join(grp2, agent)
def locator(self): """ This is the locator property. We use the string _locator to look up the locator object in the registry. """ return get_registration(self._locator)
def prim_group(self): """ This is the prim_group property. We use the string _prim_group to look up the prim_group object in the registry. An agent may be in multiple groups: it appears in the groups `members` list. But it can have only one primary group. """ return get_registration(self._prim_group)
def get_agent_at(self, x, y): """ Return agent at cell x,y If cell is empty return None. Always make location a str for serialization. """ if self.is_empty(x, y): return None agent_nm = self.locations[str((x, y))] return get_registration(agent_nm)
def get_closest_agent(self, agent): """ Get the agent' closest to agent on grid. """ closest = None min_distance_seen = MAX_WIDTH * MAX_HEIGHT for key, other_nm in self.locations.items(): other = get_registration(other_nm) if other is agent or other is None: continue d = distance(agent, other) if d < min_distance_seen: min_distance_seen = d closest = other return closest
def handle_womb(self): """ The womb just contains group names -- they will be repeated as many times as that group needs to add members. We name the new members in the `member_creator()` method. This should be re-written as dict with: {"group_name": #agents_to_create} """ if self.womb is not None: for group_nm in self.womb: group = regis.get_registration(group_nm) if group is not None and group.member_creator is not None: group.num_members_ever += 1 agent = group.member_creator("", group.num_members_ever) agent.env = group.env regis.register(agent.name, agent) join(group, agent) del self.womb[:]
def get_neighbor_of_groupX(self, agent, group, save_neighbors=False, hood_size=1): """ If the agent has any neighbors in group X, return the first one encountered. We may get the groupX object itself, or we may get passed its name. """ hood = self.get_square_hood(agent, save_neighbors=save_neighbors, hood_size=hood_size) if isinstance(group, str): # lookup group by name group = get_registration(group) if group is None: return None for agent_name in hood: if group.ismember(agent_name): return group[agent_name] return None
def restore_globals(env): global flock flock = get_registration(BIRD_GROUP)