Skip to content

Commit

Permalink
Merge pull request #614 from BDonnot/master
Browse files Browse the repository at this point in the history
More customization for env.reset
  • Loading branch information
BDonnot authored Jun 14, 2024
2 parents 74c6948 + 9e21965 commit 184b281
Show file tree
Hide file tree
Showing 23 changed files with 1,840 additions and 101 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,9 @@ grid2op/tests/req_38_np121
test_make_2_envs.py
getting_started/env_py38_grid2op110_ray110.ipynb
getting_started/env_py38_grid2op110_ray210.ipynb
grid2op/tests/req_chronix2grid
grid2op/tests/venv_test_chronix2grid/


# profiling files
**.prof
23 changes: 21 additions & 2 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,31 @@ Change Log
- [???] "asynch" multienv
- [???] properly model interconnecting powerlines


- TODO A number of max buses per sub
- TODO in the runner, save multiple times the same sceanrio


[1.10.3] - 2024-xx-yy
-------------------------
- TODO A number of max buses per sub
- TODO Automatic "experimental_read_from_local_dir"
- TODO Notebook for stable baselines
- TODO in the reset options: datetime start and max number of steps

- [BREAKING] `env.chronics_hander.set_max_iter(xxx)` is now a private function. Use
`env.set_max_iter(xxx)` or even better `env.reset(options={"max step": xxx})`.
Indeed, `env.chronics_hander.set_max_iter()` will likely have
no effect at all on your environment.
- [BREAKING] for all the `Handler` (*eg* `CSVForecastHandler`) the method `set_max_iter` is
now private (for the same reason as the `env.chronics_handler`). We do not recommend to
use it (will likely have no effect). Prefer using `env.set_max_iter` instead.
- [BREAKING] now the `runner.run()` method only accept kwargs argument
(because it should always have been like this)
- [FIXED] a bug in the `MultiFolder` and `MultifolderWithCache` leading to the wrong
computation of `max_iter` on some corner cases
- [ADDED] possibility to skip some step when calling `env.reset(..., options={"init ts": ...})`
- [ADDED] possibility to limit the duration of an episode with `env.reset(..., options={"max step": ...})`
- [ADDED] possibility to specify the "reset_options" used in `env.reset` when
using the runner with `runner.run(..., reset_options=xxx)`

[1.10.2] - 2024-05-27
-------------------------
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.3.dev0'
release = '1.10.3.dev1'
version = '1.10'


Expand Down
4 changes: 2 additions & 2 deletions examples/backend_integration/Step5_modify_topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ def get_topo_vect(self) -> np.ndarray:
local_topo = (1, 2, 1, 2, 1, 2)
elif env_name == "l2rpn_wcci_2022_dev":
raise RuntimeError("Storage units are not handled by the example backend, and there are some on the grid.")
sub_id = 3
local_topo = (1, 2, 1, 2, 1)
# sub_id = 3
# local_topo = (1, 2, 1, 2, 1)
else:
raise RuntimeError(f"Unknown grid2op environment name {env_name}")
action = env.action_space({"set_bus": {"substations_id": [(sub_id, local_topo)]}})
Expand Down
14 changes: 11 additions & 3 deletions grid2op/Chronics/chronicsHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,21 @@ def get_name(self):
"""
return str(os.path.split(self.get_id())[-1])

def set_max_iter(self, max_iter: int):
def _set_max_iter(self, max_iter: int):
"""
This function is used to set the maximum number of
iterations possible before the chronics ends.
You can reset this by setting it to `-1`.
.. danger::
As for grid2op 1.10.3, due to the fix of a bug when
max_iter and fast_forward were used at the same time
you should not use this function anymore.
Please use `env.set_max_iter()` instead of
`env.chronics_hander.set_max_iter()`
Parameters
----------
max_iter: ``int``
Expand All @@ -175,9 +183,9 @@ def set_max_iter(self, max_iter: int):
"""

if not isinstance(max_iter, int):
if not isinstance(max_iter, (int, dt_int, np.int64)):
raise Grid2OpException(
"The maximum number of iterations possible for this chronics, before it ends."
"The maximum number of iterations possible for this time series, before it ends should be an int"
)
if max_iter == 0:
raise Grid2OpException(
Expand Down
2 changes: 1 addition & 1 deletion grid2op/Chronics/handlers/baseHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def __init__(self, array_name, max_iter=-1, h_forecast=(5, )):
self.path : Optional[os.PathLike] = None
self.max_episode_duration : Optional[int] = None

def set_max_iter(self, max_iter: Optional[int]) -> None:
def _set_max_iter(self, max_iter: Optional[int]) -> None:
"""
INTERNAL
Expand Down
4 changes: 2 additions & 2 deletions grid2op/Chronics/handlers/csvForecastHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ def load_next(self, dict_):
def set_chunk_size(self, chunk_size):
super().set_chunk_size(self._nb_row_per_step * int(chunk_size))

