Skip to content

Spaces

Space descriptors for an environment's observation, action and reward arrays - shape, dtype and element-wise bounds, plus a sample that draws a conforming array. Discrete for integers, Continuous for floats.

An array shape, i.e. a tuple of ints (() for a scalar).

Bases: Space

A floating-point space: every element lies in [minimum, maximum].

navix uses it for reward_space (shape=(), bounds [-1, 1] by default) and for float observations. Bounds may be infinite (-jnp.inf / jnp.inf) to express "unbounded"; sample then falls back to a finite range (see below).

Builds a Continuous space.

Parameters:

Name Type Description Default
shape tuple[int, ...]

shape of the array the space describes. () is a scalar (e.g. a reward).

required
minimum Array

element-wise lower bound (inclusive); a scalar broadcasts to shape. May be -jnp.inf.

required
maximum Array

element-wise upper bound (inclusive); a scalar broadcasts to shape. May be jnp.inf.

required
dtype

floating dtype of the sampled array (default jnp.float32).

float32

Returns:

Name Type Description
Continuous Continuous

the space.

Draws values uniformly from [minimum, maximum), independently per element. Infinite bounds are first mapped to the largest finite value of dtype (via jnp.nan_to_num), so an unbounded space still yields a finite draw rather than nan.

Parameters:

Name Type Description Default
key Array

a jax.random PRNG key.

required

Returns:

Name Type Description
Array Array

shape shape, dtype dtype.

Raises:

Type Description
AssertionError

if dtype is not a floating type.

Bases: Space

An integer-valued space: every element is one of the n_elements integers 0, 1, ..., n_elements - 1.

With shape=() it describes a single categorical value - the usual case for an action index (Environment.action_space). With a non-empty shape it describes an array of independent categoricals, e.g. a categorical observation is Discrete over entity tags with shape=(H, W).

The number of distinct values, n_elements (i.e. maximum + 1). For an action space, len(env.action_set).

Builds a Discrete space over 0 .. n_elements - 1.

Parameters:

Name Type Description Default
n_elements int | Array

number of distinct values; must be >= 1. Stored as maximum = n_elements - 1 (with minimum = 0), so space.n recovers it.

required
shape tuple[int, ...]

shape of the integer array the space describes. () (the default) is a single scalar.

()
dtype

integer dtype of the sampled array (default jnp.int32). Unsigned dtypes are allowed - sample draws with a signed generator and casts.

int32

Returns:

Name Type Description
Discrete Discrete

the space, with minimum = 0 and

Discrete

maximum = n_elements - 1.

Draws integers uniformly from 0 .. n_elements - 1, independently per element.

Parameters:

Name Type Description Default
key Array

a jax.random PRNG key.

required

Returns:

Name Type Description
Array Array

shape shape, dtype dtype.

Bases: PyTreeNode

Describes one array that flows through an environment: its shape, its dtype, and its element-wise value bounds.

An Environment exposes three of these - observation_space, action_space and reward_space - so a caller knows what reset/step will return and what step expects, without having to run the environment. sample draws a random array that conforms to the space, which is useful for smoke tests and for shaping neural-network inputs/outputs.

Use the create classmethod of a concrete subclass (Discrete or Continuous) to build one; the bare constructor does no validation or bound broadcasting.

Attributes:

Name Type Description
shape Shape

the shape of the array the space describes. () means a single scalar (e.g. a discrete action index or a scalar reward); (H, W) / (H, W, 3) etc. describe a grid or image observation, with every element independently constrained to [minimum, maximum].

dtype dtype

the array's dtype (e.g. jnp.int32 for a Discrete action, jnp.uint8 for a pixel observation, jnp.float32 for a reward).

minimum Array

element-wise lower bound (inclusive). A scalar array broadcasts to shape.

maximum Array

element-wise upper bound (inclusive). A scalar array broadcasts to shape.

Draws one array of shape shape and dtype dtype whose elements lie within [minimum, maximum].

Parameters:

Name Type Description Default
key Array

a jax.random PRNG key.

required

Returns:

Name Type Description
Array Array

the sampled array, shape shape, dtype dtype.

Raises:

Type Description
NotImplementedError

Space is abstract; call sample on a Discrete or Continuous instance.