Class DiscretizeObservation<TObs, TAct>
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
TObsThe env's observation type: arrays (NDArray, or object holding them).
TActThe action type, passed through.
- Inheritance
-
ObservationWrapper<NDArray, TObs, TAct>TransformObservation<NDArray, 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
envEnv<TObs, TAct>The env to wrap.
optionsDiscretizeObservationOptionsThe options.
Exceptions
- ArgumentNullException
Thrown when
optionsis 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
envEnv<TObs, TAct>The env; its observation space must be a finite
Box.binsobjectThe bins per element: one number for every element, or a sequence of one per element.
multidiscreteboolWhether observations are
MultiDiscretevectors instead of oneDiscretenumber.
Exceptions
- TypeError
Thrown when the space is no
Box(Gymnasium's message), orenvis 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
Low
The box's lower bounds, flattened (low).
public NDArray Low { get; }
Property Value
Multidiscrete
Whether observations are MultiDiscrete (multidiscrete).
public bool Multidiscrete { get; }
Property Value
NDims
The number of elements discretized (n_dims).
[PyName("n_dims")]
public int NDims { get; }
Property Value
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
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
observationobjectA discretized observation: a number (or 0-d array) for
Discrete, an array forMultiDiscrete.
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.