Why can't an object use a method as an attribute in the Python package ComplexNetworkSim?

StackOverflow https://stackoverflow.com/questions/13387898

  •  29-11-2021
  •  | 
  •  

Pregunta

I'm trying to use the Python package ComplexNetworkSim, which inherits from networkx and SimPy, to simulate an agent-based model of how messages propagate within networks.

Here is my code:

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()

(There may be other problems downstream with this code and it is not fully tested.)

My problem is that the myAgent object is not properly calling the method Run as an attribute. Specifically, this is the error message that I get when I try to run the above code:

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'

Does anybody know why this is? I can't figure how my code differs substantially from the example in ComplexNetworkSim.

¿Fue útil?

Solución

I've run your code on my machine and there the Run method gets called.

My best guess is what Paulo Scardine wrote, but since i can't reproduce the problem i can't actually debug it.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top