palaestrai.types package#

Submodules#

palaestrai.types.box module#

class palaestrai.types.box.Box(low: ~typing.SupportsFloat | ~numpy.ndarray[~typing.Any, ~numpy.dtype[~typing.Any]] | ~typing.List[~typing.Any], high: ~typing.SupportsFloat | ~numpy.ndarray[~typing.Any, ~numpy.dtype[~typing.Any]] | ~typing.List[~typing.Any], shape: ~typing.Sequence[int] | None = None, dtype: type[~numpy.floating[~typing.Any]] | type[~numpy.integer[~typing.Any]] = <class 'numpy.float32'>, seed: int | ~numpy.random._generator.Generator | None = None)[source]#

Bases: Box, Space

A box in R^n, i.e.each coordinate is bounded.

There are two common use cases:

  • Identical bound for each dimension::
    >>> Box(low=-1.0, high=2.0, shape=(3, 4), dtype=np.float32)
    Box(3, 4)
    
  • Independent bound for each dimension::
    >>> Box(low=np.array([-1.0, -2.0]), high=np.array([2.0, 4.0]), dtype=np.float32)
    Box(2,)
    
classmethod from_string(s)[source]#

Dynamically constructs any descendant space object from a string.

On the Space class, this method acts as a factory to construct any known descrendant module from a string representation. It dynamically locates and instantiates any module that is listed in Space.TYPES.

All descendant types must also implement this class method as an alternative constructor (the known ones do). I.e., you can use palaestrai.types.Discrete.from_string("Discrete(2)") as well as palaestrai.types.Space.from_string("Discrete(2)").

Parameters:

s (str) – The string representation of a space object.

reshape_to_space(value: Iterable, **kwargs) ndarray[source]#

Reshape the data into the shape of the space with the dtype

scale(data, data_min=0, data_max=1)[source]#

Scale the given data linearly between box.low and box.high

data_min and data_max represent the minimum and maximum value the data can take If they are given as a scalar, the method will treat them as min/max value over all dimensions, If they vary per dimension they have to be given in the same shape as box.shape

to_string()[source]#

Returns the string representation of the space object.

String representations of space objects must read like a Python constructor, such as Box(low=[1.0, 0.0], high=[42.0, 42.42]). The name of the class is used to dynamically construct a new object from a string representation.

Returns:

The object’s string representation.

to_vector(data: ndarray, **kwargs) ndarray[source]#

Flattens the data to a 1d array

palaestrai.types.discrete module#

class palaestrai.types.discrete.Discrete(n: int | integer[Any], seed: int | Generator | None = None, start: int | integer[Any] = 0)[source]#

Bases: Discrete, Space

A discrete space in \(\{ start, start + 1, \dots, start + n-1 \}\).

Example:

>>> Discrete(2)
classmethod from_string(s)[source]#

Dynamically constructs any descendant space object from a string.

On the Space class, this method acts as a factory to construct any known descrendant module from a string representation. It dynamically locates and instantiates any module that is listed in Space.TYPES.

All descendant types must also implement this class method as an alternative constructor (the known ones do). I.e., you can use palaestrai.types.Discrete.from_string("Discrete(2)") as well as palaestrai.types.Space.from_string("Discrete(2)").

Parameters:

s (str) – The string representation of a space object.

reshape_to_space(value: Any, **kwargs) ndarray[source]#

Reshape the flat representation of data into a single number

Kwargs:

dtype: The dtype of the returned array. default: float

to_string()[source]#

Returns the string representation of the space object.

String representations of space objects must read like a Python constructor, such as Box(low=[1.0, 0.0], high=[42.0, 42.42]). The name of the class is used to dynamically construct a new object from a string representation.

Returns:

The object’s string representation.

to_vector(data: ndarray, **kwargs) ndarray[source]#

Flatten the discrete data to a ndarray of size self.n

palaestrai.types.mode module#

class palaestrai.types.mode.Mode(value)[source]#

Bases: Enum

