Skip to content

Spaces

Specifications/contracts for describing the shape and domain of observations and actions used within an environment.

envrax.spaces.Space

Bases: ABC

Abstract base class for action and observation spaces.

Source code in envrax/spaces.py
Python
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Space(ABC):
    """Abstract base class for action and observation spaces."""

    @abstractmethod
    def sample(self, rng: chex.Array) -> chex.Array:
        """Sample a random element from this space."""
        ...

    @abstractmethod
    def contains(self, x: chex.Array) -> bool:
        """Return True if x is a valid element of this space."""
        ...

    @abstractmethod
    def batch(self, n: int) -> "Space":
        """
        Return a batched version of this space with a leading dimension `n`.

        Parameters
        ----------
        n : int
            Batch size.

        Returns
        -------
        batched : Space
            Space with a leading `n` dimension.
        """
        ...

sample(rng) abstractmethod

Sample a random element from this space.

Source code in envrax/spaces.py
Python
13
14
15
16
@abstractmethod
def sample(self, rng: chex.Array) -> chex.Array:
    """Sample a random element from this space."""
    ...

contains(x) abstractmethod

Return True if x is a valid element of this space.

Source code in envrax/spaces.py
Python
18
19
20
21
@abstractmethod
def contains(self, x: chex.Array) -> bool:
    """Return True if x is a valid element of this space."""
    ...

batch(n) abstractmethod

Return a batched version of this space with a leading dimension n.

Parameters:

Name Type Description Default
n int

Batch size.

required

Returns:

Name Type Description
batched Space

Space with a leading n dimension.

Source code in envrax/spaces.py
Python
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@abstractmethod
def batch(self, n: int) -> "Space":
    """
    Return a batched version of this space with a leading dimension `n`.

    Parameters
    ----------
    n : int
        Batch size.

    Returns
    -------
    batched : Space
        Space with a leading `n` dimension.
    """
    ...

envrax.spaces.Discrete dataclass

Bases: Space

Discrete action space — n equally-likely integer actions.

Parameters:

Name Type Description Default
n int

Number of discrete actions.

required
dtype Type

Element dtype. Defaults to jnp.int32.

required
Source code in envrax/spaces.py
Python
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
@dataclass(frozen=True)
class Discrete(Space):
    """
    Discrete action space — `n` equally-likely integer actions.

    Parameters
    ----------
    n : int
        Number of discrete actions.
    dtype : Type
        Element dtype. Defaults to `jnp.int32`.
    """

    n: int
    dtype: Type = jnp.int32

    def sample(self, rng: chex.Array) -> chex.Array:
        """
        Sample a random action uniformly from `[0, n)`.

        Parameters
        ----------
        rng : chex.Array
            JAX PRNG key.

        Returns
        -------
        action : chex.Array
            int32 — Sampled action index.
        """
        return jax.random.randint(
            rng,
            shape=(),
            minval=0,
            maxval=self.n,
            dtype=self.dtype,
        )

    def contains(self, x: chex.Array) -> bool:
        """
        Return True if `x` is a valid action index.

        Parameters
        ----------
        x : chex.Array
            Action to validate. Expected to be an integer scalar.

        Returns
        -------
        valid : bool
            True if `x` lies in `[0, n)`, False otherwise.
        """
        return bool((x >= 0) & (x < self.n))

    def batch(self, n: int) -> "MultiDiscrete":
        """
        Batch `n` copies into a `MultiDiscrete` with identical sub-spaces.

        Parameters
        ----------
        n : int
            Batch size.

        Returns
        -------
        batched : MultiDiscrete
            `MultiDiscrete(nvec=(self.n,) * n, dtype=self.dtype)`.
        """
        return MultiDiscrete(nvec=(self.n,) * n, dtype=self.dtype)

sample(rng)

Sample a random action uniformly from [0, n).

Parameters:

Name Type Description Default
rng Array

JAX PRNG key.

required

Returns:

Name Type Description
action Array

