rlightning.types¶
Types module for reinforcement learning data structures.
This module provides core data types used throughout the RLightning framework for representing environment returns, policy responses, and batched data.
- Available types:
BatchedData: Container for batched environment or policy data.
EnvRet: Environment return data structure.
MultiAgentEnvRet: Multi-agent environment return.
PolicyResponse: Policy action response structure.
MultiAgentPolicyResponse: Multi-agent policy response.
EnvMeta: Environment metadata container.
- class rlightning.types.BatchedData(ids: List[Any], data: List[Any | MockModule('ray.ObjectRef')])[source]¶
Bases:
objectContainer for batched data with associated identifiers.
Holds either actual data or Ray ObjectRefs, maintaining the order of (id, data) pairs. Allows duplicate IDs.
- is_future¶
True if any data element is a Ray ObjectRef.
- static from_dict(data_dict: Dict) BatchedData[source]¶
Create BatchedData from a dictionary.
- Parameters:
data_dict – Dictionary mapping IDs to data.
- Returns:
BatchedData instance.
- class rlightning.types.EnvMeta(env_id: str = None, action_space: MockModule('gymnasium.spaces.Space') | None = None, observation_space: MockModule('gymnasium.spaces.Space') | None = None, num_envs: int | None = None)[source]¶
Bases:
objectEnvironment metadata container.
Stores metadata about an environment including its spaces and configuration.
- action_space¶
Gymnasium action space.
- Type:
MockModule(‘gymnasium.spaces.Space’) | None
- observation_space¶
Gymnasium observation space.
- Type:
MockModule(‘gymnasium.spaces.Space’) | None
- class rlightning.types.EnvRet(env_id: str, observation: Any, last_reward: float = 0.0, last_terminated: bool = False, last_truncated: bool = False, info: Dict[str, ~typing.Any]=<factory>, _extra: Dict[str, ~typing.Any]=<factory>, ts_env_sent_ns: int = <factory>)[source]¶
Bases:
objectEnvironment return data structure for single-agent interactions.
Represents the return value from environment step() or reset() calls, containing observation, reward, termination status, and additional info.
- observation¶
Observation after environment step/reset.
- Type:
Any
- last_info¶
Additional info dictionary from the environment.
- compute_sent_latency(now_ns: int | None = None) float[source]¶
Compute latency in seconds from env-sent timestamp to now.
Use for env->policy or env->buffer transfer time (e.g. in policy _rollout_hook or in buffer add_transition).
- Parameters:
now_ns – Current time in nanoseconds. If None, uses time.time_ns().
- Returns:
Latency in seconds, or 0.0 if ts_env_sent_ns is missing or invalid.
- cuda(device: str | int | None = None) EnvRet[source]¶
Move all tensor attributes to CUDA.
- Parameters:
device – CUDA device index or string. Defaults to ‘cuda’.
- Returns:
Self with tensors moved to CUDA.
- classmethod fields() Tuple[source]¶
Get field names excluding internal fields.
- Returns:
Tuple of field names for serialization.
- classmethod get_defaults() Dict[str, Any][source]¶
Get default values for all serializable fields.
- Returns:
Dictionary mapping field names to default values.
- mark_env_sent() EnvRet[source]¶
Record the timestamp when the env return is sent.
- Returns:
Self for method chaining.
- numpy() EnvRet[source]¶
Convert all tensor attributes to numpy arrays.
- Returns:
Self with tensors converted to numpy arrays.
- class rlightning.types.MultiAgentEnvRet(env_id: str, observation: Any, last_reward: Dict[str, float]=0.0, last_terminated: Dict[str, bool]=False, last_truncated: Dict[str, bool]=False, info: Dict[str, ~typing.Any]=<factory>, _extra: Dict[str, ~typing.Any]=<factory>, ts_env_sent_ns: int = <factory>)[source]¶
Bases:
EnvRetEnvironment return for multi-agent interactions.
Extends EnvRet with dictionary-based rewards and termination flags for multiple agents.
- class rlightning.types.MultiAgentPolicyResponse(env_id: str, **data: Any)[source]¶
Bases:
PolicyResponsePolicy response for multi-agent interactions.
Extends PolicyResponse with support for multiple agents, where actions are stored in a dictionary keyed by agent ID.
- static make_example(action_spaces: Dict[str, MockModule('gymnasium.Space')], env_id: str | None = None, **data: Any) MultiAgentPolicyResponse[source]¶
Create an example MultiAgentPolicyResponse.
- Parameters:
action_spaces – Dictionary mapping agent IDs to action spaces.
env_id – Optional environment identifier.
**data – Additional response data.
- Returns:
MultiAgentPolicyResponse with sampled actions for all agents.
- class rlightning.types.PolicyResponse(env_id: str, **data: Any)[source]¶
Bases:
SimpleNamespacePolicy response container for single-agent interactions.
Holds the action and any additional data produced by a policy for a single environment step.
- env_id¶
Environment identifier this response is for.
- Additional attributes are set dynamically via **data.
- compute_sent_latency(now_ns: int | None = None) float[source]¶
Compute latency in seconds from env-sent timestamp to now.
Use for env->policy or env->buffer transfer time (e.g. in policy _rollout_hook or in buffer add_transition).
- Parameters:
now_ns – Current time in nanoseconds. If None, uses time.time_ns().
- Returns:
Latency in seconds, or 0.0 if ts_env_sent_ns is missing or invalid.
- cpu() PolicyResponse[source]¶
Move all tensor attributes to CPU.
- Returns:
Self with tensors moved to CPU.
- cuda(device: str | int | None = None) PolicyResponse[source]¶
Move all tensor attributes to CUDA.
- Parameters:
device – CUDA device index or string. Defaults to ‘cuda’.
- Returns:
Self with tensors moved to CUDA.
- static make_example(action_space: MockModule('gymnasium.Space'), env_id: str | None = None, **data: Any) PolicyResponse[source]¶
Create an example PolicyResponse.
- Parameters:
action_space – Gymnasium action space for sampling.
env_id – Optional environment identifier.
**data – Additional response data.
- Returns:
PolicyResponse with sampled action.
- mark_policy_sent() PolicyResponse[source]¶
Record the timestamp when the policy response is sent.
- Returns:
Self for method chaining.
- numpy() PolicyResponse[source]¶
Convert all tensor attributes to NumPy arrays.
- Returns:
Self with tensors converted to NumPy arrays.