Пример #1
0
def switch(agent_nm, grp1_nm, grp2_nm, exec_key):
    """
    Move agent from grp1 to grp2.
    We first must recover agent objects from the registry.
    """
    from registry.registry import get_agent
    agent = get_agent(agent_nm, exec_key)
    if agent is None:
        if DEBUG.debug_lib:
            print("In switch; could not find agent: " + str(agent))
        return
    grp1 = get_agent(grp1_nm, exec_key)
    if grp1 is None:
        if DEBUG.debug_lib:
            print("In switch; could not find from group: " + str(grp1))
        return
    grp2 = get_agent(grp2_nm, exec_key)
    if grp2 is None:
        if DEBUG.debug_lib:
            print("In switch; could not find to group: " + str(grp2))
        return
    split_em = split(grp1, agent)
    joined_em = join(grp2, agent)
    if DEBUG.debug_lib and split_em and joined_em:
        print("Switched agent " + str(agent) + " from grp " + grp1_nm +
              "(id: " + str(id(grp1)) + ")" + " to grp " + grp2_nm + "(id: " +
              str(id(grp2)) + ")")
Пример #2
0
 def setUp(self):
     self.model = Model(model_nm="Test model", grp_struct=DEF_GRP_STRUCT)
     self.exec_key = self.model.exec_key
     self.agent = Agent("Test agent", exec_key=self.model.exec_key)
     self.agent2 = Agent("Test agent 2", exec_key=self.model.exec_key)
     self.blue_grp = get_agent(BLUE_GRP_NM, self.exec_key)
     self.red_grp = get_agent(RED_GRP_NM, self.exec_key)
Пример #3
0
def populate_board(patterns, pattern_num):
    agent_locs = patterns[pattern_num]
    grp = game_grps["dead"]
    for loc in agent_locs:
        agent = create_agent(loc[X], loc[Y], game_agent_action)
        grp += create_agent
        get_agent().place_member(agent, xy=loc)
Пример #4
0
def tsetter_action(agent, **kwargs):
    """
    Action for trend setters
    """
    return common_action(
        agent,
        get_agent(RED_FOLLOWERS, agent.exec_key),
        get_agent(BLUE_FOLLOWERS, agent.exec_key),
        lt,
        gt,
        **kwargs
    )
Пример #5
0
def follower_action(agent, **kwargs):
    """
    Action for followers
    """
    return common_action(
        agent,
        get_agent(RED_TSETTERS, agent.exec_key),
        get_agent(BLUE_TSETTERS, agent.exec_key),
        lt,
        gt,
        **kwargs
    )
Пример #6
0
    def rpt_census(self, acts, moves):
        """
        This is where we override the default census report.
        Report the amount of cheese and wine of cheese_agent
        """
        cheesey = get_agent(CHEESE_AGENT, exec_key=self.exec_key)
        cheese_rpt = f"Holdings of cheese agent\
                      \ncheese amount: {cheesey[GOODS]['cheese'][AMT_AVAIL]}\
                      \nwine amount: {cheesey[GOODS]['wine'][AMT_AVAIL]}"

        winey = get_agent(WINE_AGENT, exec_key=self.exec_key)
        wine_rpt = f"Holdings of wine agent\
                    \ncheese amount: {winey[GOODS]['cheese'][AMT_AVAIL]}\
                    \nwine amount: {winey[GOODS]['wine'][AMT_AVAIL]}"

        return cheese_rpt + "\n" + wine_rpt
Пример #7
0
 def test_get_agent(self):
     """
     See if we get an agent we have registered back.
     """
     reg_agent(TEST_AGENT_NM, self.test_agent, self.exec_key)
     self.assertEqual(self.test_agent,
                      get_agent(TEST_AGENT_NM, self.exec_key))
Пример #8
0
 def test_agent_load_from_disk(self, dump, load):
     registry.save_reg(self.exec_key)
     registry.load_reg(self.exec_key)
     loaded_agent = get_agent(TEST_AGENT_NM, self.exec_key)
     (acted, moved) = loaded_agent()
     self.assertTrue(acted)
     self.assertTrue(not moved)
Пример #9
0
 def test_del_agent(self):
     """
     Test deleting an agent.
     """
     reg_agent(TEST_AGENT_NM, self.test_agent, self.exec_key)
     del_agent(TEST_AGENT_NM, self.exec_key)
     self.assertEqual(None, get_agent(TEST_AGENT_NM,
                                      exec_key=self.exec_key))
Пример #10
0
def start_panic(exec_key):
    maxPosn = panic_grps[CALM][WIDTH] * panic_grps[CALM][HEIGHT]
    num_panic = panic_grps[PANIC][PANICKED]
    for i in range(0, num_panic):
        agent_posn = rand.randint(0, maxPosn)
        agent_name = "Calm" + str(agent_posn)
        agent = get_agent(agent_name, exec_key)
        if agent is not None and agent.group_name() == CALM:
            get_model(exec_key).add_switch(agent_name, CALM, PANIC)