int32 — Sampled action index.

Source code in envrax/spaces.py
Python
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def sample(self, rng: chex.Array) -> chex.Array:
    """
    Sample a random action uniformly from `[0, n)`.

    Parameters
    ----------
    rng : chex.Array
        JAX PRNG key.

    Returns
    -------
    action : chex.Array
        int32 — Sampled action index.
    """
    return jax.random.randint(
        rng,
        shape=(),
        minval=0,
        maxval=self.n,
        dtype=self.dtype,
    )

contains(x)

Return True if x is a valid action index.

Parameters:

Name Type Description Default
x Array

Action to validate. Expected to be an integer scalar.

required

Returns:

Name Type Description
valid bool

True if x lies in [0, n), False otherwise.

Source code in envrax/spaces.py
Python
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def contains(self, x: chex.Array) -> bool:
    """
    Return True if `x` is a valid action index.

    Parameters
    ----------
    x : chex.Array
        Action to validate. Expected to be an integer scalar.

    Returns
    -------
    valid : bool
        True if `x` lies in `[0, n)`, False otherwise.
    """
    return bool((x >= 0) & (x < self.n))

batch(n)

Batch n copies into a MultiDiscrete with identical sub-spaces.

Parameters:

Name Type Description Default
n int

Batch size.

required

Returns:

Name Type Description
batched MultiDiscrete

MultiDiscrete(nvec=(self.n,) * n, dtype=self.dtype).

Source code in envrax/spaces.py
Python
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
def batch(self, n: int) -> "MultiDiscrete":
    """
    Batch `n` copies into a `MultiDiscrete` with identical sub-spaces.

    Parameters
    ----------
    n : int
        Batch size.

    Returns
    -------
    batched : MultiDiscrete
        `MultiDiscrete(nvec=(self.n,) * n, dtype=self.dtype)`.
    """
    return MultiDiscrete(nvec=(self.n,) * n, dtype=self.dtype)

envrax.spaces.Box dataclass

Bases: Space

Continuous box observation space with scalar bounds.

Parameters:

Name Type Description Default
low float | int

Lower bound (inclusive) applied to all elements.

required
high float | int

Upper bound (inclusive) applied to all elements.

required
shape Tuple[int, ...]

Shape of a single observation.

required
dtype Type

Element dtype. Defaults to jnp.float32.

required
Source code in envrax/spaces.py
Python
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
@dataclass(frozen=True)
class Box(Space):
    """
    Continuous box observation space with scalar bounds.

    Parameters
    ----------
    low : float | int
        Lower bound (inclusive) applied to all elements.
    high : float | int
        Upper bound (inclusive) applied to all elements.
    shape : Tuple[int, ...]
        Shape of a single observation.
    dtype : Type
        Element dtype. Defaults to `jnp.float32`.
    """

    low: float | int
    high: float | int
    shape: Tuple[int, ...]
    dtype: Type = jnp.float32

    def sample(self, rng: chex.Array) -> chex.Array:
        """
        Sample a random observation within `[low, high]`.

        Parameters
        ----------
        rng : chex.Array
            JAX PRNG key.

        Returns
        -------
        obs : chex.Array
            `dtype[*shape]` — Sampled observation array.
        """
        if jnp.issubdtype(self.dtype, jnp.integer):
            return jax.random.randint(
                rng,
                shape=self.shape,
                minval=int(self.low),
                maxval=int(self.high) + 1,
                dtype=self.dtype,
            )

        return jax.random.uniform(
            rng,
            shape=self.shape,
            minval=self.low,
            maxval=self.high,
        ).astype(self.dtype)

    def contains(self, x: chex.Array) -> bool:
        """
        Return True if `x` is a valid observation within the space.

        Parameters
        ----------
        x : chex.Array
            Observation to validate. Expected to match `self.shape`.

        Returns
        -------
        valid : bool
            True if `x.shape == self.shape` and every element lies in `[low, high]`.
        """
        return bool(
            (x.shape == self.shape) & jnp.all(x >= self.low) & jnp.all(x <= self.high)
        )

    def batch(self, n: int) -> "Box":
        """
        Prepend a leading `n` dimension to the shape.

        Parameters
        ----------
        n : int
            Batch size.

        Returns
        -------
        batched : Box
            `Box` with shape `(n, *self.shape)` and unchanged bounds/dtype.
        """
        return Box(
            low=self.low,
            high=self.high,
            shape=(n, *self.shape),
            dtype=self.dtype,
        )

