AnyLogic 9
Expand
Font size

Source creates agents with parameters read from DB

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

If you need to generate agents at specified moments of time, see the following article: Source creates agents according to timestamps in DB.

Here we describe how to read agent attributes from the database while generating the agents according to a Source block configured for interarrival time, rate, rate schedule, or arrival schedule.

For example, assume that we are simulating a shuttle service at an airport. There are two shuttle buses operating on the Blue route and Pink route, with an interval of 20 minutes for each route.

  • 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.

As you can see, there is a regular pattern, and we can easily incorporate this logic into the model. We want to use just one Source block to generate agents and avoid the hassle of defining departure timestamps.

To make Source generate agents according to the pattern, and read agent parameters from DB

  1. Assume we have a database table with two columns: route and destination (both of Type: String). The data is as shown in the following figure:

  2. Open the properties of your Source block. Set Arrivals defined by to Interarrival time.
  3. In the Interarrival time field below, type 10 and select minutes from the list on the right. This block will generate one agent every 10 minutes.
  4. Next, we want the block to generate Blue and Pink buses alternately. We will read the pattern from the database. Below, select the Set agents parameters from DB option.
  5. Select the name of the database table containing the data on agents in the Database table list.
  6. Since we want our agents-buses to have attributes, we need to define a custom agent type: Bus.
    To create a new agent type, open the Agent section of the Source block. In the New agent property, click in the edit box, then select New agent from the menu that appears. Follow the steps of the New agent wizard, specify the Agent type name, select an animation shape, and set up the parameters on the subsequent pages of the wizard.
  7. When the new agent is created, the value of the New agent property of the Source block is set to it. In the table below, you can map the agent parameters to the columns of the specified database table. Select a parameter using the check box on the left, then select a column from the drop-down list on the right.
    If there is a type mismatch, the column will not be available for selection.

Obviously, the database table only contains a limited number of records. In the previous example, the Source block generates agents infinitely. Once it reaches the last record, it starts from the first record again. This is fine for the given example. However, in some cases, you may want the Source block to go through the table once and then stop.

One example is athletes starting the race according to the start list. The interval between starts is one minute. Assume the following table defines the start list:

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

  1. Repeat the previous scenario: import data into the built-in AnyLogic database, create an Athlete custom agent type, set Source to generate agents with the interarrival time of one minute, and Set agents parameters from DB.
  2. In the Source properties, select the Limited number of arrivals check box. This will cause the block to stop generating agents once it reaches the last record in the specified database table.
How can we improve this article?