Skip to content

Commit

Permalink
fixing last test
Browse files Browse the repository at this point in the history
  • Loading branch information
BDonnot committed Jul 27, 2021
1 parent 02f3ced commit 47f796f
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 18 deletions.
3 changes: 2 additions & 1 deletion grid2op/Action/ActionSpace.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class ActionSpace(SerializableActionSpace):
"""

def __init__(self, gridobj,
def __init__(self,
gridobj,
legal_action,
actionClass=BaseAction # need to be a base grid2op type (and not a type generated on the fly)
):
Expand Down
2 changes: 2 additions & 0 deletions grid2op/Environment/BaseEnv.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,8 @@ def _create_opponent(self):
if not issubclass(self._opponent_class, BaseOpponent):
raise EnvError("Impossible to make an opponent with a type that does not inherit from BaseOpponent.")

self._opponent_action_class._add_shunt_data()
self._opponent_action_class._update_value_set()
self._opponent_action_space = self._helper_action_class(gridobj=type(self.backend),
legal_action=AlwaysLegal,
actionClass=self._opponent_action_class
Expand Down
1 change: 1 addition & 0 deletions grid2op/Environment/Environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,7 @@ def copy(self):
self._voltage_controler = None

res = copy.deepcopy(self)

res.backend = tmp_backend.copy()
res._observation_space = tmp_obs_space.copy()
res.current_obs = obs_tmp.copy()
Expand Down
9 changes: 2 additions & 7 deletions grid2op/Opponent/RandomLineOpponent.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ def __init__(self, action_space):
self._attacks = None
self._lines_ids = None

# this is the constructor:
# it should have the exact same signature as here

def init(self, partial_env, lines_attacked=[], **kwargs):
"""
INTERNAL
Expand Down Expand Up @@ -67,10 +64,8 @@ def init(self, partial_env, lines_attacked=[], **kwargs):
# Pre-build attacks actions
self._attacks = []
for l_id in self._lines_ids:
a = self.action_space({
'set_line_status': [(l_id, -1)]
})
self._attacks.append(a)
att = self.action_space({'set_line_status': [(l_id, -1)]})
self._attacks.append(att)
self._attacks = np.array(self._attacks)

def attack(self, observation, agent_action, env_action,
Expand Down
38 changes: 28 additions & 10 deletions grid2op/Space/GridObjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -2132,6 +2132,7 @@ def init_grid(cls, gridobj, force=False, extra_name=None, force_module=None):
name_res = "{}_{}".format(cls.__name__, gridobj.env_name)
if gridobj.glop_version != grid2op.__version__:
name_res += f"_{gridobj.glop_version}"

if name_res in globals():
if not force:
# no need to recreate the class, it already exists
Expand All @@ -2144,7 +2145,7 @@ def init_grid(cls, gridobj, force=False, extra_name=None, force_module=None):
GridObjects._make_cls_dict_extended(gridobj, cls_attr_as_dict, as_list=False)
res_cls = type(name_res, (cls, ), cls_attr_as_dict)
if hasattr(cls, "_INIT_GRID_CLS"):
# original class is not modified
# original class is already from an initialized environment, i keep track of it
res_cls._INIT_GRID_CLS = cls._INIT_GRID_CLS
else:
# i am the original class from grid2op
Expand Down Expand Up @@ -2916,20 +2917,39 @@ def init_grid_from_dict_for_pickle(name_res, orig_cls, cls_attr):
This function is used internally for pickle to build the classes of the
objects instead of loading them from the module (which is impossible as
most classes are defined on the fly in grid2op)
It is expected to create an object of the correct type. This object will then be
"filled" with the proper content automatically by python, because i provided the "state" of the
object in the __reduce__ method.
"""

# define properly the class
res_cls = type(name_res, (orig_cls, ), cls_attr)
res_cls._compute_pos_big_topo_cls()
res_cls._INIT_GRID_CLS = orig_cls # don't forget to remember the base class
if res_cls.glop_version != grid2op.__version__:
res_cls.process_grid2op_compat()
# check if the class already exists, if so returns it
if name_res in globals():
# no need to recreate the class, it already exists
res_cls = globals()[name_res]
else:
# define properly the class, as it is not found
res_cls = type(name_res, (orig_cls, ), cls_attr)
res_cls._INIT_GRID_CLS = orig_cls # don't forget to remember the base class
if hasattr(res_cls, "n_sub") and res_cls.n_sub > 0:
# that's a grid2op class iniailized with an environment, I need to initialize it too
res_cls._compute_pos_big_topo_cls()
if res_cls.glop_version != grid2op.__version__:
res_cls.process_grid2op_compat()

# add the class in the "globals" for reuse later
globals()[name_res] = res_cls

# now create an "empty" object (using new)
res = res_cls.__new__(res_cls)
return res

# test for pickle
def __reduce__(self):
"""
It here to avoid issue with pickle.
But the problem is that it's also used by deepcopy... So its implementation is used a lot
"""
cls_attr_as_dict = {}
GridObjects._make_cls_dict_extended(type(self), cls_attr_as_dict, as_list=False)
if hasattr(self, "__getstate__"):
Expand All @@ -2947,7 +2967,5 @@ def __reduce__(self):
# i am a "raw" type directly coming from grid2op
base_cls = my_cls
return GridObjects.init_grid_from_dict_for_pickle, \
(type(self).__name__,
base_cls,
cls_attr_as_dict), \
(type(self).__name__, base_cls, cls_attr_as_dict), \
my_state
1 change: 1 addition & 0 deletions grid2op/tests/test_issue_185.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def test_issue_185_act_multidiscrete_space(self):
elif env_name == ENV_WITH_ALARM_NAME:
# takes too much time
continue

with warnings.catch_warnings():
warnings.filterwarnings("ignore")
with grid2op.make(env_name, test=True) as env:
Expand Down

0 comments on commit 47f796f

Please sign in to comment.