Module Fehu.Space Source Observation and action space definitions.
Spaces specify valid observations and actions, providing validation, sampling, and serialization. See Space for discrete, continuous, and composite space types.
Action and observation spaces for environment interfaces.
Spaces define the valid observations and actions for an environment. They specify shapes, constraints, and provide methods to validate, sample, and serialize values. Each space type corresponds to common RL scenarios: discrete choices, continuous vectors, multi-dimensional arrays, and composite structures.
Space TypesDiscrete : Integer choices from a finite setBox : Continuous vectors with bounded rangesMulti_binary : Binary vectors for multi-label scenariosMulti_discrete : Multiple discrete choicesTuple : Fixed-length heterogeneous sequencesDict : Named fields with different space typesSequence : Variable-length homogeneous sequencesText : String spaces for textual observations or actions UsageCreate a discrete action space and sample from it:
let action_space = Space.Discrete.create 4 in
let action = Space.sample action_spaceCreate a continuous observation space:
let obs_space = Space.Box.create
~low:[|-1.0; -1.0|]
~high:[|1.0; 1.0|]
in
let is_valid = Space.contains obs_space observationComposite spaces for structured data:
let space =
Space.Dict.create
[
( "position",
Space.Pack (Box.create ~low:[| -10.0 |] ~high:[| 10.0 |]) );
("velocity", Space.Pack (Box.create ~low:[| -1.0 |] ~high:[| 1.0 |]));
]Source type 'a t = { shape : int array option ; Dimensionality, if applicable
contains : 'a -> bool; Validates whether a value belongs to this space
sample : ?rng :Rune.Rng.key -> unit -> 'a ; Generates a random valid value
pack : 'a -> Value.t ; Converts to universal value representation
unpack : Value.t -> ('a , string) result ; Parses from universal representation
} Typed space representing valid values of type 'a.
Spaces encapsulate validation, sampling, and serialization logic for a type.
Source type packed = | Pack : 'a t -> packed Type-erased space for heterogeneous collections.
Source val shape : 'a t -> int array optionshape space returns the shape of space, if defined.
Shape represents dimensionality for array-like spaces. Returns None for scalar or variable-length spaces.
Source val contains : 'a t -> 'a -> boolcontains space value checks whether value is valid in space.
Returns true if value satisfies all constraints of space.
sample ~rng space generates a random valid value from space.
If rng is not provided, uses a default RNG.
pack space value converts value to a universal representation.
unpack space value parses value from universal representation.
Returns Ok v if value can be converted to a valid element of space, Error msg otherwise.