Skip to content

Commit

Permalink
Issue #67: Target observation improvements and norm bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark2000 committed Sep 21, 2023
1 parent eabca32 commit 62ce222
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
26 changes: 16 additions & 10 deletions bsk_rl/envs/general_satellite_tasking/scenario/sat_observations.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ def prop_fn(self):
for module in ["dynamics", "fsw"]:
if hasattr(getattr(self, module), prop):
return np.array(getattr(getattr(self, module), prop)) / norm
else:
raise AttributeError(f"Property {prop} not found")

prop_fn.__name__ = prop
if norm != 1:
Expand Down Expand Up @@ -182,7 +184,7 @@ def __init__(
if "location_norm" in kwargs:
warn(
"location_norm is ignored and should be specified in target_properties"
)
) # pragma: no cover
self.n_ahead_observe = int(n_ahead_observe)
self.target_obs_generator(target_properties)

Expand All @@ -193,30 +195,34 @@ def target_obs_generator(self, target_properties):

def target_obs(self):
obs = {}
for i, target in enumerate(self.upcoming_targets(self.n_ahead_observe)):
for i, opportunity in enumerate(
self.find_next_opportunities(
n=self.n_ahead_observe,
filter=self._get_imaged_filter(),
types="target",
)
):
props = {}
for prop_spec in target_properties:
name = prop_spec["prop"]
norm = prop_spec.get("norm", 1.0)
if name == "priority":
value = target.priority / norm
value = opportunity["target"].priority
elif name == "location":
value = target.location / norm
value = opportunity["target"].location
elif name == "window_open":
value = self.next_windows[target][0] - self.simulator.sim_time
value = opportunity["window"][0] - self.simulator.sim_time
elif name == "window_mid":
value = (
sum(self.next_windows[target]) / 2 - self.simulator.sim_time
)
value = sum(opportunity["window"]) / 2 - self.simulator.sim_time
elif name == "window_close":
value = self.next_windows[target][1] - self.simulator.sim_time
value = opportunity["window"][1] - self.simulator.sim_time
else:
raise ValueError(
f"Invalid target property: {prop_spec['prop']}"
)
if norm != 1.0:
name += "_normd"
props[name] = value
props[name] = value / norm
obs[f"target_{i}"] = props
return obs

Expand Down
5 changes: 2 additions & 3 deletions bsk_rl/envs/general_satellite_tasking/scenario/satellites.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def __init__(
*args,
generation_duration: float = 60 * 95 / 10,
initial_generation_duration: Optional[float] = None,
access_dist_threshold: float = 1e6,
access_dist_threshold: float = 4e6,
**kwargs,
) -> None:
"""Satellite that can detect access opportunities for ground locations with
Expand All @@ -289,7 +289,7 @@ def __init__(
initial_generation_duration: Duration to initially calculate imaging windows
[s]
access_dist_threshold: Distance bound [m] for evaluating imaging windows
more exactly.
more exactly. 4e6 will capture >10 elevation windows for a 500 km orbit.
"""
super().__init__(*args, **kwargs)
self.generation_duration = generation_duration
Expand Down Expand Up @@ -752,7 +752,6 @@ def _disable_image_event(self) -> None:
and self._image_event_name in self.simulator.eventMap
):
self.simulator.delete_event(self._image_event_name)
# self.simulator.eventMap[self._image_event_name].eventActive = False

def parse_target_selection(self, target_query: Union[int, Target, str]):
"""Identify a target based on upcoming target index, Target object, or target
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ def test_add_prop_function_norm(self, sat_init):
sat.add_prop_function("other_prop", norm=10.0)
assert sat.obs_dict == {"other_prop_normd": 0.1}

def test_add_bad_prop(self, sat_init):
sat = self.make_mocked_sat()
del sat.dynamics.not_a_prop
del sat.fsw.not_a_prop
sat.add_prop_function("not_a_prop")
with pytest.raises(AttributeError):
sat.obs_dict


@patch.multiple(so.TimeState, __abstractmethods__=set())
@patch("bsk_rl.envs.general_satellite_tasking.scenario.satellites.Satellite.__init__")
Expand Down Expand Up @@ -117,9 +125,9 @@ def test_init(self, sat_init):
def test_target_state(self, sat_init):
n_ahead = 2
sat = so.TargetState(n_ahead_observe=n_ahead)
sat.upcoming_targets = MagicMock(
sat.find_next_opportunities = MagicMock(
return_value=[
MagicMock(priority=i, location=np.array([0.0, 0.0, 0.0]))
dict(target=MagicMock(priority=i, location=np.array([0.0, 0.0, 0.0])))
for i in range(n_ahead)
]
)
Expand Down Expand Up @@ -185,9 +193,9 @@ def test_bad_target_state(self, sat_init):
dict(prop="not_a_prop"),
],
)
sat.upcoming_targets = MagicMock(
sat.find_next_opportunities = MagicMock(
return_value=[
MagicMock(priority=i, location=np.array([0.0, 0.0, 0.0]))
dict(target=MagicMock(priority=i, location=np.array([0.0, 0.0, 0.0])))
for i in range(n_ahead)
]
)
Expand Down

0 comments on commit 62ce222

Please sign in to comment.