sample(rng)

Sample a random observation within [low, high].

Parameters:

Name Type Description Default
rng Array

JAX PRNG key.

required

Returns:

Name Type Description
obs Array

dtype[*shape] — Sampled observation array.

Source code in envrax/spaces.py
Python
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
def sample(self, rng: chex.Array) -> chex.Array:
    """
    Sample a random observation within `[low, high]`.

    Parameters
    ----------
    rng : chex.Array
        JAX PRNG key.

    Returns
    -------
    obs : chex.Array
        `dtype[*shape]` — Sampled observation array.
    """
    if jnp.issubdtype(self.dtype, jnp.integer):
        return jax.random.randint(
            rng,
            shape=self.shape,
            minval=int(self.low),
            maxval=int(self.high) + 1,
            dtype=self.dtype,
        )

    return jax.random.uniform(
        rng,
        shape=self.shape,
        minval=self.low,
        maxval=self.high,
    ).astype(self.dtype)

contains(x)

Return True if x is a valid observation within the space.

Parameters:

Name Type Description Default
x Array

Observation to validate. Expected to match self.shape.

required

Returns:

Name Type Description
valid bool

True if x.shape == self.shape and every element lies in [low, high].

Source code in envrax/spaces.py
Python
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
def contains(self, x: chex.Array) -> bool:
    """
    Return True if `x` is a valid observation within the space.

    Parameters
    ----------
    x : chex.Array
        Observation to validate. Expected to match `self.shape`.

    Returns
    -------
    valid : bool
        True if `x.shape == self.shape` and every element lies in `[low, high]`.
    """
    return bool(
        (x.shape == self.shape) & jnp.all(x >= self.low) & jnp.all(x <= self.high)
    )

batch(n)

Prepend a leading n dimension to the shape.

Parameters:

Name Type Description Default
n int

Batch size.

required

Returns:

Name Type Description
batched Box

Box with shape (n, *self.shape) and unchanged bounds/dtype.

Source code in envrax/spaces.py
Python
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
def batch(self, n: int) -> "Box":
    """
    Prepend a leading `n` dimension to the shape.

    Parameters
    ----------
    n : int
        Batch size.

    Returns
    -------
    batched : Box
        `Box` with shape `(n, *self.shape)` and unchanged bounds/dtype.
    """
    return Box(
        low=self.low,
        high=self.high,
        shape=(n, *self.shape),
        dtype=self.dtype,
    )

envrax.spaces.MultiDiscrete dataclass

Bases: Space

A vector of independent discrete actions, each with its own number of options.

Parameters:

Name Type Description Default
nvec Tuple[int, ...]

Number of actions for each discrete sub-space.

required
dtype Type

Element dtype. Defaults to jnp.int32.

