Restrictions#

class drama.restrictions.Restriction(base_space: Space, *, seed: int | Generator | None = None)[source]#

Base class for restrictions. All restrictions are valid gymnasium.spaces.Space’s.

class drama.restrictions.DiscreteRestriction(base_space: Discrete, *, seed: int | Generator | None = None)[source]#

Representation of a gymnasium.spaces.Discrete restriction.

class drama.restrictions.ContinuousRestriction(base_space: Box, *, seed: int | Generator | None = None)[source]#

Representation of a gymnasium.spaces.Box restriction.

class drama.restrictions.DiscreteSetRestriction(base_space: Discrete, *, allowed_actions: Set[int] | None = None, seed: int | Generator | None = None)[source]#

Representation of a gymnasium.spaces.Discrete restriction as a set of allowed actions.

add(x: int) None[source]#

Adds a discrete action to the set of allowed actions.

Parameters:

x – The discrete action

contains(x: int) bool[source]#

Check if a discrete action is allowed.

Parameters:

x – The discrete action

Returns:

True if the action is allowed. False otherwise

property is_np_flattenable: bool#

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

Returns:

True

remove(x: int) None[source]#

Removes a discrete action from the set of allowed actions.

Parameters:

x – The discrete action

sample(mask: Any | None = None) int[source]#

Randomly sample an action from the allowed set.

Parameters:

mask – The mask used for sampling (currently no effect)

Returns:

Valid discrete action

class drama.restrictions.DiscreteVectorRestriction(base_space: Discrete, *, allowed_actions: ndarray[bool] | None = None, seed: int | Generator | None = None)[source]#

Representation of a gymnasium.spaces.Discrete restriction as a boolean vector of allowed and forbidden actions.

contains(x: int) bool[source]#

Check if a discrete action is allowed.

Parameters:

x – The discrete action

Returns:

True if the action is allowed. False otherwise

property is_np_flattenable: bool#

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

Returns:

True

sample(mask: Any | None = None) int[source]#

Randomly sample an action from the allowed set.

Parameters:

mask – The mask used for sampling (currently no effect)

Returns:

Valid discrete action

class drama.restrictions.IntervalUnionRestriction(base_space: Box)[source]#

Representation of a one-dimensional gymnasium.spaces.Box restriction as an AVL tree of allowed intervals.

add(x, y, root: Node = 'root')[source]#

Add an interval to the action space

Parameters:
  • x – Lower bound of the interval

  • y – Upper bound of the interval

  • rootNode to start the insertion from or ‘root’ for inserting over the whole tree, default is ‘root’

Returns:

Updated root Node of the action space

contains(x: array | float, root: object = 'root')[source]#

Check if a continuous action is allowed.

Parameters:
  • x – The continuous action

  • rootNode to start the search from or ‘root’ for searching the whole tree, default is ‘root’

Returns:

True if the action is allowed. False otherwise

draw = None#
first_interval_after_or_within(x, root: Node = 'root')[source]#

The last interval after or within a continuous action

Parameters:
  • x – The continuous action

  • rootNode to start the search from or ‘root’ for searching the whole tree, default is ‘root’

Returns:

Tuple containing the lower and upper boundaries of the interval and a variable indicating if the number lies in the interval.

For example: (root.x, root.y), True

getBal(root: Node = 'root')[source]#

Calculate the balance factor

Parameters:

rootNode to calculate the balance factor for or ‘root’ for the balance factor of the whole tree, default is ‘root’

Returns:

The balance factor

getHeight(root: Node = 'root')[source]#

Returns the height of a Node

Parameters:

rootNode to return the height from or ‘root’ for the height of the whole tree, default is ‘root’

Returns:

The height of the node in the tree

intervals()[source]#

Return all intervals of the allowed action space in an ordered way.

Returns:

List of tuples containing the ordered intervals.

For example: [(0.1,0.5), (0.7,0.9)]

property is_np_flattenable: bool#

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

Returns:

True

lRotate(z: Node)[source]#

Performs a left rotation. Switches roles of parent and child nodes.

Parameters:

z – Parent Node for the rotation

Returns:

Updated parent Node

last_interval_before_or_within(x, root: Node = 'root')[source]#

The last interval before or within a continuous action

Parameters:
  • x – The continuous action

  • rootNode to start the search from or ‘root’ for searching the whole tree, default is ‘root’

Returns:

Tuple containing the lower and upper boundaries of the interval and a variable indicating if the number lies in the interval.

