Expert system recommender for power grid contingency analysis based on ExpertOp4Grid principles.
Project description
ExpertOp4Grid Recommender
Expert system recommender for power grid contingency analysis based on ExpertOp4Grid principles. This tool analyzes N-1 contingencies in Grid2Op/pypowsybl environments, builds overflow graphs, applies expert rules to filter potential actions, and identifies relevant corrective measures to alleviate line overloads.
Features
- Contingency Simulation: Simulates N-1 contingencies in a Grid2Op environment.
- Overflow Graph Generation: Builds and visualizes overflow graphs using
alphaDeespandnetworkx. - Expert Rule Engine: Filters potential grid actions (line switching, topology changes) based on predefined rules derived from operator expertise.
- Action Prioritization: Identifies and scores relevant corrective actions (line reconnections, disconnections, node splitting/merging).
- Modular Structure: Organized code for better maintainability and testing.
Installation
-
Clone the repository:
git clone https://github.com/marota/Expert_op4grid_recommender.git cd Expert_op4grid_recommender
-
Recommended: Create a virtual environment:
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install the package and dependencies:
- Core dependencies: Make sure you have the necessary libraries installed. If
alphaDeespor specificgrid2opversions are not on PyPI, you might need to install them manually first according to their own instructions. - Install this package: For development (recommended), use editable mode:
pip install -e .
Or for a standard installation:pip install .
- Install test dependencies (optional):
pip install -e .[test]
- Core dependencies: Make sure you have the necessary libraries installed. If
Usage Example
Configure the desired scenario in expert_op4grid_recommender/config.py (Date, Timestep, Contingency Lines, etc.).
Then, run the main analysis script from the project root directory:
python expert_op4grid_recommender/main.py --date 2024-08-28 --timestep 36 --lines-defaut FRON5L31LOUHA P.SAOL31RONCI
The script will:
- Set up the Grid2Op environment.
- Simulate the specified contingency.
- Build and save an overflow graph visualization in the
Overflow_Graph/directory. - Apply expert rules to filter actions loaded from the action space file.
- Identify and print a list of prioritized corrective actions.
Pypowsybl Backend
The pypowsybl backend provides a high-performance alternative to Grid2Op for network simulation and what-if analysis.
Performance Optimizations
- Incremental Branching: Remedial actions are simulated directly from the converged N-1 state using network variants, significantly reducing computation time.
- Vectorized State Reading: Core network state (flows, voltages, bus assignments) is read using vectorized pypowsybl operations.
- Batched Action Application: Multiple topological changes are applied in batches to minimize overhead.
[!NOTE] Simulation Simplification: To maximize speed during what-if analyses (N-1 and remedial actions), voltage control for transformers and shunts is disabled in variant simulations. The base N-state observation maintains full regulation to provide a high-fidelity reference point.
An option that can be activated for specific use is to rebuild an action space from one segmentation of a grid to another or the full grid:
python expert_op4grid_recommender/main.py --rebuild-actions --repas-file allLogics.json --grid-snapshot-file data/snapshot/pf_20240828T0100Z_20240828T0100Z.xiidm
From all known logics on the full grid, and targeted action ids in the ACTION_FILE, it rebuilds the actions to be applied on the grid snapshot (in detailed topology format with switches) at the date of interest.
Configuration
Key parameters can be adjusted in expert_op4grid_recommender/config.py:
DATE,TIMESTEP,LINES_DEFAUT: Define the specific case to analyze.ENV_FOLDER,ENV_NAME: Specify the Grid2Op environment location.ACTION_FILE_PATH: Path to the JSON file containing the action space.USE_DC_LOAD_FLOW: Set toTrueto use DC power flow if AC flow fails.PARAM_OPTIONS_EXPERT_OP: Thresholds and parameters for the overflow graph analysis.
Action Discovery and Scoring
After building the overflow graph and filtering candidate actions with expert rules, the ActionDiscoverer evaluates and scores each candidate action by type. Each type has its own filtering criteria to narrow down candidates before scoring. The resulting scores are returned in an action_scores dictionary with four keys. Each type contains "scores" (action scores sorted by descending value) and "params" (underlying hypotheses and parameters used for scoring):
action_scores = {
"line_reconnection": {
"scores": {action_id: score, ...}, # sorted desc
"params": {
"percentage_threshold_min_dispatch_flow": float,
"max_dispatch_flow": float,
}
},
"line_disconnection": {
"scores": {action_id: score, ...}, # sorted desc
"params": {
"min_redispatch": float,
"max_redispatch": float,
"peak_redispatch": float, # value where score peaks (at 80% of range)
}
},
"open_coupling": {
"scores": {action_id: score, ...}, # sorted desc
"params": { # per-action details
action_id: {
"node_type": str, # "amont", "aval", or other
"bus_of_interest": int, # bus number used for scoring
"in_negative_flows": float,
"out_negative_flows": float,
"in_positive_flows": float,
"out_positive_flows": float,
}, ...
}
},
"close_coupling": {
"scores": {action_id: score, ...}, # sorted desc
"params": {
"percentage_threshold_min_dispatch_flow": float,
"max_dispatch_flow": float,
}
},
}
Line Reconnection Score (delta-theta)
Filtering: Only disconnected lines that are reconnectable and appear on dispatch paths of the overflow graph are considered. Among those, each candidate is checked for a valid red loop path: the path must not be blocked by other disconnected lines that have no active bypass. Additionally, the dispatch flow at the path extremities must exceed a minimum threshold (default 10% of the global max dispatch flow) to ensure the reconnection would have a significant impact.
Scoring: The remaining candidates are scored by the voltage angle difference (delta-theta) across the line's endpoints:
score = |theta_or - theta_ex|
A lower delta-theta indicates that the line can be reconnected with less stress on the grid. Actions are sorted by ascending delta-theta (lower is better).
Line Disconnection Score (asymmetric bell curve)
Disconnection candidates are lines on the constrained path (blue path) of the overflow graph. The score evaluates whether the redispatch flow from disconnecting the line falls within a useful range:
Flow bounds:
max_overload_flow: maximum absolute redispatch flow on the overflow graph (MW)min_redispatch = (rho_max_overloaded - 1.0) * max_overload_flow-- the minimum flow needed to bring the worst overloaded line below 100%max_redispatch: the binding constraint across all lines with increased loading, computed as:
For each line with delta_rho > 0:
ratio = capacity_line * (1 - rho_before) / (rho_after - rho_before)
max_redispatch = min(max_redispatch, ratio)
Scoring function: An asymmetric bell curve based on a Beta(3.0, 1.5) kernel, normalized so the peak equals 1 and occurs at 80% of the [min, max] range (i.e., closer to max_redispatch):
x = (observed_flow - min_redispatch) / (max_redispatch - min_redispatch)
If 0 <= x <= 1: score = Beta_kernel(x; alpha=3.0, beta=1.5) / peak_value
If x < 0: score = -2.0 * x^2 (quadratic penalty)
If x > 1: score = -2.0 * (x - 1)^2 (quadratic penalty)
The score is positive when the disconnection relieves the right amount of flow, with higher scores for actions closer to the maximum useful redispatch. It becomes negative when the redispatch is too small (ineffective) or too large (would create new overloads).
Node Splitting Score (open coupling -- weighted repulsion)
Node splitting candidates are substations that are either hubs of the overflow graph or lie on the constrained path. The scoring uses AlphaDeesp to evaluate how well splitting a substation into two buses separates opposing flows.
The score is based on the weighted repulsion of flows on the bus of interest:
TotalFlow = NegativeInflow + NegativeOutflow + PositiveInflow + PositiveOutflow
For upstream (amont) nodes:
Repulsion = NegativeOutflow - PositiveOutflow
WeightFactor = (NegativeOutflow - OtherFlows) / TotalFlow
For downstream (aval) nodes:
Repulsion = NegativeInflow - PositiveInflow
WeightFactor = (NegativeInflow - OtherFlows) / TotalFlow
Score = WeightFactor * Repulsion
A higher score indicates a better separation of the overload-relieving (negative/red) flows from the overload-aggravating (positive/green) flows.
Node Merging Score (close coupling -- delta phase)
Filtering: Only substations that lie on loop dispatch paths (red loops) and currently have 2 or more connected buses are candidates. They are further filtered by requiring a minimum dispatch flow at the node (at least 10% of the global max dispatch flow) to ensure the merge would have a significant impact on the overload.
Scoring: The score is the delta phase (voltage angle difference) between the two buses being merged:
score = theta2 - theta1
where theta1 is the voltage angle of the bus connected to the red loop (identified as the bus carrying more negative/overload-relieving dispatch flow on the overflow graph), and theta2 is the voltage angle of the other bus. A positive score means flows would naturally go from the higher-phase bus towards the red loop bus, which is the desired direction to relieve overloads.
Dependencies
This project relies on several external libraries, including:
numpypandasnetworkxpypowsyblgrid2op(Ensure you have a compatible version installed)alphaDeesp(Ensure this library is installed in your environment)expertop4grid>=0.2.8
See pyproject.toml for the full list.
Testing
To run the unit and integration tests, navigate to the project root and use pytest:
pytest
Note: Some integration tests (@pytest.mark.slow) require the Grid2Op environment data to be present and may take longer to run.
License
This project is licensed under the Mozilla Public License 2.0 (MPL 2.0). See the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file expert_op4grid_recommender-0.1.6.tar.gz.
File metadata
- Download URL: expert_op4grid_recommender-0.1.6.tar.gz
- Upload date:
- Size: 238.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fd137e0b61c7cc448016840b10e4a6be64a90bf0c2ab0bc71a43a717c789e8f5
|
|
| MD5 |
7f1cd6b7647dcd236a725c83017b01f7
|
|
| BLAKE2b-256 |
ea61504eec4394d2144b37b9d2ac90864e43f1564e2a71e56fa069ab8eabe09b
|
Provenance
The following attestation bundles were made for expert_op4grid_recommender-0.1.6.tar.gz:
Publisher:
python-publish.yml on marota/Expert_op4grid_recommender
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
expert_op4grid_recommender-0.1.6.tar.gz -
Subject digest:
fd137e0b61c7cc448016840b10e4a6be64a90bf0c2ab0bc71a43a717c789e8f5 - Sigstore transparency entry: 1075999627
- Sigstore integration time:
-
Permalink:
marota/Expert_op4grid_recommender@186a851c4e16c40cc6b3632d3b2b7d8f8ef14170 -
Branch / Tag:
refs/tags/v0.1.6 - Owner: https://github.com/marota
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@186a851c4e16c40cc6b3632d3b2b7d8f8ef14170 -
Trigger Event:
release
-
Statement type:
File details
Details for the file expert_op4grid_recommender-0.1.6-py3-none-any.whl.
File metadata
- Download URL: expert_op4grid_recommender-0.1.6-py3-none-any.whl
- Upload date:
- Size: 182.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a573666f636b7d5c24eb7fa804cde13c41d2c66c30d5cd182c5e18662a666db1
|
|
| MD5 |
33f233cafc4b31a2d359122bcfe8acfe
|
|
| BLAKE2b-256 |
815d29acb930fa046f5c627c6e32ea750ad02415028a64b09580770a27fb3d37
|
Provenance
The following attestation bundles were made for expert_op4grid_recommender-0.1.6-py3-none-any.whl:
Publisher:
python-publish.yml on marota/Expert_op4grid_recommender
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
expert_op4grid_recommender-0.1.6-py3-none-any.whl -
Subject digest:
a573666f636b7d5c24eb7fa804cde13c41d2c66c30d5cd182c5e18662a666db1 - Sigstore transparency entry: 1075999635
- Sigstore integration time:
-
Permalink:
marota/Expert_op4grid_recommender@186a851c4e16c40cc6b3632d3b2b7d8f8ef14170 -
Branch / Tag:
refs/tags/v0.1.6 - Owner: https://github.com/marota
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@186a851c4e16c40cc6b3632d3b2b7d8f8ef14170 -
Trigger Event:
release
-
Statement type: