mlpf


Namemlpf JSON
Version 0.0.9 PyPI version JSON
download
home_page
SummaryMachine learning for power flow
upload_time2023-08-16 15:43:10
maintainer
docs_urlNone
authorViktor Todosijevic
requires_python
licenseMIT
keywords machine learning power flow
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <p align="center">
<img src="https://github.com/viktor-ktorvi/mlpf/assets/69254199/333dfd18-7c60-4874-a89b-92eecf32ac96?raw=True" width="650">
</p>



__MLPF__ is a python library for (optimal) power flow calculations with machine learning.

It offers a few main features such as:

:chart_with_downwards_trend: Efficient loss functions compatible with both _PyTorch_
<img src="https://pytorch.org/assets/images/pytorch-logo.png" height=30></img>
and _scikit-learn_
<img src="https://upload.wikimedia.org/wikipedia/commons/0/05/Scikit_learn_logo_small.svg" height=30></img>.

💱 Conversion functions to go from [PPCs](https://rwl.github.io/PYPOWER/api/pypower.caseformat-module.html) to _NumPy_ arrays or _Torch_ tensors.

<br>
As well as some optional high level utilities:

ðŸ—‚ï¸ Data classes that make it easy to go from PPCs to custom (or [PyTorch Geometric](https://pytorch-geometric.readthedocs.io/en/latest/)
<img src="https://raw.githubusercontent.com/pyg-team/pyg_sphinx_theme/master/pyg_sphinx_theme/static/img/pyg_logo.png" height=30></img>
) data objects with all the info needed for (O)PF extracted.

📊 Metrics specific to (O)PF for logging and evaluation.

🔎 Visualization and description tools to take a quick look at your data.

Contributions welcome!

## Installation

```commandline
pip install mlpf
```

The previous command will install all the dependencies for working with numpy and scikit-learn. It will **not**, however, install all the dependencies needed for
working with torch. To use the torch functionalities, please
install [PyTorch](https://pytorch.org/), [PyTorch Geometric](https://pytorch-geometric.readthedocs.io/en/latest/install/installation.html) with its dependencies(torch-scatter etc.)
and optionally [TorchMetrics](https://torchmetrics.readthedocs.io/en/stable/).

Installation directly from the master branch:

```
pip install git+https://github.com/viktor-ktorvi/mlpf.git
```

## Basic usage

:cd: :file_folder:    Load your data in the [PYPOWER case format](https://rwl.github.io/PYPOWER/api/pypower.caseformat-module.html).

:open_file_folder: :arrow_heading_up:    Extract powers, voltages, limits and cost coefficients from the PPCs into either _NumPy_ arrays or _torch_ tensors.

ðŸ‹ðŸ»â€â™€ï¸ :chart_with_downwards_trend:    Plug the data into your machine learning models.

:chart_with_upwards_trend: :bar_chart:     Use the loss functions to evaluate your results or to train your models(the _torch_ versions of the loss functions are differentiable!).

### In depth examples

|                          | Power flow                                                                                                                                                                                                                                   | Optimal power flow                                                                                                                                                                                                                                                   |
|--------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| 
| Data extraction and loss | <ul><li>[NumPy/scikit-learn](examples/sklearn/loss/power_flow.py)</li><li>[torch](examples/torch/loss/power_flow.py)</li></ul>                                                                                                               | <ul><li>[NumPy/scikit-learn](examples/sklearn/loss/optimal_power_flow.py)</li><li>[torch](examples/torch/loss/optimal_power_flow.py)</li></ul>                                                                                                                       | 
| Supervised learning      | <ul><li>[scikit-learn linear regression](examples/sklearn/supervised/power_flow/linear_regression.py)<li>[torch MLP](examples/torch/supervised/power_flow/mlp.py)</li><li>[torch GCN](examples/torch/supervised/power_flow/gcn.py)</li></ul> | <ul><li>[scikit-learn linear regression](examples/sklearn/supervised/optimal_power_flow/linear_regression.py)<li>[torch MLP](examples/torch/supervised/optimal_power_flow/mlp.py)</li><li>[torch GCN](examples/torch/supervised/optimal_power_flow/gcn.py)</li></ul> |
| Unsupervised learning    | <ul><li>[torch MLP](examples/torch/unsupervised/power_flow/mlp.py)</li><li>[torch GCN](examples/torch/unsupervised/power_flow/gcn.py)</li></ul>                                                                                              | <ul><li>[torch MLP](examples/torch/unsupervised/optimal_power_flow/mlp.py)</li><li>[torch GCN](examples/torch/unsupervised/optimal_power_flow/gcn.py)</li></ul>                                                                                                      |

### Quick start

#### Power flow

<details>
<summary>ppc</summary>

```python
import copy

import pandapower as pp
import pandapower.networks as pn

from pypower.ppoption import ppoption
from pypower.runpf import runpf

net = pn.case118()

ppc = pp.converter.to_ppc(net, init="flat")

ppopt = ppoption(OUT_ALL=0, VERBOSE=0)
ppc, converged = runpf(copy.deepcopy(ppc), ppopt=ppopt)
```

</details>

##### NumPy/scikit-learn

```python
import numpy as np

from mlpf.data.conversion.numpy.power_flow import (
    extract_line_arrays,
    extract_power_arrays,
    extract_voltage_arrays
)

from mlpf.loss.numpy.power_flow import (
    active_power_errors,
    reactive_power_errors
)

# extract quantities
active_powers, reactive_powers = extract_power_arrays(ppc)
voltage_magnitudes, voltage_angles = extract_voltage_arrays(ppc)
edge_index, conductances, susceptances = extract_line_arrays(ppc)

active_errors = active_power_errors(edge_index, active_powers, voltage_magnitudes, voltage_angles, conductances, susceptances)
reactive_errors = reactive_power_errors(edge_index, reactive_powers, voltage_magnitudes, voltage_angles, conductances, susceptances)

print(f"Total P loss = {np.sum(active_errors):.3e} p.u.")
print(f"Total Q loss = {np.sum(reactive_errors):.3e} p.u.")
```

##### Torch

```python
import torch

from mlpf.data.conversion.torch.power_flow import (
    extract_line_tensors,
    extract_power_tensors,
    extract_voltage_tensors
)
from mlpf.loss.torch.power_flow import (
    active_power_errors,
    reactive_power_errors
)

# extract quantities
# note: going from float64 to float32(the standard in torch) will increase the PF loss significantly
active_powers, reactive_powers = extract_power_tensors(ppc, dtype=torch.float64)
voltage_magnitudes, voltage_angles = extract_voltage_tensors(ppc, dtype=torch.float64)
edge_index, conductances, susceptances = extract_line_tensors(ppc, dtype=torch.float64)

active_errors = active_power_errors(edge_index, active_powers, voltage_magnitudes, voltage_angles, conductances, susceptances)
reactive_errors = reactive_power_errors(edge_index, reactive_powers, voltage_magnitudes, voltage_angles, conductances, susceptances)

print(f"Total P loss = {torch.sum(active_errors):.3e} p.u.")
print(f"Total Q loss = {torch.sum(reactive_errors):.3e} p.u.")
```

#### Optimal power flow

<details>
<summary>ppc</summary>

```python
import copy

import pandapower as pp
import pandapower.networks as pn

from pypower.ppoption import ppoption
from pypower.runopf import runopf

net = pn.case118()
ppc = pp.converter.to_ppc(net, init="flat")

ppopt = ppoption(OUT_ALL=0, VERBOSE=0)
ppc = runopf(copy.deepcopy(ppc), ppopt=ppopt)
```

</details>

##### NumPy/scikit-learn

```python
import numpy as np

from mlpf.data.conversion.numpy.optimal_power_flow import (
    extract_active_power_limits_arrays,
    extract_cost_coefficients_array,
    extract_demand_arrays,
    extract_reactive_power_limits_arrays,
    extract_voltage_limits_arrays,
)

from mlpf.data.conversion.numpy.power_flow import (
    extract_power_arrays,
    extract_voltage_arrays
)

from mlpf.loss.numpy.bound_errors import (
    lower_bound_errors,
    upper_bound_errors
)

from mlpf.loss.numpy.costs import polynomial_costs

# extract quantities
active_powers, reactive_powers = extract_power_arrays(ppc)
voltage_magnitudes, voltage_angles = extract_voltage_arrays(ppc)

voltages_min, voltages_max = extract_voltage_limits_arrays(ppc)
active_powers_min, active_powers_max = extract_active_power_limits_arrays(ppc)
reactive_powers_min, reactive_powers_max = extract_reactive_power_limits_arrays(ppc)

active_power_demands, _ = extract_demand_arrays(ppc)
active_powers_generation = (active_powers + active_power_demands) * ppc["baseMVA"]

cost_coefficients = extract_cost_coefficients_array(ppc)

# calculate errors
voltage_upper_errors = upper_bound_errors(voltage_magnitudes, voltages_max)
voltage_lower_errors = lower_bound_errors(voltage_magnitudes, voltages_min)

active_upper_errors = upper_bound_errors(active_powers, active_powers_max)
active_lower_errors = lower_bound_errors(active_powers, active_powers_min)

reactive_upper_errors = upper_bound_errors(reactive_powers, reactive_powers_max)
reactive_lower_errors = lower_bound_errors(reactive_powers, reactive_powers_min)

cost_errors = np.sum(polynomial_costs(active_powers_generation, cost_coefficients)) - ppc["f"]

print(f"Total V max violation = {np.sum(voltage_upper_errors):.3e} p.u.")
print(f"Total V min violation = {np.sum(voltage_lower_errors):.3e} p.u.")

print(f"Total P max violation = {np.sum(active_upper_errors):.3e} p.u.")
print(f"Total P min violation = {np.sum(active_lower_errors):.3e} p.u.")

print(f"Total Q max violation = {np.sum(reactive_upper_errors):.3e} p.u.")
print(f"Total Q min violation = {np.sum(reactive_lower_errors):.3e} p.u.")

print(f"Total costs = {cost_errors:.3e} $/h")
```

##### Torch

```python
import torch

from mlpf.data.conversion.torch.optimal_power_flow import (
    extract_active_power_limits_tensors,
    extract_cost_coefficients_tensor,
    extract_demand_tensors,
    extract_reactive_power_limits_tensors,
    extract_voltage_limits_tensors,
)

from mlpf.data.conversion.torch.power_flow import (
    extract_power_tensors,
    extract_voltage_tensors
)

from mlpf.loss.torch.bound_errors import (
    lower_bound_errors,
    upper_bound_errors
)

from mlpf.loss.torch.costs import polynomial_costs

# extract quantities
active_powers, reactive_powers = extract_power_tensors(ppc, dtype=torch.float64)
voltage_magnitudes, voltage_angles = extract_voltage_tensors(ppc, dtype=torch.float64)

voltages_min, voltages_max = extract_voltage_limits_tensors(ppc, dtype=torch.float64)
active_powers_min, active_powers_max = extract_active_power_limits_tensors(ppc, dtype=torch.float64)
reactive_powers_min, reactive_powers_max = extract_reactive_power_limits_tensors(ppc, dtype=torch.float64)

active_power_demands, _ = extract_demand_tensors(ppc, dtype=torch.float64)
active_powers_generation = (active_powers + active_power_demands) * ppc["baseMVA"]

cost_coefficients = extract_cost_coefficients_tensor(ppc, dtype=torch.float64)

# calculate errors
voltage_upper_errors = upper_bound_errors(voltage_magnitudes, voltages_max)
voltage_lower_errors = lower_bound_errors(voltage_magnitudes, voltages_min)

active_upper_errors = upper_bound_errors(active_powers, active_powers_max)
active_lower_errors = lower_bound_errors(active_powers, active_powers_min)

reactive_upper_errors = upper_bound_errors(reactive_powers, reactive_powers_max)
reactive_lower_errors = lower_bound_errors(reactive_powers, reactive_powers_min)

cost_errors = torch.sum(polynomial_costs(active_powers_generation, cost_coefficients)) - ppc["f"]

print(f"Total V max violation = {torch.sum(voltage_upper_errors):.3e} p.u.")
print(f"Total V min violation = {torch.sum(voltage_lower_errors):.3e} p.u.")

print(f"Total P max violation = {torch.sum(active_upper_errors):.3e} p.u.")
print(f"Total P min violation = {torch.sum(active_lower_errors):.3e} p.u.")

print(f"Total Q max violation = {torch.sum(reactive_upper_errors):.3e} p.u.")
print(f"Total Q min violation = {torch.sum(reactive_lower_errors):.3e} p.u.")

print(f"Total costs = {cost_errors:.3e} $/h")
```

### Development

```
git clone https://github.com/viktor-ktorvi/mlpf.git
cd mlpf

conda env create -f environment.yml
conda activate mlpfenv
```


            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "mlpf",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "machine learning,power flow",
    "author": "Viktor Todosijevic",
    "author_email": "todosijevicviktor998@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/2c/94/66b2b1b1fc236a826e040a9795b3cfbc236f0a3f8f736f83af9199e1f669/mlpf-0.0.9.tar.gz",
    "platform": null,
    "description": "<p align=\"center\">\r\n<img src=\"https://github.com/viktor-ktorvi/mlpf/assets/69254199/333dfd18-7c60-4874-a89b-92eecf32ac96?raw=True\" width=\"650\">\r\n</p>\r\n\r\n\r\n\r\n__MLPF__ is a python library for (optimal) power flow calculations with machine learning.\r\n\r\nIt offers a few main features such as:\r\n\r\n:chart_with_downwards_trend: Efficient loss functions compatible with both _PyTorch_\r\n<img src=\"https://pytorch.org/assets/images/pytorch-logo.png\" height=30></img>\r\nand _scikit-learn_\r\n<img src=\"https://upload.wikimedia.org/wikipedia/commons/0/05/Scikit_learn_logo_small.svg\" height=30></img>.\r\n\r\n\u00f0\u0178\u2019\u00b1 Conversion functions to go from [PPCs](https://rwl.github.io/PYPOWER/api/pypower.caseformat-module.html) to _NumPy_ arrays or _Torch_ tensors.\r\n\r\n<br>\r\nAs well as some optional high level utilities:\r\n\r\n\u00f0\u0178\u2014\u201a\u00ef\u00b8 Data classes that make it easy to go from PPCs to custom (or [PyTorch Geometric](https://pytorch-geometric.readthedocs.io/en/latest/)\r\n<img src=\"https://raw.githubusercontent.com/pyg-team/pyg_sphinx_theme/master/pyg_sphinx_theme/static/img/pyg_logo.png\" height=30></img>\r\n) data objects with all the info needed for (O)PF extracted.\r\n\r\n\u00f0\u0178\u201c\u0160 Metrics specific to (O)PF for logging and evaluation.\r\n\r\n\u00f0\u0178\u201d\u017d Visualization and description tools to take a quick look at your data.\r\n\r\nContributions welcome!\r\n\r\n## Installation\r\n\r\n```commandline\r\npip install mlpf\r\n```\r\n\r\nThe previous command will install all the dependencies for working with numpy and scikit-learn. It will **not**, however, install all the dependencies needed for\r\nworking with torch. To use the torch functionalities, please\r\ninstall [PyTorch](https://pytorch.org/), [PyTorch Geometric](https://pytorch-geometric.readthedocs.io/en/latest/install/installation.html) with its dependencies(torch-scatter etc.)\r\nand optionally [TorchMetrics](https://torchmetrics.readthedocs.io/en/stable/).\r\n\r\nInstallation directly from the master branch:\r\n\r\n```\r\npip install git+https://github.com/viktor-ktorvi/mlpf.git\r\n```\r\n\r\n## Basic usage\r\n\r\n:cd: :file_folder:    Load your data in the [PYPOWER case format](https://rwl.github.io/PYPOWER/api/pypower.caseformat-module.html).\r\n\r\n:open_file_folder: :arrow_heading_up:    Extract powers, voltages, limits and cost coefficients from the PPCs into either _NumPy_ arrays or _torch_ tensors.\r\n\r\n\u00f0\u0178\u2039\u00f0\u0178\u00bb\u00e2\u20ac\u00e2\u2122\u20ac\u00ef\u00b8 :chart_with_downwards_trend:    Plug the data into your machine learning models.\r\n\r\n:chart_with_upwards_trend: :bar_chart:     Use the loss functions to evaluate your results or to train your models(the _torch_ versions of the loss functions are differentiable!).\r\n\r\n### In depth examples\r\n\r\n|                          | Power flow                                                                                                                                                                                                                                   | Optimal power flow                                                                                                                                                                                                                                                   |\r\n|--------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| \r\n| Data extraction and loss | <ul><li>[NumPy/scikit-learn](examples/sklearn/loss/power_flow.py)</li><li>[torch](examples/torch/loss/power_flow.py)</li></ul>                                                                                                               | <ul><li>[NumPy/scikit-learn](examples/sklearn/loss/optimal_power_flow.py)</li><li>[torch](examples/torch/loss/optimal_power_flow.py)</li></ul>                                                                                                                       | \r\n| Supervised learning      | <ul><li>[scikit-learn linear regression](examples/sklearn/supervised/power_flow/linear_regression.py)<li>[torch MLP](examples/torch/supervised/power_flow/mlp.py)</li><li>[torch GCN](examples/torch/supervised/power_flow/gcn.py)</li></ul> | <ul><li>[scikit-learn linear regression](examples/sklearn/supervised/optimal_power_flow/linear_regression.py)<li>[torch MLP](examples/torch/supervised/optimal_power_flow/mlp.py)</li><li>[torch GCN](examples/torch/supervised/optimal_power_flow/gcn.py)</li></ul> |\r\n| Unsupervised learning    | <ul><li>[torch MLP](examples/torch/unsupervised/power_flow/mlp.py)</li><li>[torch GCN](examples/torch/unsupervised/power_flow/gcn.py)</li></ul>                                                                                              | <ul><li>[torch MLP](examples/torch/unsupervised/optimal_power_flow/mlp.py)</li><li>[torch GCN](examples/torch/unsupervised/optimal_power_flow/gcn.py)</li></ul>                                                                                                      |\r\n\r\n### Quick start\r\n\r\n#### Power flow\r\n\r\n<details>\r\n<summary>ppc</summary>\r\n\r\n```python\r\nimport copy\r\n\r\nimport pandapower as pp\r\nimport pandapower.networks as pn\r\n\r\nfrom pypower.ppoption import ppoption\r\nfrom pypower.runpf import runpf\r\n\r\nnet = pn.case118()\r\n\r\nppc = pp.converter.to_ppc(net, init=\"flat\")\r\n\r\nppopt = ppoption(OUT_ALL=0, VERBOSE=0)\r\nppc, converged = runpf(copy.deepcopy(ppc), ppopt=ppopt)\r\n```\r\n\r\n</details>\r\n\r\n##### NumPy/scikit-learn\r\n\r\n```python\r\nimport numpy as np\r\n\r\nfrom mlpf.data.conversion.numpy.power_flow import (\r\n    extract_line_arrays,\r\n    extract_power_arrays,\r\n    extract_voltage_arrays\r\n)\r\n\r\nfrom mlpf.loss.numpy.power_flow import (\r\n    active_power_errors,\r\n    reactive_power_errors\r\n)\r\n\r\n# extract quantities\r\nactive_powers, reactive_powers = extract_power_arrays(ppc)\r\nvoltage_magnitudes, voltage_angles = extract_voltage_arrays(ppc)\r\nedge_index, conductances, susceptances = extract_line_arrays(ppc)\r\n\r\nactive_errors = active_power_errors(edge_index, active_powers, voltage_magnitudes, voltage_angles, conductances, susceptances)\r\nreactive_errors = reactive_power_errors(edge_index, reactive_powers, voltage_magnitudes, voltage_angles, conductances, susceptances)\r\n\r\nprint(f\"Total P loss = {np.sum(active_errors):.3e} p.u.\")\r\nprint(f\"Total Q loss = {np.sum(reactive_errors):.3e} p.u.\")\r\n```\r\n\r\n##### Torch\r\n\r\n```python\r\nimport torch\r\n\r\nfrom mlpf.data.conversion.torch.power_flow import (\r\n    extract_line_tensors,\r\n    extract_power_tensors,\r\n    extract_voltage_tensors\r\n)\r\nfrom mlpf.loss.torch.power_flow import (\r\n    active_power_errors,\r\n    reactive_power_errors\r\n)\r\n\r\n# extract quantities\r\n# note: going from float64 to float32(the standard in torch) will increase the PF loss significantly\r\nactive_powers, reactive_powers = extract_power_tensors(ppc, dtype=torch.float64)\r\nvoltage_magnitudes, voltage_angles = extract_voltage_tensors(ppc, dtype=torch.float64)\r\nedge_index, conductances, susceptances = extract_line_tensors(ppc, dtype=torch.float64)\r\n\r\nactive_errors = active_power_errors(edge_index, active_powers, voltage_magnitudes, voltage_angles, conductances, susceptances)\r\nreactive_errors = reactive_power_errors(edge_index, reactive_powers, voltage_magnitudes, voltage_angles, conductances, susceptances)\r\n\r\nprint(f\"Total P loss = {torch.sum(active_errors):.3e} p.u.\")\r\nprint(f\"Total Q loss = {torch.sum(reactive_errors):.3e} p.u.\")\r\n```\r\n\r\n#### Optimal power flow\r\n\r\n<details>\r\n<summary>ppc</summary>\r\n\r\n```python\r\nimport copy\r\n\r\nimport pandapower as pp\r\nimport pandapower.networks as pn\r\n\r\nfrom pypower.ppoption import ppoption\r\nfrom pypower.runopf import runopf\r\n\r\nnet = pn.case118()\r\nppc = pp.converter.to_ppc(net, init=\"flat\")\r\n\r\nppopt = ppoption(OUT_ALL=0, VERBOSE=0)\r\nppc = runopf(copy.deepcopy(ppc), ppopt=ppopt)\r\n```\r\n\r\n</details>\r\n\r\n##### NumPy/scikit-learn\r\n\r\n```python\r\nimport numpy as np\r\n\r\nfrom mlpf.data.conversion.numpy.optimal_power_flow import (\r\n    extract_active_power_limits_arrays,\r\n    extract_cost_coefficients_array,\r\n    extract_demand_arrays,\r\n    extract_reactive_power_limits_arrays,\r\n    extract_voltage_limits_arrays,\r\n)\r\n\r\nfrom mlpf.data.conversion.numpy.power_flow import (\r\n    extract_power_arrays,\r\n    extract_voltage_arrays\r\n)\r\n\r\nfrom mlpf.loss.numpy.bound_errors import (\r\n    lower_bound_errors,\r\n    upper_bound_errors\r\n)\r\n\r\nfrom mlpf.loss.numpy.costs import polynomial_costs\r\n\r\n# extract quantities\r\nactive_powers, reactive_powers = extract_power_arrays(ppc)\r\nvoltage_magnitudes, voltage_angles = extract_voltage_arrays(ppc)\r\n\r\nvoltages_min, voltages_max = extract_voltage_limits_arrays(ppc)\r\nactive_powers_min, active_powers_max = extract_active_power_limits_arrays(ppc)\r\nreactive_powers_min, reactive_powers_max = extract_reactive_power_limits_arrays(ppc)\r\n\r\nactive_power_demands, _ = extract_demand_arrays(ppc)\r\nactive_powers_generation = (active_powers + active_power_demands) * ppc[\"baseMVA\"]\r\n\r\ncost_coefficients = extract_cost_coefficients_array(ppc)\r\n\r\n# calculate errors\r\nvoltage_upper_errors = upper_bound_errors(voltage_magnitudes, voltages_max)\r\nvoltage_lower_errors = lower_bound_errors(voltage_magnitudes, voltages_min)\r\n\r\nactive_upper_errors = upper_bound_errors(active_powers, active_powers_max)\r\nactive_lower_errors = lower_bound_errors(active_powers, active_powers_min)\r\n\r\nreactive_upper_errors = upper_bound_errors(reactive_powers, reactive_powers_max)\r\nreactive_lower_errors = lower_bound_errors(reactive_powers, reactive_powers_min)\r\n\r\ncost_errors = np.sum(polynomial_costs(active_powers_generation, cost_coefficients)) - ppc[\"f\"]\r\n\r\nprint(f\"Total V max violation = {np.sum(voltage_upper_errors):.3e} p.u.\")\r\nprint(f\"Total V min violation = {np.sum(voltage_lower_errors):.3e} p.u.\")\r\n\r\nprint(f\"Total P max violation = {np.sum(active_upper_errors):.3e} p.u.\")\r\nprint(f\"Total P min violation = {np.sum(active_lower_errors):.3e} p.u.\")\r\n\r\nprint(f\"Total Q max violation = {np.sum(reactive_upper_errors):.3e} p.u.\")\r\nprint(f\"Total Q min violation = {np.sum(reactive_lower_errors):.3e} p.u.\")\r\n\r\nprint(f\"Total costs = {cost_errors:.3e} $/h\")\r\n```\r\n\r\n##### Torch\r\n\r\n```python\r\nimport torch\r\n\r\nfrom mlpf.data.conversion.torch.optimal_power_flow import (\r\n    extract_active_power_limits_tensors,\r\n    extract_cost_coefficients_tensor,\r\n    extract_demand_tensors,\r\n    extract_reactive_power_limits_tensors,\r\n    extract_voltage_limits_tensors,\r\n)\r\n\r\nfrom mlpf.data.conversion.torch.power_flow import (\r\n    extract_power_tensors,\r\n    extract_voltage_tensors\r\n)\r\n\r\nfrom mlpf.loss.torch.bound_errors import (\r\n    lower_bound_errors,\r\n    upper_bound_errors\r\n)\r\n\r\nfrom mlpf.loss.torch.costs import polynomial_costs\r\n\r\n# extract quantities\r\nactive_powers, reactive_powers = extract_power_tensors(ppc, dtype=torch.float64)\r\nvoltage_magnitudes, voltage_angles = extract_voltage_tensors(ppc, dtype=torch.float64)\r\n\r\nvoltages_min, voltages_max = extract_voltage_limits_tensors(ppc, dtype=torch.float64)\r\nactive_powers_min, active_powers_max = extract_active_power_limits_tensors(ppc, dtype=torch.float64)\r\nreactive_powers_min, reactive_powers_max = extract_reactive_power_limits_tensors(ppc, dtype=torch.float64)\r\n\r\nactive_power_demands, _ = extract_demand_tensors(ppc, dtype=torch.float64)\r\nactive_powers_generation = (active_powers + active_power_demands) * ppc[\"baseMVA\"]\r\n\r\ncost_coefficients = extract_cost_coefficients_tensor(ppc, dtype=torch.float64)\r\n\r\n# calculate errors\r\nvoltage_upper_errors = upper_bound_errors(voltage_magnitudes, voltages_max)\r\nvoltage_lower_errors = lower_bound_errors(voltage_magnitudes, voltages_min)\r\n\r\nactive_upper_errors = upper_bound_errors(active_powers, active_powers_max)\r\nactive_lower_errors = lower_bound_errors(active_powers, active_powers_min)\r\n\r\nreactive_upper_errors = upper_bound_errors(reactive_powers, reactive_powers_max)\r\nreactive_lower_errors = lower_bound_errors(reactive_powers, reactive_powers_min)\r\n\r\ncost_errors = torch.sum(polynomial_costs(active_powers_generation, cost_coefficients)) - ppc[\"f\"]\r\n\r\nprint(f\"Total V max violation = {torch.sum(voltage_upper_errors):.3e} p.u.\")\r\nprint(f\"Total V min violation = {torch.sum(voltage_lower_errors):.3e} p.u.\")\r\n\r\nprint(f\"Total P max violation = {torch.sum(active_upper_errors):.3e} p.u.\")\r\nprint(f\"Total P min violation = {torch.sum(active_lower_errors):.3e} p.u.\")\r\n\r\nprint(f\"Total Q max violation = {torch.sum(reactive_upper_errors):.3e} p.u.\")\r\nprint(f\"Total Q min violation = {torch.sum(reactive_lower_errors):.3e} p.u.\")\r\n\r\nprint(f\"Total costs = {cost_errors:.3e} $/h\")\r\n```\r\n\r\n### Development\r\n\r\n```\r\ngit clone https://github.com/viktor-ktorvi/mlpf.git\r\ncd mlpf\r\n\r\nconda env create -f environment.yml\r\nconda activate mlpfenv\r\n```\r\n\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Machine learning for power flow",
    "version": "0.0.9",
    "project_urls": null,
    "split_keywords": [
        "machine learning",
        "power flow"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f261143345c5f47fae762f2a8f0fe214917b1a5dd70959272146e06cfb833089",
                "md5": "0e8d960a205d6b2203554bc550bb9ba2",
                "sha256": "b48876a3d2402b7a657b7b9311c8a4637e59e3de158d3bafa1fafbdf9a1693f0"
            },
            "downloads": -1,
            "filename": "mlpf-0.0.9-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "0e8d960a205d6b2203554bc550bb9ba2",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 54072,
            "upload_time": "2023-08-16T15:43:08",
            "upload_time_iso_8601": "2023-08-16T15:43:08.278723Z",
            "url": "https://files.pythonhosted.org/packages/f2/61/143345c5f47fae762f2a8f0fe214917b1a5dd70959272146e06cfb833089/mlpf-0.0.9-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2c9466b2b1b1fc236a826e040a9795b3cfbc236f0a3f8f736f83af9199e1f669",
                "md5": "55ad2ee90c0431c8fba09a55632a95e0",
                "sha256": "6704a5c9b1735a60d2598f4d1f82d602d190abb9245e5a061b6a182c0d7a2a85"
            },
            "downloads": -1,
            "filename": "mlpf-0.0.9.tar.gz",
            "has_sig": false,
            "md5_digest": "55ad2ee90c0431c8fba09a55632a95e0",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 45167,
            "upload_time": "2023-08-16T15:43:10",
            "upload_time_iso_8601": "2023-08-16T15:43:10.296534Z",
            "url": "https://files.pythonhosted.org/packages/2c/94/66b2b1b1fc236a826e040a9795b3cfbc236f0a3f8f736f83af9199e1f669/mlpf-0.0.9.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-08-16 15:43:10",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "mlpf"
}
        
Elapsed time: 0.17451s