Creating a population of agents

In AnyLogic you can create a number of agents of the same type simply by creating an agent population. You can build scalable systems by creating agent populations with the number of agents defined by a parameter of the model.

  To create a population of agents

  1. Drag the Agent  element from the Agent palette on the diagram of the agent type where you want to create the population of agents (usually - Main type). The New agent dialog box will open. 
  1. First, choose, whether you want to create the agent population, one single agent, or you just define the agent pattern. Click Population of agents. The wizard will take you to the next step.

  2. If you already have some custom agent types in your model, you will be asked whether you want to create a new type (I want to create a new agent type option), or use the existing agent type (I want to use an existing agent type).
  3. Here you should choose whether you will read agent attributes from database, or create a new agent type "from scratch". To learn how to create agent population using a database, refer here. Choose the Create the agent type "from scratch" option.
  4. On the same page of the wizard, specify the Agent type name (e.g. Person). You will see that the Agent population name changed automatically.
  5. If you plan to use the agents of this type in the flowcharts (e.g. in the Process Modeling Library flowchart, or as a pedestrian in Pedestrian Library flowchart), select the checkbox Agent will be used in flowcharts and then choose the role for this agent from the drop-down list to the right. Click Next.

  6. Specify the animation for the agent. Here you can choose some animation shape for your agent. If you want the agents to be animated both in 2D and 3D, choose 3D and select the animation shape from the list below. If you need the shape that is shown in 2D only, choose 2D. Choose the shape from the list, or if no shapes satisfy your needs, just choose None. You can draw animation on the agent type later whenever you need. When done, click Next to proceed to the next step.

  7. Specify the parameters for this agent type. Click <add new...> in the list on the left to create a new parameter. Having selected a parameter in this list, you can set its name, type and initial value using the controls in the right panel. Select the parameter and click the "cross" button at the bottom to remove the parameter from the list. Click Next when finished defining all the required parameters for this agent type.

  8. On the next page of the wizard you have to define the initial number of agents in the population. Choose whether you want to Create population with specified number of agents, or you want to Create initially empty population and add agents later on dynamically (e.g. in the Startup code of Main). In the example below, we are creating the population with 1000 agents. Click Next and you will get to the last page of the wizard.

  9. Here you may configure the environment where these agents will live. Choose the type of the space (Continuous, GIS, or Discrete), and configure it using the controls below. Having chosen the continuous or discrete space type, define the space dimensions, width and height, in the Size fields (in the case of discrete space, you should also define the number of rows and columns that make up the space, in the Cells fields). 
  10. The Network type drop-down list allows you to choose how the network of agent contacts should be created. You can find more information on agent networks here. In the example below, the network type is User-defined.
  11. You can make AnyLogic randomly locate agents of this population in the space. To do this, select the option Apply random layout.

  12. Click Finish to end the process.

When finished, you will see the  element on the diagram - it is agent population. The population contains the specified number of agents - instances of the agent type. You can see the created agent type in the model tree (in the figure given below it is  Patient): 

Open the diagram of this agent type by double-clicking it in the Projects view. You will see the animation and parameters you have chosen for this agent while creating it with the wizard. You can add variables, parameters, statecharts, and any other elements defining the agent state and behavior. 

You can specify any expression evaluating to integer type as a number of agents living in the population. Thus, you can parameterize the number of agents. You may need this to facilitate modifying the number of agents of several populations. 

 If you use a parameter as a replication factor of an agent population, AnyLogic creates a number of agents equal to the initial value of the parameter. However, AnyLogic does not automatically adjust the number of agents in case you change the parameter at runtime. If such behavior is needed, you should use the parameter change handler to take care of creation and destruction of agents.

Accessing individual agents living in a population

In order to access any individual agent from people population, you can use people(index) function, which takes an index of the agent in the vector, from 0 to people.size() – 1.