Module ai.simulators.action_spaces
Action spaces for simulators.
Expand source code
"""Action spaces for simulators."""
from ._base import Base
from ._discrete import Discrete
__all__ = ["Base", "Discrete"]
Classes
class Base
-
Base simulator action space.
Ancestors
- abc.ABC
Subclasses
Instance variables
var as_discrete : Discrete
-
This object cast to a discrete action space. Equivalent to calling
cast_to(Discrete)
.Expand source code
@property def as_discrete(self) -> "action_spaces.Discrete": """This object cast to a discrete action space. Equivalent to calling `cast_to(Discrete)`.""" return self.cast_to(action_spaces.Discrete)
Methods
def cast_to(self, t: ~T) ‑> ~T
-
Casts the action space to the specific type.
Args
t
:T
- Type to which the space should be cast
Raises
RuntimeError
- If the object is not castable to the requested type.
Returns
T
- Casted version of the object.
Expand source code
def cast_to(self, t: T) -> T: """Casts the action space to the specific type. Args: t (T): Type to which the space should be cast Raises: RuntimeError: If the object is not castable to the requested type. Returns: T: Casted version of the object. """ if not isinstance(self, t): raise RuntimeError(f"Failed casting {self} to {t}") return self
def contains(self, state: numpy.ndarray, action: Any) ‑> bool
-
Determines if the given action is in the action space or not.
Args
state
:np.ndarray
- State.
action
:Any
- Action.
Returns
bool
- True if the given action is legal, otherwise False.
Expand source code
@abstractmethod def contains(self, state: np.ndarray, action: Any) -> bool: """Determines if the given action is in the action space or not. Args: state (np.ndarray): State. action (Any): Action. Returns: bool: True if the given action is legal, otherwise False. """ raise NotImplementedError
def sample(self, state: numpy.ndarray) ‑> Any
-
Samples an action from the action space.
Args
state
:np.ndarray
- State
Returns
Any
- An action.
Expand source code
@abstractmethod def sample(self, state: np.ndarray) -> Any: """Samples an action from the action space. Args: state (np.ndarray): State Returns: Any: An action. """ raise NotImplementedError
class Discrete
-
Discrete action space.
Discrete action spaces identify actions using the an integer and have a fixed size. Moreover, all action are not necessarily legal in every state. Legal actions are given by the action mask, a boolean vector whose elements at legal action indices are
True
and illegal action indicesFalse
.Ancestors
- Base
- abc.ABC
Subclasses
- ai.simulators._cart_pole.CartPole.ActionSpace
- ai.simulators._connect_four.ConnectFour.ActionSpace
- ai.simulators._grid.ActionSpace
- ai.simulators._tictactoe.TicTacToe.ActionSpace
Instance variables
var size : int
-
The size of the discrete action space.
Expand source code
@property @abstractmethod def size(self) -> int: """The size of the discrete action space.""" raise NotImplementedError
Methods
def action_mask(self, state: numpy.ndarray) ‑> numpy.ndarray
-
Computes the action mask, indicating legal actions in the given state.
Args
state
:np.ndarray
- State.
Returns
np.ndarray
- Action mask
Expand source code
def action_mask(self, state: np.ndarray) -> np.ndarray: """Computes the action mask, indicating legal actions in the given state. Args: state (np.ndarray): State. Returns: np.ndarray: Action mask """ return self.action_mask_bulk(np.expand_dims(state, 0))[0]
def action_mask_bulk(self, states: numpy.ndarray) ‑> numpy.ndarray
-
Computes the action masks for the given states.
Args
states
:np.ndarray
- States, where the first dimension is the batch
dimension.
Returns
np.ndarray
- Action masks.
Expand source code
@abstractmethod def action_mask_bulk(self, states: np.ndarray) -> np.ndarray: """Computes the action masks for the given states. Args: states (np.ndarray): States, where the first dimension is the batch dimension. Returns: np.ndarray: Action masks. """ raise NotImplementedError
Inherited members