AB-CE / abce

Agent-based computational Economics, the Python library that makes AB modelling easier
http://abce.readthedocs.io
190 stars 64 forks source link

Can't access individual agents in group with index #129

Closed juliazhang95 closed 3 years ago

juliazhang95 commented 6 years ago

In the documentation, it says that individual agents in a group can be accessed with an index. When I tried this, it gave me a TypeError

Code used:

# Define all agents in the economy
class Citizen(abce.Agent):    
    def init(self, money):        
        # initial money balance
        self.create('money', money)

        # years of schooling
        self.employee_qualification = random.randint(0, 21)

        def get_salary():
            return 100

simulation = abce.Simulation(name='economy', processes=1)    
citizens = simulation.build_agents(Citizen, 'citizen', number=num_citizens, money = 1000)

citizens[0].get_salary()

Error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-28-459e186e15ce> in <module>()
     13 citizens = simulation.build_agents(Citizen, 'citizen', number=num_citizens, money = 1000)
     14 
---> 15 citizens[0].get_salary()

/usr/local/lib/python3.6/dist-packages/abce/group.py in __call__(self, *args, **kwargs)
     55     def __call__(self, *args, **kwargs):
     56         for group_names, ids, command, _, __ in self.actions:
---> 57             self._agents.do(group_names, ids, command, args, kwargs)
     58         return Chain([self._agents.post_messages(action[0], action[1]) for action in self.actions])
     59         # itertools.chain, does not work here

/usr/local/lib/python3.6/dist-packages/abce/singleprocess.py in do(self, groups, ids, command, args, kwargs)
     57         self.rets = []
     58         for group, iss in zip(groups, ids):
---> 59             for i in iss:
     60                 if i is not None:
     61                     ret = self.agents[group][i]._execute(command, args, kwargs)

TypeError: 'int' object is not iterable

Please let me know if I am attempting to access the agents incorrectly, or doing something wrong!

DavoudTaghawiNejad commented 6 years ago

Dear Julia, we have fixed this bug. Please run pip install abce --upgrade

rht commented 3 years ago

Yes, the bug is no longer in master. I ran a slightly modified version of the code above, and encountered no problem

import random
import abcEconomics as abce

# Define all agents in the economy
class Citizen(abce.Agent):    
    def init(self, money):        
        # initial money balance
        self.create('money', money)

        # years of schooling
        self.employee_qualification = random.randint(0, 21)

    def get_salary(self):
        return 100

simulation = abce.Simulation(name='economy', processes=1)    
citizens = simulation.build_agents(Citizen, 'citizen', number=10, money = 1000)

print(citizens[0].get_salary())