Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relative Motion #214

Draft
wants to merge 22 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 21 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"sphinx_rtd_theme",
"nbsphinx",
"sphinx.ext.mathjax",
"sphinxcontrib.youtube",
]

templates_path = ["_templates"]
Expand Down Expand Up @@ -234,7 +235,6 @@ def generate_index(self, index_path, file_paths, dir_paths, source_dir):
f.write(lines)

def generate_autodoc(self, doc_path, source_file):

# Make header
short_name = source_file.name.replace(".py", "")
lines = ""
Expand Down
2 changes: 2 additions & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ for working with these environments.
A whitepaper on the design philosophy behind BSK-RL and an example use case can be
:download:`downloaded here <_static/stephenson_bskrl_2024.pdf>`.

.. youtube:: 8qR-AGrCFQw

Quickstart
----------
Installation
Expand Down
3 changes: 2 additions & 1 deletion docs/source/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ Development Version
* Improve performance of :class:`~bsk_rl.obs.Eclipse` observations by about 95%.
* Logs a warning if the initial battery charge or buffer level is incompatible with its capacity.
* Optimize communication when all satellites are communicating with each other.

* Enable Vizard visualization of the environment by setting the ``vizard_dir`` and ``vizard_settings``
options in the environment.


Version 1.0.1
Expand Down
464 changes: 464 additions & 0 deletions examples/continuous_orbit_manuevers.ipynb

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ dependencies = [
]

[project.optional-dependencies]
docs = ["ipykernel", "ipywidgets", "nbdime", "nbsphinx", "sphinx-rtd-theme"]
rllib = ["dm_tree", "pyarrow", "ray[rllib]", "scikit-image", "torch", "typer"]
docs = ["ipykernel", "ipywidgets", "nbdime", "nbsphinx", "sphinx-rtd-theme", 'sphinxcontrib-youtube']
rllib = ["dm_tree", "pyarrow", "ray[rllib]==2.35.0", "scikit-image", "torch", "typer"]

[project.scripts]
finish_install = "bsk_rl.finish_install:pck_install"
18 changes: 18 additions & 0 deletions src/bsk_rl/act/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,25 @@ class MyActionSatellite(Satellite):
+----------------------------+---------+-------------------------------------------------------------------------------------------------------+
| :class:`Scan` | 1 | Scan nadir, collecting data when pointing within a threshold. |
+----------------------------+---------+-------------------------------------------------------------------------------------------------------+

Continuous Actions
-------------------

Use :class:`ContinuousAction` for actions with a continuous action space. Currently, satellites
can only have a single continuous action in their ``action_spec``.

+----------------------------+-------------+-------------------------------------------------------------------------------------------------------+
| **Action** |**Dimension**| **Description** |
+----------------------------+-------------+-------------------------------------------------------------------------------------------------------+
| :class:`MagicThrust` | 4 | Instantaneously change the satellite's velocity, and drift for some duration. |
+----------------------------+-------------+-------------------------------------------------------------------------------------------------------+



"""

from bsk_rl.act.actions import Action
from bsk_rl.act.continuous_action import ContinuousAction, MagicThrust
from bsk_rl.act.discrete_actions import (
Charge,
Desat,
Expand All @@ -63,4 +79,6 @@ class MyActionSatellite(Satellite):
"Downlink",
"Image",
"Scan",
"ContinuousAction",
"MagicThrust",
]
1 change: 0 additions & 1 deletion src/bsk_rl/act/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def select_action_builder(satellite: "Satellite") -> "ActionBuilder":


class ActionBuilder(ABC):

def __init__(self, satellite: "Satellite") -> None:
"""Base class for all action builders.

Expand Down
133 changes: 133 additions & 0 deletions src/bsk_rl/act/continuous_action.py

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the hardcoded '5700.0' specify here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened a new issue to track this, since we use hardcoded 5700 as a normalization factor (approx. 1 orbit) in various places. #225

Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
"""Continuous actions set satellite behavior based on some continuous value."""

import logging
from abc import abstractmethod
from typing import TYPE_CHECKING, Optional

import numpy as np
from gymnasium import spaces

from bsk_rl.act.actions import Action, ActionBuilder

if TYPE_CHECKING: # pragma: no cover
from bsk_rl.sats import Satellite
from bsk_rl.scene.targets import Target

logger = logging.getLogger(__name__)


