ENH: seed Monte Carlo per simulation index for worker-invariant inputs#15
Open
thc1006 wants to merge 1 commit into
Open
ENH: seed Monte Carlo per simulation index for worker-invariant inputs#15thc1006 wants to merge 1 commit into
thc1006 wants to merge 1 commit into
Conversation
MonteCarlo seeded the stochastic models once per worker (parallel) or once before the loop (serial), so the generated inputs depended on how many workers ran and which worker drew which simulation. Two runs of the same seed with different worker counts produced different inputs. Seed per simulation index instead: spawn one SeedSequence child per index from the run's root seed, and reseed the stochastic models from child_seeds[i] before simulation i. SeedSequence.spawn is invariant to the spawn count, so index i always maps to the same child, including in append mode. Each index seed is split three ways so the environment, rocket and flight do not share a stream. Inputs are now identical across serial, parallel(2) and parallel(N) for a fixed seed, and a split append run reproduces a single run. This changes the numbers a fixed seed produces (the per-model decorrelation, plus a serial index that now counts from 0 like the parallel path already did), so stored baselines regenerate. Adds tests/unit/simulation/test_montecarlo_determinism.py covering serial reproducibility, worker-invariance, append reproducibility, and the None-seed path. Marked slow (each simulation rebuilds a full rocket) per the existing test_monte_carlo_simulate convention.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This makes Monte Carlo input sampling reproducible regardless of how the run is executed. Today the stochastic models are seeded once per worker (parallel) or once before the loop (serial), so the inputs you get depend on the worker count and on which worker draws which simulation. Two runs with the same seed but different worker counts produce different inputs.
The change seeds per simulation index instead. From the run's root seed I spawn one
SeedSequencechild per index, and reseed the models fromchild_seeds[i]before simulationi.SeedSequence.spawnis invariant to the spawn count, so indexialways maps to the same child, including in append mode. Each index seed is then split three ways so the environment, rocket, and flight don't share a draw stream.After this, the generated inputs are identical across serial, parallel(2), and parallel(N) for a fixed seed, and a split append run (3, then +3) matches a single run of 6.
Behavior changes worth calling out
Both shift the numbers a fixed seed produces, so any stored baseline regenerates:
indexfield now counts from 0, matching the parallel path, which already did.Tests
New
tests/unit/simulation/test_montecarlo_determinism.py: serial reproducibility, worker-invariance (serial == parallel(2) == parallel(4), bit-identical per index), append reproducibility, and the None-seed path. They stubFlightso only input sampling runs, and drop the one stdlib-randomthrust_sourceso the inputs are byte-reproducible from the numpy seed alone. Markedslow(each sim rebuilds a rocket), matching the existingtest_monte_carlo_simulate.Locally: ruff and pylint both 10/10, and the four tests pass with
--runslow.Known gap
List-valued stochastic attributes are drawn with the stdlib
random.choice(a global, unseeded generator), so a model with multi-element lists isn't fully worker-invariant yet. BPC isn't affected (its lists are single-element). A general fix would seed the stdlibrandomper index too, or move those draws onto numpy. I'll note it upstream when this goes to RocketPy.Unrelated, but flagging it since I ran the suite:
test_monte_carlo_simulateis already failing on develop (processed_resultshas no"apogee", and the sims themselves don't error), independent of this change. Happy to look at that separately if it's useful.