Index
cosmic_warp.rl
COSMIC WARP reinforcement-learning layer.
A thin, GPU-resident, framework-neutral environment over the spacecraft
simulator. The core (:class:VecEnv + the :class:Task protocol) has no
learning-framework dependency; PyTorch is used only for optional zero-copy
tensor views. Tasks are application code: examples/attitude_task.py is a
reference implementation.
from cosmic_warp.core.config import create_simulator_from_yaml
from cosmic_warp.rl import VecEnv
from attitude_task import AttitudeTask # examples/attitude_task.py
sim = create_simulator_from_yaml("examples/rl_attitude_training.yaml")
env = VecEnv(sim, AttitudeTask(), max_episode_steps=500)
obs = env.reset()
for _ in range(horizon):
obs, reward, terminated, truncated = env.step(action)
VecEnv
VecEnv(
sim: SpacecraftSimulator,
task: Task,
max_episode_steps: int,
)
Vectorized, GPU-resident RL environment over a spacecraft simulator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sim
|
SpacecraftSimulator
|
A constructed simulator (e.g. from |
required |
task
|
Task
|
The task defining action mapping, observation and reward. |
required |
max_episode_steps
|
int
|
Episode length after which an environment is truncated (time limit) and auto-reset, in [1, 2**31 - 1]. |
required |
Source code in cosmic_warp/rl/env.py
actions
class-attribute
instance-attribute
Normalized actions [N, act_dim] (f32), read by step().
terminal_obs
class-attribute
instance-attribute
Pre-reset observations [N, obs_dim] (f32) of the last step.
reward
class-attribute
instance-attribute
Rewards [N] (f32) of the last step.
terminated
class-attribute
instance-attribute
Terminated flags [N] (bool) of the last step.
truncated
class-attribute
instance-attribute
Truncated (time-limit) flags [N] (bool) of the last step.
max_episode_steps
property
writable
Episode length after which an environment is truncated and auto-reset. Setting it re-captures the step graph on the next step.
reset
Reset every environment and return the initial observation [N, O].
Source code in cosmic_warp/rl/env.py
step
Advance every environment by one control step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
action
|
Optional[array]
|
Normalized actions |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if |
Returns:
| Type | Description |
|---|---|
array
|
|
array
|
reused buffers — copy into trainer storage each step, do not retain |
array
|
a view across steps. |
Source code in cosmic_warp/rl/env.py
invalidate_graph
Drop the captured CUDA graph so the next :meth:step re-captures it.
The capture bakes in every scalar passed by value to a kernel launch
(task parameters such as reward weights) and the
randomizer.enabled branch. Call this after changing any of them
mid-run; otherwise the change silently has no effect on CUDA. Setting
:attr:max_episode_steps calls it for you.
Source code in cosmic_warp/rl/env.py
observations_torch
actions_torch
rewards_torch
terminated_torch
truncated_torch
Task
Bases: Protocol
Minimal task interface consumed by :class:~cosmic_warp.rl.env.VecEnv.
A task exposes its per-environment action/observation widths and three
device-side operations. VecEnv calls bind once at construction,
then ingest_actions / write_reward / write_obs every step.
bind
ingest_actions
write_obs
Pack the observation into obs [N, obs_dim] (f32).
When masked == 1 only rows where reset_env is True are written
(the post-reset rewrite for auto-reset environments); when masked ==
0 every row is written.
Source code in cosmic_warp/rl/tasks.py
write_reward
Write the per-environment reward into reward [N] (f32).
action is the (already ingested) normalized action of the current
step, available for control-effort penalties.