commonpower.control.safety_layer.safety_layers.ActionReplacementWithOptSafetyLayer

class ActionReplacementWithOptSafetyLayer(penalty: BasePenalty, solver: OptSolver | None = None)[source]

Bases: BaseSafetyLayer

Action replacement safety layer. Action violating the constraints is replaced by safe action determined through an optimization method.

Parameters:
Returns:

ActionReplacementWithOptSafetyLayer

Methods

action_distance

Euclidean norm between action expressed by the model and the action in dict

compute_safe_action

Checks whether the actions proposed by the controller satisfy the constraints of the controlled entities and replaces by pyomo-generated output if necessary.

get_penalty

Get penalty depending on the penalty class used.

initialize

Initializes the safety layer :param nodes: list of controlled entities to be safeguarded :type nodes: List[ModelEntity] :param top_level_nodes: list of controlled entities in highest level of model tree :type top_level_nodes: List[ModelEntity] :param solver: solver for optimization problem which will be called by Pyomo :type solver: OptSolver

is_action_feasible

Check if action is feasible.

prepare_model

Clone model from sys to run local optimization over

set_action_from_model

Corrects an action according to a model.

set_action_in_model

Sets the model values to the action to initialize the optimization.

solve_model

Finds a feasible action by solving a local model.

Attributes

DISTANCE_EPS

action_distance(model: ConcreteModel, action: dict) SumExpression[source]

Euclidean norm between action expressed by the model and the action in dict

Parameters:
  • model (ConcreteModel) – pyomo optimization model

  • action (dict) – action description

Returns:

SumExpression

distance between the action and the model values expressed

in pyomo class, convert to float by running value() over it

compute_safe_action(action: Dict | None = None) Tuple[Dict, bool, float][source]

Checks whether the actions proposed by the controller satisfy the constraints of the controlled entities and replaces by pyomo-generated output if necessary.

Parameters:

action (dict) – action suggested by the controller

Returns:

safe_action (dict) – verified action action_corrected (bool): whether the action was corrected or not correction_penalty (float): penalty for action correction (0 if action was not corrected)

get_penalty(action_distance: float) float[source]

Get penalty depending on the penalty class used.

Parameters:

action_distance (float) – distance between the safe and unsafe action, only used if penalty is distance based

Returns:

float – computed penalty for the action

is_action_feasible(action: dict) dict[source]

Check if action is feasible.

Parameters:

action (dict) – action to check

Returns:

bool – True if action is feasible

prepare_model() ConcreteModel[source]

Clone model from sys to run local optimization over

Returns:

ConcreteModel

pyomo optimization model, representing

the part of the system under supervision of the safety

set_action_from_model(model: ConcreteModel, action: dict) dict[source]

Corrects an action according to a model.

Parameters:
  • model (ConcreteModel) – pyomo model to ge the values from

  • action (dict) – action to correct

Returns:

dict – safe action according to the model

set_action_in_model(model: ConcreteModel, action: dict, fix_values: bool = False) ConcreteModel[source]

Sets the model values to the action to initialize the optimization. If fix values is True, the model values will be fixed, which is useful to check the feasibility of the given action.

Parameters:
  • model (ConcreteModel) – pyomo optimization model

  • action (dict) – action to set into the model

  • fix_values (bool) – optionally fixes the values for the optimization problem

Returns:

ConcreteModel – model with values set

solve_model(model: ConcreteModel) float[source]

Finds a feasible action by solving a local model. Returns the distance of the original unsafe action to the solved model.

Parameters:

model (ConcreteModel) – model to solve

Returns:

float – distance from action or None