Font size

Phase 4. Maintenance Center logic

Define the transport requests to the maintenance center

Now let's add two collections that will store requests for service crews. We need two collections to separately store requests for service crews that use trucks (autoRequests) and helicopters (aviaRequests).

A collection represents a group of objects, known as its elements. Typically, they represent data items that form a natural group such as an autopark (elements are trucks).

We use collections but not agent populations here because we will constantly add and remove requests from collections. Agent populations are mostly used when agents live in a population for a long time, but not appear there for a while and then are permanently removed from population like in our case.

  1. Open  Main diagram. Add two  Collection elements from the  Agent palette.
  2. The collections called aviaRequests and autoRequests are both of Collection class: LinkedList and Element class: ServiceRequest.

We will define a function which will search for the idle transport to fulfill the maintenance task.

Define the logic of the transport management

  1. Drag the  Function element from  Agent palette into  Main diagram.
  2. Name the function findTransport.
  3. This function searches for an idle transport. If the transport is found, the function should return it. So select the Returns value option and set the function to return the value of Type: Transport.
  4. The function should analyze the type of the incoming service request. This requires adding one function argument — we need it to pass the service request to the function. Expand the Arguments section of the function properties and define the request argument of Type ServiceRequest in the table.
  5. Finally, define the function algorithm itself. Expand the Function body section of the properties and type the Java code there as shown in the figure below.

In the function body, we define the for loop. This loop does not always iterate through one agent population but analyzes the type of the service request and iterates through the trucks population if it is the request for the auto transport (by checking the condition request.type == AUTO). If the condition is false, it means that a helicopter is required, so the loop iterates through another population: helicopters.

Inside the loop, we check whether the currently iterated agent is idle or not by using the agent’s function inState(). If the control of the agent’s statechart is currently in the  AtCenter state, it means that we have found the idle transport. In this case the line return t; ends the function execution and returns the found value (idle vehicle). If there is no idle transport at all, this line will not be executed, so after the for loop the next line of code will be executed: return null; This way we tell the model that the truck was not found (the result is null, the idle transport does not exist).

In AnyLogic you enter pieces of code in numerous properties of various model elements. It is important to understand exactly where you are writing the code (which agent type and method it belongs to) and how you can access other model elements from there.

The elements of the same agent type are accessed simply by their names.

To access an element of an embedded object you should put a dot "." after the embedded object name and then write the element name. For example, to get to the  AtCenter state that belongs to the statechart contained in  Transport, from within  Main, we call Transport.AtCenter in  Main.

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

Next, we’ll configure  Turbine.

How can we improve this article?