Module ml4opf.formulations.dc.violation
Class interface for DCOPF constraints, objective, etc.
Classes
class DCViolation (data: dict[str, torch.Tensor])
-
OPFViolation
for DCPPowerModel/DCOPFInitialize internal Module state, shared by both nn.Module and ScriptModule.
Ancestors
- OPFViolation
- torch.nn.modules.module.Module
- abc.ABC
Methods
def angle_difference(self, va: torch.Tensor) ‑> torch.Tensor
-
Compute the angle differences per branch given the voltage angles per bus.
The branch indices are assumed to be constant for the batch, matching the reference case.
\text{dva} = \boldsymbol{\theta}_{f} - \boldsymbol{\theta}_{t}
Args
va
:Tensor
- Voltage angles per bus ( \boldsymbol{\theta} ). (batch_size, nbus)
Returns
Tensor
- Angle differences per branch. (batch_size, nbranch)
def balance_residual(self,
pd: torch.Tensor,
pg: torch.Tensor,
pf: torch.Tensor,
embed_method: str = 'pad',
clamp: bool = True) ‑> torch.Tensor-
Compute power balance residual.
\text{g_balance} = \text{pg_bus} - \text{pd_bus} - \text{gs_bus} - \text{pf_bus} - \text{pt_bus}
Args
pd
:Tensor
- Power demand per bus. (batch_size, nbus)
pg
:Tensor
- Power generation per generator. (batch_size, ngen)
pf
:Tensor
- Power flow per branch. (batch_size, nbranch)
embed_method
:str
, optional- Embedding method to convert component-wise values to bus-wise – one of "pad", "dense_matrix", or "matrix". Defaults to "pad".
clamp
:bool
, optional- Clamp to extract only violations. Defaults to True.
Returns
Tensor
- Power balance residual. (batch_size, nbus)
def calc_violations(self,
pd: torch.Tensor,
pg: torch.Tensor,
va: torch.Tensor,
pf: torch.Tensor | None = None,
reduction: str = 'mean',
clamp: bool = True,
embed_method: str = 'pad') ‑> dict[str, torch.Tensor]-
Compute all DCOPF violations.
Args
pd
:Tensor
- Power demand per bus. (batch_size, nbus)
pg
:Tensor
- Power generation per generator. (batch_size, ngen)
va
:Tensor
- Voltage angles per bus. (batch_size, nbus)
pf
:Tensor
, optional- Power flow per branch. Defaults to None.
reduction
:str
, optional- Reduction method. Defaults to "mean".
clamp
:bool
, optional- Clamp to extract only violations. Defaults to True.
embed_method
:str
, optional- Method to convert component-wise values to bus-wise – one of "pad", "dense_matrix", or "matrix". Defaults to "pad".
Returns
dict[str, Tensor]
- Dictionary of all violations:
-
"pg_lower": Lower bound violation of power generation. (batch_size, ngen)
-
"pg_upper": Upper bound violation of power generation. (batch_size, ngen)
-
"dva_lower": Lower bound violation of voltage angle difference. (batch_size, nbranch)
-
"dva_upper": Upper bound violation of voltage angle difference. (batch_size, nbranch)
-
"pf_lower": Lower bound violation of power flow. (batch_size, nbranch)
-
"pf_upper": Upper bound violation of power flow. (batch_size, nbranch)
-
"p_balance": Power balance violation. (batch_size, nbus)
-
"ohm": Ohm's law violation. (batch_size, nbranch)
def dva_bound_residual(self, dva: torch.Tensor, clamp: bool = False) ‑> torch.Tensor
-
Calculate the voltage angle difference bound residual.
g_{\text{lower}} = \text{angmin} - \text{dva} g_{\text{upper}} = \text{dva} - \text{angmax}
Args
dva
:Tensor
- Voltage angle differences per branch. (batch_size, nbranch)
clamp
:bool
, optional- Clamp the residual to be non-negative (extract violations). Defaults to False.
Returns
Tensor
- Lower bound residual. (batch_size, nbranch)
Tensor
- Upper bound residual. (batch_size, nbranch)
def objective(self, pg: torch.Tensor) ‑> torch.Tensor
-
Compute DCOPF objective function.
Cost is assumed to be constant for the batch, matching the reference case.
\text{objective} = \sum_i^n \text{cost}_{2,i} + \text{cost}_{1,i} \cdot \text{pg}_i
Args
pg
:Tensor
- Power generation per generator. (batch_size, ngen)
Returns
Tensor
- Objective function value. (batch_size, 1)
def ohm_residual(self, pf: torch.Tensor, dva: torch.Tensor, clamp: bool = False) ‑> torch.Tensor
-
Compute Ohm's law violation.
\text{g_ohm} = - b \cdot \text{dva} - \text{pf}
Args
pf
:Tensor
- Power flow per branch. (batch_size, nbranch)
dva
:Tensor
- Voltage angle differences per branch. (batch_size, nbranch)
clamp
:bool
, optional- Clamp to extract only violations. Defaults to False.
Returns
Tensor
- Ohm's law violation. (batch_size, nbranch)
def pf_bound_residual(self, pf: torch.Tensor, clamp: bool = False) ‑> torch.Tensor
-
Calculate the power flow bound residual.
g_{\text{lower}} = -\text{rate_a} - \text{pf} g_{\text{upper}} = \text{pf} - \text{rate_a}
Args
pf
:Tensor
- Power flow per branch. (batch_size, nbranch)
clamp
:bool
, optional- Clamp the residual to be non-negative (extract violations). Defaults to False.
Returns
Tensor
- Lower bound residual. (batch_size, nbranch)
Tensor
- Upper bound residual. (batch_size, nbranch)
def pf_from_va(self, va: torch.Tensor) ‑> torch.Tensor
-
Compute power flow given voltage angles.
\mathbf{p}_f = -\text{b} \cdot (\boldsymbol{\theta}_{f} - \boldsymbol{\theta}_{t})
Args
va
:Tensor
- Voltage angles per bus ( \boldsymbol{\theta} ). (batch_size, nbus)
Returns
Tensor
- Power flow per branch ( \mathbf{p}_f ). (batch_size, nbranch)
def pg_bound_residual(self, pg: torch.Tensor, clamp: bool = False) ‑> torch.Tensor
-
Calculate the power generation bound residual.
g_{\text{lower}} = \text{pmin} - \text{pg} g_{\text{upper}} = \text{pg} - \text{pmax}
Args
pg
:Tensor
- Active power generation per generator. (batch_size, ngen)
clamp
:bool
, optional- Clamp the residual to be non-negative (extract violations). Defaults to False.
Returns
Tensor
- Lower bound residual. (batch_size, ngen)
Tensor
- Upper bound residual. (batch_size, ngen)
Inherited members