.

Trainz Developer Blog

Using the Trainz AI in sessions, Part 1

Rating: 5 votes, 4.60 average.
The Trainz AI is a very powerful, very flexible system. However, this complexity and power can lead to perceived unreliability and recurring problems. Without a clear understanding of what it is doing and why, these problems can prove to be frustrating and difficult to solve.

All AI orders that cause the AI to do some driving operate in much the same way. A route is calculated for the AI to follow, in the form of a list of actions (mostly of the form "take junction X in direction Y"). Then the AI is asked to complete that list of instructions. This will run until it either completes the list, or gets completely stuck and gives up. If it gets completely stuck, the order will try to re-calculate the route from its current location, and it will try again.

Making the list of instructions is done with a breadth-first search, to find the shortest path to the destination. Note that the shortest path will generally use the inside route on multiple track curves, and will change direction and go backwards if it thinks it can get there via a shorter path that way. If your route is more at the 'model railway' end of the scale and has a looped track, you may even find the AI decides to drive backwards around the circuit rather than forwards.

The AI Direction marker

The AI direction marker tells the AI which way traffic is expected to flow on a specific track. It should be considered as a "No Entry" sign. It applies at the junction that the pointy end of the marker points towards. It tells the AI not to plan a route which crosses that junction and enters into the one-way track in the "wrong" direction.

Note that it doesn't affect an AI that already started on a specific track, e.g. at the start of a session.

Also, it does not convey additional priority information. If there are three tracks between A and B, an 'up' line with a track direction marker preventing trains from B from entering the line, a 'down' line with a track direction marker preventing trains from A from entering the line, and a reversible line with no markers; an AI at 'A' will not prefer the 'up' line to the reversible one. It will pick the shorter route, whichever this happens to be. You might want to consider using a track priority marker instead to help with this case.

The Track priority marker

Trainz allows track to be tagged with a 'priority' level 1, 2, or 3. Both track and trains default to 'priority 2'. AI trains can also be allocated a priority, and AIs will prefer to use tracks of their own priority.

If you were to set high speed lines to have priority 1, normal lines to be priority 2, and freight yards and loops to have priority 3, a train with a priority of 1 will try to use the high-speed lines wherever possible, and a train with a priority of 2 will try to stick to the standard lines (avoiding both the high speed lines and the yards and loops). A train with priority of 3 will try to avoid using the mainline (in this example, this might be a good idea for a yard shunter, which will then prefer not to block the mainline if at all possible).

Note that the priority setting has nothing at all to do with which AI gets to go first at a junction when there is a conflict it only affects the routing of trains along different tracks. If you want to control which AI gets to go first over a junction, you'll need to do that by issuing a sequence of AI orders that will achieve that result, e.g. by making one train wait for the other to pass before continuing.

'Drive ...' and 'Navigate ...' orders

The difference between the 'drive' and 'navigate' commands is how they calculate the route ahead when they see other traincars on the track.

'Navigate ...' will attempt to route around other traincars. 'Navigate ...' orders can get an AI to pick its way through a full yard, working around all the other traincars. However, because AI orders calculate paths when they first start running, if the other traincars have moved by the time the AI gets to that location, it will still take the loop, and things can look rather odd.

'Drive' doesn't attempt to route around other trains at all. It expects to queue up behind the train in front, and will continue on when it moves. This solves the problem where 'Navigate' will appear to take loops at random, and in a busy mainline situation 'Drive' can produce reliable, predictable pathing which 'Navigate' orders will not. However, trying to use 'Drive' in a yard, where the other traincars won't move out of the way will result in a stuck AI waiting for a stationary consist to be moved out of its way for it.

You will need to use 'Navigate' wherever stationary traincars will be encountered - all yard movements, switching operations, and so on. 'Drive' is best in a multiple track mainline situation where tracks are operated in one direction only. Where you have a reversible mainline (e.g. single track with passing loops), you'll need to experiment, and decide which one is best at each point in the schedule.

These orders also come in '... via trackmark' versions. The 'via' versions complete early, when the '... to trackmark' version would have started braking. This allows them to hand off at speed to the next order rather than forcing the train to slow down.

Comments

  1. Bill69's Avatar
    I think I understand the AI system fairly well. However although the above is all true there are other things to take into consideration. The main one being signalling, as signals must be placed to allow a given train to complete it's orders. This is even more important in TS12 than it has been in previous versions. TRS2004 and TRS2006 had a very good operating AI system and even TRS2009 wasn't bad but TR12 with SP1 and HF1,2 & 3 can be very difficult to enforce. In the latest version a train seems to be very reluctant to see a trackmark that is beyond two junctions therefore more trackmarks have to be used to keep the train on it's intended path unless additional orders such as 'control junction' and 'free junction' are used.
    There also seems to be a problem when a trailing junction set against the train is encountered, even when it has a protecting signal and a trackmark is placed soon after the junction. Earlier versions up to TRS2009 did not have this problem.

    Cheers,
    Bill69
  2. TrainzDev's Avatar
    Quote Originally Posted by Bill69
    However although the above is all true there are other things to take into consideration.
    That's why this is only part 1.
  3. TrainzDev's Avatar
    Quote Originally Posted by TrainzDev
    That's why this is only part 1.
    Part 2 (dealing with some more complex topics) is here.
  4. ish6's Avatar
    Thanks! Very insightful!!!

    Ish
  5. norfolksouthern37's Avatar
    I have a question.

    Lets say I have a very long stretch of double-tracked main line, it is bi directional on both tracks. Every so often there is a crossover point, where one train can go from one track to the other. If I simply route a train from one end of the line to the other, it frequently changes from one track to the next depending on what one it thinks is closest, even though we are on double track. So really all he is doing is unnecessarily blocking two tracks. I thought this could be fixed if I turned the crossover points from track priority x to one below or above, but that doesn't help, he still wants to go right over them. So my question is, how can I avoid having them go from one side to the other yet still leave the option to be able to do so (i.e not using direction markers that would stop the AI and foul the signals).

    Another thing this causes is that the AI train gets to the crossover point, and there is a signal in front of that junction, the signal will be clear for line straight ahead, but at the last second the AI will switch the junction causing the signal to drop to red and SPAD. This is highly annoying.
  6. Tony_Hilliam's Avatar
    The AI is crossing over because it wants to take the shortest route. The bi-directional track says this is fine. Your options are really to make the track uni-directional (using direction markers) or providing the train a more specific route path to follow using via trackmark. This will effectively add waypoints to keep it on the piece of track you want it to stay on.

    Normally the AI will path ahead so any last second change leading to a SPAD sounds like something else is happening/changing (i.e. why is the AI waiting until the last second?)
  7. ish6's Avatar
    ---- Thanks Tony!!! Good to know these things!

    Ish
  8. Dap's Avatar
    When I am dealing with double track with cross overs, I use statigically placed track marks at the cross overs so I can keep the train routed on the track I intend. This does not give AI the option to make the decision of which route to choose. Nav via trackmark is the driver command I use. After reading this blog, I may go back to using Drive via. Seems like it would keep AI more under my control.

    David