gpt4 book ai didi

python-2.7 - 为什么对象不能使用方法作为 Python 包 ComplexNetworkSim 中的属性?

转载 作者:行者123 更新时间:2023-12-04 18:22:06 25 4
gpt4 key购买 nike

我正在尝试使用 Python 包 ComplexNetworkSim , 它继承自 networkxSimPy , 模拟 agent-based model消息如何在网络中传播。

这是我的代码:

from ComplexNetworkSim import NetworkSimulation, NetworkAgent, Sim
import networkx as nx

#define constants for our example of states
NO_MESSAGE = 0
MESSAGE = 1

class Message(object):
def __init__(self,topic_pref):
self.relevance = topic_pref

class myAgent(NetworkAgent):
def __init__(self, state, initialiser):
NetworkAgent.__init__(self, state, initialiser)
self.state = MESSAGE
self.topic_pref = 0.5

def Run(self):
while True:
if self.state == MESSAGE:
self.message = self.Message(topic_pref, self, TIMESTEP)
yield Sim.hold, self, NetworkAgent.TIMESTEP_DEFAULT
elif self.state == NO_MESSAGE:
yield Sim.hold, self, NetworkAgent.TIMESTEP_DEFAULT

# Network and initial states of agents
nodes = 30

G = nx.scale_free_graph(nodes)
states = [MESSAGE for n in G.nodes()]

# Simulation constants
MAX_SIMULATION_TIME = 25.0
TRIALS = 2

def main():
directory = 'test' #output directory

# run simulation with parameters
# - complex network structure
# - initial state list
# - agent behaviour class
# - output directory
# - maximum simulation time
# - number of trials
simulation = NetworkSimulation(G,
states,
myAgent,
directory,
MAX_SIMULATION_TIME,
TRIALS)
simulation.runSimulation()

if __name__ == '__main__':
main()

(此代码下游可能存在其他问题,未完全测试。)

我的问题是myAgent 对象没有正确调用作为属性运行的方法。具体来说,这是我在尝试运行上述代码时收到的错误消息:

Starting simulations...
---Trial 0 ---
set up agents...
Traceback (most recent call last):
File "simmessage.py", line 55, in <module>
main()
File "simmessage.py", line 52, in main
simulation.runSimulation()
File "/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/ComplexNetworkSim-0.1.2-py2.7.egg/ComplexNetworkSim/simulation.py", line 71, in runSimulation
self.runTrial(i)
File "/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/ComplexNetworkSim-0.1.2-py2.7.egg/ComplexNetworkSim/simulation.py", line 88, in runTrial
self.activate(agent, agent.Run())
AttributeError: 'myAgent' object has no attribute 'Run'

有人知道这是为什么吗?我不知道我的代码与 ComplexNetworkSim 中的示例有何显着不同.

最佳答案

我已经在我的机器上运行了您的代码,并调用了 Run 方法。

我最好的猜测是 Paulo Scardine 写的,但由于我无法重现问题,所以我实际上无法调试它。

关于python-2.7 - 为什么对象不能使用方法作为 Python 包 ComplexNetworkSim 中的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13387898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com