KSP-KOS / KOS

Fully programmable autopilot mod for KSP. Originally By Nivekk
Other
691 stars 229 forks source link

Add support for reproducible pseudo random number sequences #2768

Closed plonk75 closed 3 years ago

plonk75 commented 3 years ago

As far as I understand, there is currently no way to seed the PRNG with an arbitrary seed. I've searched the code for "random" and found what I think is the random number generator delegate in /kOS.Safe/Function/Math.cs, but I see no way to actually seed it to get reproducible random number sequences.

I believe to understand how the current MATH:RANDOM() method works internally when I read the existing C# code, but as I'm not particularly familiar with C# or the overall design of kOS, I don't see where to inject a seed value and how to keep the seeded Random object in the right scope, making it even reseedable. So I can't make a PR myself.

Background: When my sparetime allows it, I'm "working" on a self improving spaceplane launch script, basically using a "randomize last best parameter set - run scripts - evaluate flight - pick next best parameter set - repeat" approach. IIRC, it was Kevin Gisi who published a video on YT where a script finds a transfer orbit from Kerbin to the Mun using this or a very similar approach.

Problem is: The required number of flights to probe the parameter space is exponentially growing with every dimension in parameter space. That's where the fun part starts, finding optimizations. When optimizing, e.g. using the accumulated results of several flights to extrapolate some kind of gradient in parameter space and cost function value, and using this data to do a less random "quasi conjugate gradient" approach, I see no way of actually repeating the randomized modifications on the parameters, and this makes assessment of optimizations very difficult.

At this point reproducible pseudo random number sequences would be extremely nice.

Please excuse my clunky wording and language. I hope you get the idea.

Dunbaratu commented 3 years ago

It shouldn't be too hard to make an option to force a seed.

Dunbaratu commented 3 years ago

This should have been closed by PR #2801 but I had a typo in the "Fixes #" tag there so I have to close it manually now.