-
Notifications
You must be signed in to change notification settings - Fork 118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can not copy a GymEnv environment when using a LightSim2Grid backend #672
Comments
Hello, I thinks it's a lightsim2grid issue. Can you put an issue there too please? Also you should probably avoid doing deep copies of grid2op environment. It does not really work well. The best is probably to use the If you really want to rely on copy.deepcopy you can |
Hello, Thank you for your answer 😃
Alright, I will also put the issue on LightSim2Grid |
Hello again, I think I have managed to do what you suggested and I would like your advice before doing the PR. The solution would be to add a import grid2op
from grid2op.gym_compat import GymEnv
from lightsim2grid import LightSimBackend
import copy
# I add a __deepcopy__ method to a children class from GymEnv
class GymEnvWithDeepCopy(GymEnv):
def __init__(self, env_init, shuffle_chronics = True, render_mode = "rgb_array", with_forecast = False):
super().__init__(env_init, shuffle_chronics, render_mode, with_forecast)
def __deepcopy__(self, memo=None):
if memo is None:
memo = {}
# I initialize another instance
res = type(self)(
self.init_env.copy(),
shuffle_chronics=self._shuffle_chronics,
render_mode=self.init_env.render_mode,
with_forecast=self.init_env.with_forecast
)
##########################
# I deepcopy the other attributes
for key, value in vars(self).items():
if key not in ('init_env', '_shuffle_chronics'): # Skip already copied attributes
setattr(res, key, copy.deepcopy(value, memo))
##########################
return res
# I create a GymEnvWithDeepCopy env from a grid2op env with a LightSimBackend
env = grid2op.make("l2rpn_case14_sandbox", backend=LightSimBackend())
env_gym_withDP = GymEnvWithDeepCopy(env)
# This line was raising an error when using GymEnv
env_gym_withDP_copy = copy.deepcopy(env_gym_withDP)
print("Do they have the same attributes' names?")
print("Yes" if dir(env_gym_withDP) == dir(env_gym_withDP_copy) else "No")
# I obtain Yes which means that every attributes has been initialized or deepcopied I think the code between ### is useful because even if the other attributes ( Current (and wanted) output:
What do you think? |
Environment
3.12
1.10.5
0.9.2.post2
ubuntu
Bug description
I create a
GymEnv
environment from a grid2op environment. If I use aLightSimBackend
backend when creating the grid2op environment, then I obtain an error if I try to copy theGymEnv
environment.It works on an older environment with older versions (python
3.8
, Grid2op1.9.8
, LightSim2Grid0.7.0.post1
)How to reproduce
Code snippet
Current output
The text was updated successfully, but these errors were encountered: