3

我正在尝试使用继承自networkxSimPy的 Python 包ComplexNetworkSim来模拟消息如何在网络中传播的基于代理的模型

这是我的代码:

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 对象没有正确调用方法 Run as a attribute。具体来说,这是我尝试运行上述代码时收到的错误消息:

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中的示例有何本质不同。

4

1 回答 1

1

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

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

于 2012-11-15T07:48:42.670 回答