Better randomization of engine failures

Post your problems and installation issues here!

Better randomization of engine failures

Unread postby GaryG » Sat Dec 03, 2011 1:37 pm

Hi

For previous information, see the "Donner Pass scenario 5] Grade crossing (bad weather)" thread.

I put on my thinking cap and came to the conclusion that as written, the FailRange value returned is always 2000 - it is not a range. The lua file calculation appears to create a random number that is perhaps in the range of 1-2000 then it's divided by 2000. If this is the case, the number returned from the EngineScript is meaningless and has no effect on the value of the testing of the random number. Am I correct in this assumption? I need someone who knows how both the math.random function and how the math in the lua script works to reply.

The lua script now checks the result and if greater than 0.95 fails the engine. These tests are set to occur once each 60 seconds so if the engine uses these calculations, the odds are ~5 in 100 or ~1 in 20 that it will fail in any particular minute.

What I'm thinking is to change the lua script so the odds become an actual 1-FailRange chance of failure. I think I am correct if I take a random number in the 0-1 range, multiply it by the FailRange (in this case 2000) then test for a result of less than 1 to get a 1 in 2000 odds result. Because there is not a division being done, a zero random number shouldn't cause a failure. Again, I need someone who knows to let me know how the rewrite of the lua script should be done to accomplish this.

Thanks,
GaryG
GaryG
 
Posts: 208
Joined: Tue Feb 08, 2011 2:24 pm
Location: Vancouver. BC, Canada

Re: Better randomization of engine failures

Unread postby Kali » Sat Dec 03, 2011 1:54 pm

1000/2000 in lua is 0.5 , not 0. So basically any number over 1900 will be a fail. Why they didn't just check that and save a division I don't know... is that number used anywhere else?
Kali
 
Posts: 1600
Joined: Mon Mar 14, 2011 1:00 am
Location: England-by-Sea

Re: Better randomization of engine failures

Unread postby GaryG » Sat Dec 03, 2011 8:52 pm

Hi Kali

No volunteers so later this evening I'm going to experiment with multiplying the random number returned by the FailRange then fail the loco if the result is greater than 1.0 which would give a 1 in FailRange chance of failing each elapsed minute. FailRange appears to be a very poorly named parameter, there is no range definable by a single number.

For the test, I'll try a fixed 200 and test every ten seconds, capturing the result in the log and see what happens.

I don't know what language is used and can't find examples where math.random() actually has optional parameters like the lua script appears to have, as used "math.random( 1, gFailRange )". I'm just throwing darts at the programming dartboard blindfolded. *!!wink!!*

Some time has elapsed...

Well, it does look like it won't be too difficult. I tried one loco and a number of 200 for the "range". First run failed after 24 passes and the second run failed after 92 passes. The actual random numbers being checked were all between zero and 200 so it looks like my idea will work. Going to use the (hopefully) final code for the three loco types used in this scenario and see what happens. The only changes are for the dodgy locomotive lua files. I'm still not certain if we can actually restart a failed loco.

GaryG
GaryG
 
Posts: 208
Joined: Tue Feb 08, 2011 2:24 pm
Location: Vancouver. BC, Canada

Re: Better randomization of engine failures

Unread postby Kali » Sat Dec 03, 2011 9:19 pm

Engine failures in a nutshell:

Code: Select all
local FAIL_ABOVE = 1950
local FAIL_RANGE = 2000

function Update( p_nInterval )
... stuff

if math.random( 1, FAIL_RANGE ) > FAIL_ABOVE and not failed then
  failed = true
  Call("SetControlValue", "Startup", 0, 0 )
end

... more stuff
end


however from what you've said, that does exactly the same thing that the RSC code does.

A version with more chance of failing the wider the throttle is open:

Code: Select all
-- publically twiddleable stuff

local FAIL_MAX_CHANCE = 2000
local FAIL_CHANCE_PERCENT = 5
local FAIL_WITH_POWER_RANGE = 250

--- leave it alone below here

local FAIL_ABOVE = FAIL_MAX_CHANCE * ((100- FAIL_CHANCE_PERCENT )/ 100 )
local FAIL_EVERY_TEST = 60
local gnFailureTimeout = 0

function Update( p_nInterval )

 lnReg = Call("GetControlValue", "Regulator", 0 )

if gnFailureTimeout > FAIL_EVERY_TEST then

 if math.random( FAIL_MAX_CHANCE ) > ( FAIL_ABOVE - ( FAIL_WITH_POWER_RANGE * lnReg )) and not failed then
   failed = true
   Call("SetControlValue", "Startup", 0, 0 )
 end

 gnFailureTimeout = 0

else
  gnFailureTimeout = gnFailureTimeout + p_nInterval
end

end
Last edited by Kali on Sun Dec 04, 2011 5:54 am, edited 1 time in total.
Kali
 
Posts: 1600
Joined: Mon Mar 14, 2011 1:00 am
Location: England-by-Sea

Re: Better randomization of engine failures

Unread postby GaryG » Sun Dec 04, 2011 1:25 am

Hi Kali

From what I can tell, "if math.random( 1, FAIL_RANGE )" just returns random numbers in the zero to one range, I don't think the two parameters make a difference.

I am currently testing:
if gFailed == FALSE then
gFailRange = Call( "*:GetControlValue", "FailRange", 0 );
gFailureChance = math.random();
gFailureChance = gFailureChance * gFailRange;
if gFailureChance < 1.0 then
Call ( "SetPowerProportion", 0, 0 );
Call( "*:SetControlValue", "Startup", 0, -1 );
gFailed = TRUE;
end
end