An enumeration.

TEST = 2#
TRAIN = 1#

palaestrai.types.multi_binary module#

class palaestrai.types.multi_binary.MultiBinary(n: ndarray[Any, dtype[integer[Any]]] | Sequence[int] | int, seed: int | Generator | None = None)[source]#

Bases: MultiBinary, Space

A binary space of n dimensions

MultiBinary implements an n-dimensional space of boolean values. E.g., MultiBinary(5) accepts a vector of 5 boolean values.

Parameters:
  • n (NDArray[np.integer[Any]] | Sequence[int] | int) – The dimensions of the space. If a single integer is given, the space is flat; an array of integers allows multiple axes.

  • seed (Optional[int, np.random.Generator]) – Allows to optionally seed the space

classmethod from_string(s)[source]#

Dynamically constructs any descendant space object from a string.

On the Space class, this method acts as a factory to construct any known descrendant module from a string representation. It dynamically locates and instantiates any module that is listed in Space.TYPES.

All descendant types must also implement this class method as an alternative constructor (the known ones do). I.e., you can use palaestrai.types.Discrete.from_string("Discrete(2)") as well as palaestrai.types.Space.from_string("Discrete(2)").

Parameters:

s (str) – The string representation of a space object.

reshape_to_space(value: Iterable, **kwargs) ndarray[source]#

Turn a list of objects into binary data represented by a list.

Kwargs:

dtype: The dtype of the returned array. default: int

to_string()[source]#

Returns the string representation of the space object.

String representations of space objects must read like a Python constructor, such as Box(low=[1.0, 0.0], high=[42.0, 42.42]). The name of the class is used to dynamically construct a new object from a string representation.

Returns:

The object’s string representation.

to_vector(data: ndarray, **kwargs) ndarray[source]#

Represent a given binary data as a flat vector.

palaestrai.types.multi_discrete module#

class palaestrai.types.multi_discrete.MultiDiscrete(nvec: ~numpy.ndarray[~typing.Any, ~numpy.dtype[~numpy.integer[~typing.Any]]] | ~typing.List[int], dtype: str | ~typing.Type[~numpy.integer[~typing.Any]] = <class 'numpy.int64'>, seed: int | ~numpy.random._generator.Generator | None = None)[source]#

Bases: MultiDiscrete, Space

A series of discrete action spaces

The multi-discrete action space consists of a series of discrete action spaces with different number of actions in each. It is useful to represent game controllers or keyboards where each key can be represented as a discrete action space. It is parametrized by passing an array of positive integers specifying number of actions for each discrete action space.

For example:

MultiDiscrete([ 5, 2, 2 ])

classmethod from_string(s)[source]#

Dynamically constructs any descendant space object from a string.

On the Space class, this method acts as a factory to construct any known descrendant module from a string representation. It dynamically locates and instantiates any module that is listed in Space.TYPES.

All descendant types must also implement this class method as an alternative constructor (the known ones do). I.e., you can use palaestrai.types.Discrete.from_string("Discrete(2)") as well as palaestrai.types.Space.from_string("Discrete(2)").

Parameters:

s (str) – The string representation of a space object.

reshape_to_space(value: Iterable, **kwargs) ndarray[source]#

Turn data given in a flat form into distinct discrete values.

The method tries to infer the form of the data from the data, if that is ambiguous it falls back to as_argmax.

Kwargs:

as_argmax: Explicitly tell the method to treat the data as if it was produced by MultiDiscrete::flatten, with as_argmax = True; This parameter is required if sum(self.nvec) == product(self.nvec) default: None

to_string()[source]#

Returns the string representation of the space object.

String representations of space objects must read like a Python constructor, such as Box(low=[1.0, 0.0], high=[42.0, 42.42]). The name of the class is used to dynamically construct a new object from a string representation.

Returns:

The object’s string representation.

to_vector(data: ndarray, **kwargs) ndarray[source]#

Combine the n data points into a 1D vector.

