Skip to main content

State

Trait State 

Source
pub trait State<const R: usize>:
    Debug
    + Clone
    + Send
    + Sync {
    type Observation: Observation<R>;

    const RANK: usize = R;

    // Required methods
    fn shape() -> [usize; R];
    fn observe(&self) -> Self::Observation;
    fn is_valid(&self) -> bool;

    // Provided method
    fn numel(&self) -> usize { ... }
}
Expand description

The complete state of an environment (Markov property)

Provided Associated Constants§

Source

const RANK: usize = R

The rank of this state space — i.e. the number of axes (tensor order), not the size of any axis.

“Rank” here means the count of indices needed to address an element (NumPy ndim, Burn’s Tensor<B, R>), not matrix rank or CP-decomposition rank. This is automatically set to match the const generic parameter R.

Required Associated Types§

Required Methods§

Source

fn shape() -> [usize; R]

Returns the size of each axis in this state space.

The returned array has length R (the rank), where each element is the cardinality of that axis — the number of possible values along it. All values must be greater than zero.

Source

fn observe(&self) -> Self::Observation

Generate an observation from this state (may be partial)

Source

fn is_valid(&self) -> bool

Validates whether this state satisfies all constraints.

This method checks if the state is legal according to its type’s invariants. It does not check environment-specific legality - that’s the environment’s responsibility.

§Returns

Returns true if the state satisfies all structural constraints, false otherwise.

Provided Methods§

Source

fn numel(&self) -> usize

Returns the total number of scalar elements in this state’s representation.

This value is critical for:

  • Allocating buffers for state serialization
  • Determining neural network input layer dimensions
  • Validating state transformations (e.g., flattening/unflattening)
§Relationship to Shape

For consistency, numel() must equal the product of all dimensions returned by shape(). The default implementation enforces this by computing the product directly. Override only if the state uses a non-product layout.

§Returns

The total number of scalar elements needed to represent this state.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§