If you have cars of different types in your model (such as a car, truck, bus, and so on) with specific custom attributes (the speed, length, and so on) 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.
To define a custom car type
- Open the Road Traffic Library palette. Drag and drop the Car Source flowchart block onto the agent type diagram, for example, Main.
- In the block properties, in the Car section, locate the New car property. Click in the edit box to open the drop-down list.
- Click Create new agent type.
-
In the Name edit box, specify the name of the car. By default, Car is used.
Car is an agent type, so its name should start with an uppercase letter.
- Customize the animation of the car: by default, the Car shape is selected. Click on its name to see other options, both in the 2D and 3D palettes. Select the shape you want.
- Click Next.
-
On the next page of the wizard, specify additional parameters for the car type. Click Add and specify the name, type, and default value of the parameter. Repeat this for each parameter you need.
To remove a parameter from the list, move the mouse pointer over it, then click theDelete icon that appears on the right.
Consider adding a parameter that will store the speed of the vehicle. Enter the name of the parameter in the Name box: speed. Since this parameter defines speed, select Speed from the Type drop-down list. Set the speed units for this parameter in the list that appears below: kilometers per hour. Finally, set the Default value for the parameter: 75.You can add more parameters to the car diagram after you have finished creating it. - Click Finish. AnyLogic will create and open the new car type diagram with the defined parameters.
- Now you can use the new car type in the Road Traffic Library block parameters. It is automatically specified in the properties of the Car Source block you used to create the car.
- 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
-
The Parameters preview properties are available only if this car type has any parameters. Here you can rearrange the parameters of this car. Cars of this type will list parameters in their property view in this defined order.
- Agent actions
-
On startup — The code to execute 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. e recommend that you do your car initialization not here, but in the On exit action property of the CarSource block that generates cars in your model.
On destroy — The code to execute when destroying a car of this type. This parameter is derived from the agent type and is rarely used for cars.
On arrival to target destination — This parameter is derived from the agent type and is not applied to the movement defined with a Road Traffic Library flowchart.
On before step — This parameter is derived from the agent type and is rarely used for cars.
On step — This parameter is derived from the agent type and is rarely used for cars.
- Agent in flowcharts
-
Use in flowcharts as — Specifies the role for agents of this type in the flowcharts. To allow these these agents to be used as cars in the blocks of the Road Traffic Library, leave the Car option selected. This will give the agent of this type car-specific functions: getAverageSpeed(), getDistanceDriven(), getRoad(), and so on.
On enter flowchart block — The code to execute when this car enters the flowchart.
On exit flowchart block — The code to execute when this car exits the flowchart.
On seize resource — This parameter is derived from the agent type and is rarely used for cars.
On release resource — This parameter is derived from the agent type and is rarely used for cars.
- Dimensions and movement
-
The parameters in this section are derived from the agent type and are rarely used for cars. 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 the agent type and are rarely used for cars.
- Advanced Java
-
The parameters in this section are derived from the agent type and are rarely used for cars.
- Advanced
-
The parameters in this section are derived from the agent type and are rarely used for cars.
To generate the cars of a custom type, select this type (for example, Bus) in the New car parameter of the corresponding CarSource block. Then the cars in that flowchart will be of that custom type, and you can use car in the flowchart blocks to explicitly address the additional functionality of that 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 the Initial speed and Preferred speed properties of the CarSource block, type car.speed.
-
How can we improve this article?
-