Class Space<T>
A space whose elements are T: Box and Discrete are
Space<NDArray> (a discrete element is a 0-d int64 array, NumPy's np.int64, DR-08). An env's
ActionSpace and ObservationSpace are typed this way (DR-07), and custom spaces derive from it.
public abstract class Space<T> : Space, IDisposable
Type Parameters
TThe element type, what Sample(object?, object?) returns.
- Inheritance
-
Space<T>
- Implements
- Derived
- Inherited Members
- Extension Methods
Remarks
Reference: gymnasium/spaces/space.py.
Constructors
Space(Shape?, DType?, SpaceSeed?)
Records the shape and dtype and seeds the generator when a seed is given.
protected Space(Shape? shape, DType? dtype, SpaceSeed? seed)
Parameters
shapeShape?The shape of every element, or null when elements are not arrays.
dtypeDTypeThe element dtype, or null when elements are not arrays.
seedSpaceSeed?An integer seed, a generator to share, or null for a lazy entropy-seeded one.
Exceptions
- Error
Thrown when an integer
seedis negative.
Methods
FromJsonable(object)
Converts JSON-friendly data back to samples (Gymnasium's from_jsonable); the default casts each item. It
hides the untyped FromJsonable(object), which returns the same samples boxed.
public virtual IList<T> FromJsonable(object jsonable)
Parameters
jsonableobjectThe data, a list for the default.
Returns
- IList<T>
The samples.
Exceptions
- ArgumentException
Thrown by the default when the data is not a list.
- InvalidCastException
Thrown by the default when an item is not a
T.
JsonList(object)
JSON-friendly data as the list a space's from_jsonable iterates over.
protected static IList<object?> JsonList(object jsonable)
Parameters
jsonableobjectThe data.
Returns
Exceptions
- ArgumentException
Thrown when the data is not a list.
Sample(object?, object?)
Draws a random element (Gymnasium's sample). A mask restricts which elements are drawn and a probability
mask weights them; the two cannot be combined, and each space documents the forms it accepts.
public abstract T Sample(object? mask = null, object? probability = null)
Parameters
maskobjectA space-specific mask, or null.
probabilityobjectA space-specific probability mask, or null.
Returns
- T
The element; arrays are new and owned by the caller.
ToJsonable(IEnumerable<T>)
Converts a batch of samples to JSON-friendly data (Gymnasium's to_jsonable); the default keeps them as they
are, in a list, as Gymnasium's base does.
public virtual object ToJsonable(IEnumerable<T> samples)
Parameters
samplesIEnumerable<T>The samples.
Returns
- object
The JSON-friendly data; a list of one value per sample unless the space documents another form.