AnyLogic
Expand
Font size

Phase 1. Creating different types of agents

Agents are the main building blocks of this AnyLogic model. Agent is a unit of model design that can have behavior, memory (history), timing, contacts, etc. You can define as many agent types in your model as you need. We will create and define a single agent, an agent type and three agent populations in this model.

The agent internal state and behavior can be implemented in a number of ways. The state of the agent can be represented by a number of variables, by the statechart state, etc. The behavior can be so to say passive (e.g. there are agents that only react to message arrivals or to function calls and do not have their own timing), or active, when internal dynamics (timeouts or system dynamics processes) of the agent causes it to act. In the latter case agents most probably would have event and/or statechart objects inside.

In our model we will define agents’ behavior using parameters, variables, collections, functions, events, option lists and also statecharts.

Creating a model

Create a new model

  1. Click the  New toolbar button. The New Model dialog box is displayed.
  2. Specify the name of the model. Type Maintenance in the Model name edit box.

    AnyLogic: New model dialog

  3. Specify the location where you want to store your model files. Browse for the existing folder using the Browse button, or type the name of the folder you want to create in the Location edit box.
  4. Select the Model time units: hours.
  5. Click Finish to complete the process.

New model is created. It already has one agent type called  Main and experiment called  Simulation experiment.

In the center of the workspace you will see the graphical editor. It shows the diagram of the  Main agent type. The blue frame on the diagram is the model frame that defines the size of the model window and the part of the graphical diagram that is shown in the window at the model runtime.

AnyLogic: Workspace displaying graphical editor and model frame

To the left of the graphical editor you can see the  Projects view and the  Palette view sharing the same area. The  Projects view provides access to AnyLogic models currently opened in the workspace. The workspace tree provides easy navigation throughout the models. The  Palette view contains all graphical elements you can add onto the graphical editor of your agent just by dragging and dropping. Model elements are grouped by categories in a number of stencils (palettes).

On the right side of the workspace you can see the  Properties view. The  Properties view is used to view and modify the properties of a currently selected model element(s). When you select something — e.g., in the  Projects view or in the graphical editor — the  Properties view displays the properties of the selection.

Now we can continue developing our model created by AnyLogic model creation wizard.

Creating agents

First we need to create a single agent — the maintenance center, then an empty population for maintenance transport, three populations for the wind turbines, trucks, and helicopters, and an agent type to represent service requests. Then we will be able to define the processes inside the types of agents — on their diagrams.

Add a maintenance center

  1. In the  Palette view, hover your mouse over the vertical navigation bar on the left side of the view and select Agent there to open the  Agent palette.

    AnyLogic: Opening Agent palette

  2. Drag the  Agent element from the  Agent palette into  Main diagram. The New agent wizard will pop up automatically.

    AnyLogic: Adding Agent to diagram

  3. Select A single agent option on Step 1. We need only one maintenance center that will send transport to turbines for scheduled maintenance and any failures/repairs.

    AnyLogic: Selecting single agent option

  4. We are creating a new type of agent and do not need to use any templates. Specify the Agent type name: MC and the Agent name: center. Click Next to proceed.

    AnyLogic: Specifying MC agent details

  5. Choose the 3D animation figure Hangar from the Buildings section and click Finish.

    AnyLogic: Choosing Hangar 3D figure

Now you can find the new agent type  MC in the model tree on the same level as  Main and the agent  center itself under  Main > AnyLogic: Agents folder containing MC and center agents Agents.

AnyLogic: View of agent animation on agent type diagram and Main

The agent animation is shown on the agent type diagram as well as on  Main.

We want our agents to live in a continuous space, 1000x600 pixels, with a random layout. The agent populations that we will add on  Main later will also live in the same space.

  1. Click  Main in the model tree.
  2. Expand the Space and network section of the Main agent type's properties.
  3. Set the space's Width to 1000, Height to 600. Then change the Layout type to Random.

Now we need to change the scale of the model and of this agent’s animation.

  1. Double-click  Main in the model tree and pan the agent’s diagram down.
  2. Above the X axis, you will find the Scale element. Open its  Properties view.
  3. Set the Scale is property to be Specified explicitly. Then specify the Scale: 5 pixels per kilometer.

    AnyLogic: Setting explicit scale to 5 pixels per kilometer

As you can see in the image above, if we will keep the animations of our agents resized corresponding to the model scale, these agents (such as maintenance center here) will become invisible in our model’s animation scene. So we will use greater scales for the agents to make them visible during the animation.

  1. Double-click  MC in the model tree.
  2. In the graphical editor, select the maintenance center animation shape, and in its properties, clear the Resize automatically to match agent scale option.

    AnyLogic: Clearing Resize automatically to match agent scale option for maintenance center animation

  3. Pan the agent’s diagram down and for the agent’s scale set the option Scale is: Specified explicitly. Then specify the Scale: 10 pixels per kilometer.
  4. Set the 3D object’s Additional scale to 75%.

Add the wind turbines

  1. Drag the  Agent element from the  Agent palette onto  Main diagram.
  2. This time, select Population of agents. Click Next on Step 2 (the default setting I want to create a new agent type). On the next wizard page type the name of the new type: Turbine and agent population name will auto-fill as turbines. Click Next.

    AnyLogic: Creating Turbine agent population

  3. Choose 3D animation figure Wind Turbine Rotating from the Energy section of the list.

    AnyLogic: Selecting Wind Turbine Rotating animation figure

  4. We do not need to add any parameters here, so skip Step 5.
  5. On Step 6, set up to create 25 agents in this population.
  6. Click Finish.
  7. Open the  Turbine agent diagram and set the agent’s Scale to 10 pixels per kilometer the same way we did for the  MC agent.
  8. We don’t want the animation of the turbine to resize automatically according to the change of the agent’s scale. This option is only applicable to individual 3D objects, but the Wind Turbine Rotating animation figure is a group comprised of several such objects, therefore we have to disable the Resize automatically to match agent scale option for each object individually.
    Go to the Projects view and expand the Turbine > Presentation > level section to access the wind_turbine group. In the properties of wind_turbine_tower and wind_turbine_blades 3D objects disable the Resize automatically to match agent scale option.
  9. For both these objects specify the Additional scale: 50%.

    AnyLogic: Setting additional scale for turbine objects

  10. Since we disabled the automatic resize option and changed the scale, we have to manually configure the alignment of elements that comprise the wind_blades group. In its properties change the code in Z property field to the following: 22.25 * Scale.DEFAULT_SCALE.pixelsPerUnit(METER).
