Table of Contents

Class OneOf

Namespace
Gym.Spaces
Assembly
Gym.dll

The direct sum of spaces: every element is an element of exactly one subspace, tagged with its index, as the pair (index, value) (an object?[] whose index is a 0-d int64 array, NumPy's np.int64). Use it for an action that is one of several kinds, each with its own parameters.

public class OneOf : Space<object?[]>, IDisposable, IReadOnlyList<Space>, IReadOnlyCollection<Space>, IEnumerable<Space>, IEnumerable, IEquatable<OneOf>
Inheritance
OneOf
Implements
Inherited Members
Extension Methods

Remarks

A sample first draws the subspace uniformly, then samples it; a mask or probability mask is a tuple with one entry per subspace, of which only the drawn subspace's is used.

The space owns its subspaces: disposing it disposes them. Reference: gymnasium/spaces/oneof.py.

Constructors

OneOf(params Space[])

The sum of the given subspaces.

public OneOf(params Space[] spaces)

Parameters

spaces Space[]

The subspaces, in order; at least one.

Exceptions

TypeError

Thrown for a null subspace.

ValueError

Thrown for no subspaces.

OneOf(IEnumerable<Space>, SpaceSeed?)

The sum of spaces (Gymnasium's OneOf(spaces, seed=seed)).

public OneOf(IEnumerable<Space> spaces, SpaceSeed? seed = null)

Parameters

spaces IEnumerable<Space>

The subspaces, in order; at least one. The space owns them from now on.

seed SpaceSeed?

An integer seed (it seeds the space's generator, which draws one int32 seed per subspace, then is seeded again with the same value), a generator to share, or null.

Exceptions

TypeError

Thrown when spaces or a subspace is null, with Gymnasium's messages.

ValueError

Thrown for no subspaces: Empty OneOf spaces are not supported.

Error

Thrown when an integer seed is negative.

Properties

Count

The number of subspaces (Gymnasium's len(space)).

public int Count { get; }

Property Value

int

IsNpFlattenable

Whether every subspace flattens to a box.

public override bool IsNpFlattenable { get; }

Property Value

bool

this[int]

The subspace at index (Gymnasium's space[index]).

public Space this[int index] { get; }

Parameters

index int

The index; negative counts from the end, as in Python.

Property Value

Space

The subspace.

Exceptions

IndexError

Thrown for an index outside the space: tuple index out of range.

Spaces

The subspaces (Gymnasium's spaces).

public IReadOnlyList<Space> Spaces { get; }

Property Value

IReadOnlyList<Space>

Methods

Contains(object?)

Whether x is an element (Gymnasium's contains): a pair whose index is a Python int or an np.int64 within the subspaces and whose value belongs to that subspace.

public override bool Contains(object? x)

Parameters

x object

The candidate: an object?[] or an ITuple of two items.

Returns

bool

true for an element.

Remarks

An index of another NumPy integer type (a 0-d int32 array) is refused, as Gymnasium's isinstance(x[0], (np.int64, int)) refuses np.int32; a bool is a Python int.

Dispose(bool)

Releases what a derived space owns (a Box's bounds); called once, by Dispose().

protected override void Dispose(bool disposing)

Parameters

disposing bool

true from Dispose(); no space has a finalizer.

Equals(OneOf?)

Gymnasium's ==: the same subspaces, equal in order.

public bool Equals(OneOf? other)

Parameters

other OneOf

The other space.

Returns

bool

true when equal.

Equals(object?)

Determines whether the specified object is equal to the current object.

public override bool Equals(object? obj)

Parameters

obj object

The object to compare with the current object.

Returns

bool

true if the specified object is equal to the current object; otherwise, false.

FromJsonable(object)

Converts Gymnasium's [index, value] lists back to pairs (Gymnasium's from_jsonable).

public override IList<object?[]> FromJsonable(object jsonable)

Parameters

jsonable object

The list of pairs.

Returns

IList<object[]>

The samples; each index is a new 0-d int64 array.

Exceptions

ArgumentException

Thrown when the data is not a list of pairs.

GetEnumerator()

Returns an enumerator that iterates through the collection.

public IEnumerator<Space> GetEnumerator()

Returns

IEnumerator<Space>

An enumerator that can be used to iterate through the collection.

GetHashCode()

Serves as the default hash function.

public override int GetHashCode()

Returns

int

A hash code for the current object.

Sample(object?, object?)

Draws one element (Gymnasium's sample): a subspace index, uniformly, then a sample of that subspace under its entry of mask or probability.

public override object?[] Sample(object? mask = null, object? probability = null)

Parameters

mask object

null, or a tuple (an object?[] or an ITuple) with one mask (or null) per subspace.

probability object

null, or a tuple with one probability mask (or null) per subspace.

Returns

object[]

A new pair {index, value}; the caller owns the index array and any arrays in the value.

Remarks

The index is drawn before the arguments are checked, as in Gymnasium, so a refused call still moves the generator.

Exceptions

ValueError

Thrown when both masks are given.

AssertionError

Thrown for a mask that is not a tuple of one entry per subspace, with Gymnasium's messages.

SeedCore(BigInteger?)

Gymnasium's OneOf.seed for an integer or null: null seeds the space and every subspace from entropy; an integer seeds the space's generator, draws one seed below int32's maximum per subspace, and seeds the generator again with the same value, so integer and list seeding leave it alike.

protected override object SeedCore(BigInteger? seed)

Parameters

seed BigInteger?

The seed, or null.

Returns

object

The space's seed followed by the subspaces', as a list.

Exceptions

Error

Thrown for a negative seed.

SeedListCore(IReadOnlyList<object?>)

Gymnasium's OneOf.seed for a list: the space's own seed, then one seed (of any form) per subspace.

protected override object SeedListCore(IReadOnlyList<object?> seeds)

Parameters

seeds IReadOnlyList<object>

The seeds, one more than the subspaces.

Returns

object

The seeds used.

Exceptions

ValueError

Thrown when the list's length is not the number of subspaces + 1.

Error

Thrown when the space's own seed is negative or not a Python int.

SeedTypeError(object?)

Gymnasium's OneOf.seed error for a seed that is not an integer, a list, a tuple or null.

protected override Exception SeedTypeError(object? seed)

Parameters

seed object

The refused seed.

Returns

Exception

TypeError: Expected None, int, or tuple of ints, actual type: <class 'dict'>.

ToJsonable(IEnumerable<object?[]>)

Converts samples to Gymnasium's JSON form: one [index, value] list per sample, the value being the first item of the subspace's to_jsonable of that one sample.

public override object ToJsonable(IEnumerable<object?[]> samples)

Parameters

samples IEnumerable<object[]>

Samples of this space.

Returns

object

The list of pairs.

Exceptions

KeyError

Thrown for a Dict subspace, whose JSON form is a dict: Gymnasium indexes it with [0] and raises KeyError: 0, and so does this.

ToString()

Gymnasium's repr: OneOf(Discrete(2), Box(-1.0, 1.0, (2,), float32)).

public override string ToString()

Returns

string

The text.