AnyLogic gives you the ability to dynamically create and destroy agents. To enable dynamic creation or destruction, the agent population must be declared as replicated (even if you expect to create only one agent of this type). 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:
Person add_people() - this function creates a new agent of type Person and adds it into the population (and returns it so you can do additional setup), and
void remove_people( Person personToRemove ) - this function removes the given agent from the population and destroys it.
Please note that 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.). Should you need to create or destroy an agent from another agent of the same population or from itself, you have to access the Main agent first. For example, if a person gives birth to another person, you should write within the "parent" person:
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).
Demonstration model: Event Generating New Agents