AnyLogic
Expand
Font size

Creating custom car types

If you have cars of different types in your model (e.g. car, lorry, bus, etc.) with specific custom attributes (car speed, length, etc.) and you want them to look different at model runtime, you should create custom car types and generate cars of different car types in your model. This document describes how to do it.

To define a custom car type

  1. Drag the Car Type  element from the Road Traffic Library palette into the graphical editor.

  2. The New agent wizard will open.
  3. On the first page of the wizard, specify Agent type name (e.g. Bus). Click Next to proceed to the next step.

  4. Choose animation for the cars of this type. If you want the cars to be animated both in 2D and 3D, choose 3D and select 3D animation shape from the list below. Otherwise, select 2D and select the figure suiting your needs from the list. If there is no one, choose None, you can draw the animation later. Finished with selecting the animation, click Next to proceed to the next step.


  5. Specify the parameters for this car type. For example, let us create a parameter for the vehicle’s speed. Click <add new...> in the list to create new parameter.

  6. You will see the section in the right panel of the wizard that allows you to configure the parameter selected in the table on the left. Set the parameter’s name in the Parameter field: speed. Since this parameter defines speed, select Speed from the Type drop-down list below. Set the speed units for this parameter in the list on the right: kilometers per hour. Finally, specify the parameter’s Default value: 75.

  7. When you have finished, click Finish at the bottom of the wizard.

The graphical diagram of the created car type will open. You will see the chosen car animation there and the parameters you have defined in the Wizard.

Here you can modify the car animation. If you have not set any animation shape for this car type in the Car Type creation wizard, you can add your custom 3D object or 2D figure here on the car type diagram, putting the figure center in the point (0,0)). This figure will be used in the model to animate cars of this particular type.

Car type properties

General

Name — The name of the car type.

Since AnyLogic generates Java class for each car type, you should follow Java naming guidelines and start the name with an uppercase letter.

Ignore — If selected, the car type is excluded from the model.

Parameters preview

Parameters preview properties are available only when this car type has any parameters. Here you can rearrange parameters of this car. Сars of this type will list parameters in their property view in this well-defined order.

Agent actions

On startup — Code to be executed after all agents throughout the whole model are constructed, connected, and initialized, and before anything else is done. This is a place for some additional initialization of the car. The order of execution of On startup code of different agents is not guaranteed. We recommend to perform car initialization not here, but in the On exit action property of the CarSource block that generates cars in your model.

On destroy — Code to be executed on destroying car of this type. This parameter is derived from agent type and is rarely used for cars. For details refer to the Agent article.

On arrival to target destination — This parameter is derived from agent type and is not applied to the movement defined with a Road Traffic Library flowchart.

On before step — This parameter is derived from agent type and is rarely used for cars. For details refer to the Agent article.

On step — This parameter is derived from agent type and is rarely used for cars. For details refer to the Agent article.

Agent in flowcharts

Use in flowcharts as — Here you choose the role for agents of this type in the flowcharts. To enable using these agents as cars in the blocks of the Road Traffic Library, leave the car option selected. This way the agent of this type obtains car-specific functions: getAverageSpeed(), getDistanceDriven(), getRoad(), etc.

On enter flowchart block — Here you can write the code to be executed when this car enters the flowchart.

On exit flowchart block — Here you can write the code to be executed when this car exits the flowchart.

On seize resource — This parameter is derived from agent type and is rarely used for cars.

On release resource — This parameter is derived from agent type and is rarely used for cars.

Dimensions and movement

The parameters in this section are derived from agent type and are rarely used for cars. For details refer to the Agent article. Define the speed and length of the car in the Car properties section of the CarSource block that generates cars in your model.

Space and network

The parameters in this section are derived from agent type and are rarely used for cars. For details refer to the Agent article.

Advanced Java

The parameters in this section are derived from agent type and are rarely used for cars. For details refer to the Agent article.

Advanced

The parameters in this section are derived from agent type and are rarely used for cars. For details refer to the Agent article.

Generating cars of a custom type and accessing custom car attributes

To generate the cars of a custom type you should choose this type (e.g. Bus) in the New car parameter of the corresponding CarSource block. Then the cars in this flowchart will be of this custom type, and you will be able to use car in the flowchart blocks to explicitly address the additional functionality of this car type.

For example, you can use the previously specified speed parameter to set up the speed for Bus cars generated by CarSource. In both Initial speed and Preferred speed properties of the CarSource, type car.speed.

How can we improve this article?