Class Tuple
The cartesian product of spaces: every element is a tuple (an object?[], DR-13's generic form)
holding one element of each subspace, in order, such as Blackjack's (player sum, dealer card, usable ace).
public class Tuple : Space<object?[]>, IDisposable, IReadOnlyList<Space>, IReadOnlyCollection<Space>, IEnumerable<Space>, IEnumerable, IEquatable<Tuple>
- Inheritance
-
Tuple
- Implements
- Inherited Members
- Extension Methods
Remarks
Named as Gymnasium names it, so using System; makes Tuple ambiguous with Tuple
(CS0104): Gym.NET's files alias it, using Tuple = Gym.Spaces.Tuple; (port plan ยง3.8).
The space owns its subspaces: disposing it disposes them. Reference: gymnasium/spaces/tuple.py.
Constructors
Tuple(params Space[])
The product of the given subspaces.
public Tuple(params Space[] spaces)
Parameters
spacesSpace[]The subspaces, in order.
Exceptions
Tuple(IEnumerable<Space>, SpaceSeed?)
The product of spaces (Gymnasium's Tuple(spaces, seed=seed)).
public Tuple(IEnumerable<Space> spaces, SpaceSeed? seed = null)
Parameters
spacesIEnumerable<Space>The subspaces, in order; the tuple owns them from now on.
seedSpaceSeed?An integer seed (it seeds the tuple's own generator, which then draws one
int32seed per subspace), a generator to share, or null.
Exceptions
- TypeError
Thrown for a null subspace, Gymnasium's "does not inherit from
gymnasium.Space".- ArgumentNullException
Thrown when
spacesis null.- Error
Thrown when an integer
seedis negative.
Properties
Count
The number of subspaces (Gymnasium's len(space)).
public int Count { get; }
Property Value
IsNpFlattenable
Whether every subspace flattens to a box.
public override bool IsNpFlattenable { get; }
Property Value
this[int]
The subspace at index (Gymnasium's space[index]).
public Space this[int index] { get; }
Parameters
indexintThe index; negative counts from the end, as in Python.
Property Value
- Space
The subspace.
Exceptions
- IndexError
Thrown for an index outside the tuple:
tuple index out of range.
Spaces
The subspaces (Gymnasium's spaces).
public IReadOnlyList<Space> Spaces { get; }
Property Value
Methods
Contains(object?)
Whether x is an element (Gymnasium's contains): a tuple (or a list, a .NET array, or
an array along its first axis, which Gymnasium promotes) with one member of each subspace, in order.
public override bool Contains(object? x)
Parameters
xobjectThe candidate.
Returns
CountOf(Space)
How many subspaces equal value (Gymnasium's count, from Python's Sequence
mixin; named CountOf because Count is the length).
public int CountOf(Space value)
Parameters
valueSpaceThe space to look for; compared with Equals(object), Python's
==.
Returns
- int
The number of equal subspaces.
Dispose(bool)
protected override void Dispose(bool disposing)
Parameters
Equals(Tuple?)
Gymnasium's ==: the same subspaces, equal in order.
public bool Equals(Tuple? other)
Parameters
otherTupleThe other tuple.
Returns
Equals(object?)
Determines whether the specified object is equal to the current object.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with the current object.
Returns
FromJsonable(object)
Converts Gymnasium's per-subspace JSON lists back to tuples (Gymnasium's from_jsonable).
public override IList<object?[]> FromJsonable(object jsonable)
Parameters
jsonableobjectThe list of per-subspace lists.
Returns
Exceptions
- ValueError
Thrown when the per-subspace lists differ in length (Python's
zip(strict=True)).
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.
IndexOf(Space, int, int?)
The position of the first subspace equal to value within
[start, stop) (Gymnasium's index, from Python's Sequence
mixin); negative bounds count from the end, as in Python.
public int IndexOf(Space value, int start = 0, int? stop = null)
Parameters
valueSpaceThe space to look for.
startintThe first position searched.
stopint?One past the last position searched, or null for the end.
Returns
- int
The position.
Exceptions
- ValueError
Thrown when no subspace in the range equals
value(Python's bareValueError).
Sample(object?, object?)
Draws one element (Gymnasium's sample): one sample from each subspace, in order, optionally under a tuple
of per-subspace masks or probability masks.
public override object?[] Sample(object? mask = null, object? probability = null)
Parameters
maskobjectnull, or a tuple (an object?[] or an ITuple) with one mask (or null) per subspace.
probabilityobjectnull, or a tuple with one probability mask (or null) per subspace.
Returns
- object[]
A new tuple of the subspaces' samples; arrays in it are owned by the caller.
Exceptions
- ValueError
Thrown when both masks are given.
- AssertionError
Thrown for a mask that is not a tuple of the right length, with Gymnasium's messages.
SeedCore(BigInteger?)
Gymnasium's Tuple.seed for an integer or null: null seeds every
subspace from entropy; an integer seeds the tuple's generator, which draws one seed below int32's maximum
per subspace (integers(iinfo(int32).max, size=len)).
protected override object SeedCore(BigInteger? seed)
Parameters
seedBigInteger?The seed, or null.
Returns
- object
The subspaces' seeds, as a list.
Exceptions
- Error
Thrown for a negative seed.
SeedListCore(IReadOnlyList<object?>)
Gymnasium's Tuple.seed for a list: one seed (of any form) per subspace.
protected override object SeedListCore(IReadOnlyList<object?> seeds)
Parameters
seedsIReadOnlyList<object>The seeds.
Returns
- object
The subspaces' seeds.
Exceptions
- ValueError
Thrown when the list's length is not the number of subspaces.
SeedTypeError(object?)
Gymnasium's Tuple.seed error for a seed that is not an integer, a list, a tuple or null.
protected override Exception SeedTypeError(object? seed)
Parameters
seedobjectThe refused seed.
Returns
- Exception
TypeError: Expected seed type: list, tuple, int or None, actual type: <class 'dict'>.
ToJsonable(IEnumerable<object?[]>)
Converts samples to Gymnasium's JSON form, a list with one entry per subspace, each the subspace's
to_jsonable of that position across the batch.
public override object ToJsonable(IEnumerable<object?[]> samples)
Parameters
samplesIEnumerable<object[]>Samples of this space.
Returns
- object
The per-subspace lists.
ToString()
Gymnasium's repr: Tuple(Discrete(2), Box(-1.0, 1.0, (2,), float32)).
public override string ToString()
Returns
- string
The text.