Monte Carlo results aren't reproducible across serial and parallel runs (seeding is per-worker, not per-simulation)
Maintainer thường phản hồi trong vòng 3 ngày
Chưa có ai nhận issue này.
- #1054 của @thc1006 — đã đóng, không merge
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 35/100
Hướng nghiên cứu
Read rocketpy/simulation/monte_carlo.py, especially simulate(), __run_in_serial, __run_in_parallel, and __sim_producer, to trace how seeds and simulation indices are handled. Review linked PR #1054 and the open question about where the root seed should live before choosing an approach. Run tests/integration/simulation/test_monte_carlo.py::test_monte_carlo_simulate; done means the same simulation index gets reproducible inputs across serial and parallel runs, worker counts, and appends.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Describe the bug
MonteCarlo.simulate() seeds the stochastic models per worker in parallel mode, and effectively once (at model construction) in serial mode. Because of that, the random inputs a given simulation index gets depend on how the run was executed rather than on the simulation index itself. Serial and parallel runs produce different input samples for the same setup, and parallel runs aren't reproducible run to run at all.
Two things are going on in rocketpy/simulation/monte_carlo.py (on develop):
-
Parallel mode spawns one seed per worker from a fresh, unseeded
SeedSequence, so there's no fixed seed to reproduce against:# __run_in_parallel, line 342 seeds = np.random.SeedSequence().spawn(n_workers)Each worker then reseeds every stochastic model from its own per-worker seed and draws sequentially for whatever simulations it happens to pull off the shared counter:
# __sim_producer, lines 406-408 self.environment._set_stochastic(seed) self.rocket._set_stochastic(seed) self.flight._set_stochastic(seed)# __sim_producer, line 411 sim_idx = sim_monitor.increment() - 1So simulation
i's inputs depend on which worker won it and in what order, which is nondeterministic across runs and changes withn_workers. -
Serial mode (
__run_in_serial) never reseeds duringsimulate(); it just draws one long sequential stream from eachStochastic*model's generator (seeded once at construction viadefault_rng(seed)). That stream is unrelated to the per-worker streams parallel mode produces.
The upshot: simulate(n) serial, simulate(n, parallel=True, n_workers=2), and simulate(n, parallel=True, n_workers=4) all sample different inputs for the same stochastic setup, and the parallel ones differ every time you run them.
To Reproduce
Conceptually, with the same stochastic environment, rocket, and flight:
mc_serial = MonteCarlo("out_serial", environment, rocket, flight)
mc_serial.simulate(10)
mc_par2 = MonteCarlo("out_par2", environment, rocket, flight)
mc_par2.simulate(10, parallel=True, n_workers=2)
mc_par4 = MonteCarlo("out_par4", environment, rocket, flight)
mc_par4.simulate(10, parallel=True, n_workers=4)
Compare the sampled inputs in the three .inputs.txt files: simulation i does not get the same inputs across the three, and re-running either parallel case gives a different set again.
Expected behavior
Simulation index i should get the same random inputs regardless of whether the run is serial or parallel and regardless of n_workers. A Monte Carlo run should be reproducible from a single seed, and append should extend the same stream rather than restart it.
Additional context
The reason the current scheme can't give that is that the seed is tied to the worker, not the simulation. Seeding per simulation index fixes it. Derive one root SeedSequence for the whole run and spawn one child per simulation, then reseed the stochastic models from child_seeds[i] right before running simulation i:
# once, at the start of simulate()
root = np.random.SeedSequence(random_seed) # root seed for the run
child_seeds = root.spawn(number_of_simulations)
# before running simulation i (same call in serial and inside each worker)
self.environment._set_stochastic(child_seeds[i])
self.rocket._set_stochastic(child_seeds[i])
self.flight._set_stochastic(child_seeds[i])
SeedSequence.spawn is prefix stable, so child i is the same object whether you spawned 10 or 100 children and whether you're in serial or in any worker. That makes the sampled inputs identical across serial, parallel(2), and parallel(N), and it makes append=True deterministic since later indices keep extending the same prefix.
One open design point: MonteCarlo / simulate() don't currently take a random_seed (today the only seed lives on the Stochastic* constructors and only reaches the serial path). The fix needs a root seed entry point somewhere, either a new random_seed argument on simulate()/MonteCarlo or deriving the root from the stochastic models' existing seeds. I can put up a PR once we agree on where the root seed should live.
Breaking change
Yes. This changes the exact numbers a given seed produces, so any stored Monte Carlo baseline regenerates, including tests/integration/simulation/test_monte_carlo.py::test_monte_carlo_simulate. The migration is a one-time baseline refresh; there's no API break for users who don't pin exact samples.
- Ngôn ngữ chính
- Python
- Star
- 1.1k
- Fork
- 280
- Merge trung bình
- 3 ngày 2 giờ
- Pull request đã merge (30 ngày)
- 24
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của RocketPy-Team/RocketPy
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
RocketPy-Team/RocketPy#1255 ·
Maintainer thường phản hồi trong vòng 3 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 92/100
RocketPy-Team/RocketPy#1241 ·
Maintainer thường phản hồi trong vòng 3 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
RocketPy-Team/RocketPy#1202 ·
Maintainer thường phản hồi trong vòng 3 ngày
-
Monte Carlo test fixtures write their logs into the working directoryCó thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
RocketPy-Team/RocketPy#1189 ·
Maintainer thường phản hồi trong vòng 3 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
RocketPy-Team/RocketPy#1171 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 3 ngày
Tất cả issue của RocketPy-Team/RocketPy
Issue tương tự
-
docs(contributing): the two things that silently block a PRCó thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mởavailable documentation good first issue
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 95/100
Nixitov/Riptide-Native#944 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
JTHCode/secretguard#24 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
area: middleware documentation good first issue
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
-
enhancement good first issue
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
Maintainer thường phản hồi trong vòng 1 ngày
-
good first issue
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 95/100
omarelshamy25x/shamy-hijri#63 ·
Maintainer thường phản hồi trong vòng 1 ngày