-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpower_flow_errors.py
More file actions
75 lines (54 loc) · 2.41 KB
/
power_flow_errors.py
File metadata and controls
75 lines (54 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import torch
from torch_geometric.datasets import OPFDataset
from torch_geometric.loader import DataLoader
from torch_geometric.nn import GraphConv, to_hetero
from torchmetrics import MetricCollection
from opf_dataset_utils.errors.power_flow import calculate_power_flow_errors
from opf_dataset_utils.metrics.aggregation import AggregationTypes
from opf_dataset_utils.metrics.power import PowerTypes
from opf_dataset_utils.metrics.power_flow import PowerFlowError
from opf_dataset_utils.metrics.units import UnitTypes
class Model(torch.nn.Module):
def __init__(self):
super().__init__()
self.conv1 = GraphConv(-1, 16)
self.conv2 = GraphConv(16, 2)
def forward(self, x, edge_index):
x = self.conv1(x, edge_index).relu()
x = self.conv2(x, edge_index)
return x
def main():
"""
Calculate the power flow errors of the solution and an untrained model.
Returns
-------
"""
dataset = OPFDataset(
"data", case_name="pglib_opf_case14_ieee", split="val", topological_perturbations=False, num_groups=1
)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
loader = DataLoader(dataset, batch_size=32, shuffle=True)
batch = next(iter(loader)).to(device)
untrained_model = to_hetero(Model(), batch.metadata())
untrained_model.to(device)
with torch.no_grad():
untrained_predictions = untrained_model(batch.x_dict, batch.edge_index_dict)
mean_abs_errors_solution = calculate_power_flow_errors(batch, batch.y_dict).abs().mean()
mean_abs_errors_untrained = calculate_power_flow_errors(batch, untrained_predictions).abs().mean()
print("Mean power flow errors:")
print(f"\tSolution: {mean_abs_errors_solution:.5e} [p.u.]")
print(f"\tUntrained model prediction: {mean_abs_errors_untrained:.5f} [p.u.]")
metric_dict = {}
for aggr in AggregationTypes:
for power_type in PowerTypes:
for unit in UnitTypes:
metric_dict[f"{aggr} absolute {power_type} power flow error [{unit}]"] = PowerFlowError(
aggr=aggr, power_type=power_type, unit=unit, value_type="absolute"
)
metrics = MetricCollection(metric_dict).to(device)
metrics(batch, untrained_predictions)
print("Power flow error metrics:")
for name, value in metrics.compute().items():
print(f"\t{name:>60}: {value:>.5f}")
if __name__ == "__main__":
main()