Skip to content

Commit 7d3a926

Browse files
committed
adding some docs [skip ci]
1 parent 07341ce commit 7d3a926

File tree

3 files changed

+115
-7
lines changed

3 files changed

+115
-7
lines changed

docs/environment.rst

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,58 @@ increase the training time, especially at the beginning. This is due to the fact
104104
`env.reset` is called, the whole chronics is read from the hard drive. If you want to lower this
105105
impact then you might consult the :ref:`environment-module-data-pipeline` page of the doc.
106106

107+
Go to the next scenario
108+
++++++++++++++++++++++++
109+
110+
Starting grid2op 1.9.8 we attempt to make an easier user experience in the
111+
selection of time series, seed, initial state of the grid, etc.
112+
113+
All of the above can be done when calling `env.reset()` function.
114+
115+
For customizing the seed, you can for example do:
116+
117+
.. code-block:: python
118+
119+
import grid2op
120+
env_name = "l2rpn_case14_sandbox"
121+
env = grid2op.make(env_name)
122+
123+
obs = env.reset(seed=0)
124+
125+
For customizing the time series id you want to use:
126+
127+
.. code-block:: python
128+
129+
import grid2op
130+
env_name = "l2rpn_case14_sandbox"
131+
env = grid2op.make(env_name)
132+
133+
obs = env.reset(options={"time serie id": 1}) # time serie by id (sorted alphabetically)
134+
# or
135+
obs = env.reset(options={"time serie id": "0001"}) # time serie by name (folder name)
136+
137+
For customizing the initial state of the grid, for example forcing the
138+
powerline 0 to be disconnected in the initial observation:
139+
140+
.. code-block:: python
141+
142+
import grid2op
143+
env_name = "l2rpn_case14_sandbox"
144+
env = grid2op.make(env_name)
145+
146+
init_state_dict = {"set_line_status": [(0, -1)]}
147+
obs = env.reset(options={"init state": init_state_dict})
148+
149+
150+
Feel free to consult the documentation of the :func:`Environment.reset` function
151+
for more information (this doc might be outdated, the one of the function should
152+
be more up to date with the code).
153+
154+
.. info::
155+
In the near future (next few releases) we will also attempt to make the
156+
customization of the `parameters` or the `skip number of steps`, `maximum duration
157+
of the scenarios` also available in `env.reset()` options.
158+
107159
.. _environment-module-chronics-info:
108160

109161
Time series Customization
@@ -141,10 +193,15 @@ the call to "env.reset". This gives the following code:
141193
# and now the loop starts
142194
for i in range(episode_count):
143195
###################################
144-
env.set_id(THE_CHRONIC_ID)
196+
# with recent grid2op
197+
obs = env.reset(options={"time serie id": THE_CHRONIC_ID})
145198
###################################
146199
147-
obs = env.reset()
200+
###################################
201+
# 'old method (oldest grid2op version)'
202+
# env.set_id(THE_CHRONIC_ID)
203+
# obs = env.reset()
204+
###################################
148205
149206
# now play the episode as usual
150207
while True:

