NPF patch (occupation check)

Forum for technical discussions regarding development. If you have a general suggestion, problem or comment, please use one of the other forums.

Moderator: OpenTTD Developers

Post Reply
TBOT
Route Supervisor
Route Supervisor
Posts: 441
Joined: 30 Jul 2003 18:36
Location: The Codecave

NPF patch (occupation check)

Post by TBOT »

I've done some work on improving the NPF, mainly because it very reluctant at considering alternative routes when the optimal one was crowded/jammed.

So I've modified the cost function a bit:

1. Occupied track is weighed against the distance from the train:
This allows a train to look at how crowded a route is, and consider less crowded alternatives. The distance from the train is taken from the cost to reach a node. A weight is calculated upon that:
With x being the cost to reach this node, and high_limit, low_limit and max_weight as added 'npf_rail_occupied_*' variables.

Code: Select all

for x <= high_limit: w = max_weight
for x >= low_limit: w = 1.0
for high_limit < x < low_limit: linear interpolation between max_weight and 1.0
This cost of an occupied tile is multiplied with this weight (with the exception of extra cost for red signals).

The initial values chosen for the variables are:
max_weight = 3
high_limit = 1000
low_limit = 5000

This means that till a distance of roughly 10 tiles the weight of 3 applies (i.e. triple the length of the occupied track may be chosen as alternative), and that after roughly 50 tiles no extra weight is given.

Note that these are very rough guesses, based on what seemed to work for me. The values may be heavily dependent on train speed (maybe the limits should be scaled with it?) and network layout, so feel free to play around.


2. Red presignal entries which are not the first encountered also get a penalty:
In general this allows custom tweaking of weights. An example may be crossovers on a double track, which should be very strongly discouraged if the destination track is occupied. The more red presignal entries, the more likely a suboptimal alternative is chosen, which in the example may be an alternative approach with tunnels/bridges.

The penalty can be changed with the 'npf_rail_red_entry_penalty' variable.


3. The higher firstred penalty now also applies to presignal entries:
In line with the 2nd change, so the direct choice for a red presignal entry is even more strongly discouraged.



Attached is a patch upon r4341 (I hope I did it right ;)), as well as a windows executable. Please feel free to test around and tweak the variables (especially for different train speeds).

Edit: updated attachments to use integer math and fixed the interpolation. Making the technical note useless ;).


Technical note:
In the weight implementation I use a floating point variable, for which no unified type was available. While floats should be supported on pretty much every platform, their behaviour may not be the same and therefore may cause very rare desyncs in network games.
Also the config file doesn't support input of floats (as far as I could see), so the max_weight variable is limited to rounded numbers only.
Should floats be a major objection I guess the weight can be given in 1/10'ths or something.
Any thoughts?
Attachments
OpenTTD-npfpatch-r4342.zip
Windows executable
(581.15 KiB) Downloaded 147 times
npfpatch-r4342.patch
Patch upon r4341
(6.31 KiB) Downloaded 190 times
Last edited by TBOT on 10 Apr 2006 10:28, edited 1 time in total.
"Peace cannot be kept by force. It can only be achieved by understanding." - Albert Einstein
suikerpluimpje
Engineer
Engineer
Posts: 18
Joined: 03 Apr 2006 09:51
Location: Enschede (The Netherlands)

Post by suikerpluimpje »

sounds very nice. I will try it this afternoon.
madman2003
Engineer
Engineer
Posts: 116
Joined: 20 May 2004 17:21

Post by madman2003 »

Floating point math is not acceptable for these kinds of things as far as i know, for the very reason you already mentioned.

Since the end result is going to be an int, why not do integer math, just scale up the calculations and at the end define cost as weight/scalefactor. That way you have minimized rounding errors as far as i can tell.
User avatar
XeryusTC
Tycoon
Tycoon
Posts: 15415
Joined: 02 May 2005 11:05
Skype: XeryusTC
Location: localhost

Post by XeryusTC »

suikerpluimpje wrote:sounds very nice. I will try it this afternoon.
Same thing here, I will try it when I fix the patches I already applied (I kinda screwed up) and I'll add this one.
Don't panic - My YouTube channel - Follow me on twitter (@XeryusTC) - Play Tribes: Ascend - Tired of Dropbox? Try SpiderOak (use this link and we both get 1GB extra space)
Image
OpenTTD: manual #openttdcoop: blog | wiki | public server | NewGRF pack | DevZone
Image Image Image Image Image Image Image
TBOT
Route Supervisor
Route Supervisor
Posts: 441
Joined: 30 Jul 2003 18:36
Location: The Codecave

Post by TBOT »

madman2003 wrote:Since the end result is going to be an int, why not do integer math, just scale up the calculations and at the end define cost as weight/scalefactor. That way you have minimized rounding errors as far as i can tell.
Yeah, that's the way to go without floats, already figured that one out. Still floats are fairly intuitive to use in something like this :).

Anyway, this shouldn't spoil the fun for now (it's still a concept), and the chance it'll go wrong is virtually non-existant (the error has to survive integer cutoff, and after that still be big enough to actually change a route).
"Peace cannot be kept by force. It can only be achieved by understanding." - Albert Einstein
madman2003
Engineer
Engineer
Posts: 116
Joined: 20 May 2004 17:21

Post by madman2003 »

This patch (if it behaves on the long run) might be nice to have openttd, but it will never get in if it uses floats. I've included a patch which does it the way i would do it, but ofcource you know more about the patch :-)
Attachments
openttd_npf_patch_10-04-2006.diff
Converted to integer math, does it work right?
(6.28 KiB) Downloaded 158 times
TBOT
Route Supervisor
Route Supervisor
Posts: 441
Joined: 30 Jul 2003 18:36
Location: The Codecave

Post by TBOT »

madman2003 wrote:This patch (if it behaves on the long run) might be nice to have openttd, but it will never get in if it uses floats. I've included a patch which does it the way i would do it, but ofcource you know more about the patch :-)
Updated the original attachments. It now uses the scalefactor (your patch would cause integer cutoff errors though). Also, I've fixed an error in the formula, as the linear interpolation was actually applied wrongfully :roll: (swapped a sign...).
"Peace cannot be kept by force. It can only be achieved by understanding." - Albert Einstein
Post Reply

Return to “OpenTTD Development”

Who is online

Users browsing this forum: No registered users and 46 guests