Reusable AnyLogic agents: scale simulation projects with modular models

Two hands work on solving a puzzle

If you work in the world of simulation, you know that flexibility and efficiency are key to delivering a seamless modeling experience. When handling new simulation projects, you want to avoid rebuilding features from scratch. Using modular models in AnyLogic helps to scale the simulation experience by reusing adaptable, shareable components.

In this article, we'll cover the core principles for designing modular models that are scalable, adaptable, and easily shared across teams. We'll also demonstrate how to create agent libraries that enhance simulation workflows for manufacturing, healthcare systems, or logistics, making development more efficient.

Read also a Modeling and Simulation chapter from the book "Evolving Toolbox for Complex Project Management" for insight on applying simulation techniques in strategic project management.

The importance of reusable agents for simulation projects

When working on simulation projects, efficiency goes beyond just the speed of building a model; it's also about ensuring that the agents you create can be easily reused and adapted for future simulations. In AnyLogic, building reusable agents or process blocks is essential, and this need becomes more apparent as you tackle larger-scale simulation projects and as existing models evolve.

In the following sections, we will discuss what are generally considered the sins of beginner modelers and state key concepts to enhance modularity, enabling a more flexible simulation experience.

Copy-paste syndrome—the tale of a modeler trapped in repetition

Without proper training, beginner modelers often fall into the habit of copying and pasting similar objects, a practice we can call the "copy-paste syndrome."

Process-centric simulation project example with copy-paste blocks

Process-centric simulation project example with "copy-paste syndrome"

This approach may work for small simulation projects, but it becomes problematic when required changes can hinder an individual's growth and even affect the organization as a whole. Small models built this way might seem manageable, but larger-scale simulations quickly reveal the inefficiency of this method.

Agent-based simulation project with the same blocks copied and pasted

The result of "copy-paste syndrome" in the agent-based simulation project

Java code copied several times

Java copy-paste

If you recognize yourself in this, you're not alone. Many are still using these practices. But after reading this article, you'll be ready to embrace reusability principles and create a streamlined, effective simulation experience that others will admire.

Use of generic agents—steering clear of redundancy

As we reflect on the shortcomings of past practices, some may say, "I never engaged in that," or dismiss it as something from long ago. However, others might look back at their current simulation projects and realize, with dismay, that they still engage in these outdated methods.

If you find yourself in that group, don't be alarmed—there's still an opportunity to evolve and enhance your simulation experience. Embrace this chance for growth as you transition towards more effective scale simulation practices.

The AnyLogic team prioritizes your simulation experience; for example, here is a video about creating custom blocks.

This is a good start, as these custom blocks can be easily shared with others by sending the model on which they were created. There are countless fascinating possibilities you can explore:

  • Customize menus and user interface frameworks to enhance usability.
  • Tailor agent types that include commonly used characteristics in your simulation projects (for example, a Citizen agent with attributes like age, height, nationality, etc., or a Worker agent that features a state chart for busy and idle states). Other agents can inherit from these to acquire certain traits.
  • Implement simulation experiments by designing an agent that runs a Custom Experiment, enabling you to conduct frequently used tests that may not be available by default.
  • Let your creativity guide you—if you use something repeatedly, transform it into an agent. This principle applies universally.

However, a challenge arises. Imagine Team A created a custom block called patientHandler that manages how a patient is received by a nurse and taken to the doctor's office before discharge. Team A shares this with Team B, and they appreciate the progress. A month later, Team C requests similar functionality. Team A sends the version of patientHandler to Team C, while Team B also sends their modified version after making some changes. Now, Team C faces a dilemma; they must choose between the two variations, and they will likely make their adjustments as well.

Simulation project dilemma explained

Simulation project dilemma for Team C

Before long, multiple versions of patientHandler are in circulation, each with different functionalities and features. While you've successfully developed a reusable agent, the organization would struggle with effective reuse across teams, leading to confusion and inefficiencies in managing simulation projects.

Encapsulation—the chronicles of a parameterized approach

To address the challenges outlined earlier, there are steps you should follow to streamline your simulation projects:

  • Avoid hardcoding. Seriously, this is crucial. Hardcoded values limit flexibility and adaptability.
  • Ensure your agent is independent of external factors. This autonomy allows for better reuse and integration across different models.
  • Protect your agent from external influences. It's vital to maintain the integrity of your agent's functionality.

Example of hardcoding values with external variables

Example of hardcoding values using external variables

The idea of parametrization means that all data your agent uses must be present in a parameter. The concept of encapsulation means that everything in the agent should work within the agent itself, with some parts kept private to protect its inner workings. This involves using private variables and adding methods to access them safely. These practices help create a stronger simulation experience and allow your agent to work reliably in larger simulations.

For more insights on using getters and setters, check out the video:

After you apply these practices, your agents can be reusable, flexible, and safe to use. But you are still missing one thing—packaging the agent into a reusable component that is easy to share.

Creating a library—rising from the foundations of reusability

You've come a long way on the journey toward good practices, and now you're ready to create a library with your parameterized and encapsulated agents. Fortunately, building libraries is straightforward, with clear instructions available in the AnyLogic Help documentation.

This approach allows to add a new library to the AnyLogic palette with all the parameterized and encapsulated agents intended for reuse in future simulation projects. Sharing this library with your team—or even across wider groups—becomes effortless. It also eliminates the issue of multiple versions, as you control the library's functions and handle requested updates with access to the source code.

Repository and abstraction—the gateway of collaborative power

As the creator of a library, you may not want to always be responsible for adding new features or managing user requests, especially as the library gains popularity. Instead, it's wise to give others the ability to make changes on their own. Using a shared repository like GitHub allows other teams to clone the latest version, make changes, and push their updates.

However, you'll want to ensure that certain methods are consistently implemented across agents or classes with minimal dependencies to keep things flexible and easy to test. This is where Java Interfaces come in—they help maintain consistent behavior and are essential if you're looking to streamline your simulation projects.

Sharing libraries with the world

A library can be shared not only within your organization but also with the world, either for free or with a license. AnyLogic has already created some external libraries, such as Pypeline and Alpyne.

At Noorjax Consulting, we have created several libraries we share; you can find them on our products page:

  • ChatGPT Bridge: Allows you to have an AI chat in your model that helps you with information on your model's data outputs as your simulation project progresses.
  • Robotic Arm: This is the typical manufacturing component used in production in multiple industries.
  • Stacker Crane: Uses an automated system to store, retrieve, and reposition agents in the native storage object.
  • Automated Sorting System: A complex library that enhances the simulation experience by automatically creating sorting conveyors and integrating with external software to prioritize and route objects.
  • Special Optimization and Monte Carlo experiments: A solution to automate confidence intervals stop criteria for optimization and Monte Carlo experiments.
  • Elevator Group for Normal Agents and Resources: A group of elevators that work together to scale simulation for moving agents and resources within the process modeling library

What's next?

It's time to focus on building your simulation projects! Whether you're working on a library, have created one, or think you have something close, reach out to us for expert consultations. Noorjax can easily help you scale your simulation ideas and bring a full simulation experience to life.


Take your simulation projects further—download AnyLogic today and start building scalable, impactful simulations.

Download AnyLogic

Verwandte Posts