Skip to content

Commit 7b30f58

Browse files
authored
Merge pull request #643 from BDonnot/bd_dev
Fix gymnasium 1.0 compat
2 parents 4129cd3 + 9792478 commit 7b30f58

28 files changed

+287
-153
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ getting_started/venv_310_ray/
416416
grid2op/tests/venv_test_autoclass/
417417
test_eduardo.py
418418
grid2op/tests/failed_test*
419+
venv_312
419420

420421
# profiling files
421422
**.prof

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ Next release
7575
(wrong sign for the slack generator)
7676
- [FIXED] the environment would not load in case of an incorrect "layout.json"
7777
instead of raising a warning.
78+
- [FIXED] some issue with gym_compat module for "newest" version of
79+
gymnasium (1.0.0)
7880
- [IMPROVED] error message when forecasts are not correctly set-up
7981

8082
[1.10.3] - 2024-07-12

docs/gym.rst

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@ your base code.
2929

3030
More information on the section :ref:`gymnasium_gym`
3131

32-
Before grid2op 1.2.0 only some classes fully implemented the open AI gym interface:
32+
Before grid2op 1.2.0 only some classes fully implemented the gymnasium interface:
3333

3434
- the :class:`grid2op.Environment` (with methods such as `env.reset`, `env.step` etc.)
3535
- the :class:`grid2op.Agent` (with the `agent.act` etc.)
3636
- the creation of pre defined environments (with `grid2op.make`)
3737

3838

3939
Starting from 1.2.0 we implemented some automatic converters that are able to automatically map
40-
grid2op representation for the action space and the observation space into open AI gym "spaces". More precisely these
40+
grid2op representation for the action space and the observation space into gymnasium "spaces". More precisely these
4141
are represented as gym.spaces.Dict.
4242

43-
As of grid2op 1.4.0 we tighten the gap between openAI gym and grid2op by introducing the dedicated module
43+
As of grid2op 1.4.0 we tighten the gap between gymnasium and grid2op by introducing the dedicated module
4444
`grid2op.gym_compat` . Withing this module there are lots of functionalities to convert a grid2op environment
45-
into a gym environment (that inherit `gym.Env` instead of "simply" implementing the open ai gym interface).
45+
into a gymnasium environment (that inherit `gymnasium.Env` instead of "simply" implementing the gymnasium interface).
4646

4747

4848
A simple usage is:
@@ -55,12 +55,12 @@ A simple usage is:
5555
env_name = "l2rpn_case14_sandbox" # or any other grid2op environment name
5656
g2op_env = grid2op.make(env_name) # create the gri2op environment
5757
58-
gym_env = GymEnv(g2op_env) # create the gym environment
58+
gym_env = GymEnv(g2op_env) # create the gymnasium environment
5959
60-
# check that this is a properly defined gym environment:
60+
# check that this is a properly defined gymnasium environment:
6161
import gym
62-
print(f"Is gym_env and open AI gym environment: {isinstance(gym_env, gym.Env)}")
63-
# it shows "Is gym_env and open AI gym environment: True"
62+
print(f"Is gym_env a gymnasium environment: {isinstance(gym_env, gym.Env)}")
63+
# it shows "Is gym_env a gymnasium environment: True"
6464
6565
.. note::
6666

@@ -73,9 +73,9 @@ A simple usage is:
7373
.. warning::
7474
The `gym` package has some breaking API change since its version 0.26. We attempted,
7575
in grid2op, to maintain compatibility both with former versions and later ones. This makes **this
76-
class behave differently depending on the version of gym you have installed** !
76+
class behave differently depending on the version of gymnasium you have installed** !
7777

78-
The main changes involve the functions `env.step` and `env.reset` (core gym functions)
78+
The main changes involve the functions `env.step` and `env.reset` (core gymnasium functions)
7979

8080
This page is organized as follow:
8181

@@ -164,7 +164,7 @@ You can transform the observation space as you wish. There are some examples in
164164

