Skip to content

Commit 9cc93a1

Browse files
BUG: stop dict_generator from sampling initial_solution (#1109)
Generate stochastic samples from declared constructor inputs only, and validate StochasticFlight.initial_solution on construct.
1 parent 25271f4 commit 9cc93a1

3 files changed

Lines changed: 35 additions & 2 deletions

File tree

‎rocketpy/stochastic/stochastic_flight.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def __init__(
9494
heading=heading,
9595
)
9696

97+
self._validate_initial_solution(initial_solution)
9798
self.initial_solution = initial_solution
9899
self.terminate_on_apogee = terminate_on_apogee
99100
if max_time is None:

‎rocketpy/stochastic/stochastic_model.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -621,13 +621,19 @@ def dict_generator(self):
621621
622622
Notes
623623
-----
624-
1. The dictionary is generated by iterating over the class attributes and:
624+
1. The dictionary is generated by iterating over the *declared*
625+
stochastic inputs (constructor kwargs), not every attribute on
626+
``self``. This avoids treating opaque tuples such as
627+
``initial_solution`` as ``(nominal, spread, sampler)`` triples.
625628
a. If the attribute is a tuple, the value is generated using the\
626629
distribution function specified in the tuple.
627630
b. If the attribute is a list, the value is randomly chosen from the list.
628631
"""
629632
generated_dict = {}
630-
for arg, value in self.__dict__.items():
633+
for arg in self.__stochastic_dict:
634+
if not hasattr(self, arg):
635+
continue
636+
value = getattr(self, arg)
631637
if isinstance(value, tuple):
632638
dist_sampler = value[-1]
633639
generated_dict[arg] = dist_sampler(value[0], value[1])

‎tests/unit/stochastic/test_stochastic_flight.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,29 @@ def test_stochastic_flight_optional_attributes(flight_calisto_robust):
4545
assert obj.terminate_on_apogee is True
4646
assert obj.time_overshoot is True
4747
assert obj.max_time == 987.6
48+
49+
50+
def test_dict_generator_skips_initial_solution_tuple(flight_calisto_robust):
51+
"""Regression for #1109: tuple initial_solution must not be sampled."""
52+
initial_solution = tuple(float(i) for i in range(14))
53+
stochastic_flight = StochasticFlight(
54+
flight=flight_calisto_robust,
55+
initial_solution=initial_solution,
56+
rail_length=(5.2, 0.1),
57+
)
58+
generated = next(stochastic_flight.dict_generator())
59+
assert "initial_solution" not in generated
60+
assert stochastic_flight.initial_solution == initial_solution
61+
62+
63+
def test_dict_generator_skips_initial_solution_list(flight_calisto_robust):
64+
"""List-form initial_solution must not be randomly subset-sampled."""
65+
initial_solution = [float(i) for i in range(14)]
66+
stochastic_flight = StochasticFlight(
67+
flight=flight_calisto_robust,
68+
initial_solution=initial_solution,
69+
inclination=[85, 86, 87],
70+
)
71+
generated = next(stochastic_flight.dict_generator())
72+
assert "initial_solution" not in generated
73+
assert stochastic_flight.initial_solution == initial_solution

0 commit comments

Comments
 (0)