Search code examples
anylogic

How do I decide direction of movement for a Train agent in AnyLogic?


I'm using the rail library and am dynamically giving my Train tracks to move to. Since I do not know in advance where my Train will be going I need to dynamically decide if it will be moving forward or backward. For testing purposes of other algorithms and system, I've used a hardcoded list of booleans based on a predefined route (list of tracks).

I know the Train agent can return its orientation on the track, but how can I use that information to figure out the direction of movement based on the next track in a given route? I do have access to the next railway track object.

Note the train is static when trying to compute the direction of the next movement. So I cannot use any function dealing with offsets to target or direction of movement.

I did look through the functions for Railway Track, Switch, Train, and Railway Network to find a solution but was unable to.

Another idea I had was to have a try catch block around the MoveTo block somehow, so I can just catch an error for moving in the wrong direction and try the other direction.


Solution

  • I was able to solve this problem with 4 pieces of information. The object of the track I'm currently on, the object of the next track I'm moving to, the orientation of the train relative to the current track, and knowing what end of the track a given switch is on.

    I am maintaining a discrete list of each track object my train travels on. This list is how I'm getting current and next track objects.

    As per the AnyLogic Track docs, you can call track.getEndSwitch() or track.getStartSwitch(). These two functions return their respective switches or null if there is not one. For this solution you'll just need to use one.

    When we look at the documentation for switches. we can find the function railSwitch.stateExist(currentTrack, nextTrack). This function will return true if there is a state the given railSwitch will the currentTrack and nextTrack Railway Track objects. So if this returns true and railSwitch == trackEndSwitch we are moving toward the end of the track.

    Finally, we can use the train documentation to find the train.getOrientation(true) function. The argument is bool that tells the function to use the front or rear of the train; this may be important depending on the length of your train. It returns true if the train is the same orientation.

    Now we can take two pieces of information to decided train movement direction. If we XOR the orientation of the train (true for same as track) and the switch being moved toward (true for the end switch) it will give us the value of forward or backward move for the train. False means forward movement, True means backward movement.

    | Orientation | Toward End Switch | Direction of Movement (x ^ y)

    | Toward Track End | Yes | False

    | Toward Track End | No | True

    | Toward Track Begin| Yes | True

    | Toward Track Begin| No | False