Ensure envs are closed

This commit is contained in:
Dominik Moritz Roth 2023-10-29 13:41:06 +01:00
parent f1d3b5333f
commit da042eebd9

View File

@ -41,6 +41,7 @@ def example_mp(env_name="fancy_ProMP/HoleReacher-v0", seed=1, iterations=1, rend
if terminated or truncated:
print(reward)
obs = env.reset()
env.close()
def example_custom_mp(env_name="fancy_ProMP/Reacher5d-v0", seed=1, iterations=1, render=True):
@ -80,6 +81,7 @@ def example_custom_mp(env_name="fancy_ProMP/Reacher5d-v0", seed=1, iterations=1,
print(i, reward)
obs = env.reset()
env.close()
return obs
class Custom_MPWrapper(fancy_gym.envs.mujoco.reacher.MPWrapper):
@ -147,12 +149,12 @@ def example_fully_custom_mp(seed=1, iterations=1, render=True):
env = gym.make(custom_env_id_ProMP, render_mode='human' if render else None)
if render:
env.render()
rewards = 0
obs = env.reset()
if render:
env.render()
# number of samples/full trajectories (multiple environment steps)
for i in range(iterations):
ac = env.action_space.sample()
@ -164,6 +166,12 @@ def example_fully_custom_mp(seed=1, iterations=1, render=True):
rewards = 0
obs = env.reset()
try: # Some mujoco-based envs don't correlcty implement .close
env.close()
except:
pass
def example_fully_custom_mp_alternative(seed=1, iterations=1, render=True):
"""
Instead of defining the mp_args in a new custom MP_Wrapper, they can also be provided during registration.
@ -200,12 +208,12 @@ def example_fully_custom_mp_alternative(seed=1, iterations=1, render=True):
env = gym.make(custom_env_id_ProMP, render_mode='human' if render else None)
if render:
env.render()
rewards = 0
obs = env.reset()
if render:
env.render()
# number of samples/full trajectories (multiple environment steps)
for i in range(iterations):
ac = env.action_space.sample()
@ -234,6 +242,12 @@ def example_fully_custom_mp_alternative(seed=1, iterations=1, render=True):
rewards = 0
obs = env.reset()
try: # Some mujoco-based envs don't correlcty implement .close
env.close()
except:
pass
def main():
render = False
# DMP