Expert Agent python package exploiting the expert knowledge in two ways:
- Focus the exploration phase of an RL agent (DeepQ) on specific zones of a power grid
- Reduce the action space to most relevant ones and improve the scalability or RL agents. There are two variants of this approach:
- Heuristic-based: A set of well known heuristics with some greedy search over the reduced action space is used to solve the overload and congestion problems;
- Learning-based: A PPO tries to learn the effective topological manipulations on the grid, by considering the reduced action space. Its combination with some heuristics helps to remedy most of the overload and congestion problems.
- Credits for Javaness winning solution at the L2RPN 2023 IDF AI challenge which has inspired the heuristic part of this work and most of the code is adapted and reused. The adapted code is on a forked repository which could be found here.
- Credits for CurriculumAgent, which has inspired the search for reduced action space. Herein, we have replaced the greedy search over all the action space, by those suggested using expert knowlege.
- The action suggested by expert knowledge uses the ExpertOp4Grid package.
To be able to run the experiments in this repository, the following steps show how to install this package and its dependencies from source.
conda create -n expert_agent python=3.10
conda activate venv_gnncd my-project-folder
pip3 install -U virtualenv
python3 -m virtualenv venv_expert_agent
source venv_expert_agent/bin/activateImportant
These steps are mandatory to be able to use the package and its different functionalities
git clone git@github.com:Mleyliabadi/ExpertOp4Grid.git
cd ExpertOp4Grid
pip install -U .git clone git@github.com:Mleyliabadi/l2rpn-2023-ljn-agent.git
cd l2rpn-2023-ljn-agent
pip install -U .
git clone git@github.com:AI4REALNET/grid2op-scenario.git
cd grid2op-scenario
cp -r ai4realnet_small /home/<USERNAME>/data_grid2op/.git clone git@github.com:AI4REALNET/T2.1_deep_expert.git
cd T2.1_deep_expert
pip3 install -U .[recommended]pip3 install -e .[recommended]The trained models are not stored in git: they are files attached to a GitHub release (see Pretrained models and local_data). The scripts and notebooks download them automatically the first time they need them. To download them in advance (for example before working offline):
python -m ExpertAgent.utils.local_data downloadπ ExpertAgent
βββ π configs
β βββ ...
βββ π getting_started
β βββββββ 0_extract_actions.ipynb
β βββββββ 1_apply_deepqexpert.ipynb
β βββββββ 2_apply_expert_agent_heuristic.ipynb
β βββββββ 3_apply_expert_agent_rl.ipynb
βββ π ExpertAgent
β βββ π assets
β βββββββ ...
β βββ π DeepQExpert
β βββββββ ...
β βββ π ExpertAgent
β βββββββ agentHeuristic.py
β βββββββ agentRL.py
β βββ π utils
β βββββββ extractExpertActions.py
β βββββββ extractAttackingExpertActions.py
β βββββββ local_data.py β(paths in local_data, download of the released models)
β βββββββ ...
βββ π local_data β(not in git: models, checkpoints, logs and results, see below)
βββ setup.py
Everything produced by an agent (model weights, training checkpoints, tensorboard logs and evaluation results) is written to a single local_data directory at the root of the repository, with one folder per environment, then per agent, then per run:
local_data/
βββ l2rpn_case14_sandbox/
β βββ DeepQExpert/
β βββ models-v1/ # the released model (downloaded)
β β βββ model/ # what the agent loads
β β βββ eval/ # results of evaluating it
β βββ run_20261127-153000/ # one of your own training runs
β βββ model/
β βββ logs/ # tensorboard
βββ ai4realnet_small/
β βββ PPO_SB3/
β β βββ models-v1/
β β β βββ model/PPO_SB3.zip
β β β βββ eval/
β β βββ run_<date-time>/
β β βββ model/ # final model, best model, evaluations.npz
β β βββ checkpoints/ # PPO_SB3_<n>_steps.zip
β β βββ logs/
β βββ ExpertHeuristic/
β β βββ run_<date-time>/eval/
β βββ ExpertActions/ # output of the expert action extraction scripts
βββ .downloads/ # the downloaded release files, kept as a cache
- A training run gets a new folder named
run_<date-time>, so it never overwrites an earlier one. The scripts print where they save.main_expert_rl_train.py,main_expert_rl_eval.pyandmain_expert_heuristic.pyaccept--run_nameto choose the name. local_datais ignored by git (see.gitignore), so nothing in it is committed by accident.- The inputs of the agents, such as the expert action spaces in
ExpertAgent/assets, are small and stay in git. - To put
local_datasomewhere else (a larger disk, a cluster scratch space, a Docker volume), set the environment variableEXPERTAGENT_LOCAL_DATAto the directory to use.
In code, the paths are built with the helpers of ExpertAgent.utils.local_data:
from ExpertAgent.utils.local_data import get_run_dir, get_release_model
# local_data/ai4realnet_small/PPO_SB3/my_run/model (created if needed)
save_path = get_run_dir("ai4realnet_small", "PPO_SB3", "my_run", "model")
# the released model: downloaded on first use, then only returns the path to give to agent.load
load_path = get_release_model("PPO_SB3", "ai4realnet_small")The models used by the scripts, the notebooks and the app are published as files ("assets") of a GitHub release of ainetus/T2.1_deep_expert, rather than committed to git. This keeps the repository small, and each model version is published once and never changes.
| Agent | Trained on | Release | File | Content |
|---|---|---|---|---|
| DeepQExpert | l2rpn_case14_sandbox |
models-v1 |
DeepQExpert_l2rpn_case14_sandbox_models-v1.zip |
Extended DeepQ agent, 1M training steps (27 MB) |
PPO_SB3 (ExpertAgentRL) |
ai4realnet_small |
models-v1 |
PPO_SB3_ai4realnet_small_models-v1.zip |
PPO policy used by ExpertAgentRL and the app (60 MB) |
The list of released models, with their checksums, is RELEASE_MODELS in ExpertAgent/utils/local_data.py. The code always uses the release named there.
How the download works
- The first call to
get_release_model(...), made by the scripts and notebooks, downloads the file intolocal_data/.downloads/<release>/. It checks its sha256 checksum, then extracts it intolocal_data/<env>/<agent>/<release>/model/. Later calls do not download anything. - The download can also be started by hand:
python -m ExpertAgent.utils.local_data list # released models, and whether they are downloaded python -m ExpertAgent.utils.local_data download # all of them python -m ExpertAgent.utils.local_data download PPO_SB3 # only one agent
- Private repository: downloading needs a GitHub token that can read the repository, in the environment variable
GITHUB_TOKEN(orGH_TOKEN). The token is only sent toapi.github.com:export GITHUB_TOKEN=<your token> python -m ExpertAgent.utils.local_data download
- Without internet access: download the zip files from the release page in a browser, put them in
local_data/.downloads/models-v1/, then runpython -m ExpertAgent.utils.local_data download. It checks them and extracts them without connecting to GitHub. - Another repository: to download from another repository (for example the fork), set
EXPERTAGENT_MODELS_REPO=<owner>/<name>. That repository needs a release with the same tag and the same files.
Docker: rather than copying a model folder into the image, download the model while building it, after the package is installed:
RUN python -m ExpertAgent.utils.local_data download PPO_SB3For a private repository, give the token as a build secret, so that it is not stored in the image:
RUN --mount=type=secret,id=github_token \
GITHUB_TOKEN=$(cat /run/secrets/github_token) python -m ExpertAgent.utils.local_data download PPO_SB3docker build --secret id=github_token,env=GITHUB_TOKEN -t expert-agent .A published release is never modified: replacing a file in it would make the copies already downloaded differ from new ones, and it would break the checksum the code checks. A new model always goes into a new release with a new tag (models-v2, models-v3, ...). Older releases stay available, so older versions of the code keep working.
- Train the model. It is saved in
local_data/<env>/<agent>/<run>/model/. - Build the release file. This prints its sha256 and the entry to put in
RELEASE_MODELS:The file is written to# DeepQExpert python -m ExpertAgent.utils.local_data package \ --model_dir local_data/l2rpn_case14_sandbox/DeepQExpert/<run>/model \ --agent DeepQExpert --env l2rpn_case14_sandbox --tag models-v2 # PPO_SB3 (the agent loads model/PPO_SB3.zip, hence --load_path) python -m ExpertAgent.utils.local_data package \ --model_dir local_data/ai4realnet_small/PPO_SB3/<run>/model \ --agent PPO_SB3 --env ai4realnet_small --tag models-v2 --load_path PPO_SB3
dist/(ignored by git). Building it twice from the same model gives the same file and checksum. - Create the release
models-v2on GitHub (Releases β Draft a new release, new tagmodels-v2) and attach the file. With the GitHub CLI this is:Put in the release notes how each model was trained (environment, number of steps, main parameters) and its evaluation results.gh release create models-v2 dist/*_models-v2.zip --repo ainetus/T2.1_deep_expert \ --title "Models v2" --notes "What changed and how the models were trained"
- Replace the entry of the model in
RELEASE_MODELSwith the printed one, test withpython -m ExpertAgent.utils.local_data download, and commit. A model that did not change keeps its existing entry, and its older release.
A set of jupyter notebooks are provided to ease the use of the package for the users. Here is the list of notebooks:
0. 00_action_extraction_explained.ipynb : It explains step by step how the reduced action spaces are extracted with the expert system (ExpertAgent/utils/extractExpertActions.py and extractAttackingExpertActions.py), and how the agents use them
- 01_deep_q_expert.ipynb : It shows how to use the extended DeepQ agent
- 02_expert_agent_heuistics.ipynb : It shows how to use the heuristic agent using the expert knowledge
- 03_expert_agent_RL.ipynb : It show how to use the RL based agent harnessing expert knowledge to reduce the action space.
This agent applies an extended DeepQ algorithm for power grids and specifically works good with l2rpn_case14_sandbox environment.
To train this agent, the following command could be executed from root and CLI:
python ExpertAgent/DeepQExpert/train.py \
--num_train_steps=1000000 \
--name="DeepQExpert"At the end of the training, the weights of the model and some information concerning the neural network architecture are saved in local_data/l2rpn_case14_sandbox/DeepQExpert/run_<date-time>/model, and the tensorboard logs in .../logs. --save_path and --logs_dir can still be given to save elsewhere.
To evaluate the released model (downloaded on first use), the following command could be executed from root and using CLI:
python ExpertAgent/DeepQExpert/evaluate.pyThe results are written to local_data/l2rpn_case14_sandbox/DeepQExpert/models-v1/eval. To evaluate one of your own runs instead, set load_path = get_run_dir(env_name, name, "<run_name>", "model") in the script.
At the end of the evaluation, a graphic representing the performance (reward/alive time) of the agent is visualized to the user.
The heuristic version of the ExpertAgent does not require any training and the evaluation could be run using a main function included in the root of the package. This agent is already provided to work for ai4realnet_small scenario of AI4REALNET project and power grid usecase (first).
python main_expert_heuristic.py --nb_episode=15 --nb_process=1 --max_step=2016 --verbose=True The results are written to local_data/ai4realnet_small/ExpertHeuristic/run_<date-time>/eval (or to --logs_dir).
At the end of the evaluation a graphic representing the performance (reward/alive time) of the agent is visualized to the user.
The RL based agent learning the reduced action space obtained using Expert Knowledge should be trained. The training could be launched using the corresponding main file provided in the root of the repository as:
python main_expert_rl_train.py --run_name=my_runThe run is saved in local_data/ai4realnet_small/PPO_SB3/my_run/: the final and best models in model/, the checkpoints in checkpoints/ and the tensorboard logs in logs/. Without --run_name, the folder is named run_<date-time>.
An already trained agent is also provided as a released model. It can be loaded easily as follows (see notebook for a full example):
from ExpertAgent.utils.local_data import get_release_model
from ExpertAgent.ExpertAgent import ExpertAgentRL
env, env_gym = create_env(...)
nn_kwargs = {...}
# downloaded on first use into local_data/ai4realnet_small/PPO_SB3/models-v1/model
load_path = get_release_model("PPO_SB3", "ai4realnet_small")
agent = ExpertAgentRL(name="PPO_SB3",
env=env,
action_space=env.action_space,
gymenv=env_gym,
gym_act_space=env_gym.action_space,
gym_obs_space=env_gym.observation_space,
nn_kwargs=nn_kwargs
)
agent.load(load_path)One the agent is trained or loaded, the evaluation could be done using the following command and the main file main_expert_rl_eval.py:
python main_expert_rl_eval.py --nb_episode=15 --nb_process=1 --max_step=2016 --verbose=True By default it evaluates the released model and writes the results to local_data/ai4realnet_small/PPO_SB3/models-v1/eval. Add --run_name=my_run to evaluate one of your own runs instead (results in local_data/ai4realnet_small/PPO_SB3/my_run/eval).
At the end of the evaluation a graphic representing the performance (reward/alive time) of the agent is visualized to the user.