Kwargs:
as_argmax: whether to turn the data into an argmax representation. I.e.

of the form: [0, 0, … , 0, 1, 0, … ,0] with […].argmax() = (n*MX + m*X + x) for a MultiDiscrete([N, M, X]) = [n, m, x]; If False the data is given of the form: [0, 0, …, 0, 1, 0, …, 0, 1, 0, …, 0, 1, 0, …, 0] with the 1s being at n, N+m, N+M+x, respectively, for a MultiDiscrete([N, M, X]) = [n, m, x] default: False

value: The value that the non zero entries in the transformed data will have

palaestrai.types.space module#

exception palaestrai.types.space.CanNotConvertException[source]#

Bases: Exception

class palaestrai.types.space.Space[source]#

Bases: ABC

Base class for space definitions

Derived classes allow a minimal mathematical representation of a space concept. Derived classes should also derive from a gymnasium.spaces class.

TYPES = ['Box', 'Dict', 'Discrete', 'MultiBinary', 'MultiDiscrete', 'Tuple']#
abstract contains(x)[source]#

Return True if the value given is a valid member of the space.

Parameters:

x – Any value

Returns:

True iff x is a member of the space

classmethod from_string(s) Discrete | Box | MultiDiscrete | MultiBinary | Tuple[source]#

Dynamically constructs any descendant space object from a string.

On the Space class, this method acts as a factory to construct any known descrendant module from a string representation. It dynamically locates and instantiates any module that is listed in Space.TYPES.

All descendant types must also implement this class method as an alternative constructor (the known ones do). I.e., you can use palaestrai.types.Discrete.from_string("Discrete(2)") as well as palaestrai.types.Space.from_string("Discrete(2)").

Parameters:

s (str) – The string representation of a space object.

abstract reshape_to_space(value: Iterable, **kwargs) ndarray[source]#

Return a list of data in the form of the space

Should raise a CanNotConvertException if the values can not be converted

Parameters:

value – The values to be represented

Returns:

The values in the form of the space

abstract sample()[source]#

Uniformly randomly sample a random element of this space.

seed(seed=None)[source]#

Seed the PRNG of this space.

abstract to_string()[source]#

Returns the string representation of the space object.

String representations of space objects must read like a Python constructor, such as Box(low=[1.0, 0.0], high=[42.0, 42.42]). The name of the class is used to dynamically construct a new object from a string representation.

Returns:

The object’s string representation.

abstract to_vector(data: ndarray, **kwargs) ndarray[source]#

Return a flat numpy array of data provided in the shape of the space.

Should give a warning if the shape of the data is unexpected.

Parameters:

data – The data to be transformed

Returns:

The data represented in a flat list

palaestrai.types.tuple module#

class palaestrai.types.tuple.Tuple(spaces, seed: int | Sequence[int] | Generator | None = None)[source]#

Bases: Tuple, Space

A tuple (i.e., product) of simpler spaces

Example usage: self.observation_space = spaces.Tuple(Discrete(2), Discrete(3))

classmethod from_string(s)[source]#

Dynamically constructs any descendant space object from a string.

On the Space class, this method acts as a factory to construct any known descrendant module from a string representation. It dynamically locates and instantiates any module that is listed in Space.TYPES.

All descendant types must also implement this class method as an alternative constructor (the known ones do). I.e., you can use palaestrai.types.Discrete.from_string("Discrete(2)") as well as palaestrai.types.Space.from_string("Discrete(2)").

Parameters:

s (str) – The string representation of a space object.

reshape_to_space(value: Iterable, **kwargs) ndarray[source]#

Reshape value using the contained spaces

to_string()[source]#

Returns the string representation of the space object.

String representations of space objects must read like a Python constructor, such as Box(low=[1.0, 0.0], high=[42.0, 42.42]). The name of the class is used to dynamically construct a new object from a string representation.

Returns:

The object’s string representation.

to_vector(data: ndarray, **kwargs) ndarray[source]#

Flatten data using the contained spaces

Module contents#