projectmesa / mesa

Mesa is an open-source Python library for agent-based modeling, ideal for simulating complex systems and exploring emergent behaviors.
https://mesa.readthedocs.io
Apache License 2.0
2.44k stars 874 forks source link

adding agents in a poisson fashion #508

Open hannantahir opened 6 years ago

hannantahir commented 6 years ago

Hi All,

I would like to add agents (without any positional information) on step basis (time) based on average arrival rate in a poisson fashion. These agent will have a certain randomly chosen stay time. This randomly chosen stay time will be alloctaed to every agent when it is a added into the simulation. These agents will do some stuff and once the stay time of an agent is over, agent will be removed. There will also be a maximum number of agents in the simulations and in case, if maximum agents are already present in the simulation at a certain time, the next new agent should wait until a previous one is removed.

I was wondering if there is an easy way of doing this in Mesa or any Mesa example that can give me a similar idea? Thanks in advance.

dmasad commented 6 years ago

That's an interesting question! The way I would do it is to have an agent_arrival method that's called by the step method of your model class. Within agent_arrival you could draw how many agents arrive, and then create each of them (including assigning them a random stay time) and add them to the schedule if the maximum number of agents haven't been added yet. Then in the agents' step method, they would count down their stay time, and remove themselves (self.model.schedule.remove(self)) once the count reaches zero.

hannantahir commented 6 years ago

Hi dmasad, Thanks for your suggestion. I am trying your suggestion but somehow I cannot import the method agent_arrival into the step method of the model class. Could you please guide me where should I write the agent_arrival method? Currently, I put the below method in the agents.py. Could you please suggest me how should I import and call this from the step method of the model class?

class PatientAgent(Agent):
    def __init__(self, unique_id, model, initial_state):
          super().__init__(unique_id, model)

    def agent_arrival(self,rate):
          number_of_new_agents = numpy.random.poisson(rate)
          print(self.model.schedule.time,' : ',number_of_new_agents)
          if number_of_new_agents > 0:
              for i in range(self.number_of_new_agents):
                  initial_state = random.randint(0,3)
                  a = PatientAgent(i, self, initial_state)
                  self.model.schedule.add(a)

In the step method of the model class, I am calling the following:

self.agent_arrival(rate)

but it gives error that object has no attribute 'agent_arrival'. May be I am putting this method in a wrong place or do you see any problem in the code?

If you can guide me to any example where agents are added into the simulations during run time or in the step function, I can then have a look and see how it is implemented.