class ContinuousActionBuilder(ActionBuilder):
def __init__(self, satellite: "Satellite") -> None:
"""Processes actions for a continuous action space.

Args:
satellite: Satellite to create actions for.
"""
self.action_spec: list[ContinuousAction]
super().__init__(satellite)
assert len(self.action_spec) == 1, "Only one continuous action is supported."

@property
def _action(self) -> "ContinuousAction":
return self.action_spec[0]

@property
def action_space(self) -> spaces.Box:
"""Continuous action space."""
return self._action.space

@property
def action_description(self) -> list[str]:
"""Return a human-readable description of the continuous action space."""
return self._action.action_description()

def set_action(self, action: np.ndarray) -> None:
"""Activate the action by setting the continuous value."""
self._action.set_action(action)


class ContinuousAction(Action):
builder_type = ContinuousActionBuilder

def __init__(self, name: str = "discrete_act") -> None:
"""Base class for discrete, integer-indexable actions.

Args:
name: Name of the action.
"""
super().__init__(name=name)

@property
@abstractmethod
def space(self) -> spaces.Box:
"""Return the action space."""
pass

@property
@abstractmethod
def action_description(self) -> list[str]:
"""Return a description of the action space."""
pass

@abstractmethod
def set_action(self, action: np.ndarray) -> None:
"""Activate an action by a continuous value."""
pass


class MagicThrust(ContinuousAction):
# TODO set the fsw mode to carry out after action
def __init__(
self,
name: str = "thrust_act",
max_dv: float = float("inf"),
max_duration: float = float("inf"),
fsw_action: Optional[str] = None,
) -> None:
"""Instantaneously change the satellite's velocity, and drift for some duration.

TODO: Support specifying frame of thrust.

Args:
name: Name of the action.
max_dv: Maximum delta-V that can be applied. [m/s]
"""
super().__init__(name)
self.max_dv = max_dv
self.max_duration = max_duration
self.fsw_action = fsw_action

@property
def space(self) -> spaces.Box:
"""Return the action space."""
return spaces.Box(
low=np.array(
[-self.max_dv, -self.max_dv, -self.max_dv, self.simulator.sim_rate]
),
high=np.array([self.max_dv, self.max_dv, self.max_dv, self.max_duration]),
shape=(4,),
dtype=np.float32,
)

@property
def action_description(self) -> list[str]:
"""Description of the continuous action space."""
return ["dV_N_x", "dV_N_y", "dV_N_z", "duration"]

def set_action(self, action: np.ndarray) -> None:
"""Thrust the satellite with a given inertial delta-V and drift for some duration."""
assert len(action) == 4, "Action must have 4 elements."
dv_N = action[0:3]
dt = action[3]

self.satellite.log_info(
f"Thrusting with inertial dV {dv_N} with {dt} second drift."
)
self.satellite.fsw.action_magic_thrust(dv_N)
self.satellite.update_timed_terminal_event(
self.satellite.simulator.sim_time + dt
)

# Activate the FSW action for the drift period
getattr(self.satellite.fsw, self.fsw_action)()
self.satellite.log_info(f"FSW action {self.fsw_action} activated.")
17 changes: 17 additions & 0 deletions src/bsk_rl/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,26 @@
...
)

Multiple reward systems can be added to the environment by instead passing an iterable of
reward systems to the ``data`` field of the environment constructor:

.. code-block:: python

env = ConstellationTasking(
...,
data=(ScanningTimeReward(), SomeOtherReward()),
...
)

On the backend, this creates a :class:`~bsk_rl.data.composition.ComposedDataStore` that
handles the combination of multiple reward systems.
"""

from bsk_rl.data.base import GlobalReward
from bsk_rl.data.fuel_penalty import FuelPenalty
from bsk_rl.data.nadir_data import ScanningTimeReward
from bsk_rl.data.no_data import NoReward
from bsk_rl.data.rso_data import RSOInspectionReward
from bsk_rl.data.unique_image_data import UniqueImageReward

__doc_title__ = "Data & Reward"
Expand All @@ -87,4 +102,6 @@
"NoReward",
"UniqueImageReward",
"ScanningTimeReward",
"RSOInspectionReward",
"FuelPenalty",
]
2 changes: 1 addition & 1 deletion src/bsk_rl/data/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def reward(self, new_data_dict: dict[str, Data]) -> dict[str, float]:
self.data += new_data

nonzero_reward = {k: v for k, v in reward.items() if v != 0}
logger.info(f"Data reward: {nonzero_reward}")
logger.info(f"Total reward: {nonzero_reward}")
return reward


Expand Down
Loading