I seems to work but the 2000 number in the other file gives a probability of a failure of in 5.5 hours. I just took the train over the hill in bad weather without a failure so I think I'll try 1000 instead. I like the idea of checking the power and letting it affect the randomness as well.

Additionally, has anyone noticed the density of the loco exhaust is based on Tractive Effort and calculated in this lua as well. Looking at the exhaust plumes when power is applied can show locos that aren't supplying power.

GaryG

EDITED - The first partial post was submitted by accident...
GaryG
 
Posts: 208
Joined: Tue Feb 08, 2011 2:24 pm
Location: Vancouver. BC, Canada

Re: Better randomization of engine failures

Unread postby Kali » Sun Dec 04, 2011 5:52 am

Yes,I keep having to go edit every script with TE-based smoke to get rid of it - engines do not smoke less when they get faster! I'm not having a mini-rant about that again here though.

math.random( 1, 2000 ) is the same as math.random( 2000 ) so perhaps try the latter. To quote the lua 5.0 manual - we are still stuck with 5.0 as far as I know:

The Lua Manual wrote:When called without arguments, math.random returns a pseudo-random real number in the range [0,1). When called with a number n, math.random returns a pseudo-random integer in the range [1,n]. When called with two arguments, l and u, math.random returns a pseudo-random integer in the range [l,u]


Hmm, ok, someone did get round to using SetPowerProportion - must find out what that does.

Edit: just fixed my code, the idea was fine, I was just using a non-existant variable so the argument was actually math.random( 1, nil ) which is the same as math.random( 1 ).

My second one only checks once a minute, so you can cheat by shutting the throttle when it checks - keeping track of how long the throttle has been open is called "watching the temperature gauge" and I have code for engine temp control with fans & whatever somewhere. That is actually more complicated than this part.
Kali
 
Posts: 1600
Joined: Mon Mar 14, 2011 1:00 am
Location: England-by-Sea

Re: Better randomization of engine failures

Unread postby GaryG » Sun Dec 04, 2011 1:44 pm

Hi

Well it appears to work. In the modified xml and bin files, the Default value of the FailRange section is the maximum for the random number generator so if you want two hour randomness, use 120. I set the SD's to two hours and the Dash to three hours (they are newer and should be more reliable).

When I ran the last test, one SD failed at ninteen minutes and a second failed at twenty minutes; nothing else had failed when I shut the program down at thirty minutes. Looking at the loco exhausts after a failure will definitely show failed locos if the throttle is at run 3 or higher. Any failed loco exhaust does not increase with higher throttle settings.

GaryG

FOR TESTING PURPOSES - DO NOT INSTALL IF YOU DO BACKUP THE FILES THAT WILL BE CHANGED FIRST!
You do not have the required permissions to view the files attached to this post.
GaryG
 
Posts: 208
Joined: Tue Feb 08, 2011 2:24 pm
Location: Vancouver. BC, Canada

Re: Better randomization of engine failures

Unread postby rninrvr » Sun Dec 04, 2011 3:41 pm

Gary,

I have not run Donner yet, but, as always, am game to try out your patches so have loaded them into the RW folders. Will now try to run some of the scenarios to see what is going to happen.
Sharon
Activities are what make any rail simulator run
rninrvr
 
Posts: 35
Joined: Sat Dec 26, 2009 2:27 pm

Re: Better randomization of engine failures

Unread postby GaryG » Sun Dec 04, 2011 4:59 pm

Hi

It appears there is a glitch in my lua code. I think caused by guessing at the programming language.

I had;
gFailureChance = math.random();
gFailureChance = gFailureChance * gFailRange;

I don't think you can use the a = a [*\+- ...] format in the same calculation.

I'm now trying:
gFailureChance = math.random() * gFailRange;

If someone has better knowledge with the language syntax used in the lua file than I do, please volunteer to take over!!!

GaryG
GaryG
 
Posts: 208
Joined: Tue Feb 08, 2011 2:24 pm
Location: Vancouver. BC, Canada

Re: Better randomization of engine failures

Unread postby GaryG » Sun Dec 04, 2011 5:34 pm

I think I have it fixed. Running the Scenario now with the log running to watch the lua fail test results - not even really moving the train, I want to watch the randomness.

As an aside, it is very interesting seeing how busy the SchedulerModule is among all the other items that show.

GaryG
GaryG
 
Posts: 208
Joined: Tue Feb 08, 2011 2:24 pm
Location: Vancouver. BC, Canada

Re: Better randomization of engine failures

Unread postby Kali » Sun Dec 04, 2011 7:10 pm

Yes you can. You have to in fact, there's no *= operator.
Kali
 
Posts: 1600
Joined: Mon Mar 14, 2011 1:00 am
Location: England-by-Sea

Re: Better randomization of engine failures

Unread postby GaryG » Sun Dec 04, 2011 8:19 pm

Hi

I got it working. I left it running while I went out for a while.

An SD40-2 failed at 10 minutes
An SD40T-2 failed at 60 minutes
After 89 minutes, two more locos could have failed but hadn't.

GaryG
You do not have the required permissions to view the files attached to this post.
GaryG
 
Posts: 208
Joined: Tue Feb 08, 2011 2:24 pm
Location: Vancouver. BC, Canada


Return to Problems and Peculiarities

Who is online

Users browsing this forum: No registered users and 1 guest

cron