TransitionStorage
TransitionStorage(capacity: int, observation_shape: tuple[int, ...], observation_dtype: np.dtype) -> NonePre-allocated circular-buffer storage for replay transitions and their reward-shaping metadata.
Owns the numpy columns (states, actions, rewards, next_states, dones, n_steps, and optional reward-shaping metadata), the wraparound insert mechanics, and npz payload (de)serialization. Sampling policy (uniform vs prioritized) is intentionally not this class's concern.
Initialize the storage.
Parameters:
capacity(int) – Maximum number of transitions to store.observation_shape(tuple[int, ...]) – Shape of the observations.observation_dtype(dtype) – The numpy dtype for storing observations.
Properties
observation_shape
observation_shape: tuple[int, ...]Get the shape of observations stored in the buffer.
Returns:
tuple[int, ...]– The observation shape tuple.
reward_shaping_metadata_spec
reward_shaping_metadata_spec: dict[str, np.dtype]Get the reward-shaping metadata columns stored in the buffer.
Returns:
dict[str, dtype]– A mapping of metadata key to numpy dtype.
Methods
copy_reward_shaping_metadata
copy_reward_shaping_metadata(indices: np.ndarray, reward_shaping_metadata_out: dict[str, np.ndarray] | None) -> NoneCopy sampled reward-shaping metadata into caller-provided output arrays.
Parameters:
indices(ndarray) – Sampled replay indices.reward_shaping_metadata_out(dict[str, ndarray] | None) – Output arrays keyed like the stored metadata.
insert
insert(state: list[np.ndarray], action: list[int], reward: list[float], next_state: list[np.ndarray], done: list[bool], n_steps: list[int], reward_shaping_metadata: Mapping[str, np.ndarray | int | float | list[int | float]] | None) -> np.ndarrayWrite a batch of transitions into the circular buffer, advancing idx/size.
Parameters:
state(list[ndarray]) – List of states.action(list[int]) – List of actions.reward(list[float]) – List of rewards.next_state(list[ndarray]) – List of next states.done(list[bool]) – List of done flags.n_steps(list[int]) – List of step counts.reward_shaping_metadata(Mapping[str, ndarray | int | float | list[int | float]] | None) – Optional compact reward-shaping metadata columns.
Returns:
ndarray– The absolute buffer indices written, in insertion order.
load_payload
load_payload(data: Mapping[str, Any]) -> NoneRestore this storage's columns from a loaded npz payload, reallocating buffers.
Parameters:
data(Mapping[str, Any]) – The loaded npz mapping (as returned bynp.load).
to_payload
to_payload() -> dict[str, object]Build the npz-serializable payload for this storage's current contents.
Returns:
dict[str, object]– A mapping of npz key to array/scalar value.
Values
actions
actions = np.zeros((capacity,), dtype=np.int64)capacity
capacity = capacitydones
dones = np.zeros((capacity,), dtype=np.bool_)idx
idx = 0n_steps
n_steps = np.ones((capacity,), dtype=np.int32)next_states
next_states = np.zeros((capacity, *observation_shape), dtype=self.observation_dtype)observation_dtype
observation_dtype = np.dtype(observation_dtype)rewards
rewards = np.zeros((capacity,), dtype=np.float32)size
size = 0states
states = np.zeros((capacity, *observation_shape), dtype=self.observation_dtype)