Skip to content

Commit

Permalink
Merge pull request #609 from BDonnot/master
Browse files Browse the repository at this point in the history
Upgrade to 1.10.2.dev3
  • Loading branch information
BDonnot authored May 24, 2024
2 parents 3b0b687 + 0feefd9 commit 0183478
Show file tree
Hide file tree
Showing 71 changed files with 1,520 additions and 273 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,8 @@ grid2op/tests/20240429_failed_tests_small.txt
grid2op/tests/20240429_teq_test.txt
grid2op/tests/req_38_np121
test_make_2_envs.py
getting_started/env_py38_grid2op110_ray110.ipynb
getting_started/env_py38_grid2op110_ray210.ipynb

# profiling files
**.prof
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ Change Log
and `FromChronix2grid` are not supported at the moment.
- [ADDED] an "Handler" (`JSONInitStateHandler`) that can set the grid to an initial state (so as to make
compatible the `FromHandlers` time series class with this new feature)
- [ADDED] some more type hints in the `GridObject` class
- [ADDED] Possibility to deactive the support of shunts if subclassing `PandaPowerBackend`
(and add some basic tests)
- [FIXED] a small issue that could lead to having
"redispatching_unit_commitment_availble" flag set even if the redispatching
data was not loaded correctly
Expand All @@ -68,6 +71,8 @@ Change Log
and most of the time incorrectly)
- [FIXED] on `RemoteEnv` class (impact all multi process environment): the kwargs used to build then backend
where not used which could lead to"wrong" backends being used in the sub processes.
- [FIXED] a bug when the name of the times series and the names of the elements in the backend were
different: it was not possible to set `names_chronics_to_grid` correctly when calling `env.make`
- [IMPROVED] documentation about `obs.simulate` to make it clearer the
difference between env.step and obs.simulate on some cases
- [IMPROVED] type hints on some methods of `GridObjects`
Expand All @@ -79,6 +84,8 @@ Change Log
- [IMPROVED] the way the "grid2op compat" mode is handled
- [IMPROVED] the coverage of the tests in the "test_basic_env_ls.py" to test more in depth lightsim2grid
(creation of multiple environments, grid2op compatibility mode)
- [IMPROVED] the function to test the backend interface in case when shunts are not supported
(improved test `AAATestBackendAPI.test_01load_grid`)

[1.10.1] - 2024-03-xx
----------------------
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
author = 'Benjamin Donnot'

# The full version, including alpha/beta/rc tags
release = '1.10.2.dev2'
release = '1.10.2.dev3'
version = '1.10'


Expand Down
61 changes: 59 additions & 2 deletions docs/environment.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,58 @@ increase the training time, especially at the beginning. This is due to the fact
`env.reset` is called, the whole chronics is read from the hard drive. If you want to lower this
impact then you might consult the :ref:`environment-module-data-pipeline` page of the doc.

Go to the next scenario
++++++++++++++++++++++++

Starting grid2op 1.9.8 we attempt to make an easier user experience in the
selection of time series, seed, initial state of the grid, etc.

All of the above can be done when calling `env.reset()` function.

For customizing the seed, you can for example do:

.. code-block:: python
import grid2op
env_name = "l2rpn_case14_sandbox"
env = grid2op.make(env_name)
obs = env.reset(seed=0)
For customizing the time series id you want to use:

.. code-block:: python
import grid2op
env_name = "l2rpn_case14_sandbox"
env = grid2op.make(env_name)
obs = env.reset(options={"time serie id": 1}) # time serie by id (sorted alphabetically)
# or
obs = env.reset(options={"time serie id": "0001"}) # time serie by name (folder name)
For customizing the initial state of the grid, for example forcing the
powerline 0 to be disconnected in the initial observation:

.. code-block:: python
import grid2op
env_name = "l2rpn_case14_sandbox"
env = grid2op.make(env_name)
init_state_dict = {"set_line_status": [(0, -1)]}
obs = env.reset(options={"init state": init_state_dict})
Feel free to consult the documentation of the :func:`Environment.reset` function
for more information (this doc might be outdated, the one of the function should
be more up to date with the code).

