Table of Contents

Class DiscretizeObservation<TObs, TAct>

Namespace
Gym.Wrappers
Assembly
Gym.dll

Discretizes a finite Box observation uniformly, as Gymnasium's DiscretizeObservation does: each of the box's elements into bins equal bins, the indices combined into one Discrete number (row-major) or kept apart as a MultiDiscrete vector.

public class DiscretizeObservation<TObs, TAct> : TransformObservation<NDArray, TObs, TAct>, IDynamicAttributes, IWrapper, IEnv, IDisposable, IRecordConstructorArgs

Type Parameters

TObs

The env's observation type: arrays (NDArray, or object holding them).

TAct

The action type, passed through.

Inheritance
Env<NDArray, TAct>
Wrapper<NDArray, TAct, TObs, TAct>
DiscretizeObservation<TObs, TAct>
Implements
Inherited Members
Extension Methods

Remarks

The bin edges are NumPy's np.linspace(low[i], high[i], bins[i] + 1) in the box's dtype; an observation is clipped to [low, high - 1e-8] (the margin in the box's dtype, so for float32 it often vanishes) and each element located with np.digitize. A Discrete observation is a 0-d int64 array (Python's int).

Reference: gymnasium/wrappers/transform_observation.py.

Constructors

DiscretizeObservation(Env<TObs, TAct>, DiscretizeObservationOptions)

Builds the instance from its options record: the properties set, the constructor's defaults for the rest.

public DiscretizeObservation(Env<TObs, TAct> env, DiscretizeObservationOptions options)

Parameters

env Env<TObs, TAct>

The env to wrap.

options DiscretizeObservationOptions

The options.

Exceptions

ArgumentNullException

Thrown when options is null.

TypeError

Thrown when a required argument is unset, with Python's missing-argument message.

DiscretizeObservation(Env<TObs, TAct>, object, bool)

Wraps env.

public DiscretizeObservation(Env<TObs, TAct> env, object bins, bool multidiscrete = false)

Parameters

env Env<TObs, TAct>

The env; its observation space must be a finite Box.

bins object

The bins per element: one number for every element, or a sequence of one per element.

multidiscrete bool

Whether observations are MultiDiscrete vectors instead of one Discrete number.

Exceptions

TypeError

Thrown when the space is no Box (Gymnasium's message), or env is null.

ValueError

Thrown when a bound is infinite, or the bins do not match the elements, with Gymnasium's messages.

Properties

BinEdges

The inner bin edges of each element (bin_edges), in the box's dtype.

public NDArray[] BinEdges { get; }

Property Value

NDArray[]

Bins

The bins of each element (bins).

public long[] Bins { get; }

Property Value

long[]

High

The box's upper bounds, flattened (high).

public NDArray High { get; }

Property Value

NDArray

Low

The box's lower bounds, flattened (low).

public NDArray Low { get; }

Property Value

NDArray

Multidiscrete

Whether observations are MultiDiscrete (multidiscrete).

public bool Multidiscrete { get; }

Property Value

bool

NDims

The number of elements discretized (n_dims).

[PyName("n_dims")]
public int NDims { get; }

Property Value

int

Methods

Dispose(bool)

Releases the bounds and edges, then disposes the wrapper (and the env beneath) as every wrapper does.

protected override void Dispose(bool disposing)

Parameters

disposing bool

true from Dispose().

RevertObservation(object)

The bin bounds a discretized observation came from (revert_observation): for each element, the edges edges[idx] and edges[idx + 1] of its bin.

public (NDArray Low, NDArray High) RevertObservation(object observation)

Parameters

observation object

A discretized observation: a number (or 0-d array) for Discrete, an array for MultiDiscrete.

Returns

(NDArray Low, NDArray High)

New arrays of the lower and upper edges, in the env's observation dtype; the caller owns them.

Exceptions

IndexOutOfRangeException

Thrown when an index is beyond its element's bins.