When typing Java code, use Code completion master, accessible by clicking Ctrl + Space (macOS: Alt + space) in the code field. It provides you with the list of available functions and model elements, so you can simply choose the required item from the list and avoid mistypes when referring to it.

Add the transport

  1. Drag the  Agent element from the  Agent palette onto  Main diagram.
  2. One more time select Population of agents. Click Next on Step 2 (I want to create a new agent type). On the next wizard page (step 3) define the Agent type name: Transport and the Agent population name: transport. Click Next.
  3. This is an empty population that does not need animation. We will use this agent type to define the logic of our model. Select None for animation type, skip the parameters step, select the Create initially empty population, I will add agents at model runtime option and click Finish.

Add trucks and helicopters to the model

  1. Drag the  Agent element from the  Agent palette onto  Main diagram.
  2. Again, select Population of agents. Click Next on Step 2 (I want to create a new agent type). On the next wizard page (step 3) define the Agent type name: Truck, and the agent population name will auto-fill as trucks. Click Next.
  3. Select the Lorry 3D animation figure from the section Road transport on the next step. We do not need to add any parameters here in the wizard. Set up the population to have 5 trucks.
  4. After you create the  Truck agent type, select it the model tree and go to its Properties view. First expand the Dimensions and movement section. We suppose that the average truck’s speed equals 70 kilometers per hour.
  5. Expand the Advanced section and make  Truck inherit  Transport configuration by setting up the parameter Extends other agent:  Transport.

    AnyLogic: Setting Truck to inherit Transport configuration

  6. Select the truck animation shape, and in its properties, clear the Resize automatically to match agent scale option.
  7. Modify the  Truck scale. In the scale element properties, clear the Inherited from parent option and set the scale to 10 pixels per kilometer.
  8. Now  Truck agent is fully set up. We need to add one more population for helicopters. Go back to  Main and drag the  Agent element from the  Agent palette again and select Population on the first step.
  9. Type the new agent type name Helicopter and the population name helicopters will auto-fill. You can find the Helicopter 3D animation figure in the section Military. We want to use 2 helicopters for the turbine maintenance. This population lives in the same space.
  10.  Helicopter also Extends other agent:  Transport. We suppose that helicopters move with the speed 200 kilometers per hour.
  11. Select the helicopter animation shape, and in its properties, clear the Resize automatically to match agent scale option.
  12. Next, specify the Additional scale: 50%.
  13. Modify the Helicopter scale. In the scale element properties, clear the Inherited from parent option and set the scale to 5 pixels per kilometer.

Define service request pattern

  1. Drag the  Agent element from the  Agent palette into  Main diagram.
  2. Select Agent type only. We need to add an agent type to define logic of the model with the parameters we will set up on its diagram.

    AnyLogic: Creating a new agent type

  3. Type its name ServiceRequest in the corresponding field and click Next. Select None for the animation type and proceed to the next step.
  4. On step 4 we will create a parameter and an option list.
    Option List is the element for defining agent attributes that have limited choice of alternative options. In our case, we need to differentiate between trucks and helicopters which are both used by the maintenance center and respond to the service requests. The parameter is called type and its type is the Option List. Since there are no option lists in the model yet, the wizard suggests that you create a new option list: TransportType. Type the options in the table, one per line: AUTO, AVIA.

    AnyLogic: Creating a parameter and an option list in AnyLogic

  5. Click Finish. The option lists do not have any graphical icons that you can click to select them in the graphical editor. You can find Option Lists in the model elements tree:

    AnyLogic: Option Lists in the model elements tree in AnyLogic

  6. After you click Finish, AnyLogic will open the  ServiceRequest agent type diagram. There you can find the parameter type that we have created. Add one more  Parameter from the  Agent palette. Call it turbine and select its Type from the drop-down list: Turbine. Now  ServiceRequest is fully set up.

    Adding a parameter to an agent diagram in AnyLogic

The agent elements will appear on  Main. You can notice that the populations are marked with the [..]. Move the agents beyond the model window and place the center of each animation figure in the axis origin.

AnyLogic: Model tree displaying agent elements in AnyLogic

You can find the space settings in the properties of  Main, section Space and network.

Run the model

  1. Build your project by clicking the  Build model toolbar button. If there are some errors in your model, the building fails and the Problems view appears listing all the errors found in your model. Double-click an error in the list to open the location of the error and fix it. After the model is successfully built, you can start it. Running the simulation, you automatically bring the current model up to date.
  2. Choose the experiment you want to run from the drop-down list of the  Run toolbar button. Your simulation experiment is called AnyLogic: Simulation icon Maintenance/Simulation. Later on you can use the  Run toolbar button to start the previously run experiment.
  3. Having started the model, you will see the presentation of your top-level agent type  Main. Since we have only started developing the model, at this moment we can see the maintenance center, turbines, and our transport distributed randomly in the continuous space.

    AnyLogic: Model simulation display

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

Next, we will place the transport — trucks and helicopters — in the maintenance center.

How can we improve this article?