Skip to content

Commit

Permalink
Merge pull request #526 from BDonnot/bd_dev
Browse files Browse the repository at this point in the history
Bd dev
  • Loading branch information
BDonnot authored Sep 15, 2023
2 parents e2eacfb + 5ee73ab commit acb4c4d
Show file tree
Hide file tree
Showing 13 changed files with 967 additions and 271 deletions.
3 changes: 2 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: 2.1
executors:
grid2op-executor:
docker:
- image: bdonnot/grid2op:test
- image: python:3.10-buster
working_directory: /Grid2Op

python37:
Expand All @@ -29,6 +29,7 @@ jobs:
parallelism: 4
steps:
- checkout
- run: apt-get update -y
- run: apt-get install -y coinor-cbc
- run: python -m pip install virtualenv
- run: python -m virtualenv venv_test
Expand Down
7 changes: 5 additions & 2 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ Change Log

[1.9.5] - 2023-xx-yy
---------------------
- XXX

- [FIXED] issue https://github.com/rte-france/Grid2Op/issues/518
- [FIXED] issue https://github.com/rte-france/Grid2Op/issues/446
- [FIXED] issue https://github.com/rte-france/Grid2Op/issues/523 by having a "_BackendAction" folder instead of a file
- [FIXED] issue https://github.com/rte-france/Grid2Op/issues/522 and adding back certain notebooks to the CI
- [FIXED] and issue when disconnecting loads / generators on msot recent pandas version

