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.Discreterestriction.
- class drama.restrictions.ContinuousRestriction(base_space: Box, *, seed: int | Generator | None = None)[source]#
Representation of a
gymnasium.spaces.Boxrestriction.
- class drama.restrictions.DiscreteSetRestriction(base_space: Discrete, *, allowed_actions: Set[int] | None = None, seed: int | Generator | None = None)[source]#
Representation of a
gymnasium.spaces.Discreterestriction 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
- class drama.restrictions.DiscreteVectorRestriction(base_space: Discrete, *, allowed_actions: ndarray[bool] | None = None, seed: int | Generator | None = None)[source]#
Representation of a
gymnasium.spaces.Discreterestriction 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
- class drama.restrictions.IntervalUnionRestriction(base_space: Box)[source]#
Representation of a one-dimensional
gymnasium.spaces.Boxrestriction as an AVL tree of allowed intervals.- contains(x: array | float, root: object = 'root')[source]#
Check if a continuous action is allowed.
- Parameters:
x – The continuous action
root –
Nodeto 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
root –
Nodeto 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:
root –
Nodeto 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:
root –
Nodeto 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
- last_interval_before_or_within(x, root: Node = 'root')[source]#
The last interval before or within a continuous action
- Parameters:
x – The continuous action
root –
Nodeto 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
root –
Nodeto 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
root –
Nodeto 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.
- remove(x, y, root: Node = 'root', adjust_size: bool = True)[source]#
Removes an interval from 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:
root –
Nodenode of the action space, default is ‘root’- Returns:
Sampled continuous action
- size: Decimal = 0#
- 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.Boxrestriction 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:
- clone_and_remove(x)[source]#
Returns a copy of the
BucketSpaceRestrictionwithout buckets containing a specific value- Parameters:
x – Buckets containing this value should be removed from the allowed action space
- Returns:
- 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.
- 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