Proper initialization of parameters in AnyLogic, don’t get caught off guard
- augustopereira1
- 6 minutes ago
- 3 min read
This is a guest post from Augusto Pereira. Augusto has seven years of experience in simulation using AnyLogic, developing models for logistics, railways, and mining, focusing on coal and iron ore. He has also taught basic and advanced AnyLogic training courses and is currently a Senior Analyst at Mercado Livre.
Introduction
AnyLogic features several blocks specifically designed for agent creation and initialization, including the source block from the process library and the pedSource block from the pedestrian library, among others. In most cases, these blocks are sufficient to model the arrival of new agents into the process flow.
However, in some cases, we need to create agents beforehand programmatically and then send them into a process flow. In such cases, enter type blocks are used instead of source blocks. Examples of these cases include the creation of agents dependent on some dynamic combination of system factors, such as values of variables, available resources, queue lengths, etc., or even situations in which each agent must be parameterized in a distinct way.
When creating agents this way, we need to be aware of a behaviour in AnyLogic that may seem counterintuitive: the reset of the agent’s parameters to their default value.
The problem
To illustrate this situation, consider a simple model in which an agent of type MyAgent is created in an event and then sent into a process flow.
The agent has the following parameters and variables:

Where param1 and var1 are of type double, and param2 and var2 are boolean.
The event that creates and sets up the agent is as follows:

More experienced modellers may have already noticed a problem. If you haven’t noticed anything suspicious, stay alert.
One would expect the values set for parameters and variables to remain until they are set again, right? However, notice what happens when printing the values of these parameters when the agent reaches the enter block:

Houston, we have a problem bug!
The solution
We can see that the values set for the variables remained, while the values set for the parameters were reset to their default values (0.0 for doubles and false for booleans).
This happens because the agent’s parameters were set incorrectly. Conceptually, in AnyLogic, parameters are immutable. Thus, when sending the agent to a block, the agent’s parameters are re-evaluated, and the initialized values are preserved. In other words:
The constructor generated by AnyLogic stores the “original expression” of the parameters, and some blocks reuse these expressions as a source.
Nevertheless, this problem only occurs if the agent is sent to process blocks. If this does not happen, AnyLogic does not need to re-evaluate the parameters, and manual changes will be retained.
On the other hand, this problem does not occur with variables, since they are simple Java instance fields and are not assumed to be immutable.
The solution to this problem is straightforward: we should initialize the agent with its parameters directly in the constructor:

Now, when printing the parameter values as the agent reaches the enter block, we get the expected values:

Conclusion
This behavior may seem strange at first glance, but it makes perfect sense once we understand how AnyLogic handles parameters and variables. While variables are simply fields that we can freely change during the simulation, parameters carry the notion of “immutability” and therefore need to be set at the agent’s creation.
In other words: if the agent is going to enter a process flow, there’s no point in trying to set parameters afterward — they will be re-evaluated and return to their original value. The approach is simple: initialize everything in the constructor, and you're all set.
You can download the example model used in this article from the link below:
Augusto Pereira
Augusto Pereira is a simulation specialist and guest writer for The AnyLogic Modeler. Contact him on LinkedIn to get in touch.
What next?
If you liked this post, you could read more posts by following the links above. Why not subscribe to our blog or follow us on any of the social media accounts for future updates? The links are in the Menu bar at the top, or the footer at the bottom. You can also join the mobile app here!
If you really want to make a difference in supporting us please consider joining our Patreon community here
If you want to contact us for some advice, maybe a potential partnership or project or just to say "Hi!", feel free to get in touch here, and we will get back to you soon!