For example: (root.x, root.y), True

nearest_element(x, root: Node = 'root')[source]#

Finds the minimum closest allowed action for a continuous action.

Parameters:
  • x – The continuous action

  • rootNode to start the search from or ‘root’ for searching the whole tree, default is ‘root’

Returns:

Nearest element in the allowed action space. Returns x if it is valid.

nearest_elements(x, root: Node = 'root')[source]#

Finds the closest allowed actions for a continuous action.

Parameters:
  • x – The continuous action

  • rootNode to start the search from or ‘root’ for searching the whole tree, default is ‘root’

Returns:

Nearest elements in the action space. Returns x if it is valid.

rRotate(z: Node)[source]#

Performs a right rotation. Switches roles of parent and child nodes.

Parameters:

z – Parent Node for the rotation

Returns:

Updated parent Node

remove(x, y, root: Node = 'root', adjust_size: bool = True)[source]#

Removes an interval from the action space

Parameters:
  • x – Lower bound of the interval

  • y – Upper bound of the interval

  • rootNode to start the removal from or ‘root’ for removing over the whole tree, default is ‘root’

  • adjust_size – Whether the size attribute of the tree should be modified

Returns:

Updated root Node of the action space

root_tree = None#
sample(root: Node = 'root') ndarray[source]#

Randomly sample a continuous action from a uniform distribution over the allowed action space

Parameters:

rootNode node of the action space, default is ‘root’

Returns:

Sampled continuous action

size: Decimal = 0#
smallest_interval(root: Node = 'root')[source]#

Return the Node of the smallest interval

Parameters:

rootNode to start the search from or ‘root’ for searching the whole tree, default is ‘root’

Returns:

Node of the smallest interval

class drama.restrictions.BucketSpaceRestriction(base_space: Box, bucket_width=1.0, epsilon=0.01, available_buckets: ndarray = None)[source]#

Representation of a one-dimensional gymnasium.spaces.Box restriction as a binary vector indicating the availability of equally sized buckets.

add(x, with_epsilon=True)[source]#

Add buckets containing a specific value to the allowed action space

Parameters:
  • x – Buckets containing this value should be added to the allowed action space

  • with_epsilon – If True, a subset of epsilon around x is added. Otherwise, only buckets containing the specific value are added.

clone()[source]#

Returns a copy of the BucketSpaceRestriction

Returns:

BucketSpaceRestriction copy

clone_and_remove(x)[source]#

Returns a copy of the BucketSpaceRestriction without buckets containing a specific value

Parameters:

x – Buckets containing this value should be removed from the allowed action space

Returns:

BucketSpaceRestriction copy

contains(x)[source]#

Check if a continuous action is allowed.

Parameters:

x – The continuous action

Returns:

True if the action is allowed. False otherwise

property intervals#

Return all intervals of the allowed action space in an ordered way.

Returns:

List of tuples containing the ordered intervals.

For example: [(0.1,0.5), (0.7,0.9)]

property is_np_flattenable: bool#

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

Returns:

True

remove(x, with_epsilon=True)[source]#

Remove buckets containing a specific value from the allowed action space

Parameters:
  • x – Buckets containing this value should be removed from the allowed action space

  • with_epsilon – If True, a subset of epsilon around x is removed. Otherwise, only buckets containing the specific value are removed.

reset()[source]#

Resets the action space to the unrestricted state

sample(mask: None = None)[source]#

Randomly sample a continuous action from a uniform distribution over the allowed action space

Parameters:

mask – The mask used for sampling (currently no effect)

Returns:

Sampled continuous action

class drama.restrictions.PredicateRestriction(base_space: Space, *, predicate: Callable[[Any], bool] | None = None, seed: int | Generator | None = None)[source]#

Representation of an arbitrary space as the set of elements for which a predicate is True.

contains(x: Any) bool[source]#

Check if an action is allowed and the predicate is True.

Parameters:

x – The action

Returns:

True if the action is allowed. False otherwise

property is_np_flattenable: bool#

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

Returns:

False

sample(mask: Any | None = None) int[source]#

Randomly sample a set of elements for which the predicate is True

Parameters:

mask – The mask used for sampling (currently no effect)

Returns:

Sampled set of elements

class drama.restrictions.Node(x: float = None, y: float = None, left: object = None, right: object = None, height: int = 1)[source]#

Node in the AVL tree of intervals. A single instance of Node represents an allowed interval.