Class Text
The strings of a character set whose length lies in [MinLength, MaxLength], such as a command or a name.
public class Text : Space<string>, IDisposable, IEquatable<Text>
- Inheritance
-
Text
- Implements
- Inherited Members
- Extension Methods
Remarks
Characters are Unicode code points, as Python's are, not UTF-16 code units: a character outside the BMP (an emoji) counts once toward a string's length, and a charset may hold it.
Reference: gymnasium/spaces/text.py.
Constructors
Text(long, ISet<string>, long, SpaceSeed?)
Strings of a character set (Gymnasium's Text(..., charset=frozenset(...))): the characters are sorted, as
Gymnasium sorts a set so sampling does not depend on hash order.
public Text(long maxLength, ISet<string> charset, long minLength = 1, SpaceSeed? seed = null)
Parameters
maxLengthlongThe longest length, inclusive.
charsetISet<string>The characters; each must be a single code point.
minLengthlongThe shortest length, inclusive.
seedSpaceSeed?An integer seed, a generator to share, or null.
Exceptions
- ValueError
Thrown for invalid lengths, or a charset element that is not one character.
- ArgumentNullException
Thrown when
charsetis null.- Error
Thrown when an integer
seedis negative.
Text(long, long, string?, SpaceSeed?)
Strings of charset with minLength to maxLength characters
(Gymnasium's Text(max_length, min_length=1, charset=alphanumeric, seed=seed) with a string charset, whose
order is kept and duplicates dropped).
public Text(long maxLength, long minLength = 1, string? charset = null, SpaceSeed? seed = null)
Parameters
maxLengthlongThe longest length, inclusive.
minLengthlongThe shortest length, inclusive; 1 by default, so no empty string.
charsetstringThe characters, in sampling order; null is Alphanumeric (Gymnasium's default set, sorted).
seedSpaceSeed?An integer seed, a generator to share, or null.
Exceptions
- ValueError
Thrown for a negative minimum or a minimum above the maximum, with Gymnasium's messages.
- Error
Thrown when an integer
seedis negative.
Fields
Alphanumeric
Gymnasium's default charset, alphanumeric (ASCII letters and digits), as the sorted list a set becomes.
public const string Alphanumeric = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
Field Value
Properties
CharacterList
The characters in sampling order (Gymnasium's character_list).
public IReadOnlyList<string> CharacterList { get; }
Property Value
CharacterSet
The characters as a set (Gymnasium's character_set).
public IReadOnlySet<string> CharacterSet { get; }
Property Value
Characters
Every character, sorted, in one string (Gymnasium's characters); the repr prints it.
public string Characters { get; }
Property Value
IsNpFlattenable
A text flattens to the indices of its characters, padded to the longest length.
public override bool IsNpFlattenable { get; }
Property Value
MaxLength
The longest length, inclusive (Gymnasium's max_length).
public long MaxLength { get; }
Property Value
MinLength
The shortest length, inclusive (Gymnasium's min_length).
public long MinLength { get; }
Property Value
Methods
CharacterIndex(string)
A character's index in CharacterList (Gymnasium's character_index, an np.int32).
public int CharacterIndex(string character)
Parameters
characterstringThe character.
Returns
- int
Its index.
Exceptions
- KeyError
Thrown for a character outside the set.
Contains(object?)
Whether x is a string of the right length (in code points) made of the charset's characters.
public override bool Contains(object? x)
Parameters
xobjectThe candidate.
Returns
Equals(Text?)
Gymnasium's ==: the same lengths and character set (order aside).
public bool Equals(Text? other)
Parameters
otherTextThe other space.
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
GetHashCode()
Serves as the default hash function.
public override int GetHashCode()
Returns
- int
A hash code for the current object.
Sample(object?, object?)
Draws a string (Gymnasium's sample): a length drawn from [MinLength, MaxLength] (or given), then each
character drawn from the charset, uniformly or under a mask or probability mask.
public override string Sample(object? mask = null, object? probability = null)
Parameters
maskobjectnull, or a tuple
(length, charlist_mask): a fixed length (or null) and an int8 array over CharacterList whose 1s allow characters (or null).probabilityobjectnull, or a tuple
(length, probabilities)with a float64 array summing to 1.
Returns
- string
The string.
Remarks
The characters are drawn by choice(len(charset), size=length, p=p), which is exactly NumPy's
choice(character_list, size=length, p=p) draw (choice picks indices, then the items), so a text space seeded
like Gymnasium's draws the same strings. Reference: gymnasium/spaces/text.py.
Exceptions
- ValueError
Thrown when both masks are given, or when a mask allows no character and the minimum length is above 0.
- AssertionError
Thrown for a malformed mask, with Gymnasium's messages.
Seed(BigInteger?)
Reseeds the space's generator (Gymnasium's seed), typed (DR-86): a simple space's seed is one integer, so
it comes back as a BigInteger where Seed(BigInteger?) returns it boxed.
public BigInteger Seed(BigInteger? seed = null)
Parameters
seedBigInteger?Any non-negative integer, or null for a fresh generator from OS entropy.
Returns
- BigInteger
The seed used:
seeditself, or the 128-bit entropy of an unseeded call, which reproduces it.
Remarks
Footgun: a call through a Space reference binds the untyped base method, which returns the same seed as an object.
Exceptions
- Error
Thrown when
seedis negative, with Gymnasium's message.- InvalidCastException
Thrown by a subclass whose seeding returns something other than one integer.
ToString()
Gymnasium's repr: Text(1, 10, charset=0123456789ABC…xyz).
public override string ToString()
Returns
- string
The text.