Пример #11
0
 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.
     """
     from registry.registry import get_agent
     if self.is_empty(x, y):
         return None
     agent_nm = self.locations[str((x, y))]
     return get_agent(agent_nm, self.exec_key)
Пример #12
0
 def get(self):
     """
     Get agent by name from the registry.
     """
     name = request.args.get('name')
     exec_key = request.args.get('exec_key')
     if name is None:
         return err_return("You must pass an agent name.")
     agent = get_agent(name, exec_key)
     if agent is None:
         raise (wz.NotFound(f"Agent {name} not found."))
         # trying out raising an exception so comment dis out:
         # return err_return(f"Agent {name} not found.")
     return agent.to_json()
Пример #13
0
def start_panic(exec_key):
    """
    This function should be rewritten.
    We will make a new group method called `get_rand_subset(n)`.
    Then we will flip those agents to panicked.
    """
    maxPosn = panic_grps[CALM][WIDTH] * panic_grps[CALM][HEIGHT]
    num_panic = panic_grps[PANIC][PANICKED]
    for i in range(0, num_panic):
        agent_posn = rand.randint(0, maxPosn)
        agent_name = "Calm" + str(agent_posn)
        agent = get_agent(agent_name, exec_key)
        if agent is not None and agent.group_name() == CALM:
            get_model(exec_key).add_switch(agent_name, CALM, PANIC)
Пример #14
0
 def update_pop_hist(self):
     """
     This is our hook into the env to record the number of trades each
     period.
     Directly accessing self.env.pop_hist breaks encapsulation.
     But that's OK since we plan to move pop_hist into model.
     """
     if DEBUG.debug2:
         print(repr(self))
     cheesey = get_agent(CHEESE_AGENT, exec_key=self.exec_key)
     self.env.pop_hist.record_pop("cheese",
                                  cheesey[GOODS]['cheese'][AMT_AVAIL])
     self.env.pop_hist.record_pop("wine", cheesey[GOODS]['wine'][AMT_AVAIL])
     if self.last_cheese_amt == cheesey[GOODS]['cheese'][AMT_AVAIL]:
         print("At equilibrium")
     else:
         self.last_cheese_amt = cheesey[GOODS]['cheese'][AMT_AVAIL]
Пример #15
0
 def get_closest_agent_and_dist(self, agent, size=None):
     """
     Get the agent' closest to agent on grid.
     """
     closest = None
     # set min much bigger than furthest possible agent:
     min_distance_seen = MAX_WIDTH * MAX_HEIGHT
     if size is None:
         size = max(MAX_WIDTH, MAX_HEIGHT)
     for other_nm in get_neighbors(agent, size=size):
         from registry.registry import get_agent
         other = get_agent(other_nm, self.exec_key)
         d = distance(agent, other)
         if DEBUG.debug_lib:
             print("Distance to ", str(other), "is", d)
         if d < min_distance_seen:
             if DEBUG.debug_lib:
                 print("Replacing closest with", str(other))
             min_distance_seen = d
             closest = other
     return (closest, min_distance_seen)
Пример #16
0
 def get_closest_agent(self, agent):
     """
     Get the agent' closest to agent on grid.
     """
     from registry.registry import get_agent
     closest = None
     min_distance_seen = MAX_WIDTH * MAX_HEIGHT
     for key, other_nm in self.locations.items():
         if DEBUG.debug_lib:
             print("Checking ", other_nm, "for closeness")
         other = get_agent(other_nm, self.exec_key)
         if other is agent or other is None:
             continue
         d = distance(agent, other)
         if DEBUG.debug_lib:
             print("Distance to ", other_nm, "is", d)
         if d < min_distance_seen:
             if DEBUG.debug_lib:
                 print("Replacing closest with", other_nm)
             min_distance_seen = d
             closest = other
     return closest
Пример #17
0
 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.
     """
     from registry.registry import get_agent
     hood = self.get_square_hood(agent,
                                 save_neighbors=save_neighbors,
                                 hood_size=hood_size)
     if isinstance(group, str):
         # lookup group by name
         group = get_agent(group, self.exec_key)
         if group is None:
             return None
     for agent_name in hood:
         if group.ismember(agent_name):
             return group[agent_name]
     return None
Пример #18
0
 def test_registry_fetch_from_disk(self, dump, load):
     registry[self.exec_key]["name"] = "Abhinav"
     registry.save_reg(self.exec_key)
     loaded_object_name = get_agent("name", self.exec_key)
     self.assertTrue("Abhinav" == loaded_object_name)
Пример #19
0
def game_of_life_action(biosphere, **kwargs):
    dead_grp = get_agent(DEAD, biosphere.exec_key)
    print("Dead grp is:", repr(dead_grp))