Phase 3. Creating rail car types

As you might have seen while running your current model, we had numerous rail cars differed by colors only. Phase 3 will be devoted to defining different types of the rail cars.

We provide you with the handy Rail Car Type creation Wizard. Simply specify the name of the new rail car type and choose the animation from the list of ready-to-use 3D objects.

Create new rail car types
  1. Create new rail car type by dragging Rail Car Type element from the Rail Library palette on the Main diagram.

  1. Rail Car Type wizard window will open. Specify Agent type name as Locomotive and leave the Create the agent type "from scratch" selected. Press Next.

  1. In the next step choose Locomotive 14.1m as its 3D animation shape.

  1. Add 5 more rail car types in the same way: BoxCar, OpenCar, GondolaCar, TankCar and HopperCar. Set corresponding 3D shapes for them.
  2. Similarly, create Train type (by dragging Train Type from the Rail Library palette) and name it Train.
 Modify TrainSource block

We will set the trainSource block to generate trains of our custom type Train by modifying its parameters in the Trains and cars section.
  1. New train: Train


  1. In the same Trains and cars section, specify dynamic expression in the New rail car field.
                Switch to the code field by clicking on the  icon which will turn into allowing you to insert the code in the field right next to it.

                Type the expression as shown in the figure below.

We use conditional operator to set the railcar type for the rail cars of the train being created. We want to diversify railcars of our freight train. The first railcar should be of type Locomotive. Successive railcars can be of any other railcar type. 

When TrainSource creates a new train, it successively calls the expression specified in the New rail car field for every railcar of the train. Using the local variable carindex we pass the index of the currently created railcar to the expression. 

The conditional operator allows you to choose between two given alternatives depending on the value of a Boolean expression. In our example, if the carindex == 0 expression evaluates to true, then the current railcar is the first car of the train and we set its type to Locomotive. Otherwise, the expression evaluates to false and for the successive railcars we call the randomlyCreate() function and provide the classes of the railcar types to randomly choose the types for other railcars in the train.

As a result, we will have a train with a loco as the first car, followed by ten rail cars of our custom rail car types.

Watch the model in 3D mode
  1. Add 3D Window from the Presentation palette, place it below your flowchart and animation.

  1. Run the model.

    When you create a 3D window, AnyLogic adds a view area that allows you to easily navigate to the 3D view at runtime. To switch to this 3D view while the model is running, open the developer panel by clicking the Developer panel  control in the right corner of the control panel. In the developer panel, expand the  select view area to navigate list and select [window3d] from the list.

    The view area expands the 3D animation scene to the model window's full size.
  2.  Now you can see the train consisting of a locomotive as the first rail car, followed by ten rail cars of other types we have defined in this phase.


Reference model: Hump Yard - Phase 3



 Phase 2. Defining logic

Phase 4. Modeling rail car classification