AnyLogic
Expand
Font size

Adding and removing agents from population dynamically

AnyLogic gives you the ability to dynamically create and destroy agents living in a population. To enable dynamic creation or destruction, in the agent properties, the Population of agents option should be selected. The Initial number of agents field in the population properties is used to specify the initial number of agents that will be created when the container object is created and can be 0 if you do not want to have any agents of this type at that time.

Assume you have declared a population people of agents of type Person within the Main agent. Then AnyLogic automatically generates two functions (at the Main level) for adding and removing agents to that population during the model execution:

The functions are generated into the Main agent type, so they can be called directly from anywhere in Main (e.g. from an event Action code, from On startup code etc.).

To create or destroy an agent from another agent of the same population or from itself, access the Main agent first. For example, if a person gives birth to another person, you should write within the “parent” person:

main.add_people();

Another frequently used pattern: an object wishes to destroy itself:

main.remove_people( this );

You may take a look at the given simple model. It demonstrates how to create agents dynamically (in this model they are generated using an event).

Demo model: Event Generating New Agents Open the model page in AnyLogic Cloud. There you can run the model or download it (by clicking Model source files).

Adding/removing multiple agents dynamically

If you need to add several agents to population at once, you may use any Java loop. In the following model, you can find examples of both adding and removing multiple agents dynamically at the model runtime.

Demo model: Aircraft Fleet Planning Open the model page in AnyLogic Cloud. There you can run the model or download it (by clicking Model source files).

Examine the action code of the button with the “GO” label in the Main agent type. You will find two different loops there.

The while loop removes the existing aircraft agents from the aircrafts population. Since the population is named aircrafts, the name of the function removing the agents from it is remove_aircrafts. The function is called several times inside the loop, until the population becomes empty (i.e., aircrafts.size() returns 0). Each time the function deletes the first agent of the population accessed as aircrafts.get(0).

      
while ( aircrafts.size() > 0 ){
  remove_aircrafts( aircrafts.get(0) );
}      
    

To add multiple agents into the population, for loop is used here. In this example, 20 agents are created and added into the aircrafts population. Similarly, the name of the function is constructed from the population name: add_aircrafts.

      
for ( int i = 0; i < 20; ++i ){
  add_aircrafts();
}      
    

Changing the population of an agent

You can dynamically change the population of an existing agent during the model simulation using the goToPopulation() function of the agent. The new population must contain agents of the same (or compatible) agent type.

goToPopulation(AgentList newPopulation) — Moves the agent to a new population. Agent will leave its current environment (if it has belonged to any) and join environment of the new population (if it lives in an environment).
newPopulation — the new population to move to. If newPopulation is null then the agent will go into the default population of the top-level agent of the model and will leave its environment (if it has lived in any).

How can we improve this article?