commonpower.control.controllers.BaseController

class BaseController(name: str)[source]

Bases: object

This is the base class for any controller type that will be implemented. It manages assignment of controllable entities to the controller and automatically deduces the action space from the bounds of the elements within these entities. The most important functionality of the controller is to compute the control input, a function that has to be implemented by the subclasses.

Parameters:

name (str) – name of the controller

Returns:

BaseController

Methods

act_array_to_dict

Converts numpy array of actions to dictionary.

add_entity

Add a controllable entity to the controller.

add_system

When 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_bounds

Clips the control inputs to their bounds to avoid numerical errors.

compute_control_input

Has to be implemented by subclasses.

detach

Remove controller from all controlled entities

filter_history_for_time_period

Filters all element histories for a given time period

flatten_obs

Converts observation dictionary to a numpy array.

get_cost

Compute control cost for one time step

get_id

Get ID of controller.

get_input_space

Derives action space of the controller from the list of its controlled entities.

get_nodes

Get controlled nodes.

get_top_level_nodes

Retrieve the controlled entities at the highest level in the tree.

initialize

Initial set-up of controller.

reset_history

Has to be implemented by subclasses.

Attributes

obs_mask

_denormalize_input(action: OrderedDict) OrderedDict[source]

Denormalize action to original input space :param action: normalized action

Returns:

_index_entities()[source]

Called during init to record the all entity ids. We can do this only now because entity ids are assigned when they are added to the pyomo model.

act_array_to_dict(action: ndarray) OrderedDict[source]

Converts numpy array of actions to dictionary.

Parameters:

action (np.ndarray) – numpy array of actions

Returns:

OrderedDict – dictionary of input element IDs and action value for all controlled entities

add_entity(entity: ControllableModelEntity)[source]

Add a controllable entity to the controller. Recursively searches the component tree of the entity and adds all individual controllable entities from that tree to ‘nodes’.

Parameters:

entity – controllable entity to be added

Returns:

BaseController – The current controller

add_system(system: System) BaseController[source]

When 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’.

Parameters:

system (System) – system to be added.

Returns:

BaseController – The current controller.

clip_to_bounds(control_input: dict) dict[source]

Clips the control inputs to their bounds to avoid numerical errors.

Parameters:

control_input (dict) – dictionary of {control input ID: value of control input}

Returns:

dict – dictionary of clipped control inputs

compute_control_input(obs: OrderedDict | None = None, input_callback: Callable | None = None) Tuple[OrderedDict, float][source]

Has to be implemented by subclasses.

Parameters:
  • obs (OrderedDict) – observation at current time point

  • input_callback (Callable) – only needed in training mode - retrieves action selected within training algorithm

Returns:

Tuple

tuple containing
  • action (OrderedDict)

  • penalty for action adjustment performed by safety layer (float).

detach()[source]

Remove controller from all controlled entities

Returns:

None

filter_history_for_time_period(start: str | Timestamp, end: str | Timestamp) dict[source]

Filters all element histories for a given time period

Parameters:
  • start (Union[str, pd.Timestamp]) – beginning of the time period.

  • str (If) – 00:00”.

  • 00 (should be in format "2016-09-04) – 00:00”.

  • end (Union[str, pd.Timestamp]) – end of the time period. If str, should be in format “2016-09-04 00:00:00”.

Returns:

(dict) – the filtered history.

flatten_obs(obs: dict) ndarray[source]

Converts observation dictionary to a numpy array.

Parameters:

obs (dict) – dictionary of observed element IDs and their values

Returns:

np.ndarray – numpy array of all the observations

get_cost(sys_inst: ConcreteModel) float[source]

Compute control cost for one time step

Parameters:

sys_inst (ConcreteModel) – current Pyomo model with solution from optimization

Returns:

float – control cost for one time step

get_id() str[source]

Get ID of controller.

Returns:

str – controller name

get_input_space(normalize: bool = False) Dict[source]

Derives action space of the controller from the list of its controlled entities.

Parameters:

normalize (bool) – whether or not to normalize the action space

Returns:

gym.spaces.Dict – action space of each entity that has INPUT model elements

get_nodes() List[ControllableModelEntity][source]

Get controlled nodes.

Returns:

List[ControllableModelEntity] – all entities under control

get_top_level_nodes() List[ControllableModelEntity][source]

Retrieve the controlled entities at the highest level in the tree.

Returns:

List[ControllableModelEntity] – Highest-level entities under control.

initialize()[source]

Initial set-up of controller.

reset_history() None[source]

Has to be implemented by subclasses.

Returns:

None