Model API¶
OPF Heterogeneous Models¶
OPFHeteroGNN
¶
Bases: Module
OPF-specific Heterogeneous GNN with configurable message-passing backend.
Wraps HeteroConv with a selectable backend (SAGE, GCN, GIN, or GAT)
to operate on the heterogeneous power grid graph. Produces per-node
predictions for bus (va, vm) and generator (pg, qg).
Input shape per node type: (N_type, input_channels[type]).
Output shape: {'bus': (N_bus, out_channels), 'generator': (N_gen, out_channels)}.
Source code in lumina/model/opf/hetero_model.py
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
__init__(metadata, input_channels, hidden_channels=64, out_channels=2, num_layers=3, backend='sage', edge_attr_dim=None, **kwargs)
¶
Heterogeneous Graph Neural Network (HeteroGNN) model for OPF.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata
|
dict or tuple
|
Metadata containing node types and edge types. If dict: {'nodes': {node_type: dim, ...}, 'edges': {edge_type: dim, ...}} If tuple: (node_types, edge_types) |
required |
input_channels
|
dict
|
Number of input features for each node type. |
required |
hidden_channels
|
int
|
Hidden embedding size. |
64
|
out_channels
|
int
|
Size of each output sample. Defaults to 2. |
2
|
num_layers
|
int
|
Number of layers. Defaults to 3. |
3
|
backend
|
str
|
Graph convolutional layer backend. Defaults to "sage". |
'sage'
|
edge_attr_dim
|
int
|
Dimension of edge attributes for GAT. Defaults to None. |
None
|
Source code in lumina/model/opf/hetero_model.py
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |
reset_parameters()
¶
Reset parameters of the model.
Source code in lumina/model/opf/hetero_model.py
forward(x_dict, edge_index_dict, edge_attr_dict=None, minmax_scaling=False, **kwargs)
¶
Forward pass of the HeteroGNN model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_dict
|
dict
|
Node features for each node type. |
required |
edge_index_dict
|
dict
|
Edge indices for each edge type. |
required |
edge_attr_dict
|
dict
|
Edge attributes for each edge type. Defaults to None |
None
|
minmax_scaling
|
bool
|
Whether to apply min-max scaling to outputs. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Output predictions for each target node type. |
Source code in lumina/model/opf/hetero_model.py
RGAT
¶
Bases: Module
Relational Graph Attention Network for heterogeneous OPF graphs.
Uses HeteroConv wrapping per-relation GATConv layers with
multi-head attention. Each edge type gets its own GATConv parameters.
Input shape per node type: (N_type, input_channels[type]).
Output shape: {'bus': (N_bus, out_channels), 'generator': (N_gen, out_channels)}.
Source code in lumina/model/opf/hetero_model.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | |
__init__(metadata, input_channels, hidden_channels=64, out_channels=2, num_layers=3, num_heads=1, backend='sage', edge_attr_dim=None, **kwargs)
¶
Relational Graph Attention Network (RGAT) model. Implemented as HeteroConv with GATConv (Relational GAT).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata
|
dict or tuple
|
Metadata containing node types and edge types. If dict: {'nodes': {node_type: dim, ...}, 'edges': {edge_type: dim, ...}} If tuple: (node_types, edge_types) |
required |
input_channels
|
dict
|
Number of input features for each node type. |
required |
hidden_channels
|
int
|
Hidden embedding size. |
64
|
out_channels
|
int
|
Size of each output sample. Defaults to 2. |
2
|
num_layers
|
int
|
Number of layers. Defaults to 3. |
3
|
num_heads
|
int
|
Number of multi-head-attention heads. Defaults to 1. |
1
|
backend
|
str
|
Graph convolutional layer backend. Defaults to "sage". |
'sage'
|
edge_attr_dim
|
int
|
Dimension of edge attributes for GAT. Defaults to None. |
None
|
Source code in lumina/model/opf/hetero_model.py
reset_parameters()
¶
Reset parameters of the model.
Source code in lumina/model/opf/hetero_model.py
forward(x_dict, edge_index_dict, edge_attr_dict=None, minmax_scaling=False, **kwargs)
¶
Forward pass of the RGAT model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_dict
|
dict
|
Node features for each node type. |
required |
edge_index_dict
|
dict
|
Edge indices for each edge type. |
required |
edge_attr_dict
|
dict
|
Edge attributes for each edge type. Defaults to None |
None
|
minmax_scaling
|
bool
|
Whether to apply min-max scaling to outputs. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Output predictions for each target node type. |
Source code in lumina/model/opf/hetero_model.py
HEAT
¶
Bases: Module
Heterogeneous Edge-Attributed Transformer for OPF graphs.
Uses HEATConv on a homogeneous view of the heterogeneous graph.
Node and edge features are projected to hidden_channels before
being passed through HEATConv layers that incorporate edge type and
edge attribute embeddings.
Input shape per node type: (N_type, input_channels[type]).
Output shape: {'bus': (N_bus, out_channels), 'generator': (N_gen, out_channels)}.
Source code in lumina/model/opf/hetero_model.py
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 | |
__init__(metadata, input_channels, hidden_channels=64, out_channels=2, num_layers=3, attention_heads=1, edge_type_emb_dim=16, edge_attr_emb_dim=16, backend='sage', edge_attr_dim=None, **kwargs)
¶
Initialize the HEAT model.
Notes
- Externally, this model keeps the "hetero" forward signature: forward(x_dict, edge_index_dict, edge_attr_dict=None, ...)
- Internally, torch_geometric.nn.HEATConv operates on a homogeneous view. We therefore build a temporary HeteroData and call to_homogeneous().
- If edge_attr_dict is not provided by the caller (as in train_opf.py today), this model will still run by creating zero edge attributes for all relations.
Source code in lumina/model/opf/hetero_model.py
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 | |
forward(x_dict, edge_index_dict, edge_attr_dict=None, minmax_scaling=False, **kwargs)
¶
Forward pass of the HEAT model.
Converts the heterogeneous graph to a homogeneous view internally for HEATConv message passing, then maps outputs back to node types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_dict
|
dict
|
Node features for each node type. |
required |
edge_index_dict
|
dict
|
Edge indices for each edge type. |
required |
edge_attr_dict
|
dict
|
Edge attributes for each edge type. Zero-padded if not provided. Defaults to None. |
None
|
minmax_scaling
|
bool
|
Whether to apply min-max scaling to outputs. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Output predictions |
Source code in lumina/model/opf/hetero_model.py
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 | |
HGT
¶
Bases: Module
Heterogeneous Graph Transformer for OPF graphs.
Uses HGTConv layers with multi-head attention and optional dropout.
Each node type and edge type receives type-specific attention weights
following the HGT architecture.
Input shape per node type: (N_type, input_channels[type]).
Output shape: {'bus': (N_bus, out_channels), 'generator': (N_gen, out_channels)}.
Source code in lumina/model/opf/hetero_model.py
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 | |
__init__(metadata, input_channels, hidden_channels=64, out_channels=2, num_layers=3, num_heads=1, dropout=0.0, backend='sage', edge_attr_dim=None, **kwargs)
¶
Heterogeneous Graph Transformer (HGT) model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata
|
dict or tuple
|
Metadata containing node types and edge types. If dict: {'nodes': {node_type: dim, ...}, 'edges': {edge_type: dim, ...}} If tuple: (node_types, edge_types) |
required |
input_channels
|
dict
|
Number of input features for each node type. |
required |
hidden_channels
|
int
|
Hidden embedding size. |
64
|
out_channels
|
int
|
Size of each output sample. Defaults to 2. |
2
|
num_layers
|
int
|
Number of layers. Defaults to 3. |
3
|
num_heads
|
int
|
Number of multi-head-attention heads. Defaults to 1. |
1
|
backend
|
str
|
Graph convolutional layer backend. Defaults to "sage". |
'sage'
|
edge_attr_dim
|
int
|
Dimension of edge attributes for GAT. Defaults to None. |
None
|
Source code in lumina/model/opf/hetero_model.py
reset_parameters()
¶
Reset parameters of the model.
Source code in lumina/model/opf/hetero_model.py
forward(x_dict, edge_index_dict, edge_attr_dict=None, minmax_scaling=False, **kwargs)
¶
Forward pass of the HGT model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_dict
|
dict
|
Node features for each node type. |
required |
edge_index_dict
|
dict
|
Edge indices for each edge type. |
required |
edge_attr_dict
|
dict
|
Edge attributes for each edge type. Defaults to None |
None
|
minmax_scaling
|
bool
|
Whether to apply min-max scaling to outputs. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Output predictions for each target node type. |
Source code in lumina/model/opf/hetero_model.py
OPF Homogeneous Models¶
GNN_basic
¶
Bases: GNNBase
Base homogeneous GNN with NNConv layers and an MLP head.
Provides the default get_layers, forward, get_emb, and
loss methods. Subclasses (GAT, GCN, GIN, TRANSFORMER) override
get_layers and optionally get_emb to swap the convolution type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_dim
|
int
|
Number of input node features. |
required |
output_dim
|
int
|
Number of output features per node. |
required |
model_params
|
dict
|
Dictionary containing |
required |
Source code in lumina/model/opf/homo_model.py
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | |
forward(*args, **kwargs)
¶
Forward pass of GNN model.
Source code in lumina/model/opf/homo_model.py
loss(pred, label)
¶
Mean Squared Error Loss
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pred
|
Tensor
|
Predicted values. |
required |
label
|
Tensor
|
True values. |
required |
Returns:
| Type | Description |
|---|---|
|
torch.Tensor: Mean Squared Error Loss. |
Source code in lumina/model/opf/homo_model.py
GAT
¶
Bases: GNN_basic
Homogeneous GNN using GATConv (Graph Attention Network) layers.
Replaces the default NNConv with GATConv and uses LeakyReLU
activation in the MLP head.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_dim
|
int
|
Number of input node features. |
required |
output_dim
|
int
|
Number of output features per node. |
required |
model_params
|
dict
|
Model hyperparameters (see |
required |
Source code in lumina/model/opf/homo_model.py
GCN
¶
Bases: GNN_basic
Homogeneous GNN using GCNConv (Graph Convolutional Network) layers.
Overrides get_layers to use GCNConv and get_emb to skip
edge attributes (GCNConv does not use them).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_dim
|
int
|
Number of input node features. |
required |
output_dim
|
int
|
Number of output features per node. |
required |
model_params
|
dict
|
Model hyperparameters (see |
required |
Source code in lumina/model/opf/homo_model.py
get_layers()
¶
Prepare the layers of GCN model.
Source code in lumina/model/opf/homo_model.py
get_emb(*args, **kwargs)
¶
Get the node embeddings from GCN model.
Source code in lumina/model/opf/homo_model.py
GIN
¶
Bases: GNN_basic
Homogeneous GNN using GINEConv (Graph Isomorphism Network) layers.
Uses GINEConv with edge attributes and a two-layer MLP with
ReLU/Sigmoid activations inside each convolution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_dim
|
int
|
Number of input node features. |
required |
output_dim
|
int
|
Number of output features per node. |
required |
model_params
|
dict
|
Model hyperparameters (see |
required |
Source code in lumina/model/opf/homo_model.py
TRANSFORMER
¶
Bases: GNN_basic
Homogeneous GNN using TransformerConv layers.
Applies multi-head graph transformer convolutions (4 heads, no concatenation) with edge attribute support.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_dim
|
int
|
Number of input node features. |
required |
output_dim
|
int
|
Number of output features per node. |
required |
model_params
|
dict
|
Model hyperparameters (see |
required |
Source code in lumina/model/opf/homo_model.py
Base Heterogeneous GNN¶
HeteroGNN
¶
Bases: Module
Generic Heterogeneous GNN with configurable message-passing backend.
Supports SAGE, GCN, GIN, and GAT backends via HeteroConv. Each edge
type gets its own convolution instance. Produces per-node predictions for
bus and generator node types.
Input shape per node type: (N_type, input_channels[type]).
Output shape: {'bus': (N_bus, out_channels), 'generator': (N_gen, out_channels)}.
Source code in lumina/model/base/hetero_gnn.py
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
__init__(metadata, input_channels, hidden_channels=64, out_channels=2, num_layers=3, backend='sage', edge_attr_dim=None, **kwargs)
¶
Heterogeneous Graph Neural Network (HeteroGNN) model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata
|
dict or tuple
|
Metadata containing node types and edge types. If dict: {'nodes': {node_type: dim, ...}, 'edges': {edge_type: dim, ...}} If tuple: (node_types, edge_types) |
required |
input_channels
|
dict
|
Number of input features for each node type. |
required |
hidden_channels
|
int
|
Hidden embedding size. |
64
|
out_channels
|
int
|
Size of each output sample. Defaults to 2. |
2
|
num_layers
|
int
|
Number of layers. Defaults to 3. |
3
|
backend
|
str
|
Graph convolutional layer backend. Defaults to "sage". |
'sage'
|
edge_attr_dim
|
int
|
Dimension of edge attributes for GAT. Defaults to None. |
None
|
Source code in lumina/model/base/hetero_gnn.py
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |
reset_parameters()
¶
Reset parameters of the model.
Source code in lumina/model/base/hetero_gnn.py
forward(x_dict, edge_index_dict, edge_attr_dict=None, minmax_scaling=False, **kwargs)
¶
Forward pass of the HeteroGNN model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_dict
|
dict
|
Node features for each node type. |
required |
edge_index_dict
|
dict
|
Edge indices for each edge type. |
required |
edge_attr_dict
|
dict
|
Edge attributes for each edge type. Defaults to None |
None
|
minmax_scaling
|
bool
|
Whether to apply min-max scaling to outputs. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Output predictions for each target node type. |
Source code in lumina/model/base/hetero_gnn.py
ModelFactory
¶
Factory for instantiating heterogeneous GNN model architectures.
Currently supports 'heterognn', 'hgt', 'heat', and 'rgat'.
HGT, HEAT, and RGAT fall back to HeteroGNN with a warning until their
dedicated configurations are finalized.
Source code in lumina/model/base/hetero_gnn.py
create_model(model_name, metadata, input_channels, config)
staticmethod
¶
Create a model instance by name and configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name
|
str
|
Model architecture name (case-insensitive).
One of |
required |
metadata
|
dict or tuple
|
Graph metadata describing node and edge types. |
required |
input_channels
|
dict
|
Mapping of node type to input feature dimension. |
required |
config
|
dict
|
Full training configuration dict; model
hyperparameters are read from |
required |
Returns:
| Type | Description |
|---|---|
|
torch.nn.Module: An instantiated GNN model. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in lumina/model/base/hetero_gnn.py
get_available_models()
staticmethod
¶
Return the list of supported model architecture names.
Returns:
| Type | Description |
|---|---|
|
list[str]: Available model names. |
ACOPFModel
¶
Bases: Module
Placeholder ACOPF model for power systems.
Not yet implemented. Reserved for a future end-to-end ACOPF model that combines graph encoding with constraint-aware decoding.
Source code in lumina/model/base/hetero_gnn.py
Base Homogeneous GNN¶
GNNBase
¶
Bases: Module
Abstract base class for homogeneous GNN models.
Provides _argsparse to flexibly accept graph data as positional
arguments, keyword arguments, or a PyG Batch / Data object.
Subclasses must define convolutional layers and a forward method.
Source code in lumina/model/base/homo_gnn.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | |
GNNPool
¶
Bases: Module
Graph pooling module that applies a named readout operation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
readout
|
str
|
Name of the readout strategy. One of |
required |
Source code in lumina/model/base/homo_gnn.py
forward(x, batch)
¶
Apply the readout operation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Node embeddings of shape |
required |
batch
|
Tensor
|
Batch assignment vector of shape |
required |
Returns:
| Type | Description |
|---|---|
|
torch.Tensor: Pooled representation. |
Source code in lumina/model/base/homo_gnn.py
Loss Functions¶
OPFLossManager
¶
Bases: Module
Unified loss manager that wraps ACOPFLossFunction for the training loop.
Provides target extraction from both heterogeneous and homogeneous batches,
optional y_mask filtering for homogeneous data, and a standardized
compute_loss interface returning both the scalar loss and an info dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loss_type
|
str
|
Type of loss to use. One of 'mse', 'rmse', 'mae', 'mape', or 'smooth_l1'. |
'mse'
|
device
|
device
|
Device for computations. Defaults to CPU. |
None
|
**kwargs
|
Additional arguments forwarded to |
{}
|
Source code in lumina/model/opf/losses.py
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 | |
compute_loss(predictions: Dict[str, torch.Tensor], batch, return_info: bool = True, **kwargs) -> Union[torch.Tensor, Tuple[torch.Tensor, Dict]]
¶
Compute the loss given model predictions and a data batch.
Extracts targets from the batch, applies y_mask filtering for homogeneous batches, and delegates to the underlying ACOPFLossFunction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predictions
|
Dict[str, Tensor]
|
Model outputs keyed by
node type (e.g. |
required |
batch
|
A PyG |
required | |
return_info
|
bool
|
If True, return |
True
|
**kwargs
|
Reserved for future use. |
{}
|
Returns:
| Type | Description |
|---|---|
Union[Tensor, Tuple[Tensor, Dict]]
|
Union[torch.Tensor, Tuple[torch.Tensor, Dict]]: The scalar loss,
or a tuple of |
Source code in lumina/model/opf/losses.py
get_loss_info() -> Dict
¶
Return a dictionary describing the current loss configuration.
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict
|
Loss metadata including |
Source code in lumina/model/opf/losses.py
ACOPFLossFunction
¶
Bases: Module
Simplified loss function class for ACOPF training using PyTorch built-in losses.
Supports: - MSE (Mean Squared Error) - torch.nn.MSELoss - RMSE (Root Mean Squared Error) - sqrt of MSE - MAE (Mean Absolute Error) - torch.nn.L1Loss - MAPE (Mean Absolute Percentage Error) - custom implementation - SmoothL1Loss (Huber Loss) - torch.nn.SmoothL1Loss
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loss_type
|
str
|
Type of loss function to use: - 'mse': Mean Squared Error - 'rmse': Root Mean Squared Error - 'mae': Mean Absolute Error - 'mape': Mean Absolute Percentage Error - 'smooth_l1': Smooth L1 Loss (Huber Loss) |
'mse'
|
node_weights
|
dict
|
Weights for different node types. Default: {'bus': 1.0, 'generator': 1.0} |
None
|
reduction
|
str
|
Reduction method ('mean', 'sum', 'none'). Default: 'mean' |
'mean'
|
epsilon
|
float
|
Small value to avoid division by zero in MAPE. Default: 1e-8 |
1e-08
|
beta
|
float
|
Beta parameter for SmoothL1Loss. Default: 1.0 |
1.0
|
Source code in lumina/model/opf/losses.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | |
PhysicsInformedLoss
¶
Bases: ACOPFLossFunction
Physics-informed loss combining standard ML loss with physics constraint penalties.
Extends ACOPFLossFunction by adding a penalty term computed from
power system constraint violations (e.g. power flow, line limits).
The total loss is ML_loss + physics_weight * physics_penalty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_loss_config
|
dict
|
Configuration dict forwarded to
|
None
|
physics_weight
|
float
|
Scalar weight for the physics penalty term. Defaults to 1.0. |
1.0
|
constraint_types
|
list
|
Which constraint families to
penalize, e.g. |
None
|
penalty_method
|
str
|
Penalty formulation. One of |
'quadratic'
|
**kwargs
|
Additional keyword arguments forwarded to
|
{}
|
Source code in lumina/model/opf/losses.py
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | |
set_constraint_computer(constraint_computer)
¶
Attach a constraint violation computer for physics penalty evaluation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
constraint_computer
|
Object with a |
required |
Source code in lumina/model/opf/losses.py
RMSELoss
¶
Bases: Module
Root Mean Squared Error Loss.
.. math:: RMSE = \sqrt{rac{1}{N} \sum_{i=1}^{N} (y_i - \hat{y}_i)^2}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reduction
|
str
|
Reduction method to apply to the output: 'mean', 'sum', or 'none'. Default: 'mean' |
'mean'
|
epsilon
|
float
|
Small value to avoid sqrt(0). Default: 1e-8 |
1e-08
|
Source code in lumina/model/opf/losses.py
MAPELoss
¶
Bases: Module
Mean Absolute Percentage Error Loss.
.. math:: MAPE = \frac{1}{N} \sum_{i=1}^{N} \left| \frac{y_i - \hat{y}_i}{y_i} \right|
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reduction
|
str
|
Reduction method to apply to the output: 'mean', 'sum', or 'none'. Default: 'mean' |
'mean'
|
epsilon
|
float
|
Small value to avoid division by zero. Default: 1e-8 |
1e-08
|