diff_env
cosmic_warp.rl.diff_env
Differentiable vectorized environment: short-horizon windows over the bridge.
:class:DiffVecEnv chains :class:~cosmic_warp.rl.diff_bridge.DiffSimStepper
steps into H-step windows — the rollout unit of short-horizon
first-order trainers such as SHAC (example 17). Within a window the whole
chain (observation -> policy -> command -> dynamics -> reward) stays on the
torch autograd graph; between windows the carried state is detached
(backprop-through-time is truncated at window boundaries by design).
Episodes are truncation-only: termination checks are off on the
differentiable path (a terminated entity's gradients silently vanish), so
max_episode_steps must be a multiple of horizon and every environment
resets together — a plain full sim.reset() between windows, outside
autograd. Value targets (e.g. TD(lambda)) therefore always bootstrap: a time
limit is not an MDP terminal.
Window
dataclass
One H-step differentiable rollout.
obs[t] is the observation the policy acted on at step t (obs[H] is
the terminal observation for value bootstrapping); rewards[t] is the
reward of that transition. All tensors are float64 and on the autograd
graph — detach before storing for the critic.
DiffVecEnv
DiffVecEnv(
sim: SpacecraftSimulator,
task: DiffTask,
horizon: int,
max_episode_steps: int,
)
Windowed differentiable environment over a spacecraft simulator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sim
|
SpacecraftSimulator
|
Simulator built with |
required |
task
|
DiffTask
|
The differentiable task (torch obs/reward/action mapping). |
required |
horizon
|
int
|
Window length H (steps per rollout / actor update). |
required |
max_episode_steps
|
int
|
Episode time limit; must be a multiple of
|
required |
Source code in cosmic_warp/rl/diff_env.py
state
property
The carried state [num_entities, X_DIM] (f64, detached) the next window
starts from; None before :meth:reset.
reset
Full reset of every environment; returns the initial observation
[N, obs_dim] (f64, detached).
Source code in cosmic_warp/rl/diff_env.py
rollout
rollout(policy_fn: Callable[[Tensor], Tensor]) -> Window
Run one H-step window from the carried state, on the autograd graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
policy_fn
|
Callable[[Tensor], Tensor]
|
Maps the observation |
required |
Call :meth:end_window after consuming the window to advance the
carried state (detached) and handle the episode time limit. Calling
rollout again first re-runs the window from the same carried state.
The window's backward uses the parameters of its forward, so it may
run before or after end_window().
Source code in cosmic_warp/rl/diff_env.py
end_window
Detach and carry the final state; reset at the episode time limit.
Returns True when the windows just closed an episode (all envs were reset). The carried state is also written back to the simulator so its own getters reflect reality between windows.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
No window is pending: no :meth: |