Skip to content

Commit

Permalink
Issue #18: Refactor retasking required flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark2000 committed Dec 5, 2023
1 parent 0552e04 commit 5615268
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
15 changes: 14 additions & 1 deletion bsk_rl/envs/general_satellite_tasking/gym_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ def _get_info(self) -> dict[str, Any]:
satellite.id: deepcopy(satellite.info) for satellite in self.satellites
}
info["d_ts"] = self.latest_step_duration
info["requires_retasking"] = [
satellite.id
for satellite in self.satellites
if satellite.requires_retasking
]
return info

@property
Expand Down Expand Up @@ -229,7 +234,15 @@ def step(
raise ValueError("There must be the same number of actions and satellites")
for satellite, action in zip(self.satellites, actions):
satellite.info = [] # reset satellite info log
satellite.set_action(action)
if action is not None:
satellite.requires_retasking = False
satellite.set_action(action)
else:
if satellite.requires_retasking:
print(
f"Satellite {satellite.id} requires retasking "
"but received no task."
)

previous_time = self.simulator.sim_time # should these be recorded in simulator
self.simulator.run()
Expand Down
4 changes: 4 additions & 0 deletions bsk_rl/envs/general_satellite_tasking/scenario/satellites.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def __init__(
self.fsw: "FSWModel"
self.dynamics: "DynamicsModel"
self.data_store: DataStore
self.requires_retasking: bool
self.variable_interval = variable_interval
self._timed_terminal_event_name = None

Expand All @@ -104,6 +105,7 @@ def _generate_sat_args(self) -> None:
def reset_pre_sim(self) -> None:
"""Called in environment reset, before simulator initialization"""
self.info = []
self.requires_retasking = True
self._generate_sat_args()
assert self.data_store.is_fresh
self.data_store.is_fresh = False
Expand Down Expand Up @@ -235,6 +237,7 @@ def _update_timed_terminal_event(
[f"self.TotalSim.CurrentNanos * {macros.NANO2SEC} >= {t_close}"],
[
self._info_command(f"timed termination at {t_close:.1f} " + info),
self._satellite_command + ".requires_retasking = True",
]
+ extra_actions,
terminal=self.variable_interval,
Expand Down Expand Up @@ -747,6 +750,7 @@ def _update_image_event(self, target: Target) -> None:
[
self._info_command(f"imaged {target}"),
self._satellite_command + ".imaged += 1",
self._satellite_command + ".requires_retasking = True",
],
terminal=self.variable_interval,
)
Expand Down
18 changes: 18 additions & 0 deletions tests/unittest/envs/general_satellite_tasking/test_gym_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def test_get_info(self):
env.latest_step_duration = 10.0
expected = {sat.id: {"sat_index": i} for i, sat in enumerate(mock_sats)}
expected["d_ts"] = 10.0
expected["requires_retasking"] = [sat.id for sat in mock_sats]
assert env._get_info() == expected

def test_action_space(self):
Expand Down Expand Up @@ -170,6 +171,23 @@ def test_step_stopped(self, sat_death, timeout, terminate_on_time_limit):
assert terminated == (sat_death or (timeout and terminate_on_time_limit))
assert truncated == timeout

@patch.multiple(Satellite, __abstractmethods__=set())
def test_step_retask_needed(self, capfd):
mock_sat = MagicMock()
env = SingleSatelliteTasking(
satellites=[mock_sat],
env_type=MagicMock(),
env_features=MagicMock(),
data_manager=MagicMock(reward=MagicMock(return_value=25.0)),
)
env.simulator = MagicMock(sim_time=101.0)
env.step(None)
assert mock_sat.requires_retasking
mock_sat.requires_retasking = True
env.step(None)
assert mock_sat.requires_retasking
assert "requires retasking but received no task" in capfd.readouterr().out

def test_render(self):
pass

Expand Down

0 comments on commit 5615268

Please sign in to comment.