Font size

Phase 2. Defining transport base

We will be defining model logic and processes on each agent type diagram in the next phases of model development. We will start with adding a Java algorithm which will place all trucks and helicopters on the model startup at our maintenance center.

Place the transport at the maintenance center

  1. Open the  Main diagram in the graphical editor.
  2. Drag the  Function element from the  Agent palette into  Main diagram.
  3. Name the function setTransportBase. This function does not return a result, so leave the default option Just action (return nothing) selected.
  4. Expand the Function body section of the function properties and type the Java code there as shown in the figure below.

We have created the function which places the transport at the maintenance center.

The function’s algorithm contains two for loops.

First loop iterates through all the agents living in the helicopters population. The syntax of the for loop initialization line is the following: inside the parentheses you first specify the name of the type of agents living in the population (Helicopter). h is the name of the local variable we define right here, it can be any other valid name (hel, a, item, and so on. helicopters is the name of the agent population we will iterate through.

Since we may want to perform not just one, but several actions for each agent of the population, we should tell the Java compiler which particular Java statements should be executed for each loop iteration. To do it, we put the required actions inside the curly brackets. In our case it is just one line of code: { h.setLocation(center) }

This code sets the location of the currently iterated agent of the population (to access it we use the local variable h we have just defined). The location is passed as the function’s argument (it is our center agent).

The second for loop does the same for the trucks population.

Now, to make the function work, we have to call the function from the model code.

Select  Main in the model tree (or click anywhere in its graphical editor) and go to its properties. Expand the properties section Agent actions and tell it to run our function On startup of the model:

Now you can run the model again. You will see that the trucks and helicopters are “sitting” now in the maintenance center. We will define their movement and all other agents behavior in the following phases of model development.

Demo model: Maintenance — Phase 2 Open the model page in AnyLogic Cloud. There you can run the model or download it (by clicking Model source files).
How can we improve this article?