commonpower.control.controllers.RLControllerSB3
- class RLControllerSB3(name: str, obs_handler: ~commonpower.control.observation_handling.ObservationHandler | None = None, train: bool = True, device: str = 'cpu', safety_layer=None, cost_callback: ~typing.Callable = <function single_step_cost_callback>, pretrained_policy_path: str | None = None)[source]
Bases:
RLBaseControllerController class for RL agents trained with algorithms from the StableBaselines repository (https://stable-baselines3.readthedocs.io/). Single-agent RL algorithms only!
Base class for reinforcement learning (RL) controllers. Requires a safety layer to ensure constraint satisfaction. For the RL controller, there are two different modes: training and deployment. In training mode, the action is obtained through a callback from the Gym environment. During deployment, the action is computed by propagating the observation through the trained neural network policy. Saving and loading this policy and computing the action in deployment mode depend on the RL algorithm and therefore have to be implemented in the respective subclasses.
- Parameters:
name (str) – name of the controller
obs_handler (ObservationHandler) – entity that takes care of processing observations for RL controllers.
train (bool) – whether the controller is in training mode
device (str) – whether to use ‘cpu’ or ‘cuda’ (GPU)
safety_layer (BaseSafetyLayer) – safety layer instance
cost_callback (Callable) – function used within the cost function of the controller to compute additional cost terms
pretrained_policy_path (str) – directory with stored policy parameters of an existing policy
- Returns:
RLBaseController
Methods
act_array_to_dictConverts numpy array of actions to dictionary.
add_entityAdd a controllable entity to the controller.
add_systemWhen adding a system to a controller, the system tree is searched recursively and all controllable entities that do not yet have a controller are added to 'nodes'.
clip_to_boundsClips the control inputs to their bounds to avoid numerical errors.
compute_control_inputIn training mode, the control input is computed within the training algorithm and passed to this controller through a callback.
detachRemove controller from all controlled entities
filter_history_for_time_periodFilters all element histories for a given time period
flatten_obsConverts observation dictionary to a numpy array.
get_costCompute control cost for one time step
get_idGet ID of controller.
get_input_spaceDerives action space of the controller from the list of its controlled entities.
get_nodesGet controlled nodes.
get_top_level_nodesRetrieve the controlled entities at the highest level in the tree.
initializeInitial set-up of controller and safety layer
Loading a pre-trained policy from a directory.
Compute the control action based on a given observation by propagating this observation through the policy network.
reset_historyDelete history
Save neural network policy parameters and structure.
set_modeSet mode to training (True) or deployment.
set_normalize_inputsupdate_historyInsert new data into training history.
Attributes
obs_mask- load(env, config: dict, policy_kwargs: dict | None = None)[source]
Loading a pre-trained policy from a directory.
- Parameters:
env (ControlEnv) – The gym environment constructed from the power system the RL algorithm interacts with. Required to construct the neural network policy because it determines the number of inputs (observations) and outputs (actions) of the network.
config (dict) – Configuration for the StableBaselines policy class (also constructs training buffers etc., which is why this also contains algorithm parameters).
policy_kwargs (dict) – Configuration of the actual neural networks of the policy (e.g., number of neurons in the hidden layers of the actor and critic network of an ActorCriticPolicy). Depends on policy type. Consult the StableBaselines documentation (https://stable-baselines3.readthedocs.io/en/master/) for more information.
- Returns:
None
- predict_action(obs: ndarray, deterministic: bool = True) ndarray[source]
Compute the control action based on a given observation by propagating this observation through the policy network.
- Parameters:
obs (np.ndarray) – observation at current time step (has to be numpy array, not dictionary, since a dictionary cannot be processed by the neural network.)
deterministic (bool) – Whether to use a deterministic action selection algorithm
- Returns:
np.ndarray – control action