Skip to content

Commit

Permalink
fixing some issue from sonarcloud
Browse files Browse the repository at this point in the history
  • Loading branch information
BDonnot committed Feb 22, 2024
1 parent 81411a0 commit 7cb706b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 42 deletions.
1 change: 0 additions & 1 deletion grid2op/Reward/baseReward.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import grid2op
from grid2op.dtypes import dt_float
from grid2op.Action import BaseAction
# from grid2op.Environment import BaseEnv


class BaseReward(ABC):
Expand Down
4 changes: 4 additions & 0 deletions grid2op/gym_compat/box_gym_actspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,10 @@ def from_gym(self, gym_act: np.ndarray) -> BaseAction:
return res

def close(self) -> None:
"""If you override this class, this function is called when the GymEnv is deleted.
You can use it to free some memory if needed, but there is nothing to do in the general case.
"""
pass

def normalize_attr(self, attr_nm: POSSIBLE_KEYS)-> None:
Expand Down
4 changes: 4 additions & 0 deletions grid2op/gym_compat/discrete_gym_actspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@ def from_gym(self, gym_act: int) -> BaseAction:
return res

def close(self) -> None:
"""If you override this class, this function is called when the GymEnv is deleted.
You can use it to free some memory if needed, but there is nothing to do in the general case.
"""
pass


Expand Down
7 changes: 4 additions & 3 deletions grid2op/gym_compat/gymenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def decorator(func):
return NotImplementedError() # anything that is not a callbe anyway
return decorator

_TIME_SERIE_ID = "time serie id"
RESET_INFO_GYM_TYPING = Dict[Literal["time serie id", "seed", "grid2op_env_seed", "underlying_env_seeds"], Any]

class __AuxGymEnv(Generic[ObsType, ActType]):
Expand Down Expand Up @@ -155,7 +156,7 @@ def _aux_reset(self,

if return_info:
chron_id = self.init_env.chronics_handler.get_id()
info = {"time serie id": chron_id}
info = {_TIME_SERIE_ID: chron_id}
if seed is not None:
info["seed"] = seed
info["grid2op_env_seed"] = next_seed
Expand All @@ -170,7 +171,7 @@ def _aux_reset_new(self,
# used for gym > 0.26
if (self._shuffle_chronics and
isinstance(self.init_env.chronics_handler.real_data, Multifolder) and
(options is not None and "time serie id" not in options)):
(options is not None and _TIME_SERIE_ID not in options)):
self.init_env.chronics_handler.sample_next_chronics()

super().reset(seed=seed) # seed gymnasium env
Expand All @@ -184,7 +185,7 @@ def _aux_reset_new(self,
gym_obs = self.observation_space.to_gym(g2op_obs)

chron_id = self.init_env.chronics_handler.get_id()
info = {"time serie id": chron_id}
info = {_TIME_SERIE_ID: chron_id}
if seed is not None:
info["seed"] = seed
info["grid2op_env_seed"] = next_seed
Expand Down
79 changes: 41 additions & 38 deletions grid2op/gym_compat/multidiscrete_gym_actspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,74 +270,77 @@ def __init__(self,
None,
act_sp.n_sub,
type(self).ATTR_NEEDBUILD,
), # dimension will be computed on the fly, if the stuff is used
), # dimension will be computed on the fly, if the kwarg is used
"sub_change_bus": (
None,
act_sp.n_sub,
type(self).ATTR_NEEDBUILD,
), # dimension will be computed on the fly, if the stuff is used
), # dimension will be computed on the fly, if the kwarg is used
"one_sub_set": (
None,
1,
type(self).ATTR_NEEDBUILD,
), # dimension will be computed on the fly, if the stuff is used
), # dimension will be computed on the fly, if the kwarg is used
"one_sub_change": (
None,
1,
type(self).ATTR_NEEDBUILD,
), # dimension will be computed on the fly, if the stuff is used
), # dimension will be computed on the fly, if the kwarg is used
"one_line_set": (
None,
1,
type(self).ATTR_NEEDBUILD,
), # dimension will be computed on the fly, if the stuff is used
), # dimension will be computed on the fly, if the kwarg is used
"one_line_change": (
None,
1,
type(self).ATTR_NEEDBUILD,
), # dimension will be computed on the fly, if the stuff is used
), # dimension will be computed on the fly, if the kwarg is used
}
self._nb_bins = nb_bins
for el in ["redispatch", "set_storage", "curtail", "curtail_mw"]:
if el in attr_to_keep:
if el not in nb_bins:
raise RuntimeError(
f'The attribute you want to keep "{el}" is not present in the '
f'"nb_bins". This attribute is continuous, you have to specify in how '
f"how to convert it to a discrete space. See the documentation "
f"for more information."
)
nb_redispatch = act_sp.gen_redispatchable.sum()
nb_renew = act_sp.gen_renewable.sum()
if el == "redispatch":
self.dict_properties[el] = (
[nb_bins[el] for _ in range(nb_redispatch)],
nb_redispatch,
self.ATTR_NEEDBINARIZED,
)
elif el == "curtail" or el == "curtail_mw":
self.dict_properties[el] = (
[nb_bins[el] for _ in range(nb_renew)],
nb_renew,
self.ATTR_NEEDBINARIZED,
)
elif el == "set_storage":
self.dict_properties[el] = (
[nb_bins[el] for _ in range(act_sp.n_storage)],
act_sp.n_storage,
self.ATTR_NEEDBINARIZED,
)
else:
raise Grid2OpException(f'Unknown attribute "{el}"')

self._aux_check_continuous_elements(el, attr_to_keep, nb_bins, act_sp)

self._dims = None
self._functs = None # final functions that is applied to the gym action to map it to a grid2Op action
self._binarizers = None # contains all the stuff to binarize the data
self._binarizers = None # contains all the kwarg to binarize the data
self._types = None
nvec = self._get_info()
# initialize the base container
type(self)._MultiDiscreteType.__init__(self, nvec=nvec)

def _aux_check_continuous_elements(self, el, attr_to_keep, nb_bins, act_sp):
if el in attr_to_keep:
if el not in nb_bins:
raise RuntimeError(
f'The attribute you want to keep "{el}" is not present in the '
f'"nb_bins". This attribute is continuous, you have to specify in how '
f"how to convert it to a discrete space. See the documentation "
f"for more information."
)
nb_redispatch = act_sp.gen_redispatchable.sum()
nb_renew = act_sp.gen_renewable.sum()
if el == "redispatch":
self.dict_properties[el] = (
[nb_bins[el] for _ in range(nb_redispatch)],
nb_redispatch,
self.ATTR_NEEDBINARIZED,
)
elif el == "curtail" or el == "curtail_mw":
self.dict_properties[el] = (
[nb_bins[el] for _ in range(nb_renew)],
nb_renew,
self.ATTR_NEEDBINARIZED,
)
elif el == "set_storage":
self.dict_properties[el] = (
[nb_bins[el] for _ in range(act_sp.n_storage)],
act_sp.n_storage,
self.ATTR_NEEDBINARIZED,
)
else:
raise Grid2OpException(f'Unknown attribute "{el}"')

@staticmethod
def _funct_set(vect):
# gym encodes:
Expand Down

0 comments on commit 7cb706b

Please sign in to comment.