Skip to content

Commit fce9756

Browse files
thatrandomasiandevGui-FernandesBR
authored andcommitted
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 cb6106a commit fce9756

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
@@ -702,13 +702,19 @@ def dict_generator(self):
702702
703703
Notes
704704
-----
705-
1. The dictionary is generated by iterating over the class attributes and:
705+
1. The dictionary is generated by iterating over the *declared*
706+
stochastic inputs (constructor kwargs), not every attribute on
707+
``self``. This avoids treating opaque tuples such as
708+
``initial_solution`` as ``(nominal, spread, sampler)`` triples.
706709
a. If the attribute is a tuple, the value is generated using the\
707710
distribution function specified in the tuple.
708711
b. If the attribute is a list, the value is randomly chosen from the list.
709712
"""
710713
generated_dict = {}
711-
for arg, value in self.__dict__.items():
714+
for arg in self.__stochastic_dict:
715+
if not hasattr(self, arg):
716+
continue
717+
value = getattr(self, arg)
712718
if isinstance(value, tuple):
713719
dist_sampler = value[-1]
714720
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)