TT Pathfinding algorithm (for trains)


Written by Martin Koetsier.


At each switch a train encounters on it's route it will decide which way to go on basis of the following factors:

  1. Switch position (normal or reverse; see figure 1)
    If the other conditions are equal for both routes the program prefers a normal- over a reverse swichposition approximately 80% of the time.
  2. Apparent detours WITHIN THE FIRST 64 SQUARES from the switch
    - tunnels (less disturbing)
    - track apparantly leading away from the destination
  3. Signals (see figure 1)
    Factors 1 and 2 determine a trains route. If the selected route begins with a signal which shows red the train will wait in front of one-way signal. If the red signal is two-way then the other route will be choosen.

Figure 1: the switch position and signals

A train running over the red line takes the switch in normal position where a train running along the yellow line passes the switch in reverse position.

The red line section behind the switch is secured by a (two-way) signal. If a second train would try to enter the section while the signal shows red it will take the yellow line instead. If the signal would be one-way and showing red that second train will wait in front off the signal until it clears.

Once a first idea on how the pathfinding algorithm works, had come an existing game was taken to put the theory to the test. The game is situated in the East Anglia territory from Paul Margetson. In order to create some income, which gives us an endless amount of testing time, there are 14 coaltrains left. They provide a service between respectively the March coalmine or the Thetford coalmine and Beccles Power Station. The program allows a maximum of 80 trains. Therefore 66 trains (downsized to locs only) are available for testdriving in the north western part of the scenario: between (the east of) Kings Lynn and Bacton (Woods Station).

At the end of this page both the scenario itself and this document with it's pictures are available for downloading.

Figure 2: basic track lay-out

  1. a piece of track is removed to collect the 66 trains; rebuild track to start test.
  2. Test switch: here we see how trains behave in relation to destination and projected path.
  3. a siding moving partially away (1 square) from the destination; referred to as a detour.
  4. a detour on the "reverse" (switchposition direction) route.
  5. a piece of track is removed in order to count number of trains on either route.

The '<' and '>' indicate the directions the trains run in on the particular tracks.

Test information
All 66 testlocs have gathered in front of point A. They all have one destination: Bacton Woods Station. This station can only be reached by taking the testswitch in reverse position. If the testswitch is taken in normal position the train will eventually turn up at point A again. Both tracks at point E have been removed so trains will queue to be counted. Just behind the testswitch, in both directions, there is a one-way signal.
So once a train has made up it's mind with regard which route to take it will stick to that route no matter what.

Test 1
Both sidings (points C and D) can be passed in normal switchposition. Neither the "normal" (switchposition direction) route nor the "reverse" route has a tunnel. Now what do you expect when you rebuild the track at position A? Will trains find the right way or won't they? This is what happened ...

Testresults test 1
testrun *1 *2 *3 *4 *5 *6 7 8 9 10 11 12 Total Perc.
normal 21 28 23 30 29 30 60 48 52 48 51 49 469 78
reverse 4 8 13 6 7 6 6 18 14 18 15 17 132 22
(Note: the 1st testrun had only 25 and the 2nd-6th 36 testlocs.)

Conclusions from Test 1
Although the trains generally prefer the normal route (which does NOT lead them to their destination) their appears to be a random factor involved. But why don't the trains recognize that the reverse route should be preferred over the normal route? Afterall we did put one-way signals behind the testswitch. Well, it looks like the signaltype is the last check to be performed. Trains don't look indefinately far ahead.

Test 2
So how far do trains look? By trial and error the number 64 came up. Trains look for irregularities (i.e. end of track, tunnels, detours) for the first 64 squares behind the switch. So now we take out the track at the 64th square. First we have to rebuild the tracks at point E and remove the track at point A so the trains can queue up again in the starting position. Once they're all past point E we remove both tracks again and rebuild position A.

