Module Fehu.Space

Observation and action spaces.

Spaces define valid observations and actions for reinforcement learning environments. They specify shapes, constraints, and provide methods to validate, sample, and serialize values.

Each space type corresponds to a common RL scenario: discrete choices, continuous vectors, binary indicators, composite structures, and variable-length sequences.

Structural description

type spec =
  1. | Discrete of {
    1. start : int;
    2. n : int;
    }
    (*

    Integer choices in [start; start + n - 1].

    *)
  2. | Box of {
    1. low : float array;
    2. high : float array;
    }
    (*

    Continuous vector bounded per dimension.

    *)
  3. | Multi_binary of {
    1. n : int;
    }
    (*

    Binary vector of length n.

    *)
  4. | Multi_discrete of {
    1. nvec : int array;
    }
    (*

    Multiple discrete axes with per-axis cardinalities.

    *)
  5. | Tuple of spec list
    (*

    Fixed-length heterogeneous sequence.

    *)
  6. | Dict of (string * spec) list
    (*

    Named fields with different types.

    *)
  7. | Sequence of {
    1. min_length : int;
    2. max_length : int option;
    3. base : spec;
    }
    (*

    Variable-length homogeneous sequence.

    *)
  8. | Text of {
    1. charset : string;
    2. max_length : int;
    }
    (*

    Character strings from a fixed alphabet.

    *)

Structural description of a space. Two spaces are compatible when their specs are equal.

val equal_spec : spec -> spec -> bool

equal_spec a b is true iff a and b describe structurally identical spaces.

Spaces

type 'a t

The type for spaces over values of type 'a. A space is self-contained: all bounds, constraints, and serialization logic are stored in the value itself.

type packed =
  1. | Pack : 'a t -> packed
    (*

    Type-erased space for heterogeneous collections.

    *)

Operations

val spec : 'a t -> spec

spec s is the structural description of s.

val shape : 'a t -> int array option

shape s is the dimensionality of s, if defined. None for scalar or variable-length spaces.

val contains : 'a t -> 'a -> bool

contains s v is true iff v is valid in s.

val sample : 'a t -> 'a

sample s is a uniformly sampled value from s.

Random keys are drawn from the implicit RNG scope.

val pack : 'a t -> 'a -> Value.t

pack s v is v converted to the universal Value.t representation.

val unpack : 'a t -> Value.t -> ('a, string) Stdlib.result

unpack s v is Ok x if v can be converted to a valid element of s, or Error msg otherwise.

val boundary_values : 'a t -> Value.t list

boundary_values s is a list of representative edge-case values for s. Includes lower/upper bounds or canonical sentinels when known. The empty list when no boundary values apply.

Space types

module Discrete : sig ... end
module Box : sig ... end
module Multi_binary : sig ... end
module Multi_discrete : sig ... end
module Tuple : sig ... end
module Dict : sig ... end
module Sequence : sig ... end
module Text : sig ... end