Source generates agents with parameters read from DB

You can set the Source block to generate agents with attributes read from the database.

If you need to generate agents at specified moments of time, please refer here to know how to implement this.

Here we describe how to read agent attributes from the database, but generate the agents according to the Source's interarrival time, rate, rate schedule, or arrival schedule.

Say, we are simulating the shuttle service at the airport. We know that there are two shuttle buses operating on Blue route and Pink route, the interval for each route is 20 minutes.

The Blue route schedule: 00:10, 00:30, 00:50, 01:10, and so on.

The Pink route schedule: 00:20, 00:40, 01:00, 01:20, and so on.

So, you can see the regular pattern, and we can easily bring this logic into the model. We want to use just one Source block to generate agents, and do not bother ourselves with defining departure timestamps.

To make Source generate agents according to the pattern, and read agent parameters from DB
  1. Create new database table. In the table, create two columns: route and destination (both of Type: String) and enter the data as shown in the following figure:
  1. Open the properties of your Source block. In the Arrivals defined by list, select Interarrival time
  2. In the Interarrival time field below, type 10 and choose minutes from the list on the right. So, this block will generate one agent every 10 minutes.
  3. Now we want the block to generate Blue and Pink buses alternately. We will read the pattern from the database. Below, select the option Set agents parameters from DB.
  4. Select the name of the database table containing the data on agents in the Database table list. 
  5. We want our agents-buses to have attributes, so we need to define a custom agent type Bus. To create a new agent type with parameters corresponding to the columns of a database table, in the Projects view, right-click this database table (routes), and choose New > Agent type from the context menu. You will see the New agent wizard. On the first page, set the Agent type name: Bus, and set the animation shape. On the next page of the wizard, you will see the agent parameters (route and destination) already set up according to the columns of the corresponding database table.
  6. Now you have created a custom agent type with the attributes that we need. Open the properties of the Source block again and let it generate agents of this custom type. In the New agent property, choose the custom agent type name (Bus).  In the table below, you can set the mapping of agent parameters to the columns of the database table you have specified earlier. 

Obviously, database table contains the limited number of records. In the previous example the Source block generates agents infinitely. Having reached the last record in the specified database table, it continues from the first record again, and so on. That is fine for the given example. But in some cases you may want the Source block to go through the table just once and then stop.

The example: athletes starting the race according to the start list. The interval between starts is 1 minute. Say, we have the following table defining the start list:

To limit the agent arrivals to the number of records in the DB table

  1. Repeat the previous scenario: import data in the built-in AnyLogic database, create a custom agent type Athlete, set Source to generate agents with the interarrival time of one minute, and Set agents parameters from DB
  2. In the Source properties, set the Limited number of arrivals checkbox. This will make the block stop the agent generation on reaching the last record in the specified database table.