Python - mesa:如何获取每个对象的实例变量

时间:2016-12-13 09:17:29

标签: python instance-variables mesa-abm

我正在使用mesa作为我的程序。我正在尝试执行我的模型类,但是我从代理类中获得了AttributeError。

这是我的剧本:

class ComtrModel (Model):
    """ A model with some number of Agents"""
    def __init__(self,N):
        self.num_agents = N
        self.schedule = RandomActivation(self)
        for i in range (N):
            a = CommuterAgent(i)
            self.schedule.add(a)

class CommuterAgent (Agent):
    def __init__(self, model):
        self.famsize = famsize
        self.distance = distance
        self.update_need = None


    def step(self):
        if  self.distance >= 10000:
            self.update_need = self.update_need()
            return

    def update_need (self, famsize):
        if self.famsize :
            self.famsize = famsize
            return
            prob_need()

如何获取每个代理的变量?我需要检查它以确保模型正常运行。

到目前为止,这是我要执行的代码(在交互式会话中):

from src.ComtrModel import *
model = ComtrModel(5)
for i in range (10):
    model.step()

for key, value in CommuterAgent.step(model):
    print(key, value)

编辑:但它返回

Traceback (most recent call last):
  File "C:src/__init__.py", line 3, in <module>
    from src.ComtrModel import *
  File "C:\src\__init__.py", line 9, in <module>
    for key, value in CommuterAgent.step(model):
  File "C:\src\ComtrModel.py", line 40, in step
    if  self.distance >= 10000:
AttributeError: 'ComtrModel' object has no attribute 'distance'

我也试过这样的事情:

>>> hi_obj = hi()
>>> hi_obj.__dict__.keys()

但它只适用于单个对象

1 个答案:

答案 0 :(得分:0)

def __init__(self, model):
        self.famsize = famsize
        self.distance = distance
        self.update_need = None

看起来不正确。你没有通过famsize和distance作为参数。