grid2op/Runner/runner.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,15 +1220,15 @@ def run(
12201220
res: ``list``
12211221
List of tuple. Each tuple having 3[4] elements:
12221222
1223-
- "i" unique identifier of the episode (compared to :func:`Runner.run_sequential`, the elements of the
1224-
returned list are not necessarily sorted by this value)
1223+
- "id_chron" unique identifier of the episode
1224+
- "name_chron" name of the time series (usually it is the path where it is stored)
12251225
- "cum_reward" the cumulative reward obtained by the :attr:`Runner.Agent` on this episode i
12261226
- "nb_time_step": the number of time steps played in this episode.
12271227
- "total_step": the total number of time steps possible in this episode.
12281228
- "episode_data" : [Optional] The :class:`EpisodeData` corresponding to this episode run only
12291229
if `add_detailed_output=True`
12301230
- "add_nb_highres_sim": [Optional] The estimated number of calls to high resolution simulator made
1231-
by the agent
1231+
by the agent. Only preset if `add_nb_highres_sim=True` in the kwargs
12321232
12331233
Examples
12341234
--------
@@ -1274,6 +1274,40 @@ def run(
12741274
runner = Runner(**env.get_params_for_runner(), agentClass=None, agentInstance=my_agent)
12751275
res = runner.run(nb_episode=1, agent_seeds=[42], env_seeds=[0])
12761276
1277+
Since grid2op 1.10.2 you can also set the initial state of the grid when
1278+
calling the runner. You can do that with the kwargs `init_states`, for example like this:
1279+
1280+
.. code-block: python
1281+
1282+
import grid2op
1283+
from gri2op.Runner import Runner
1284+
from grid2op.Agent import RandomAgent
1285+
1286+
env = grid2op.make("l2rpn_case14_sandbox")
1287+
my_agent = RandomAgent(env.action_space)
1288+
runner = Runner(**env.get_params_for_runner(), agentClass=None, agentInstance=my_agent)
1289+
res = runner.run(nb_episode=1,
1290+
agent_seeds=[42],
1291+
env_seeds=[0],
1292+
init_states=[{"set_line_status": [(0, -1)]}]
1293+
)
1294+
1295+
.. note::
1296+
We recommend that you provide `init_states` as a list having a length of
1297+
`nb_episode`. Each episode will be initialized with the provided
1298+
element of the list. However, if you provide only one element, then
1299+
all episodes you want to compute will be initialized with this same
1300+
action.
1301+
1302+
.. note::
1303+
At the beginning of each episode, if an `init_state` is set,
1304+
the environment is reset with a call like: `env.reset(options={"init state": init_state})`
1305+
1306+
This is why we recommend you to use dictionary to set the initial state so
1307+
that you can control what exactly is done (set the `"method"`) more
1308+
information about this on the doc of the :func:`grid2op.Environment.Environment.reset`
1309+
function.
1310+
12771311
"""
12781312
if nb_episode < 0:
12791313
raise RuntimeError("Impossible to run a negative number of scenarios.")

grid2op/gym_compat/gymenv.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def _aux_step_new(self, gym_action: ActType) -> Tuple[ObsType, float, bool, bool
142142
def _aux_reset(self,
143143
seed: Optional[int]=None,
144144
return_info: Optional[bool]=None,
145-
options: Optional[Dict[Any, Any]]=None) -> Union[ObsType, Tuple[ObsType, RESET_INFO_GYM_TYPING]]:
145+
options: RESET_OPTIONS_TYPING=None) -> Union[ObsType, Tuple[ObsType, RESET_INFO_GYM_TYPING]]:
146146
# used for gym < 0.26
147147
if self._shuffle_chronics and isinstance(
148148
self.init_env.chronics_handler.real_data, Multifolder
@@ -152,7 +152,7 @@ def _aux_reset(self,
152152
if seed is not None:
153153
seed_, next_seed, underlying_env_seeds = self._aux_seed(seed)
154154

155-
g2op_obs = self.init_env.reset()
155+
g2op_obs = self.init_env.reset(options=options)
156156
gym_obs = self.observation_space.to_gym(g2op_obs)
157157

158158
if return_info:
@@ -301,6 +301,23 @@ def reset(self,
301301
ObsType,
302302
RESET_INFO_GYM_TYPING
303303
]:
304+
"""This function will reset the underlying grid2op environment
305+
and return the next state of the grid (as the gymnasium observation)
306+
and some other information.
307+
308+
Parameters
309+
----------
310+
seed : Optional[int], optional
311+
The seed for this new environment, by default None
312+
options : RESET_OPTIONS_TYPING, optional
313+
See the documentation of :func:`grid2op.Environment.Environment.reset`
314+
for more information about it, by default None
315+
316+
Returns
317+
-------
318+
Tuple[ ObsType, RESET_INFO_GYM_TYPING ]
319+
_description_
320+
"""
304321
return self._aux_reset_new(seed, options)
305322

306323
def step(self, action: ActType) -> Tuple[ObsType, float, bool, bool, STEP_INFO_TYPING]:

0 commit comments

Comments
 (0)