How Could I Define These Methods within a Class for a Model? (Python)

微笑、不失礼 提交于 2021-01-07 02:45:43

问题


I have the following class, UNetwork. It is there for reference. The actual question is related to the second class, Model.

class UNetwork(object):
    
    nodes = dict()
        
    def _init_(self, nodes):
        self.nodes = nodes

Now, the class (Model) has the following attributes:

network — an instance of class UNetwork taken at instantiation; susceptible_nodes — a list of ids for nodes that are not yet infected; initially includes all nodes from network; infected_nodes — a list of ids for nodes that are infected; initially empty; num_infected — keeps track of the number of infected nodes; initially 0.

Next, I'd like to add the following methods (which is where I'm stuck):

initialise — randomly selects n number of nodes and infects them; then prints the number of infected nodes; update — iterates over the susceptible nodes in random order and infects those who have at least one infected neighbor; then prints the number of infected nodes. The process should be asynchronous, in the sense that a node immediately becomes infected and will then infect any susceptible neighbors who are yet to be iterated over. run — repeats update until all nodes are infected or until update has been run 20 times.

How could I create these methods?

class Model(object):
    
    def __init__(self, network, susceptible_nodes, infected_nodes, num_infected):
         self.network = UNetwork() 
         self.susceptible_nodes = self.network.nodes.keys()
         self.infected_nodes = list()
         self.num_infected = len(self.infected_nodes)

    def initialise(self):
        
        
        
    def update(self):
    
    
    
    def run(self):

来源:https://stackoverflow.com/questions/64773648/how-could-i-define-these-methods-within-a-class-for-a-model-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!