Welcome to ExpertOp4Grid’s documentation!¶
Mentions¶
Quick Overview¶
This is an Expert System which tries to solve a security issue on a power grid, that is on overload over a power line, when it happens. It uses cheap but non-linear topological actions to do so, and does not require any training. For any new overloaded situations, it computes an influence graph around the overload of interest, and rank the substations and topologies to explore, to find a solution. It simulates the top ranked topologies to eventually give a score of success:
4 - it solves all overloads, 3 - it solves only the overload of interest 2 - it partially solves the overload of interest 1 - it solves the overload of interest but worsen other overloads 0 - it fails. The expert agent is based It is an implementation of the paper: “Expert system for topological action discovery in smart grids” - https://hal.archives-ouvertes.fr/hal-01897931/file/_LARGE__bf_Expert_System_for_topological_remedial_action_discovery_in_smart_grids.pdf
Influence Graph example for overloaded line 4->5. The electrical paths highlighted there will help us identify interesting topologies to reroute the flows.
Features¶
Analyse a power network when a line is in overflow
Run simulations to understand the network constraints
Return a ranking of topological actions that would solve the overflow, or reduce it
If ran manually (through command line), can also output a series of graph to help visualise the state of the network
Contribute¶
Issue Tracker: https://github.com/marota/ExpertOp4Grid/issues
Source Code: https://github.com/marota/ExpertOp4Grid
Support¶
If you are having issues, please let us know. We have a discord located at: $discordlink
License¶
Copyright 2019-2020 RTE France
This Source Code is subject to the terms of the Mozilla Public License (MPL) v2.
Installation¶
To install ExpertOp4Grid and AlphaDeesp execute the following lines:
1. (Optional)(Recommended) if you want to run in manual mode, install graphviz¶
This is for neato package, it allows to transform a dot file into a pdf file.
Warning: It is important to install graphviz executables before python packages
First install executable
On Linux
apt-get install graphviz
On Windows, use package finder (equivalent of apt-get on Windows)
winget install graphviz
Then ensure that graphviz and neato are in the path. You often have to set it manually. For example on windows you can use the following command line:
setx /M path "%path%;'C:\Users\username\graphviz-2.38\release\bin"
Then you can move to python packages installation
2. Install the package from Pypi¶
pip install ExpertOp4Grid
3. (Optional) If you want to run simulation with pypownet instead of Grid2op:¶
Clone pypownet somewhere else :
cd ..
git clone https://github.com/MarvinLer/pypownet.git
Install from within that folder:
python setup.py install --user
or
cd ExpertOp4Grid
pipenv shell
cd ../pypownet
python setup.py install
4. (Optional) Compile and output the sphinx doc (this documentation)¶
Run
./docs/make.bat html
Getting Started¶
Manual Mode¶
To execute in manual mode, type:
expertop4grid -l 9 -s 0 -c 0 -t 0
- –ltc | -l int
Integer representing the line to cut. For the moment, only one line to cut is handled
- –snapshot | -s int
If 1, will generate plots of the different grid topologies managed by alphadeesp and store it in alphadeesp/ressources/output
- –chronicscenario | -c string
Name of the folder containing the chronic scenario to consider By default, the first available folder will be chosen
- –timestep | -t int
Integer representing the timestep number at which we want to run alphadeesp simulation
- –fileconfig | -f string
Path to .ini file that provides detailed configuration of the module. If none is provided, a default config.ini is provided in package
In any case, an end result dataframe is written in root folder.
If you run the same command with ‘-s 1’ to print the plots, you will indeed see that:
On the intial state, you had an overflow to solve
The expert system indeed finds a solution topology for it at substation 4
See Algorithm Description section to learn more about the workflow and results.
In manual mode, further configuration is made through alphadeesp/config.ini
simulatorType - you can chose Grid2op or Pypownet
gridPath - path to folder containing files representing the grid. If no value is provided, a default grid will be loaded (l2rpn_2019) containing one chronic as a simple usecase example
outputPath - path to write outputs in case snapshot mode is activated. If no path is provided, ExpertOp4Grid will write image results in the current working directory (folder named output/grid/linetocut/scenario/timestep)
CustomLayout - list of couples reprenting coordinates of grid nodes. If not provided, grid2op will load grid_layout.json in grid folder
grid2opDifficulty - “0”, “1”, “2” or “competition”. Be careful: grid datasets should have a difficulty_levels.json
7 other constants for alphadeesp computation can be set in config.ini, with comments within the file
Agent Mode¶
To execute in agent mode, please refer to ExpertAgent available in l2rpn-baseline repository
https://github.com/mjothy/l2rpn-baselines/tree/mj-devs/l2rpn_baselines/ExpertAgent
Instead of configuring through config.ini, you can pass a similar python dictionary to the API
Tests¶
To launch the test suite in git repo:
pipenv run python -m pytest --verbose --continue-on-collection-errors -p no:warnings
Debug Help¶
To force specific hubs
in AlphaDeesp.compute_best_topo() function, one can force override the hubs result. Check in code, there are commented examples.
To force specific combinations for hubs
If one wants a specific hub, a user can “force” a specific node combination. Check in the code, there are commented examples
Description¶
Introduction¶
This module represents an expert agent that finds solutions to optimize a power network. The expert agent is based on a research paper (https://hal.archives-ouvertes.fr/hal-01897931/file/_LARGE__bf_Expert_System_for_topological_remedial_action_discovery_in_smart_grids.pdf)
Given a power grid and a line in overflow (referred as Line to cut) the expert agent will run simulations on the network and try to find and rank the best topological actions (changing elements of the graph from one bus to the other) to hopefuly solve the overflow.
Workflow overview¶
We can decompose the Expert System algorithm along those successive steps. Based on the overload distribution graphs it builds, it tries to identify relevant patterns in it described by expert knowledge, to eventually find good spots to reroute the flows. It ranks those substations apriori by relevance and then test them by simulation to get a final score of sucess. Notice that 2 of those steps involves running simulations: it indeed relies on a simulator backend to work.

Workflow implementation¶
The following picture shows an overview of the different inputs and outputs, and of the major modelisation steps.

Whether entering in manual mode or agent mode, different sets of inputs are provided.
Objects and steps in orange are specific to one given simulator. Two objects are manipulated as such
ObservationLoader object
Simulator object
See examples of implementation with Grid2op simulations and Pypownet simulations, in following scripts
alphaDeesp/core/grid2op/Grid2opObservationLoader.py
alphaDeesp/core/grid2op/Grid2opSimulation.py
alphaDeesp/core/pypownet/PypownetObservationLoader.py
alphaDeesp/core/pypownet/PypownetSimulation.py
It can be substituted by your favorite simulator if it provides the same interface methods and returns the same type of objects to be able to work with AlphaDeesp
Outputs of the process¶
When called, the main.py/expert_operator.py will return three objects :
ranked_combinations, expert_system_results, action
ranked_combinations¶
This dict contains all topological configurations per node, sorted from best to worst by the simulation.
expert_system_results¶
The main dataframe presenting quantitative information for all simulated topologies

Simulated flows on the target line (line to cut) before and after topological actions is operated. The delta flow is the difference between both of them
Worsened lines: new lines that got overloaded or initially overloaded lines which overload increased
Redispatched Prod: sum of all the production increase or decrease at each generator
Redispatched Load: difference between the total demand and the actual power supply in all loads (production - losses)
Internal Topology applied: topology list as used in AlphaDeesp. Represents the bus of each element at the substation (column Substation ID)
Topology applied: topology list as used in the simulator. Represents the bus of each element at the substation (column Substation ID)
Substation ID: ID of the substation on which the toology is applied
Topology score: quantitative score returned by Alphadeesp for this topology
Topology simulated score: integer score (from 0 to 4) given by the simulator when computing powerflow on the grid after applying the topology
Efficacity: flexible quantitative score to be returned by the simulator. It can for instance take into account the reward after applying the topological action
In manual mode, if option –snapshot is set to 1, plots of all simulated topos graphs are generated to dig into their consequences on the grid powerflow distribution. Plot names are meant to facilitate links between the snapshot and its correspondign line in the dataframe. See plots in the following didactic example.
action¶
This list contains all actions (generated by and for the chosen backend) that represent the topological states chosen by Alphadeesp. Sorted from best to worst, they are a syntactic sugar to give users a direct action to apply to their network. It is still recommended to parse the main dataframe to understand better what solutions are available.
Didactic example¶
We launch the expert operator in manual mode on the grid l2rpn_2019, with the Grid2opSimulator, on scenario a, at first timestep. There is an overflow on line 9 (between substation 4 and 5), so we provide ltc = 9. We want to see snapshots of the grid.
Command line
pipenv run python -m alphaDeesp.main -l 9 -s 0 -c 0 -t 0
Beginning of config.ini
Layout of the grid in its current state (also called g_pow)
The simulator will then compute several objects to provide to AlphaDeesp, which will run a greedy algorithm to determine the best topological action to solve the overload. For more details, see the section Algorithm Details
AlphaDeesp will then output a dataframe will all computed details about the best topologies found.

The topology surrounded in green has got a 4 simulated score. We can see on the corresponding snapshot that it has resolved the overflow on line 9 by connected two lines to bus 1 at substation 4, which has divided the power flow in amount of line 9
The topology surrounded in red has got a 0 simulated score. It does not resolve the power flow
The topology surrounded in orange has got a 1 simulated score. It does resolved the power flow on line 9 but created an other one on an other line
Important limitations¶
- For the moment, we allow cutting only one line when launching the expert system:
ex python3 -m alphaDeesp.main -l 9
The algorithm will only take the given timestep into account, meaning it will not try to learn from past or future behavior
Pypownet only Only works with initial state of all nodes with busbar == 0
Pypownet only At the moment, in the internal computation, a substation can have only one source of Power and one source of Consumption
AlphaDeesp algorithm details¶
Call¶
Calling the alphaDeesp engine is done like so :
alphadeesp = AlphaDeesp(g_over, df_of_g, custom_layout, printer, simulator_data,sim.substation_in_cooldown, debug = debug)
ranked_combinations = alphadeesp.get_ranked_combinations()
Alphadeesp hence gives you an oredered list of substations and topologies that should be relevant to solve your overload
Inputs¶
The following inputs will be required to be computed by the Simulation override.
g_over
A newtorkx graph representation of the grid with flow values
df_of_g
A dataframe representing a detailed view of the graph



custom_layout
The layout of the graph (list of (X,Y) coordinate for edges. Used for plotting.
printer
A printer service for logs and graphs
simulator_data
A dict composed of :
substations_elements
A local representation of the network from G_OVER (G_POW - G_POW_PRIME) using AlphaDeesp model objects from
network.py
Each PRODUCTION or CONSUMPTION has the value of G_POW (the initial values)
Each ORIGINLINE or EXTREMITYLINE has the values of flow(G_POW) - flow(G_POW_PRIME)
- {
- 0: [
PRODUCTION Object ID: 1, busbar_id: 0, value: 233.4587860107422 ,
ORIGINLINE Object ID: 1, busbar_id: 0, connected to substation: 1, flow_value: [4.16] ,
ORIGINLINE Object ID: 2, busbar_id: 0, connected to substation: 4, flow_value: [-1.27] ],
- 1: [
PRODUCTION Object ID: 2, busbar_id: 0, value: 40.0 ,
CONSUMPTION Object ID: 1, busbar_id: 0, value: 21.700000762939453 ,
ORIGINLINE Object ID: 3, busbar_id: 0, connected to substation: 2, flow_value: [2.48] ,
ORIGINLINE Object ID: 4, busbar_id: 0, connected to substation: 3, flow_value: [4.98] ,
ORIGINLINE Object ID: 5, busbar_id: 0, connected to substation: 4, flow_value: [-3.53] ,
EXTREMITYLINE Object ID: 1, busbar_id: 0, connected to substation: 0, flow_value: [4.16] ],
- 2: [
PRODUCTION Object ID: 3, busbar_id: 0, value: 0.0 ,
CONSUMPTION Object ID: 2, busbar_id: 0, value: 94.19999694824219 ,
EXTREMITYLINE Object ID: 2, busbar_id: 0, connected to substation: 3, flow_value: [-2.31] ,
EXTREMITYLINE Object ID: 3, busbar_id: 0, connected to substation: 1, flow_value: [2.48] ],
- 3: [
CONSUMPTION Object ID: 3, busbar_id: 0, value: 47.79999923706055 ,
EXTREMITYLINE Object ID: 4, busbar_id: 0, connected to substation: 4, flow_value: [35.49] ,
ORIGINLINE Object ID: 6, busbar_id: 0, connected to substation: 6, flow_value: [27.17] ,
ORIGINLINE Object ID: 7, busbar_id: 0, connected to substation: 8, flow_value: [15.38] ,
EXTREMITYLINE Object ID: 5, busbar_id: 0, connected to substation: 1, flow_value: [4.98] ,
ORIGINLINE Object ID: 8, busbar_id: 0, connected to substation: 2, flow_value: [-2.31] ],
- 4: [
CONSUMPTION Object ID: 4, busbar_id: 0, value: 7.599999904632568 ,
ORIGINLINE Object ID: 9, busbar_id: 0, connected to substation: 5, flow_value: [-40.77] ,
EXTREMITYLINE Object ID: 6, busbar_id: 0, connected to substation: 0, flow_value: [-1.27] ,
EXTREMITYLINE Object ID: 7, busbar_id: 0, connected to substation: 1, flow_value: [-3.53] ,
ORIGINLINE Object ID: 10, busbar_id: 0, connected to substation: 3, flow_value: [35.49] ],
- 5: [
PRODUCTION Object ID: 4, busbar_id: 0, value: 0.0 ,
CONSUMPTION Object ID: 5, busbar_id: 0, value: 11.199999809265137 ,
ORIGINLINE Object ID: 11, busbar_id: 0, connected to substation: 12, flow_value: [-12.76] ,
ORIGINLINE Object ID: 12, busbar_id: 0, connected to substation: 11, flow_value: [-2.99] ,
EXTREMITYLINE Object ID: 8, busbar_id: 0, connected to substation: 10, flow_value: [25.01] ,
EXTREMITYLINE Object ID: 9, busbar_id: 0, connected to substation: 4, flow_value: [-40.77] ],
- 6: [
EXTREMITYLINE Object ID: 10, busbar_id: 0, connected to substation: 7, flow_value: [0.0] ,
ORIGINLINE Object ID: 13, busbar_id: 0, connected to substation: 8, flow_value: [27.17] ,
EXTREMITYLINE Object ID: 11, busbar_id: 0, connected to substation: 3, flow_value: [27.17] ],
- 7: [
PRODUCTION Object ID: 5, busbar_id: 0, value: 0.0 ,
ORIGINLINE Object ID: 14, busbar_id: 0, connected to substation: 6, flow_value: [0.0] ],
- 8: [
CONSUMPTION Object ID: 6, busbar_id: 0, value: 29.5 ,
ORIGINLINE Object ID: 15, busbar_id: 0, connected to substation: 13, flow_value: [16.49] ,
ORIGINLINE Object ID: 16, busbar_id: 0, connected to substation: 9, flow_value: [26.06] ,
EXTREMITYLINE Object ID: 12, busbar_id: 0, connected to substation: 3, flow_value: [15.38] ,
EXTREMITYLINE Object ID: 13, busbar_id: 0, connected to substation: 6, flow_value: [27.17] ],
- 9: [
CONSUMPTION Object ID: 7, busbar_id: 0, value: 9.0 ,
ORIGINLINE Object ID: 17, busbar_id: 0, connected to substation: 10, flow_value: [25.79] ,
EXTREMITYLINE Object ID: 14, busbar_id: 0, connected to substation: 8, flow_value: [26.06] ],
- 10: [
CONSUMPTION Object ID: 8, busbar_id: 0, value: 3.5 ,
ORIGINLINE Object ID: 18, busbar_id: 0, connected to substation: 5, flow_value: [25.01] ,
EXTREMITYLINE Object ID: 15, busbar_id: 0, connected to substation: 9, flow_value: [25.79] ],
- 11: [
CONSUMPTION Object ID: 9, busbar_id: 0, value: 6.099999904632568 ,
EXTREMITYLINE Object ID: 16, busbar_id: 0, connected to substation: 12, flow_value: [2.96] ,
EXTREMITYLINE Object ID: 17, busbar_id: 0, connected to substation: 5, flow_value: [-2.99] ],
- 12: [
CONSUMPTION Object ID: 10, busbar_id: 0, value: 13.5 ,
EXTREMITYLINE Object ID: 18, busbar_id: 0, connected to substation: 13, flow_value: [15.6] ,
EXTREMITYLINE Object ID: 19, busbar_id: 0, connected to substation: 5, flow_value: [-12.76] ,
ORIGINLINE Object ID: 19, busbar_id: 0, connected to substation: 11, flow_value: [2.96] ],
- 13: [
CONSUMPTION Object ID: 11, busbar_id: 0, value: 14.899999618530273 ,
EXTREMITYLINE Object ID: 20, busbar_id: 0, connected to substation: 8, flow_value: [16.49] ,
ORIGINLINE Object ID: 20, busbar_id: 0, connected to substation: 12, flow_value: [15.6] ]
}
substation_to_node_mapping
internal_to_external_mapping
A dict linking the substation ids from substations_elements (internal) to the observation substations (external)
substation_in_cooldown
List of substation that are in cooldown
debug
Boolean flag for debugging purposes
Outputs¶
The alphaDeesp object then provides a list : ranked_combinations
This is a list of dataframes with the following columns :
score
the score of the topology from 0(worst) to 4(best)
topology
An array of integers (bus_ids) showing the topology of a node
node
The node on which the topology was applied
Simulating AlphaDeesp suggestions¶
This ranked_combinations list is then used to simulate all topologies with the Simulation override :
expert_system_results, actions = sim.compute_new_network_changes(ranked_combinations)
You eventually know which selected topologies are indeed successful.
Last Note¶
AlphaDeesp substation and topology rankings could be improved to make the selection of actions always more relevant and efficient.