required
Source code in envrax/spaces.py
Python
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
@dataclass(frozen=True)
class MultiDiscrete(Space):
    """
    A vector of independent discrete actions,
    each with its own number of options.

    Parameters
    ----------
    nvec : Tuple[int, ...]
        Number of actions for each discrete sub-space.
    dtype : Type
        Element dtype. Defaults to `jnp.int32`.
    """

    nvec: Tuple[int, ...]
    dtype: Type = jnp.int32

    def sample(self, rng: chex.Array) -> chex.Array:
        """
        Sample one action per sub-space.

        Parameters
        ----------
        rng : chex.Array
            JAX PRNG key.

        Returns
        -------
        actions : chex.Array
            `int32[len(nvec)]` — One sampled action per sub-space.
        """
        nvec_arr = jnp.array(self.nvec, dtype=self.dtype)
        return jax.random.randint(
            rng,
            shape=(len(self.nvec),),
            minval=0,
            maxval=nvec_arr,
            dtype=self.dtype,
        )

    def contains(self, x: chex.Array) -> bool:
        """
        Return True if `x` is a valid multi-discrete action vector.

        Parameters
        ----------
        x : chex.Array
            Action vector to validate. Expected to have shape `(len(nvec),)`.

        Returns
        -------
        valid : bool
            True if `x` has shape `(len(nvec),)` and each `x[i]` is in `[0, nvec[i])`.
        """
        if x.shape != (len(self.nvec),):
            return False

        nvec_arr = jnp.array(self.nvec, dtype=self.dtype)
        return bool(jnp.all(x >= 0) & jnp.all(x < nvec_arr))

    def batch(self, n: int) -> "MultiDiscrete":
        """
        Repeat `nvec` `n` times to form a wider `MultiDiscrete`.

        Parameters
        ----------
        n : int
            Batch size.

        Returns
        -------
        batched : MultiDiscrete
            `MultiDiscrete(nvec=self.nvec * n, dtype=self.dtype)`.
        """
        return MultiDiscrete(
            nvec=self.nvec * n,
            dtype=self.dtype,
        )

sample(rng)

Sample one action per sub-space.

Parameters:

Name Type Description Default
rng Array

JAX PRNG key.

required

Returns:

Name Type Description
actions Array

int32[len(nvec)] — One sampled action per sub-space.

Source code in envrax/spaces.py
Python
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
def sample(self, rng: chex.Array) -> chex.Array:
    """
    Sample one action per sub-space.

    Parameters
    ----------
    rng : chex.Array
        JAX PRNG key.

    Returns
    -------
    actions : chex.Array
        `int32[len(nvec)]` — One sampled action per sub-space.
    """
    nvec_arr = jnp.array(self.nvec, dtype=self.dtype)
    return jax.random.randint(
        rng,
        shape=(len(self.nvec),),
        minval=0,
        maxval=nvec_arr,
        dtype=self.dtype,
    )

contains(x)

Return True if x is a valid multi-discrete action vector.

Parameters:

Name Type Description Default
x Array

Action vector to validate. Expected to have shape (len(nvec),).

required

Returns:

Name Type Description
valid bool

True if x has shape (len(nvec),) and each x[i] is in [0, nvec[i]).

Source code in envrax/spaces.py
Python
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
def contains(self, x: chex.Array) -> bool:
    """
    Return True if `x` is a valid multi-discrete action vector.

    Parameters
    ----------
    x : chex.Array
        Action vector to validate. Expected to have shape `(len(nvec),)`.

    Returns
    -------
    valid : bool
        True if `x` has shape `(len(nvec),)` and each `x[i]` is in `[0, nvec[i])`.
    """
    if x.shape != (len(self.nvec),):
        return False

    nvec_arr = jnp.array(self.nvec, dtype=self.dtype)
    return bool(jnp.all(x >= 0) & jnp.all(x < nvec_arr))

batch(n)

Repeat nvec n times to form a wider MultiDiscrete.

Parameters:

Name Type Description Default
n int

Batch size.

required

Returns:

Name Type Description
batched MultiDiscrete

MultiDiscrete(nvec=self.nvec * n, dtype=self.dtype).

Source code in envrax/spaces.py
Python
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
def batch(self, n: int) -> "MultiDiscrete":
    """
    Repeat `nvec` `n` times to form a wider `MultiDiscrete`.

    Parameters
    ----------
    n : int
        Batch size.

    Returns
    -------
    batched : MultiDiscrete
        `MultiDiscrete(nvec=self.nvec * n, dtype=self.dtype)`.
    """
    return MultiDiscrete(
        nvec=self.nvec * n,
        dtype=self.dtype,
    )