165165
Default Action space
166166
******************************
167-
The default action space is also a type of gym Dict. As for the observation space above, it is a
167+
The default action space is also a type of gymnasium Dict. As for the observation space above, it is a
168168
straight translation from the attribute of the action to the key of the dictionary. This gives:
169169

170170
- "change_bus": MultiBinary(`env.dim_topo`)
@@ -177,7 +177,7 @@ straight translation from the attribute of the action to the key of the dictiona
177177
- "raise_alarm": MultiBinary(`env.dim_alarms`)
178178
- "raise_alert": MultiBinary(`env.dim_alerts`)
179179

180-
For example you can create a "gym action" (for the default encoding) like:
180+
For example you can create a "gymnasium action" (for the default encoding) like:
181181

182182
.. code-block:: python
183183
@@ -191,27 +191,27 @@ For example you can create a "gym action" (for the default encoding) like:
191191
gym_env = GymEnv(env)
192192
193193
seed = ...
194-
obs, info = gym_env.reset(seed) # for new gym interface
194+
obs, info = gym_env.reset(seed) # for new gymnasium interface
195195
196196
# do nothing
197197
gym_act = {}
198198
obs, reward, done, truncated, info = gym_env.step(gym_act)
199199
200200
#change the bus of the element 6 and 7 of the "topo_vect"
201201
gym_act = {}
202-
gym_act["change_bus"] = np.zeros(env.dim_topo, dtype=np.int8) # gym encoding of a multi binary
202+
gym_act["change_bus"] = np.zeros(env.dim_topo, dtype=np.int8) # gymnasium encoding of a multi binary
203203
gym_act["change_bus"][[6, 7]] = 1
204204
obs, reward, done, truncated, info = gym_env.step(gym_act)
205205
206206
# redispatch generator 2 of 1.7MW
207207
gym_act = {}
208-
gym_act["redispatch"] = np.zeros(env.n_gen, dtype=np.float32) # gym encoding of a Box
208+
gym_act["redispatch"] = np.zeros(env.n_gen, dtype=np.float32) # gymnasium encoding of a Box
209209
gym_act["redispatch"][2] = 1.7
210210
obs, reward, done, truncated, info = gym_env.step(gym_act)
211211
212212
# set the bus of element 8 and 9 to bus 2
213213
gym_act = {}
214-
gym_act["set_bus"] = np.zeros(env.dim_topo, dtype=int) # gym encoding of a Box
214+
gym_act["set_bus"] = np.zeros(env.dim_topo, dtype=int) # gymnasium encoding of a Box
215215
gym_act["set_bus"][[8, 9]] = 2
216216
obs, reward, done, truncated, info = gym_env.step(gym_act)
217217
@@ -238,7 +238,7 @@ If you want a full control on this spaces, you need to implement something like:
238238
env = grid2op.make(env_name)
239239
240240
from grid2op.gym_compat import GymEnv
241-
# this of course will not work... Replace "AGymSpace" with a normal gym space, like Dict, Box, MultiDiscrete etc.
241+
# this of course will not work... Replace "AGymSpace" with a normal gymnasium space, like Dict, Box, MultiDiscrete etc.
242242
from gym.spaces import AGymSpace
243243
gym_env = GymEnv(env)
244244
@@ -253,7 +253,7 @@ If you want a full control on this spaces, you need to implement something like:
253253
def to_gym(self, observation):
254254
# this is this very same function that you need to implement
255255
# it should have this exact name, take only one observation (grid2op) as input
256-
# and return a gym object that belong to your space "AGymSpace"
256+
# and return a gymnasium object that belong to your space "AGymSpace"
257257
return SomethingThatBelongTo_AGymSpace
258258
# eg. return np.concatenate((obs.gen_p * 0.1, np.sqrt(obs.load_p))
259259
@@ -268,7 +268,7 @@ And for the action space:
268268
env = grid2op.make(env_name)
269269
270270
from grid2op.gym_compat import GymEnv
271-
# this of course will not work... Replace "AGymSpace" with a normal gym space, like Dict, Box, MultiDiscrete etc.
271+
# this of course will not work... Replace "AGymSpace" with a normal gymnasium space, like Dict, Box, MultiDiscrete etc.
272272
from gym.spaces import AGymSpace
273273
gym_env = GymEnv(env)
274274
@@ -282,7 +282,7 @@ And for the action space:
282282
283283
def from_gym(self, gym_action):
284284
# this is this very same function that you need to implement
285-
# it should have this exact name, take only one action (member of your gym space) as input
285+
# it should have this exact name, take only one action (member of your gymnasium space) as input
286286
# and return a grid2op action
287287
return TheGymAction_ConvertedTo_Grid2op_Action
288288
# eg. return np.concatenate((obs.gen_p * 0.1, np.sqrt(obs.load_p))
@@ -311,7 +311,7 @@ and divide input data by `divide`):
311311
env_name = "l2rpn_case14_sandbox" # or any other grid2op environment name
312312
g2op_env = grid2op.make(env_name) # create the gri2op environment
313313
314-
gym_env = GymEnv(g2op_env) # create the gym environment
314+
gym_env = GymEnv(g2op_env) # create the gymnasium environment
315315
316316
ob_space = gym_env.observation_space
317317
ob_space = ob_space.reencode_space("actual_dispatch",
@@ -336,7 +336,7 @@ the log of the loads instead of giving the direct value to your agent. This can
336336
env_name = "l2rpn_case14_sandbox" # or any other grid2op environment name
337337
g2op_env = grid2op.make(env_name) # create the gri2op environment
338338
339-
gym_env = GymEnv(g2op_env) # create the gym environment
339+
gym_env = GymEnv(g2op_env) # create the gymnasium environment
340340
341341
ob_space = gym_env.observation_space
342342
shape_ = (g2op_env.n_load, )
@@ -350,7 +350,7 @@ the log of the loads instead of giving the direct value to your agent. This can
350350
)
351351
352352
gym_env.observation_space = ob_space
353-
# and now you will get the key "log_load" as part of your gym observation.
353+
# and now you will get the key "log_load" as part of your gymnasium observation.
354354
355355
A detailed list of such "converter" is documented on the section "Detailed Documentation by class". In
356356
the table below we describe some of them (**nb** if you notice a converter is not displayed there,
@@ -360,11 +360,11 @@ do not hesitate to write us a "feature request" for the documentation, thanks in
360360
Converter name Objective
361361
============================================= ============================================================
362362
:class:`ContinuousToDiscreteConverter` Convert a continuous space into a discrete one
363-
:class:`MultiToTupleConverter` Convert a gym MultiBinary to a gym Tuple of gym Binary and a gym MultiDiscrete to a Tuple of Discrete
363+
:class:`MultiToTupleConverter` Convert a gymnasium MultiBinary to a gymnasium Tuple of gymnasium Binary and a gymnasium MultiDiscrete to a Tuple of Discrete
364364
:class:`ScalerAttrConverter` Allows to scale (divide an attribute by something and subtract something from it)
365-
`BaseGymSpaceConverter.add_key`_ Allows you to compute another "part" of the observation space (you add an information to the gym space)
365+
`BaseGymSpaceConverter.add_key`_ Allows you to compute another "part" of the observation space (you add an information to the gymnasium space)
366366
`BaseGymSpaceConverter.keep_only_attr`_ Allows you to specify which part of the action / observation you want to keep
367-
`BaseGymSpaceConverter.ignore_attr`_ Allows you to ignore some attributes of the action / observation (they will not be part of the gym space)
367+
`BaseGymSpaceConverter.ignore_attr`_ Allows you to ignore some attributes of the action / observation (they will not be part of the gymnasium space)
368368
============================================= ============================================================
369369

370370
.. warning::
@@ -383,7 +383,7 @@ Converter name Objective
383383
.. note::
384384

385385
With the "converters" above, note that the observation space AND action space will still
386-
inherit from gym Dict.
386+
inherit from gymnasium Dict.
387387

388388
They are complex spaces that are not well handled by some RL framework.
389389

@@ -395,19 +395,19 @@ Converter name Objective
395395
Customizing the action and observation space, into Box or Discrete
396396
*******************************************************************
397397

398-
The use of the converter above is nice if you can work with gym Dict, but in some cases, or for some frameworks
398+
The use of the converter above is nice if you can work with gymnasium Dict, but in some cases, or for some frameworks
399399
it is not convenient to do it at all.
400400

401-
TO alleviate this problem, we developed 4 types of gym action space, following the architecture
401+
TO alleviate this problem, we developed 4 types of gymnasium action space, following the architecture
402402
detailed in subsection :ref:`base_gym_space_function`
403403

404404
=============================== ============================================================
405405
Converter name Objective
406406
=============================== ============================================================
407407
:class:`BoxGymObsSpace` Convert the observation space to a single "Box"
408-
:class:`BoxGymActSpace` Convert a gym MultiBinary to a gym Tuple of gym Binary and a gym MultiDiscrete to a Tuple of Discrete
408+
:class:`BoxGymActSpace` Convert a gymnasium MultiBinary to a gymnasium Tuple of gymnasium Binary and a gymnasium MultiDiscrete to a Tuple of Discrete
409409
:class:`MultiDiscreteActSpace` Allows to scale (divide an attribute by something and subtract something from it)
410-
:class:`DiscreteActSpace` Allows you to compute another "part" of the observation space (you add an information to the gym space)
410+
:class:`DiscreteActSpace` Allows you to compute another "part" of the observation space (you add an information to the gymnasium space)
411411
=============================== ============================================================
412412

413413
They can all be used like:

docs/makeenv.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ To get started with such an environment, you can simply do:
2525
You can consult the different notebooks in the `getting_stared` directory of this package for more information on
2626
how to use it.
2727

28-
Created Environment should behave exactly like a gym environment. If you notice any unwanted behavior, please address
28+
Created Environment should behave exactly like a gymnasium environment. If you notice any unwanted behavior, please address
2929
an issue in the official grid2op repository: `Grid2Op <https://github.com/rte-france/Grid2Op>`_
3030

31-
The environment created with this method should be fully compatible with the gym framework: if you are developing
32-
a new algorithm of "Reinforcement Learning" and you used the openai gym framework to do so, you can port your code
31+
The environment created with this method should be fully compatible with the gymnasium framework: if you are developing
32+
a new algorithm of "Reinforcement Learning" and you used the openai gymnasium framework to do so, you can port your code
3333
in a few minutes (basically this consists in adapting the input and output dimension of your BaseAgent) and make it work
3434
with a Grid2Op environment. An example of such modifications is exposed in the getting_started/ notebooks.
3535

docs/model_free.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Model Free Reinforcement Learning
88

99
See some example in "l2rpn-baselines" package for now !
1010

11-
The main idea is first to convert the grid2op environment to a gym environment, for example using :ref:`openai-gym`.
11+
The main idea is first to convert the grid2op environment to a gymnasium environment, for example using :ref:`openai-gym`.
1212
And then use some libaries available,
1313
for example `Stable Baselines <https://stable-baselines3.readthedocs.io/en/master/>`_ or
1414
`RLLIB <https://docs.ray.io/en/latest/rllib/index.html>`_

docs/plot.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ An possible output will look like this:
7676
Render the state of the grid
7777
-----------------------------
7878

79-
During the gym loop
80-
++++++++++++++++++++
79+
During the gymnasium loop
80+
++++++++++++++++++++++++++
8181
In Grid2Op we also made available the possibility to render the state of the grid that your agent sees before taking
82-
an action. This can be done with the provided environments following openAI gym interface like this:
82+
an action. This can be done with the provided environments following gymnasium interface like this:
8383

8484
.. code-block:: python
8585
@@ -104,7 +104,7 @@ significantly.
104104

105105
Offline, after the scenarios were played
106106
++++++++++++++++++++++++++++++++++++++++
107-
In Grid2Op, you can execute a :ref:`runner-module` to perform the "gym loops" and store the results
107+
In Grid2Op, you can execute a :ref:`runner-module` to perform the "gymnasium loops" and store the results
108108
in a standardized manner. Once stored, the results can be loaded back and "replayed" using the appropriate
109109
class. Here is how you can do this:
110110

docs/quickstart.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ that are available, without any installation thanks to
8888
`Binder <https://mybinder.org/v2/gh/rte-france/Grid2Op/master>`_ . Feel free to visit the "getting_started" page for
8989
more information and a detailed tour about the issue that grid2op tries to address.
9090

91-
The most basic code, for those familiar with openAI gym (a well-known framework in reinforcement learning) is:
91+
The most basic code, for those familiar with gymnasium (a well-known framework in reinforcement learning) is:
9292

9393
.. code-block:: python
9494
@@ -101,7 +101,7 @@ The most basic code, for those familiar with openAI gym (a well-known framework
101101
from grid2op.Agent import RandomAgent
102102
my_agent = RandomAgent(env.action_space)
103103
104-
# proceed as you would any open ai gym loop
104+
# proceed as you would any gymnasium loop
105105
nb_episode = 10
106106
for _ in range(nb_episode):
107107
# you perform in this case 10 different episodes
@@ -115,9 +115,9 @@ The most basic code, for those familiar with openAI gym (a well-known framework
115115
act = my_agent.act(obs, reward, done)
116116
obs, reward, done, info = env.step(act)
117117
118-
.. warning:: Grid2Op environments implements the interface of defined by openAI gym environment, but they don't
119-
inherit from them. You can use the Grid2Op environment as you would any Gym environment but they are
120-
not strictly speaking gym environment.
118+
.. warning:: Grid2Op environments implements the interface of defined by gymnasium environment, but they don't
119+
inherit from them. You can use the Grid2Op environment as you would any gymnasium environment but they are
120+
not strictly speaking gymnasium environment.
121121

122122
To make the use of grid2op alongside grid2op environment easier, we developed a module described in
123123
:ref:`openai-gym`.

docs/user/environment.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ In this section we present some way to use the :class:`Environment` class.
3232

3333
Basic Usage
3434
++++++++++++
35-
This example is adapted from gym documentation available at
35+
This example is adapted from gymnasium documentation available at
3636
`gym random_agent.py <https://github.com/openai/gym/blob/master/examples/agents/random_agent.py>`_ ):
3737

3838
.. code-block:: python

docs/user/runner.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ Objectives
1313
The runner class aims at:
1414

1515
i) facilitate the evaluation of the performance of :class:`grid2op.Agent` by performing automatically the
16-
"open ai gym loop" (see below)
16+
"gymnasium loop" (see below)
1717
ii) define a format to store the results of the evaluation of such agent in a standardized manner
1818
iii) this "agent logs" can then be re read by third party applications, such as
1919
`grid2viz <https://github.com/mjothy/grid2viz>`_ or by internal class to ease the study of the behaviour of
2020
such agent, for example with the classes :class:`grid2op.Episode.EpisodeData` or
2121
:class:`grid2op.Episode.EpisodeReplay`
2222
iv) allow easy use of parallelization of this assessment.
2323

24-
Basically, the runner simplifies the assessment of the performance of some agent. This is the "usual" gym code to run
24+
Basically, the runner simplifies the assessment of the performance of some agent. This is the "usual" gymnasium code to run
2525
an agent:
2626

2727
.. code-block:: python

grid2op/Chronics/multiFolder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ def sample_next_chronics(self, probabilities=None):
357357
id_sel = (self._order == selected).nonzero()[0]
358358
self._prev_cache_id = selected - 1
359359
return id_sel
360-
360+
361361
def reset(self):
362362
"""
363363
Rebuilt the :attr:`Multifolder._order`. This should be called after a call to :func:`Multifolder.set_filter`

0 commit comments

Comments
 (0)