[1.9.4] - 2023-09-04
---------------------
Expand Down
2 changes: 1 addition & 1 deletion getting_started/01_Grid2opFramework.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
"\n",
"custom_params = Parameters()\n",
"custom_params.MAX_SUB_CHANGED = 1\n",
"env = grid2op.make(\"rte_case14_redisp\", param=custom_params, test=True)"
"env = grid2op.make(\"l2rpn_case14_sandbox\", param=custom_params, test=True)"
]
},
{
Expand Down
768 changes: 671 additions & 97 deletions getting_started/02_Observation.ipynb

Large diffs are not rendered by default.

11 changes: 5 additions & 6 deletions getting_started/04_TrainingAnAgent.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@
"source": [
"from grid2op.gym_compat import GymEnv\n",
"import grid2op\n",
"from gym import Env\n",
"from gym.utils.env_checker import check_env\n",
"from gymnasium import Env\n",
"from gymnasium.utils.env_checker import check_env\n",
"try:\n",
" from lightsim2grid import LightSimBackend\n",
" bk_cls = LightSimBackend\n",
Expand Down Expand Up @@ -329,7 +329,7 @@
"metadata": {},
"outputs": [],
"source": [
"obs, reward, done, info = gym_env.step(0) # perform action labeled 0\n",
"obs, reward, terminated, truncated, info = gym_env.step(0) # perform action labeled 0\n",
"obs"
]
},
Expand All @@ -339,7 +339,7 @@
"metadata": {},
"outputs": [],
"source": [
"obs, reward, done, info = gym_env.step(53) # perform action labeled 53\n",
"obs, reward, terminated, truncated, info = gym_env.step(53) # perform action labeled 53\n",
"obs"
]
},
Expand Down Expand Up @@ -528,7 +528,6 @@
"metadata": {},
"outputs": [],
"source": [
"from l2rpn_baselines.DuelQSimple import evaluate\n",
"import shutil\n",
"from tqdm.notebook import tqdm\n",
"from grid2op.Runner import Runner\n",
Expand Down Expand Up @@ -721,7 +720,7 @@
" if not act_as_vect in actions_count:\n",
" actions_count[act_as_vect] = 0\n",
" actions_count[act_as_vect] += 1\n",
"print(\"The agent did {} different valid actions:\\n\".format(len(actions_count)))"
"print(\"The agent did {} different valid actions.\".format(len(actions_count)))"
]
},
{
Expand Down
157 changes: 1 addition & 156 deletions getting_started/07_MultiEnv.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### III.b) Practical example\n",
"### III.b) Practical example (work in progress)\n",
"\n",
"We reuse the code of the Notebook [04_TrainingAnAgent](04_TrainingAnAgent.ipynb) to train a new agent we strongly recommend you to have a look at it if it is not done already.\n",
"\n",
Expand All @@ -284,163 +284,8 @@
"\n",
"This is especially suited in the case of powersystem operations, as it can be quite computationnally expensive to solve for the powerflow equations at each nodes of the grid (also called [Kirchhoff's laws](https://en.wikipedia.org/wiki/Kirchhoff%27s_circuit_laws))\n",
"\n",
"#### What you have to do\n",
"\n",
"We recall here the code that we used in the relevant notebook to train the agent:\n",
"```python\n",
"# create an environment\n",
"env = make(env_name, test=True) \n",
"# don't forget to set \"test=False\" (or remove it, as False is the default value) for \"real\" training\n",
"\n",
"# import the train function and train your agent\n",
"from l2rpn_baselines.DuelQSimple import train\n",
"from l2rpn_baselines.utils import NNParam, TrainingParam\n",
"agent_name = \"test_agent\"\n",
"save_path = \"saved_agent_DDDQN_{}\".format(train_iter)\n",
"logs_dir=\"tf_logs_DDDQN\"\n",
"\n",
"\n",
"# we then define the neural network we want to make (you may change this at will)\n",
"## 1. first we choose what \"part\" of the observation we want as input, \n",
"## here for example only the generator and load information\n",
"## see https://grid2op.readthedocs.io/en/latest/observation.html#main-observation-attributes\n",
"## for the detailed about all the observation attributes you want to have\n",
"li_attr_obs_X = [\"gen_p\", \"gen_v\", \"load_p\", \"load_q\"]\n",
"# this automatically computes the size of the resulting vector\n",
"observation_size = NNParam.get_obs_size(env, li_attr_obs_X) \n",
"\n",
"## 2. then we define its architecture\n",
"sizes = [300, 300, 300] # 3 hidden layers, of 300 units each, why not...\n",
"activs = [\"relu\" for _ in sizes] # all followed by relu activation, because... why not\n",
"## 4. you put it all on a dictionnary like that (specific to this baseline)\n",
"kwargs_archi = {'observation_size': observation_size,\n",
" 'sizes': sizes,\n",
" 'activs': activs,\n",
" \"list_attr_obs\": li_attr_obs_X}\n",
"\n",
"# you can also change the training parameters you are using\n",
"# more information at https://l2rpn-baselines.readthedocs.io/en/latest/utils.html#l2rpn_baselines.utils.TrainingParam\n",
"tp = TrainingParam()\n",
"tp.batch_size = 32 # for example...\n",
"tp.update_tensorboard_freq = int(train_iter / 10)\n",
"tp.save_model_each = int(train_iter / 3)\n",
"tp.min_observation = int(train_iter / 5)\n",
"train(env,\n",
" name=agent_name,\n",
" iterations=train_iter,\n",
" save_path=save_path,\n",
" load_path=None, # put something else if you want to reload an agent instead of creating a new one\n",
" logs_dir=logs_dir,\n",
" kwargs_archi=kwargs_archi,\n",
" training_param=tp)\n",
"```\n",
"\n",
"Here, you will see in the next cell how to (*not really*) change it to train a agent on different environments:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"train_iter = TRAINING_STEP\n",
"# import the train function and train your agent\n",
"from l2rpn_baselines.DuelQSimple import train\n",
"from l2rpn_baselines.utils import NNParam, TrainingParam, make_multi_env\n",
"agent_name = \"test_agent_multi\"\n",
"save_path = \"saved_agent_DDDQN_{}_multi\".format(train_iter)\n",
"logs_dir=\"tf_logs_DDDQN\"\n",
"\n",
"# just add the relevant import (see above) and this line\n",
"my_envs = make_multi_env(env_init=env, nb_env=NUM_CORE)\n",
"# and that's it !\n",
"\n",
"\n",
"# we then define the neural network we want to make (you may change this at will)\n",
"## 1. first we choose what \"part\" of the observation we want as input, \n",
"## here for example only the generator and load information\n",
"## see https://grid2op.readthedocs.io/en/latest/observation.html#main-observation-attributes\n",
"## for the detailed about all the observation attributes you want to have\n",
"li_attr_obs_X = [\"gen_p\", \"gen_v\", \"load_p\", \"load_q\"]\n",
"# this automatically computes the size of the resulting vector\n",
"observation_size = NNParam.get_obs_size(env, li_attr_obs_X) \n",
"\n",
"## 2. then we define its architecture\n",
"sizes = [300, 300, 300] # 3 hidden layers, of 300 units each, why not...\n",
"activs = [\"relu\" for _ in sizes] # all followed by relu activation, because... why not\n",
"## 4. you put it all on a dictionnary like that (specific to this baseline)\n",
"kwargs_archi = {'observation_size': observation_size,\n",
" 'sizes': sizes,\n",
" 'activs': activs,\n",
" \"list_attr_obs\": li_attr_obs_X}\n",
"\n",
"# you can also change the training parameters you are using\n",
"# more information at https://l2rpn-baselines.readthedocs.io/en/latest/utils.html#l2rpn_baselines.utils.TrainingParam\n",
"tp = TrainingParam()\n",
"tp.batch_size = 32 # for example...\n",
"tp.update_tensorboard_freq = int(train_iter / 10)\n",
"tp.save_model_each = int(train_iter / 3)\n",
"tp.min_observation = int(train_iter / 5)\n",
"train(my_envs,\n",
" name=agent_name,\n",
" iterations=train_iter,\n",
" save_path=save_path,\n",
" load_path=None, # put something else if you want to reload an agent instead of creating a new one\n",
" logs_dir=logs_dir,\n",
" kwargs_archi=kwargs_archi,\n",
" training_param=tp)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### II c) Assess the performance of the trained agent"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Nothing is changing... Like really, it's the same code as in notebook 4 (we told you to have a look ;-) )"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from l2rpn_baselines.DuelQSimple import evaluate\n",
"path_save_results = \"{}_results_multi\".format(save_path)\n",
"\n",
"evaluated_agent, res_runner = evaluate(env,\n",
" name=agent_name,\n",
" load_path=save_path,\n",
" logs_path=path_save_results,\n",
" nb_episode=2,\n",
" nb_process=1,\n",
" max_steps=100,\n",
" verbose=True,\n",
" save_gif=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### II d) That is it ?\n",
"\n",
"Yes there is nothing more to say about it. As long as you use one of the compatible baselines (at the date of writing):\n",
"- [DeepQSimple](https://l2rpn-baselines.readthedocs.io/en/master/DeepQSimple.html)\n",
"- [DuelQSimple](https://l2rpn-baselines.readthedocs.io/en/master/DuelQSimple.html)\n",
"- [DuelQLeapNet](https://l2rpn-baselines.readthedocs.io/en/master/DuelQLeapNet.html)\n",
"\n",
"You do not have anything more to do :-)\n",
"\n",
"If you want to use another baseline that does not support this feature, feel free to add an issue in the l2rpn-baselines official github at this adress [https://github.com/rte-france/l2rpn-baselines/issues](https://github.com/rte-france/l2rpn-baselines/issues)"
]
}
],
"metadata": {
Expand Down
4 changes: 2 additions & 2 deletions getting_started/08_PlottingCapabilities.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@
"outputs": [],
"source": [
"from grid2op.Runner import Runner\n",
"env = grid2op.make(test=True)\n",
"env = grid2op.make(\"l2rpn_case14_sandbox\", test=True)\n",
"my_awesome_agent = CustomRandom(env.action_space)\n",
"runner = Runner(**env.get_params_for_runner(), agentClass=None, agentInstance=my_awesome_agent)"
]
Expand Down Expand Up @@ -501,7 +501,7 @@
"source": [
"# If you want to start grid2viz for better plotting, you may try the following :\n",
"# (remove the \"if False:\" or replace it with \"if True:\")\n",
"if True:\n",
"if False:\n",
" !$sys.executable -m grid2viz.main --agents_path $path_agents\n",
"else:\n",
" print(\"You need to copy paste the command in the cell above to run grid2viz\")"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# This file is part of Grid2Op, Grid2Op a testbed platform to model sequential decision making in power systems.

"""
This file is here to maintain compatibility with lightsim2grid (but will disappear "soon")
This folder / file is here to maintain compatibility with lightsim2grid (but is likely to disappear "soon")
"""

from grid2op.Action._backendAction import _BackendAction
from .._backendAction import _BackendAction
2 changes: 2 additions & 0 deletions grid2op/Action/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"TopologyChangeAction",
"TopologyChangeAndDispatchAction",
"DispatchAction",
"_BackendAction"
]

# Internals
Expand Down Expand Up @@ -49,3 +50,4 @@
TopologyChangeAndDispatchAction,
)
from grid2op.Action.dispatchAction import DispatchAction
import grid2op.Action._backendAction as _BackendAction
4 changes: 2 additions & 2 deletions grid2op/Backend/pandaPowerBackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ def _apply_load_bus(self, new_bus, id_el_backend, id_topo):
self._grid.load["in_service"].iat[id_el_backend] = True
else:
self._grid.load["in_service"].iat[id_el_backend] = False
self._grid.load["bus"].iat[id_el_backend] = -1
# self._grid.load["bus"].iat[id_el_backend] = -1 # not needed and cause bugs with certain pandas version

def _apply_gen_bus(self, new_bus, id_el_backend, id_topo):
new_bus_backend = type(self).local_bus_to_global_int(
Expand All @@ -919,7 +919,7 @@ def _apply_gen_bus(self, new_bus, id_el_backend, id_topo):
self._grid.ext_grid["bus"].iat[0] = new_bus_backend
else:
self._grid.gen["in_service"].iat[id_el_backend] = False
self._grid.gen["bus"].iat[id_el_backend] = -1
# self._grid.gen["bus"].iat[id_el_backend] = -1 # not needed and cause bugs with certain pandas version
# in this case the slack bus cannot be disconnected

def _apply_lor_bus(self, new_bus, id_el_backend, id_topo):
Expand Down
2 changes: 1 addition & 1 deletion grid2op/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
Grid2Op
"""
__version__ = '1.9.4'
__version__ = '1.9.5.dev0'

__all__ = [
"Action",
Expand Down
Loading

0 comments on commit acb4c4d

Please sign in to comment.