-1
我正在爲我的程序使用mesa。我試圖執行我的模型類,但我從代理類中得到了AttributeError。Python - mesa:如何獲取每個對象的實例變量
這是我的腳本:
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()
但它只適用於單個物體
*這是否會引發錯誤?顯示完整的追溯。 –
@DanielRoseman編輯完整回溯 – dya
但這和你以前的問題完全一樣。正如在那裏清楚地回答的那樣,您需要遍歷'schedule'列表。 –