package fehu

  1. Overview
  2. Docs

Module Fehu.SpaceSource

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 Types

  • Discrete: Integer choices from a finite set
  • Box: Continuous vectors with bounded ranges
  • Multi_binary: Binary vectors for multi-label scenarios
  • Multi_discrete: Multiple discrete choices
  • Tuple: Fixed-length heterogeneous sequences
  • Dict: Named fields with different space types
  • Sequence: Variable-length homogeneous sequences
  • Text: String spaces for textual observations or actions

Usage

Create a discrete action space and sample from it:

  let action_space = Space.Discrete.create 4 in
  let action = Space.sample action_space

Create 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 observation

Composite 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 |]));
      ]
Sourcemodule Value : sig ... end
Sourcetype 'a t = {
  1. shape : int array option;
    (*

    Dimensionality, if applicable

    *)
  2. contains : 'a -> bool;
    (*

    Validates whether a value belongs to this space

    *)
  3. sample : ?rng:Rune.Rng.key -> unit -> 'a;
    (*

    Generates a random valid value

    *)
  4. pack : 'a -> Value.t;
    (*

    Converts to universal value representation

    *)
  5. 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.

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

    Type-erased space for heterogeneous collections.

    *)
Sourceval shape : 'a t -> int array option

shape space returns the shape of space, if defined.

Shape represents dimensionality for array-like spaces. Returns None for scalar or variable-length spaces.

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

contains space value checks whether value is valid in space.

Returns true if value satisfies all constraints of space.

Sourceval sample : ?rng:Rune.Rng.key -> 'a t -> 'a

sample ~rng space generates a random valid value from space.

If rng is not provided, uses a default RNG.

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

pack space value converts value to a universal representation.

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

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.

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