AnyLogic
Expand
Font size

Replicated blocks

AnyLogic supports the replication of the flowchart blocks. A replicated block represents multiple flowchart blocks of the same type.

In a flowchart, at some moment agents may need to pass through one of many identical blocks. With replication, you may arrange your flowchart to be more simplistic and compact. It also allows for creation of scalable systems, where the number of replicated block elements can be adjusted with a parameter.

Replication is used rather rarely: consider it only when it is necessary due to the specifics of your flowchart (like in the example described below).

Example

Consider an example model, which makes use of the replicated blocks. It is a model of a shopping mall. This model studies how people form crowds in corridors and the food court area, and how obstacles like benches, information stands, and kiosks affect the congestion.

Demo model: Shopping Mall Open the model page in AnyLogic Cloud. There you can run the model or download it (by clicking Model source files).

In this mall, there are a few dozen stores. Each visitor has its own unique planned “route” that consists of specific stores. If each store in question would be modeled with a separate block, and all routes would be modeled with blocks intertwined with connectors and PedSelectOutput blocks, this flowchart would be enormous and impossible to understand.

Instead, the model uses replicated blocks (in this particular case, these are custom blocks, but for the regular library blocks, the routine is the same). All stores are defined in a single replicated block named shops. If you take a look at this block’s properties, you will see that this block has 138 instances. Anchor stores are defined in the anchorShops block (there are 9 such stores in the mall), and so on.

So, a single replicated block is used to model multiple instances of the same-type blocks. Thus, this block on the flowchart defines all shops in the model. How do we command a pedestrian to go to a specific block from this list? For this purpose, we will address them by index.

In the dynamic field of each parameter of a replicated block, you can use a local variable index of int type.

For instance, in our demo model, each store has an entrance that is represented graphically with the target line. To assign a target line to each store, the model has a collection called shopEntrances. This collection consists of 138 entrance target lines, one for each store. As the value of the Area entrance parameter of the shops block, we have the following: shopEntrances.get(index). The index variable is used as an index of each separate block. With its help, we assign the target line to each store: individual indices of the replicated block’s instances correspond to indices of target lines associated with them within the collection.

Creating a replicated block

  1. Add a flowchart block you want to replicate on the graphical diagram.
  2. If this is a standard library block, expand the Advanced section of its properties, and select the Population of agents option. If you know the number of blocks you want to create, select Population: Contains a given number of agents. Define the number of blocks you need as the Initial number of agents.
    If you are planning to create blocks according to the database data, select Loaded from database and specify the data source using the available properties below.

  3. If it is a custom block, the properties you need are in the topmost section of the block’s properties:

You can specify the number of elements in the replicated blocks not only with a number but also using one of the agent parameters. The type of this parameter should be int. This also makes possible simultaneous changing of number of elements for multiple replicated blocks.

If you will define the number of elements for the replicated block using the parameter, AnyLogic will create a number of block instances that will be equal to the parameter’s initial value. AnyLogic won’t automatically adjust the number of block instances if the parameter’s value will be changed during the model run. You can, however, initiate this action via the event handler (the On change property of the parameter).

Indication of the replicated block

In the Projects view and model tree, a replicated block’s name has square brackets after it (like the population of agents). During the model run, within these brackets will be an actual number of elements this replicated block defines.

Accessing the elements of the replicated block

To access a specific element of the replicated block, for example, delay, use the delay.get() function. As an argument for the function, pass a number that represents the index of this element within the vector of the replicated blocks. The numeration starts with 0. To identify the index of the last element, use the following code: <block name>.size() - 1).

In dynamic fields of replicated block’s parameters, you can use a local variable named index (it has the int type). With its help, you can dynamically address the index of the current block.

Connecting replicated blocks

You cannot connect other blocks to the input port of the replicated block.

Only the output port of the replicated block can be connected to other blocks.

Why this restriction? Simply put, when you connect the output port of the replicated block with another block, you command all agents that leave this replicated block to go to the specified block, and that’s the expected behavior. But when you connect another block to the input port of the replicated block, it raises the question: which specific instance of the replicated block the agents should move to?

There are the following solutions to the problem. A regular block can have a virtual connection with the replicated block via:

How can we improve this article?