Creating a network using classes for defining nodes

Question:

I am having some issues trying to use class objects as nodes in a graph.
I am creating two classes of agents that I would like to use as nodes in a random graph (e.g. Erdos Renyi).

    class man(Agent):
        def __init__(self):
    
            self.m = np.random.normal(0.5, 0.1)
            self.n= np.random.normal(0.5, 0.1)
      
    class woman(Agent):
        def __init__(self):
    
            self.m = np.random.normal(0.5, 0.1)
            self.n= np.random.normal(0.5, 0.1)     
            

def total_obs(N):
    
    obs = []
    obs_1 = []
    obs_2 = []

    for i in range(N//2):
        
        a1=man()        
        obs_1.append(a1)
        
    for i in range(N//2):
        
        a2=woman()        
        obs_2.append(a2)
    
        
    obs.extend([obs_1, obs_2])
    return population 

The above code should create two classes of agents (nodes), man and woman, with attributes m and n. The size of each population is the same (e.g., N = 50, 25 men and 25 women).
When I try to create a random graph, I get an error (see below)

class network_ER(Model):
    
    def step(self):
        self.datacollector.collect(self)
        self.schedule.step()
        
    def __init__(self, N=N, ptrans=0.5, avg_node_degree=3):

        self.num_nodes = N  
        prob = avg_node_degree / self.num_nodes

        
        self.ptrans = ptrans

        self.G = nx.erdos_renyi_graph(n=self.num_nodes, p=prob)
        self.grid = NetworkGrid(self.G)

        self.schedule = RandomActivation(self)
        self.running = True

        # Create nodes man
        for i, node in enumerate(self.G.nodes()):
            a1 = man(i+1, self)
            self.schedule.add(a1)
            self.grid.place_agent(a1, node)

I think the code related to network build has errors that I cannot spot.
By calling the network

model = network_ER(N=50)
model.step()

I get the following error (due to a1):

TypeError: __init__() takes 1 positional argument but 3 were given

What I would expect is two random graphs, one populated by men and the other one populated by women (initially they should be disconnected). Each node (man/woman) should have the attributes m and n.

Can you please suggest how to reproduce such a network?

Asked By: V_sqrt

||

Answers:

Your class man doesn’t take any arguments and you’re passing self and i+1

# Create nodes man
        for i, node in enumerate(self.G.nodes()):
            a1 = man()
            self.schedule.add(a1)
            self.grid.place_agent(a1, node)
Answered By: KapJ1coH
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.