Disclaimer: this is not an extensive list on why you should use java classes when building complex models in AnyLogic, to know more about it please check our online course here. This post only focuses on a single reason to use Java classes over AnyLogic agents.
Useful models are usually based on considerable amounts of data. And, if not built properly, as soon as you play your simulation for the first time, your memory usage bloats. Sometimes to a point where you might not even be able to run it and the simulation stops...
Prefer watching the video instead of reading the blog post? Click here
Example
Let us consider the following simple example: We have an agent called Truck, which holds a population of another agent called Order. The second agent is completely empty - no data is instantiated. In short, Main has a population of Trucks, each truck has a population of Orders.
If we try running a simulation with 1000 trucks, each with 1000 orders, and with the default simulation settings, we get this error:
Exception: java.lang.OutOfMemoryError
This was using the default value of 512mb of memory allocation for the Simulation experiment, which is odd because our agents are completely empty and don't hold any data.
Let's now increase it to 2Gb.
At least it runs now, but if you take a closer look at the developer panel, it shows that we are using roughly 1GB of memory, with just empty agents. This is due to the fact that Agents hold by default much more data than the data we provide it with, like its position in the environment, some default animation properties etc.
In this hypothetical example, an Order is just a piece of data. It would have held stuff like order identification, client, product... a typical data structure. If we turn this agent into a Java class, as it should be, the memory usage decreases dramatically
Improved memory utilization is just one of the benefits to use a Java class when possible.
You can download the final example here:
Or download the model from the anylogic cloud here
Key takeaway
If what you need is a data structure, use a Java Class. Only use agents when you need Agent specific functionality, like movement, ability to go through a flowchart, state chart usage, animation capabilities and so on.
Essentially you have the power to choose between small lightweight objects like Java Classes, which comes with all the standard Java features or use the powerful Agent object with a ton of default functionality. This kind of flexibility based on a powerful, and industry-standard, programming language is what differentiates AnyLogic from other offers in the market.
Pro-Tip
You can always automatically transform a Java Class into an AnyLogic Agent, if your requirements have changed, by right-clicking on the class and "Convert Java Class to Agent Type"
What next?
If you liked this post, you are welcome to read more posts by following the links above to similar posts. 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 of the footer at the bottom.
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!
That was really helpful! I had exactly this issue in one of my recent projects - and I had no clue that this could have helped. Thank you for sharing.