rlberry.spaces.Discrete

class rlberry.spaces.Discrete(n)[source]

Bases: Discrete

Class that represents discrete spaces.

Inherited from gymnasium.spaces.Discrete for compatibility with gym.

rlberry wraps gym.spaces to make sure the seeding mechanism is unified in the library (rlberry.seeding)

Attributes:
rngnumpy.random._generator.Generator

random number generator provided by rlberry.seeding

Methods

reseed()

get new random number generator

contains(x: Any) bool[source]

Return boolean specifying if x is a valid member of this space.

from_jsonable(sample_n: list[int]) list[numpy.int64][source]

Converts a list of json samples to a list of np.int64.

property is_np_flattenable

Checks whether this space can be flattened to a spaces.Box.

property np_random: Generator

Lazily seed the PRNG since this is expensive and only needed if sampling from this space.

As seed() is not guaranteed to set the _np_random for particular seeds. We add a check after seed() to set a new random number generator.

reseed(seed_seq=None)[source]

Get new random number generator.

Parameters:
seed_seqnp.random.SeedSequence, rlberry.seeding.Seeder or int, defaultNone

Seed sequence from which to spawn the random number generator. If None, generate random seed. If int, use as entropy for SeedSequence. If seeder, use seeder.seed_seq

sample()[source]

Generates a single random sample from this space.

A sample will be chosen uniformly at random with the mask if provided

Args:
mask: An optional mask for if an action can be selected.

Expected np.ndarray of shape (n,) and dtype np.int8 where 1 represents valid actions and 0 invalid / infeasible actions. If there are no possible actions (i.e. np.all(mask == 0)) then space.start will be returned.

Returns:

A sampled integer from the space

seed(seed: int | None = None) list[int]

Seed the PRNG of this space and possibly the PRNGs of subspaces.

property shape: tuple[int, ...] | None

Return the shape of the space as an immutable property.

to_jsonable(sample_n: Sequence[int64]) list[int][source]

Converts a list of samples to a list of ints.

Examples using rlberry.spaces.Discrete

A demo of Bandit BAI on a real dataset to select mirrors

A demo of Bandit BAI on a real dataset to select mirrors