.. info::
In the near future (next few releases) we will also attempt to make the
customization of the `parameters` or the `skip number of steps`, `maximum duration
of the scenarios` also available in `env.reset()` options.

.. _environment-module-chronics-info:

Time series Customization
Expand Down Expand Up @@ -141,10 +193,15 @@ the call to "env.reset". This gives the following code:
# and now the loop starts
for i in range(episode_count):
###################################
env.set_id(THE_CHRONIC_ID)
# with recent grid2op
obs = env.reset(options={"time serie id": THE_CHRONIC_ID})
###################################
obs = env.reset()
###################################
# 'old method (oldest grid2op version)'
# env.set_id(THE_CHRONIC_ID)
# obs = env.reset()
###################################
# now play the episode as usual
while True:
Expand Down
77 changes: 48 additions & 29 deletions getting_started/11_IntegrationWithExistingRLFrameworks.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,37 @@
"\n",
"More information are provided here: https://grid2op.readthedocs.io/en/latest/environment.html#splitting-into-raining-validation-test-scenarios\n",
"\n",
"### Use the `experimental_read_from_local_dir` flag\n",
"\n",
"This flag allows python to better \"understands\" the classes in grid2op and avoid lots of issue with pickle / multi processing etc.\n",
"\n",
"The complete documentation is available here https://grid2op.readthedocs.io/en/latest/environment.html#grid2op.Environment.BaseEnv.generate_classes\n",
"\n",
"Basically, once, and only once, outside of this process, you can call:\n",
"\n",
"```python\n",
"import grid2op\n",
"env_name = \"l2rpn_case14_sandbox\" # or any other name\n",
"\n",
"env = grid2op.make(env_name, ...) # again: redo this step each time you customize \"...\"\n",
"# for example if you change the `action_class` or the `backend` etc.\n",
"\n",
"env.generate_classes()\n",
"```\n",
"\n",
"Then, each time you want to reload the same environment, you can do:\n",
"\n",
"```python\n",
"import grid2op\n",
"env_name = SAME NAME AS ABOVE\n",
"env = grid2op.make(env_name,\n",
" experimental_read_from_local_dir=True,\n",
" ..., # SAME ENV CUSTOMIZATION AS ABOVE\n",
" )\n",
"```\n",
"\n",
"This is known to solve bug related to multi processing, but also to reduce the amount of RAM taken (in some cases) as well as creation time (in some cases)\n",
"\n",
"### Other steps\n",
"\n",
"The grid2op documentation is full of details to \"optimize\" the number of steps you can do per seconds. This number can rise from a few dozen per seconds to around a thousands per seconds with proper care.\n",
Expand Down Expand Up @@ -190,25 +221,13 @@
"metadata": {},
"outputs": [],
"source": [
"import gym\n",
"import gymnasium\n",
"import numpy as np\n",
"from grid2op.gym_compat import GymEnv\n",
"env_gym_init = GymEnv(env_glop)\n",
"env_gym = GymEnv(env_glop)\n",
"print(f\"The \\\"env_gym\\\" is a gym environment: {isinstance(env_gym, gym.Env)}\")\n",
"obs_gym = env_gym.reset()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=\"3\" color=\"red\"> In this notebook, we only present some basic (and really \"detailed\" use of the `GymEnv`). </font>\n",
" \n",
"<font size=\"3\" color=\"red\"> This is especially suited for advanced users wanting a deep control over everything happening. </font>\n",
"\n",
"<font size=\"3\" color=\"red\"> For a less advanced usage, feel free to consult the l2rpn baselines package, that embed some usefull environments, compatible with gym, that can embed some heuristics and other \"quality of life\" features. Feel free to use the l2rpn baselines package for more information.\n",
"</font>"
"print(f\"The \\\"env_gym\\\" is a gym environment: {isinstance(env_gym, gymnasium.Env)}\")\n",
"obs_gym, info = env_gym.reset()"
]
},
{
Expand Down Expand Up @@ -388,7 +407,7 @@
"outputs": [],
"source": [
"from grid2op.gym_compat import ScalerAttrConverter\n",
"from gym.spaces import Box\n",
"from gymnasium.spaces import Box\n",
"ob_space = env_gym.observation_space\n",
"ob_space = ob_space.reencode_space(\"actual_dispatch\",\n",
" ScalerAttrConverter(substract=0.,\n",
Expand Down Expand Up @@ -518,11 +537,11 @@
"source": [
"# gym specific, we simply do a copy paste of what we did in the previous cells, wrapping it in the\n",
"# MyEnv class, and train a Proximal Policy Optimisation based agent\n",
"import gym\n",
"import gymnasium\n",
"import ray\n",
"import numpy as np\n",
" \n",
"class MyEnv(gym.Env):\n",
"class MyEnv(gymnasium.Env):\n",
" def __init__(self, env_config):\n",
" import grid2op\n",
" from grid2op.gym_compat import GymEnv\n",
Expand All @@ -537,7 +556,7 @@
"\n",
" # 2. create the gym environment\n",
" self.env_gym = GymEnv(self.env_glop)\n",
" obs_gym = self.env_gym.reset()\n",
" obs_gym, info = self.env_gym.reset()\n",
"\n",
" # 3. (optional) customize it (see section above for more information)\n",
" ## customize action space\n",
Expand Down Expand Up @@ -576,9 +595,9 @@
" # 4. bis: to avoid other type of issues, we recommend to build the action space and observation\n",
" # space directly from the spaces class.\n",
" d = {k: v for k, v in self.env_gym.observation_space.spaces.items()}\n",
" self.observation_space = gym.spaces.Dict(d)\n",
" self.observation_space = gymnasium.spaces.Dict(d)\n",
" a = {k: v for k, v in self.env_gym.action_space.items()}\n",
" self.action_space = gym.spaces.Dict(a)\n",
" self.action_space = gymnasium.spaces.Dict(a)\n",
"\n",
" def reset(self):\n",
" obs = self.env_gym.reset()\n",
Expand Down Expand Up @@ -791,7 +810,7 @@
" )\n",
" }\n",
" )\n",
"obs_gym = env_sb.reset()"
"obs_gym, info = env_sb.reset()"
]
},
{
Expand Down Expand Up @@ -877,13 +896,13 @@
"outputs": [],
"source": [
"from grid2op.gym_compat import BoxGymActSpace\n",
"scale_gen = env_sb.init_env.gen_max_ramp_up + env_sb.init_env.gen_max_ramp_down\n",
"scale_gen[~env_sb.init_env.gen_redispatchable] = 1.0\n",
"scaler_gen = env_sb.init_env.gen_max_ramp_up + env_sb.init_env.gen_max_ramp_down\n",
"scaler_gen = scaler_gen[env_sb.init_env.gen_redispatchable]\n",
"env_sb.action_space = BoxGymActSpace(env_sb.init_env.action_space,\n",
" attr_to_keep=[\"redispatch\"],\n",
" multiply={\"redispatch\": scale_gen},\n",
" multiply={\"redispatch\": scaler_gen},\n",
" )\n",
"obs_gym = env_sb.reset()"
"obs_gym, info = env_sb.reset()"
]
},
{
Expand Down Expand Up @@ -937,7 +956,7 @@
"reencoded_act_space = MultiDiscreteActSpace(env_sb.init_env.action_space,\n",
" attr_to_keep=[\"set_line_status\", \"set_bus\", \"redispatch\"])\n",
"env_sb.action_space = reencoded_act_space\n",
"obs_gym = env_sb.reset()"
"obs_gym, info = env_sb.reset()"
]
},
{
Expand Down Expand Up @@ -1041,7 +1060,7 @@
" )\n",
" }\n",
" )\n",
"obs_gym = env_tfa.reset()"
"obs_gym, info = env_tfa.reset()"
]
},
{
Expand Down Expand Up @@ -1297,7 +1316,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
"version": "3.10.13"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 0183478

Please sign in to comment.