Module ml4opf.functional
Functional interface
Sub-modules
ml4opf.functional.ac
-
ACOPF Functional interface
ml4opf.functional.dc
-
DCOPF Functional interface
ml4opf.functional.ed
-
EconomicDispatch Functional interface
ml4opf.functional.incidence
-
Incidence matrices and related functions.
ml4opf.functional.soc
-
SOCOPF Functional interface
Functions
def adjacency(fbus: torch.Tensor, tbus: torch.Tensor, n_bus: int, n_branch: int) ‑> torch.Tensor
-
Sparse adjacency matrix.
Each row corresponds to a bus and each column corresponds to a bus. The value is 1 if there is a branch between the buses, and 0 otherwise.
Args
fbus
:Tensor
- From bus indices. (nbranch,)
tbus
:Tensor
- To bus indices. (nbranch,)
n_bus
:int
- Number of buses.
n_branch
:int
- Number of branches.
Returns
Tensor
- Sparse adjacency matrix. (nbus, nbus)
def angle_difference(va: torch.Tensor, i: torch.Tensor, j: torch.Tensor) ‑> torch.Tensor
def bound_residual(x: torch.Tensor, xmin: torch.Tensor, xmax: torch.Tensor) ‑> tuple[torch.Tensor, torch.Tensor]
-
Calculate the bound residuals for a tensor x.
g_\text{lower} = x_\text{min} - x g_\text{upper} = x - x_\text{max}
Args
x
:Tensor
- Input tensor. (batch, n)
xmin
:Tensor
- Lower bounds. (batch, n)
xmax
:Tensor
- Upper bounds. (batch, n)
Returns
tuple[Tensor, Tensor]
- Lower and upper bound residuals. (batch, n)
def branch_from_incidence(fbus: torch.Tensor, n_bus: int, n_branch: int) ‑> torch.Tensor
-
Sparse branch from incidence matrix.
Each row corresponds to a bus and each column corresponds to a branch. The value is 1 if the branch is from the bus, and 0 otherwise.
Args
fbus
:Tensor
- From bus indices. (nbranch,)
n_bus
:int
- Number of buses.
n_branch
:int
- Number of branches.
Returns
Tensor
- Sparse branch from incidence matrix. (nbus, nbranch)
def branch_incidence(fbus: torch.Tensor, tbus: torch.Tensor, n_bus: int, n_branch: int) ‑> torch.Tensor
-
Sparse branch incidence matrix.
Each row corresponds to a bus and each column corresponds to a branch. The value is 1 if the branch is from the bus, -1 if the branch is to the bus, and 0 otherwise.
Args
fbus
:Tensor
- From bus indices. (nbranch,)
tbus
:Tensor
- To bus indices. (nbranch,)
n_bus
:int
- Number of buses.
n_branch
:int
- Number of branches.
Returns
Tensor
- Sparse branch incidence matrix. (nbus, nbranch)
def branch_to_incidence(tbus: torch.Tensor, n_bus: int, n_branch: int) ‑> torch.Tensor
-
Sparse branch to incidence matrix.
Each row corresponds to a bus and each column corresponds to a branch. The value is 1 if the branch is to the bus, and 0 otherwise.
Args
tbus
:Tensor
- To bus indices. (nbranch,)
n_bus
:int
- Number of buses.
n_branch
:int
- Number of branches.
Returns
Tensor
- Sparse branch to incidence matrix. (nbus, nbranch)
def equality_violation(residual: torch.Tensor, clamp: bool = False) ‑> torch.Tensor
-
Return the absolute violation (always positive) if clamp=True. Otherwise, pass-through the residual.
def generator_incidence(gen_bus: torch.Tensor, n_bus: int, n_gen: int) ‑> torch.Tensor
-
Sparse generator incidence matrix.
Each row corresponds to a bus and each column corresponds to a generator. The value is 1 if the generator is at the bus, and 0 otherwise.
Args
gen_bus
:Tensor
- Generator bus indices. (ngen,)
n_bus
:int
- Number of buses.
n_gen
:int
- Number of generators.
Returns
Tensor
- Sparse generator incidence matrix. (nbus, ngen)
def inequality_violation(residual: torch.Tensor, clamp: bool = False) ‑> torch.Tensor
-
Return only the violation (always positive) if clamp=True. Otherwise, pass-through the residual.
def load_incidence(load_bus: torch.Tensor, n_bus: int, n_load: int) ‑> torch.Tensor
-
Sparse load incidence matrix.
Each row corresponds to a bus and each column corresponds to a load. The value is 1 if the load is at the bus, and 0 otherwise.
Args
load_bus
:Tensor
- Load bus indices. (nload,)
n_bus
:int
- Number of buses.
n_load
:int
- Number of loads.
Returns
Tensor
- Sparse load incidence matrix. (nbus, nload)
def map_to_bus_matrix(x: torch.Tensor, x_matrix: torch.Tensor)
-
Map component-wise values to bus-wise values using matrix multiplication.
def map_to_bus_pad(x: torch.Tensor, x_per_bus: torch.Tensor)
-
Map component-wise values to bus-wise values using zero-padding.