projectmesa/mesa-examples

Can not simultaneously execute methods from different agent

widhadyah opened this issue · 1 comments

Hi all,

I've got an issue, which previously not a problem at all. It's supposed to be simple. I don't know if I missed something here. Below the simplify script

agent.py

from mesa import Agent, Model
from mesa.time import RandomActivation
from mesa.datacollection import DataCollector
from mesa.space import SingleGrid

class A(Agent):
    def __init__(self, unique_id, model, pos):
        super().__init__(unique_id, model)

    def step(self):
        print('A')

class B(Agent):
     def __init__(self, unique_id, model, pos):
        super().__init__(unique_id, model)

     def step(self):
        print('B')

class MaterialModel(Model):
    def __init__(self,A_,B_, width, height):
        self.running = True
        self.grid = SingleGrid(width, height,torus=False)
        self.schedule = RandomActivation(self)

        for i in range(A_):
            x = randrange(self.grid.height)
            y = randrange(self.grid.width)
            pos = (x, y)
            t = A(i, self, pos)
            self.grid.position_agent(t, (pos))
            self.schedule.add(t)

        for l in range(B_):
            x = randrange(self.grid.height)
            y = randrange(self.grid.width)
            pos = (x, y)
            d = B(l, self, pos)
            self.grid.position_agent(d, (pos))
            self.schedule.add(d)

    def step(self):
        self.schedule.step()
run.py

from src.agent import *

model = MaterialModel(1,1,11,11) 
run =  5

for i in range(run):
    model.step()

Output

B
B
B
B
B

Process finished with exit code 0

How can I print 'A' from agent A's step?
Thank you in advance

It looks like the problem is that both your A and B agents have the same unique identifier, so when you add the B agent, it silently overwrites the A agent with the same name.

(For future reference -- this repository is for the examples only; more general questions like this should go in the main Mesa repository).