This is a special blog post by guest writer Aqeel Tariq. In this post he shares a simple design change using code to simplify an otherwise complex model using blocks
Story Time!
In the early stages of my career, a client approached me with a unique request: they needed a simulation model for their company's maintenance and repair process. The task seemed straightforward; 10 distinct types of problems needed solving, and technicians with specific skills were assigned to address each type. Ultimately, the client wanted to see the average utilization of each group of technicians by the end of the simulation.
To tackle this challenge, my initial approach was to create separate service blocks for each type of problem, with individual resource pools associated with each block. This way, I could calculate the average utilization for each group of technicians. I developed the model, setting up 10 services and their respective resource pools. While this approach technically worked, it quickly turned into a convoluted mess. Managing and maintaining 10 separate blocks became cumbersome and complicated.

Realizing the need for a more efficient solution, I embarked on a research journey. That's when I discovered the power of incorporating Java code within AnyLogic models. I could achieve the same results using one service block and resource pool. It was a breakthrough moment. In this blog, I aim to share this streamlined approach as a prime example for beginners: how, by implementing just a few lines of Java code, we can unlock the full potential of AnyLogic blocks and simplify complex simulations.
We are going to simulate a maintenance and repair process for a company. There are two types of problems: Battery problems and Dead Product problems. Each product will have one of those issues. We have two types of technicians, each one solving a specific problem. The Battery problem solver experts will only solve battery issues, while the Dead product problem solver experts will only address dead product issues.
Step 1: Assign a Problem to Each Upcoming Product
Create an agent "Product" and assign it a boolean variable "BatteryProblem."
In the "On at exit" property of the Source block, write the following line of code:
agent.BatteryProblem = randomTrue(0.5);
Each agent entering the system has a 50 percent chance of experiencing a battery issue. The products that do not have battery issues will be classified as dead products.
Step 2: Assign a Specific Skill to Each Technician
Create an agent "Technician" and assign it a string variable "TechnicianType".
Create the population of "Technician" agent and name it "technicians."
Don't forget to select "technicians" as the population in the "population" property of the resource pool, and in the "capacity" section, specify a quantity of four. This means that we have a total of four technicians.
In the "On Startup" property of the "Main" agent, write the following code
technicians(0).TechnicianType = "BatteryProblemSolver";
technicians(1).TechnicianType = "BatteryProblemSolver";
technicians(2).TechnicianType = "DeadProductProblemSolver";
technicians(3).TechnicianType = "DeadProductProblemSolver";
We assigned each technician a specific problem-solving skill in the above code. As a result, two technicians possess the skill to solve battery problems, while the other two technicians have the expertise to address dead product issues.
Step 3: Ensure That a Technician With Specific Skills Solves The Specific Problem
In the 'Service' block properties, under the 'Advanced' section, you can find an option labeled 'customize resource choice.' Here, you can define your conditions for determining which specific resource pool agent should be used. In our case, when a product has a battery problem, it should only be resolved by a technician with the skills to address battery issues. Therefore, the condition we need to write in this section is as follows:
agent.BatteryProblem == true ?
((Technician)unit).TechnicianType.equals("BatteryProblemSolver"):
((Technician)unit).TechnicianType.equals("DeadProductProblemSolver")
Step 4: Visualizing Average Utilization of Technician Groups Based on Their Skills with a Chat
In step two, we assign each technician a specific skill. So, we know that the first two technicians are designated as battery problem solvers. So, In the chart, we will write the following code to display the average utilization of the group of technicians capable of solving battery problems.
(technicians(0).getUtilization() + technicians(1).getUtilization()) / 2
We will follow the same approach to show the average utilization of the other groups of technicians we have. And that's it; the model is ready.

Now, the power is in your hands when it comes to categorizing technicians into various classes. By following the straightforward steps we've discussed above, you can achieve this task with just two blocks and a touch of Java code.
I hope this demonstration piqued your curiosity about Java's potential to simplify and enhance your AnyLogic career. Understanding the fundamentals of Java can unlock a world of possibilities, making complex tasks appear remarkably straightforward. So, let's embark on this journey of discovery and leverage the elegance of Java to elevate our skills in the realm of AnyLogic.
Conclusion
In conclusion, this blog has illustrated the art of streamlining logic in AnyLogic simulations by minimizing the use of unnecessary blocks. A solid grasp of AnyLogic block properties and a basic understanding of Java can empower you to simplify your simulations effectively.
Please take a look at the example model on the AnyLogic Cloud here
Aqeel Tariq
Aqeel is a simulation consultant 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!
Comments