I have a simple simulation of a company using agentpy. It is doing a great job of modelling promotions, but I'd also like to be able to model people joining and leaving the company.
Leaving is easy, I can just set a flag to inactive or something, but joining is more complicated. Do I need to make a bunch of agents during setup and set their state to as yet unknown, or can I create an agent during a step and add them in?
The person class is defined like this:
class PersonAgent(ap.Agent):
def setup(self):
p = PEOPLE_DF.iloc[self.id] # 👈 the existing people are in a spreadsheet
self.name = p.full_name
self.gender = get_gender(p.gender)
self.bvn_rank = get_rank(p.b_rank)
# self.capability = float(p.capability)
print()
def rank_transition(self):
self.bvn_rank = transition(self.b_rank, self.gender)
I'm guessing I'd do something with the __init__
, but I've had no luck figuring that out.
Yes, you can initiate new agents during a simulation step.
Here are some examples:
class MyModel(ap.Model):
def setup(self):
# Initiate agents at the start of the simulation
self.agents = ap.AgentList(self, 10, PersonAgent)
def step(self):
# Create new agents during a simulation step
self.single_new_agent = PersonAgent(self)
self.list_of_new_agents = ap.AgentList(self, 10, PersonAgent)
# Add them to the original list (if you want)
self.agents.append(self.single_new_agent)
self.agents.extend(self.list_of_new_agents)