Now we see all trains choose for the reverse route. They spotted the flaw in the normal route and acted upon that information. We make the trains queue up at point A again, rebuild the track at square 64 and remove the next section of track (square 65). Remove the tracks at point E, rebuild the track at point A and let them run. Now you'll see most trains choosing to go via the normal route (that is as long as there is space; the track can't cope with the number of trains. They'll queue up at the testswitch) and a few over the reverse route.

Conclusion from Test 2
Now we know until where a train will prospect possible routes. We started counting at 1; computers start at 0; so square 0 to 63 ... it's like the program has 6 bits available for this task.

Test 3
In this test both routes take turn in having a tunnel replace a part of the tracks (ofcourse within 64 squares from the testswitch. Actually it's just the entrance to the tunnel that must be within range). It will show that the perfect track will be the choice of all trains over the track with the tunnel.

Conclusion Test 3
Wolfgang Preiss was right when he stated that tunnels somehow had an effect on the trains finding their way. But what if both tracks have a tunnel?

Test 4
In this test we give both routes ONE tunnel. The first 7 testruns had a tunnel starting at square 5 along the normal route and a tunnel at square 11 along the reverse route. Testruns 8 till 14 the tunnel along the normal route was moved to square 35. In the last testrun this tunnel was made considerably larger than the other tunnel.

Testresults test 4
testrun 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Total Perc.
normal 59 51 54 51 58 52 54 57 48 52 53 57 60 51 58 815 82
reverse 7 15 12 15 8 14 12 9 18 14 13 9 6 15 8 175 18

Conclusions test 4
The tests show that if both directions have ONE tunnel the lenght of each tunnel or it's position relative to the tunnel in the other track do not influence the choice of trains. The number of tunnels in a track do make a difference though. Testrun 16 had 2 tunnels along the normal route. Now all trains preferred the reverse route.

Detours
In this layout I added two detours, points C and D. Let's take a closer look ...

Figure 3: siding at point C

This is the siding along the normal route. Trains enter from the bottom left of the picture. The best, shortest route is via C1 since the destination (eventhough trains can't get there via this route. They don't "know" that yet) is somewhere in the top right of the picture. C2 and C3 are the actual detours.

Figure 4: siding at point D

This is the siding along the reverse route. Trains enter from the bottom right. The best, shortest route is via D1 since the destination is somewhere in the top right of the picture. D2 and D3 are the actual detours here.

Detour-testing
If we put a detour on either one of the routes and keep the other route perfect, the perfect route will get 100% of the trains. If we match the detours exactly on both tracks (C2 versus D2 or C3 versus D3) then we get the same mixed result as in Test 1. But if we differ in detour (C2 versus D3 or D2 versus C3) then the greater detour (D3 over C2; C3 over D2) loses out. If we were to add an extra detour along the normal route, one just like C2, then C2 and this new detour compared to D3 would give another result as Test 1.

Tunnels versus detours
As stated in the beginning of this page, ONE tunnel is preferred over the smallest detour. Two tunnels in a row compared to the smallest detour would result in a mix again. So two tunnels along the normal route and a detour via D2 would produce a result as test 1. Two tunnels and a detour via D3 on the other hand would result in a 100% score for the tunnels.

Summary
This theory on the Tranport Tycoon pathfinding algorithm is not likely to be the final word on the subject. It merely describes the basic pattern with which trains find their way. The layout was relatively simple. More switches between the testswitch and the maximum, 64 square scope of trains might alter some of the outcome. Any thoughts or comments on the subject will be widely appreciated and ofcourse new findings will be published here as well.

The testresults from Test 1 and Test 4 add up to a total 1591 testruns. 1284 runs (80.70%) resulted in a choice for running along the normal switchposition versus 307 runs (19.30%) over the reverse position. I therefore assume for the moment that the random factor favours the normal- over the reverse switchposition like 4 to 1.




Download the theory and the testscenario
Download the testscenario and this webpage here. Make sure to put all but the .sv1-file in one directory or your browser will not be able to show the page properly.