def set_max_iter(self, max_iter):
super().set_max_iter(self._nb_row_per_step * int(max_iter))
def _set_max_iter(self, max_iter):
super()._set_max_iter(self._nb_row_per_step * int(max_iter))

def set_h_forecast(self, h_forecast):
super().set_h_forecast(h_forecast)
Expand Down
1 change: 1 addition & 0 deletions grid2op/Chronics/multiFolder.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ def initialize(
)
if self.action_space is not None:
self.data.action_space = self.action_space
self._max_iter = self.data.max_iter

def done(self):
"""
Expand Down
3 changes: 2 additions & 1 deletion grid2op/Chronics/multifolderWithCache.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class MultifolderWithCache(Multifolder):
env = make(...,chronics_class=MultifolderWithCache)
# set the chronics to limit to one week of data (lower memory footprint)
env.chronics_handler.set_max_iter(7*288)
env.set_max_iter(7*288)
# assign a filter, use only chronics that have "december" in their name
env.chronics_handler.real_data.set_filter(lambda x: re.match(".*december.*", x) is not None)
# create the cache
Expand Down Expand Up @@ -239,6 +239,7 @@ def initialize(
id_scenario = self._order[self._prev_cache_id]
self.data = self._cached_data[id_scenario]
self.data.next_chronics()
self._max_iter = self.data.max_iter

@property
def max_iter(self):
Expand Down
6 changes: 3 additions & 3 deletions grid2op/Chronics/time_series_from_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def __init__(
self.set_chunk_size(chunk_size)

if max_iter != -1:
self.set_max_iter(max_iter)
self._set_max_iter(max_iter)

self.init_datetime()
self.current_inj = None
Expand Down Expand Up @@ -389,10 +389,10 @@ def set_chunk_size(self, new_chunk_size):
for el in self._active_handlers:
el.set_chunk_size(new_chunk_size)

def set_max_iter(self, max_iter):
def _set_max_iter(self, max_iter):
self.max_iter = int(max_iter)
for el in self._active_handlers:
el.set_max_iter(max_iter)
el._set_max_iter(max_iter)

def init_datetime(self):
for handl in self._active_handlers:
Expand Down
29 changes: 26 additions & 3 deletions grid2op/Environment/baseEnv.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,7 @@ def foo(manager):

#: this are the keys of the dictionnary `options`
#: that can be used when calling `env.reset(..., options={})`
KEYS_RESET_OPTIONS = {"time serie id", "init state"}

KEYS_RESET_OPTIONS = {"time serie id", "init state", "init ts", "max step"}

def __init__(
self,
Expand Down Expand Up @@ -3776,14 +3775,38 @@ def fast_forward_chronics(self, nb_timestep):
00:00). This can lead to suboptimal exploration, as during this phase, only a few time steps are managed by
the agent, so in general these few time steps will correspond to grid state around Jan 1st at 00:00.
.. seealso::
From grid2op version 1.10.3, a similar objective can be
obtained directly by calling :func:`grid2op.Environment.Environment.reset` with `"init ts"`
as option, for example like `obs = env.reset(options={"init ts": 12})`
.. danger::
The usage of both :func:`BaseEnv.fast_forward_chronics` and :func:`Environment.set_max_iter`
is not recommended at all and might not behave correctly. Please use `env.reset` with
`obs = env.reset(options={"max step": xxx, "init ts": yyy})` for a correct behaviour.
Parameters
----------
nb_timestep: ``int``
Number of time step to "fast forward"
Examples
---------
This can be used like this:
From grid2op version 1.10.3 we recommend not to use this function (which will be deprecated)
but to use the :func:`grid2op.Environment.Environment.reset` functon with the `"init ts"`
option.
.. code-block:: python
import grid2op
env_name = "l2rpn_case14_sandbox"
env = grid2op.make(env_name)
obs = env.reset(options={"init ts": 123})
For the legacy usave, this can be used like this:
.. code-block:: python
Expand Down
Loading

0 comments on commit 184b281

Please sign in to comment.