Skip to content

Commit 21a780f

Browse files
committed
BUG: take the seed type a parallel run hands the rocket
A parallel run spawns a SeedSequence per worker and passes it down, and SeedSequence does not take another one as entropy, so rooting the collections from it raised TypeError. It was unreachable until now: the base _set_stochastic refuses the same type one frame earlier, so a worker never got this far. Once that is fixed the call here is the next one to fail, which is why it is fixed in the same series rather than left for whoever hits it. Copied from the full state rather than spawned from directly. spawn() advances the counter of an object the caller still holds, and a second use of the same seed would then build the components a different tree. Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
1 parent 7768d8e commit 21a780f

3 files changed

Lines changed: 50 additions & 3 deletions

File tree

‎rocketpy/stochastic/stochastic_rocket.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import warnings
44

5-
import numpy as np
65

76
from rocketpy.control import _Controller
87
from rocketpy.mathutils.vector_matrix import Vector
@@ -23,7 +22,7 @@
2322
from rocketpy.rocket.rocket import Rocket
2423
from rocketpy.stochastic.stochastic_generic_motor import StochasticGenericMotor
2524
from rocketpy.stochastic.stochastic_motor_model import StochasticMotorModel
26-
from rocketpy.tools import _seed_sequence_to_int
25+
from rocketpy.tools import _seed_sequence_from, _seed_sequence_to_int
2726

2827
from .stochastic_aero_surfaces import (
2928
StochasticAirBrakes,
@@ -203,7 +202,7 @@ def _set_stochastic(self, seed=None):
203202
"""
204203
super()._set_stochastic(seed)
205204
names = self._stochastic_collections()
206-
roots = dict(zip(names, np.random.SeedSequence(seed).spawn(len(names))))
205+
roots = dict(zip(names, _seed_sequence_from(seed).spawn(len(names))))
207206
for name in self._POSITIONED_COLLECTIONS:
208207
setattr(
209208
self, name, self.__reset_components(getattr(self, name), roots[name])

‎rocketpy/tools.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,6 +1475,18 @@ def _seed_sequence_to_int(seed_sequence):
14751475
return sum(int(word) << (32 * position) for position, word in enumerate(words))
14761476

14771477

1478+
def _seed_sequence_from(seed):
1479+
"""Returns a ``SeedSequence`` of the caller's own to spawn from.
1480+
1481+
A parallel run is handed one that ``SeedSequence`` will not take as
1482+
entropy, and spawning from it directly would advance the counter of an
1483+
object the caller still holds, so it is copied from its full state.
1484+
"""
1485+
if isinstance(seed, np.random.SeedSequence):
1486+
return np.random.SeedSequence(**seed.state)
1487+
return np.random.SeedSequence(seed)
1488+
1489+
14781490
if __name__ == "__main__": # pragma: no cover
14791491
import doctest
14801492

‎tests/unit/stochastic/test_stochastic_rocket_seeding.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
import ast
77
import inspect
88

9+
import numpy as np
10+
911
from rocketpy.rocket.components import Components
12+
from rocketpy.tools import _seed_sequence_from
1013
from rocketpy.stochastic import (
1114
StochasticAirBrakes,
1215
StochasticParachute,
@@ -275,3 +278,36 @@ def test_the_tree_is_built_by_the_reset_not_by_the_add(calisto, calisto_main_chu
275278
rocket._set_stochastic(99)
276279

277280
assert chute._seed is not None
281+
282+
283+
def test_a_worker_seed_sequence_is_copied_rather_than_spawned_from():
284+
# A parallel run hands each worker a SeedSequence. Spawning from it would
285+
# advance a counter the caller still holds, so the next use of the same
286+
# object would build a different tree.
287+
worker = np.random.SeedSequence(7).spawn(2)[0]
288+
289+
_seed_sequence_from(worker).spawn(3)
290+
291+
assert worker.n_children_spawned == 0
292+
293+
294+
def test_a_worker_seed_sequence_keeps_its_place_in_the_tree():
295+
worker = np.random.SeedSequence(7).spawn(2)[0]
296+
297+
children = _seed_sequence_from(worker).spawn(2)
298+
299+
assert [child.spawn_key for child in children] == [(0, 0), (0, 1)]
300+
301+
302+
def test_two_workers_do_not_get_the_same_collection_roots():
303+
first, second = np.random.SeedSequence(7).spawn(2)
304+
305+
one = _seed_sequence_from(first).spawn(1)[0].generate_state(4)
306+
other = _seed_sequence_from(second).spawn(1)[0].generate_state(4)
307+
308+
assert not np.array_equal(one, other)
309+
310+
311+
def test_an_integer_seed_still_roots_the_collections():
312+
assert _seed_sequence_from(42).spawn(1)[0].spawn_key == (0,)
313+
assert _seed_sequence_from(None).spawn(1)[0].spawn_key == (0,)

0 commit comments

Comments
 (0)