Creating a new agent population based on DB data

If you are creating a new agent population, and have attribute values for all agents stored in a database, you can easily create a population based on the existing data.  

Assume you have the following Excel spreadsheet, and you want to create a populatoin of five cities:

Data in Excel spreadsheet should meet the following formatting requirement: 

The following video demonstrates how to create an agent population and provide agents with attributes that are read from the MS Excel spreadsheet:

How-To video: Reading agent attributes from a database

The following video shows how to read agents' addresses/coordinates from the database and locate agents in the given points on the GIS map:

How-To video: Placing agents on the GIS map

  To create a population of agents based on the existing data

  1. Drag the Agent  element from the Agent palette on the diagram where you want to create the population (usually - Main). The New agent wizard will open. 
  1. First, choose, whether you want to create the agent population, one single agent, or you just define the agent pattern (to create agents later e.g. with the Source flowchart block). We want to create an agent populatoin, so click Population of agents. The wizard will take you to the next step.

  2. If you already have some agent types defined in the model, on the next page of the wizard choose I want to create a new agent type, and click Next.
  3. You will see the next wizard page. Since we want to read the agent attributes from the database, choose the option Use database table.
  4. On the same page of the wizard, specify Agent type name (in our case it will be City). You will see the Agent population name automatically changed to cities.
  5. If you plan to use the agents of this type in the flowcharts (e.g. in the Process Modeling Library flowchart), select the checkbox Agent will be used in flowcharts and then choose the role for this agent (Agent, Resource unit, etc.) from the drop-down list on the right. Click Next.

  6. If you have already imported the database table into the built-in AnyLogic database, select Use existing table and choose the table name in the list on the right. Otherwise, if you have not imported data yet, and have them stored in an external database, or a spreadsheet (it is our case), select the option Import table from external source
  7. In the left pane, specify the database Type (in our case it is Excel/Access, other options are Microsoft SQL Server and Other Database). 
  8. Using the controls below, specify the database location. If an Excel or an Access file is used as the data source for the database, you can use the  button to switch between the absolute and relative file path.

    In the right pane, you will see the list of database tables (if not, click the Show list of tables button). In this list, select the table that contains the data for the agent population, and click Next.

  1. On the next page of the wizard, you can set up the parameters of the new agent type. In the Agent parameters mapping table, you will see the list of parameters that will be created. By default AnyLogic proposes to create one parameter for each database table column that contains data. You can change the name of the parameter in the Parameter column. If you do not need some parameter to be created, select the corresponding row in the table, and click the  Remove button below. Click Next when finished.

  1. On the next page of the wizard, specify the animation for the 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 only in 2D, choose 2D and select the shape from the list. If no shapes satisfy your needs, just choose None. You can draw animation on the agent type diagram later on. Finished with choosing the animation, click Next to proceed to the next step.

  1. On the last page of the wizard you have to 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. If you choose Continuous space, define the space dimensions, width and height, in the Size fields, and specify how the network of agent contacts should be created (in the Network type list). We will choose GIS space, and will finally show you how to locate our agents at the coordinates read from the database.

  1. Click Finish to end the process.

When finished, you will see the  element on the Main diagram - it is agent population. The population contains the number of agents equal to the number of records in the database table we've used. In the Projects tree, you can see the created agent type ( City). Since we have chosen GIS space, GIS map was also created automatically.

Now let's show you how to locate our agents at the coordinates read from the database. Click the cities element on the Main diagram. In the Properties, open the Initial location section, choose the option Place agent(s): in the latitude/longitude, and write the following code lines:

Here we refer to the individual agent in a populatoin by the keyword self.

Now if you run the model, you will see that the agents are placed according to the coordinates we have read from the database:

Now you can develop the City agent logic further. Open the agent's diagram by double-clicking City item in the Projects view, and add any other elements you need on the diagram.