From e8fb90f1ae6e7edb27dd8b04e2bcc13e42338b42 Mon Sep 17 00:00:00 2001 From: Dominik Roth Date: Sat, 10 Feb 2024 14:03:20 +0100 Subject: [PATCH 1/3] Implement automatic rendering to all fancy envs (implementuing new gym spec regarding rendering) --- fancy_gym/envs/mujoco/ant_jump/ant_jump.py | 8 ++++++++ fancy_gym/envs/mujoco/beerpong/beerpong.py | 10 +++++++++- fancy_gym/envs/mujoco/box_pushing/box_pushing_env.py | 9 +++++++++ .../envs/mujoco/half_cheetah_jump/half_cheetah_jump.py | 7 +++++++ fancy_gym/envs/mujoco/hopper_jump/hopper_jump.py | 10 ++++++++++ .../envs/mujoco/hopper_jump/hopper_jump_on_box.py | 3 +++ fancy_gym/envs/mujoco/hopper_throw/hopper_throw.py | 9 +++++++++ .../envs/mujoco/hopper_throw/hopper_throw_in_basket.py | 8 ++++++++ fancy_gym/envs/mujoco/reacher/reacher.py | 9 +++++++++ fancy_gym/envs/mujoco/table_tennis/table_tennis_env.py | 9 +++++++++ fancy_gym/envs/mujoco/walker_2d_jump/walker_2d_jump.py | 9 +++++++++ 11 files changed, 90 insertions(+), 1 deletion(-) diff --git a/fancy_gym/envs/mujoco/ant_jump/ant_jump.py b/fancy_gym/envs/mujoco/ant_jump/ant_jump.py index 97cde0e..bcf78bf 100644 --- a/fancy_gym/envs/mujoco/ant_jump/ant_jump.py +++ b/fancy_gym/envs/mujoco/ant_jump/ant_jump.py @@ -115,6 +115,7 @@ class AntJumpEnv(AntEnvCustomXML): contact_force_range=contact_force_range, reset_noise_scale=reset_noise_scale, exclude_current_positions_from_observation=exclude_current_positions_from_observation, **kwargs) + self.render_active = False def step(self, action): self.current_step += 1 @@ -153,8 +154,15 @@ class AntJumpEnv(AntEnvCustomXML): } truncated = False + if self.render_active: + self.render() + return obs, reward, terminated, truncated, info + def render(self): + self.render_active = True + return super().render() + def _get_obs(self): return np.append(super()._get_obs(), self.goal) diff --git a/fancy_gym/envs/mujoco/beerpong/beerpong.py b/fancy_gym/envs/mujoco/beerpong/beerpong.py index 802776f..e8bab4c 100644 --- a/fancy_gym/envs/mujoco/beerpong/beerpong.py +++ b/fancy_gym/envs/mujoco/beerpong/beerpong.py @@ -44,6 +44,7 @@ class BeerPongEnv(MujocoEnv, utils.EzPickle): } def __init__(self, **kwargs): + utils.EzPickle.__init__(self) self._steps = 0 # Small Context -> Easier. Todo: Should we do different versions? # self.xml_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "assets", "beerpong_wo_cup.xml") @@ -89,7 +90,7 @@ class BeerPongEnv(MujocoEnv, utils.EzPickle): observation_space=self.observation_space, **kwargs ) - utils.EzPickle.__init__(self) + self.render_active = False @property def start_pos(self): @@ -169,8 +170,15 @@ class BeerPongEnv(MujocoEnv, utils.EzPickle): truncated = False + if self.render_active: + self.render() + return ob, reward, terminated, truncated, infos + def render(self): + self.render_active = True + return super().render() + def _get_obs(self): theta = self.data.qpos.flat[:7].copy() theta_dot = self.data.qvel.flat[:7].copy() diff --git a/fancy_gym/envs/mujoco/box_pushing/box_pushing_env.py b/fancy_gym/envs/mujoco/box_pushing/box_pushing_env.py index 9b512a8..225bccf 100644 --- a/fancy_gym/envs/mujoco/box_pushing/box_pushing_env.py +++ b/fancy_gym/envs/mujoco/box_pushing/box_pushing_env.py @@ -4,6 +4,7 @@ import numpy as np from gymnasium import utils, spaces from gymnasium.envs.mujoco import MujocoEnv from fancy_gym.envs.mujoco.box_pushing.box_pushing_utils import rot_to_quat, get_quaternion_error, rotation_distance +from fancy_gym.envs.mujoco.box_pushing.box_pushing_utils import rot_to_quat, get_quaternion_error, rotation_distance from fancy_gym.envs.mujoco.box_pushing.box_pushing_utils import q_max, q_min, q_dot_max, q_torque_max from fancy_gym.envs.mujoco.box_pushing.box_pushing_utils import desired_rod_quat @@ -60,6 +61,7 @@ class BoxPushingEnvBase(MujocoEnv, utils.EzPickle): frame_skip=self.frame_skip, observation_space=self.observation_space, **kwargs) self.action_space = spaces.Box(low=-1, high=1, shape=(7,)) + self.render_active = False def step(self, action): action = 10 * np.clip(action, self.action_space.low, self.action_space.high) @@ -108,8 +110,15 @@ class BoxPushingEnvBase(MujocoEnv, utils.EzPickle): terminated = episode_end and infos['is_success'] truncated = episode_end and not infos['is_success'] + if self.render_active: + self.render() + return obs, reward, terminated, truncated, infos + def render(self): + self.render_active = True + return super().render() + def reset_model(self): # rest box to initial position self.set_state(self.init_qpos_box_pushing, self.init_qvel_box_pushing) diff --git a/fancy_gym/envs/mujoco/half_cheetah_jump/half_cheetah_jump.py b/fancy_gym/envs/mujoco/half_cheetah_jump/half_cheetah_jump.py index 088f959..20c2a2c 100644 --- a/fancy_gym/envs/mujoco/half_cheetah_jump/half_cheetah_jump.py +++ b/fancy_gym/envs/mujoco/half_cheetah_jump/half_cheetah_jump.py @@ -60,7 +60,11 @@ class HalfCheetahEnvCustomXML(HalfCheetahEnv): default_camera_config=DEFAULT_CAMERA_CONFIG, **kwargs, ) + self.render_active = False + def render(self): + self.render_active = True + return super().render() class HalfCheetahJumpEnv(HalfCheetahEnvCustomXML): """ @@ -120,6 +124,9 @@ class HalfCheetahJumpEnv(HalfCheetahEnvCustomXML): 'max_height': self.max_height } + if self.render_active: + self.render() + return observation, reward, terminated, truncated, info def _get_obs(self): diff --git a/fancy_gym/envs/mujoco/hopper_jump/hopper_jump.py b/fancy_gym/envs/mujoco/hopper_jump/hopper_jump.py index ae431ab..7dee724 100644 --- a/fancy_gym/envs/mujoco/hopper_jump/hopper_jump.py +++ b/fancy_gym/envs/mujoco/hopper_jump/hopper_jump.py @@ -88,6 +88,12 @@ class HopperEnvCustomXML(HopperEnv): **kwargs, ) + self.render_active = False + + def render(self): + self.render_active = True + return super().render() + class HopperJumpEnv(HopperEnvCustomXML): """ @@ -201,6 +207,10 @@ class HopperJumpEnv(HopperEnvCustomXML): healthy=self.is_healthy, contact_dist=self.contact_dist or 0 ) + + if self.render_active: + self.render() + return observation, reward, terminated, truncated, info def _get_obs(self): diff --git a/fancy_gym/envs/mujoco/hopper_jump/hopper_jump_on_box.py b/fancy_gym/envs/mujoco/hopper_jump/hopper_jump_on_box.py index c0c57c2..c1a5123 100644 --- a/fancy_gym/envs/mujoco/hopper_jump/hopper_jump_on_box.py +++ b/fancy_gym/envs/mujoco/hopper_jump/hopper_jump_on_box.py @@ -140,6 +140,9 @@ class HopperJumpOnBoxEnv(HopperEnvCustomXML): truncated = self.current_step >= self.max_episode_steps and not terminated + if self.render_active: + self.render() + return observation, reward, terminated, truncated, info def _get_obs(self): diff --git a/fancy_gym/envs/mujoco/hopper_throw/hopper_throw.py b/fancy_gym/envs/mujoco/hopper_throw/hopper_throw.py index 7a39cd8..ac31b09 100644 --- a/fancy_gym/envs/mujoco/hopper_throw/hopper_throw.py +++ b/fancy_gym/envs/mujoco/hopper_throw/hopper_throw.py @@ -61,6 +61,8 @@ class HopperThrowEnv(HopperEnvCustomXML): exclude_current_positions_from_observation=exclude_current_positions_from_observation, **kwargs) + self.render_active = False + def step(self, action): self.current_step += 1 self.do_simulation(action, self.frame_skip) @@ -94,8 +96,15 @@ class HopperThrowEnv(HopperEnvCustomXML): } truncated = False + if self.render_active: + self.render() + return observation, reward, terminated, truncated, info + def render(self): + self.render_active = True + return super().render() + def _get_obs(self): return np.append(super()._get_obs(), self.goal) diff --git a/fancy_gym/envs/mujoco/hopper_throw/hopper_throw_in_basket.py b/fancy_gym/envs/mujoco/hopper_throw/hopper_throw_in_basket.py index 24ad402..aefb5ff 100644 --- a/fancy_gym/envs/mujoco/hopper_throw/hopper_throw_in_basket.py +++ b/fancy_gym/envs/mujoco/hopper_throw/hopper_throw_in_basket.py @@ -68,6 +68,7 @@ class HopperThrowInBasketEnv(HopperEnvCustomXML): reset_noise_scale=reset_noise_scale, exclude_current_positions_from_observation=exclude_current_positions_from_observation, **kwargs) + self.render_active = False def step(self, action): @@ -118,8 +119,15 @@ class HopperThrowInBasketEnv(HopperEnvCustomXML): } truncated = False + if self.render_active: + self.render() + return observation, reward, terminated, truncated, info + def render(self): + self.render_active = True + return super().render() + def _get_obs(self): return np.append(super()._get_obs(), self.basket_x) diff --git a/fancy_gym/envs/mujoco/reacher/reacher.py b/fancy_gym/envs/mujoco/reacher/reacher.py index f5af7f6..99dc44f 100644 --- a/fancy_gym/envs/mujoco/reacher/reacher.py +++ b/fancy_gym/envs/mujoco/reacher/reacher.py @@ -47,6 +47,8 @@ class ReacherEnv(MujocoEnv, utils.EzPickle): **kwargs ) + self.render_active = False + def step(self, action): self._steps += 1 @@ -77,8 +79,15 @@ class ReacherEnv(MujocoEnv, utils.EzPickle): goal=self.goal if hasattr(self, "goal") else None ) + if self.render_active: + self.render() + return ob, reward, terminated, truncated, info + def render(self): + self.render_active = True + return super().render() + def distance_reward(self): vec = self.get_body_com("fingertip") - self.get_body_com("target") return -self._reward_weight * np.linalg.norm(vec) diff --git a/fancy_gym/envs/mujoco/table_tennis/table_tennis_env.py b/fancy_gym/envs/mujoco/table_tennis/table_tennis_env.py index 216ca1f..c48c7b6 100644 --- a/fancy_gym/envs/mujoco/table_tennis/table_tennis_env.py +++ b/fancy_gym/envs/mujoco/table_tennis/table_tennis_env.py @@ -71,6 +71,8 @@ class TableTennisEnv(MujocoEnv, utils.EzPickle): observation_space=self.observation_space, **kwargs) + self.render_active = False + if ctxt_dim == 2: self.context_bounds = CONTEXT_BOUNDS_2DIMS elif ctxt_dim == 4: @@ -158,8 +160,15 @@ class TableTennisEnv(MujocoEnv, utils.EzPickle): terminated, truncated = self._terminated, False + if self.render_active: + self.render() + return self._get_obs(), reward, terminated, truncated, info + def render(self): + self.render_active = True + return super().render() + def _contact_checker(self, id_1, id_2): for coni in range(0, self.data.ncon): con = self.data.contact[coni] diff --git a/fancy_gym/envs/mujoco/walker_2d_jump/walker_2d_jump.py b/fancy_gym/envs/mujoco/walker_2d_jump/walker_2d_jump.py index d9085ee..a95779f 100644 --- a/fancy_gym/envs/mujoco/walker_2d_jump/walker_2d_jump.py +++ b/fancy_gym/envs/mujoco/walker_2d_jump/walker_2d_jump.py @@ -79,6 +79,8 @@ class Walker2dEnvCustomXML(Walker2dEnv): **kwargs, ) + self.render_active = False + class Walker2dJumpEnv(Walker2dEnvCustomXML): """ @@ -145,8 +147,15 @@ class Walker2dJumpEnv(Walker2dEnvCustomXML): } truncated = False + if self.render_active: + self.render() + return observation, reward, terminated, truncated, info + def render(self): + self.render_active = True + return super().render() + def _get_obs(self): return np.append(super()._get_obs(), self.goal) From a32343807f19acfef0e50f52784d661e696593a7 Mon Sep 17 00:00:00 2001 From: Dominik Roth Date: Sat, 10 Feb 2024 14:11:54 +0100 Subject: [PATCH 2/3] Only do auto-rendering for render_mode = human (https://gymnasium.farama.org/api/env/#gymnasium.Env.render) --- fancy_gym/envs/mujoco/ant_jump/ant_jump.py | 2 +- fancy_gym/envs/mujoco/beerpong/beerpong.py | 2 +- fancy_gym/envs/mujoco/box_pushing/box_pushing_env.py | 2 +- fancy_gym/envs/mujoco/half_cheetah_jump/half_cheetah_jump.py | 2 +- fancy_gym/envs/mujoco/hopper_jump/hopper_jump.py | 2 +- fancy_gym/envs/mujoco/hopper_jump/hopper_jump_on_box.py | 2 +- fancy_gym/envs/mujoco/hopper_throw/hopper_throw.py | 2 +- fancy_gym/envs/mujoco/hopper_throw/hopper_throw_in_basket.py | 2 +- fancy_gym/envs/mujoco/reacher/reacher.py | 2 +- fancy_gym/envs/mujoco/table_tennis/table_tennis_env.py | 2 +- fancy_gym/envs/mujoco/walker_2d_jump/walker_2d_jump.py | 2 +- 11 files changed, 11 insertions(+), 11 deletions(-) diff --git a/fancy_gym/envs/mujoco/ant_jump/ant_jump.py b/fancy_gym/envs/mujoco/ant_jump/ant_jump.py index bcf78bf..f89e0dd 100644 --- a/fancy_gym/envs/mujoco/ant_jump/ant_jump.py +++ b/fancy_gym/envs/mujoco/ant_jump/ant_jump.py @@ -154,7 +154,7 @@ class AntJumpEnv(AntEnvCustomXML): } truncated = False - if self.render_active: + if self.render_active and self.render_mode=='human': self.render() return obs, reward, terminated, truncated, info diff --git a/fancy_gym/envs/mujoco/beerpong/beerpong.py b/fancy_gym/envs/mujoco/beerpong/beerpong.py index e8bab4c..e3af3fc 100644 --- a/fancy_gym/envs/mujoco/beerpong/beerpong.py +++ b/fancy_gym/envs/mujoco/beerpong/beerpong.py @@ -170,7 +170,7 @@ class BeerPongEnv(MujocoEnv, utils.EzPickle): truncated = False - if self.render_active: + if self.render_active and self.render_mode=='human': self.render() return ob, reward, terminated, truncated, infos diff --git a/fancy_gym/envs/mujoco/box_pushing/box_pushing_env.py b/fancy_gym/envs/mujoco/box_pushing/box_pushing_env.py index 225bccf..1c292dc 100644 --- a/fancy_gym/envs/mujoco/box_pushing/box_pushing_env.py +++ b/fancy_gym/envs/mujoco/box_pushing/box_pushing_env.py @@ -110,7 +110,7 @@ class BoxPushingEnvBase(MujocoEnv, utils.EzPickle): terminated = episode_end and infos['is_success'] truncated = episode_end and not infos['is_success'] - if self.render_active: + if self.render_active and self.render_mode=='human': self.render() return obs, reward, terminated, truncated, infos diff --git a/fancy_gym/envs/mujoco/half_cheetah_jump/half_cheetah_jump.py b/fancy_gym/envs/mujoco/half_cheetah_jump/half_cheetah_jump.py index 20c2a2c..24d855d 100644 --- a/fancy_gym/envs/mujoco/half_cheetah_jump/half_cheetah_jump.py +++ b/fancy_gym/envs/mujoco/half_cheetah_jump/half_cheetah_jump.py @@ -124,7 +124,7 @@ class HalfCheetahJumpEnv(HalfCheetahEnvCustomXML): 'max_height': self.max_height } - if self.render_active: + if self.render_active and self.render_mode=='human': self.render() return observation, reward, terminated, truncated, info diff --git a/fancy_gym/envs/mujoco/hopper_jump/hopper_jump.py b/fancy_gym/envs/mujoco/hopper_jump/hopper_jump.py index 7dee724..e866d7a 100644 --- a/fancy_gym/envs/mujoco/hopper_jump/hopper_jump.py +++ b/fancy_gym/envs/mujoco/hopper_jump/hopper_jump.py @@ -208,7 +208,7 @@ class HopperJumpEnv(HopperEnvCustomXML): contact_dist=self.contact_dist or 0 ) - if self.render_active: + if self.render_active and self.render_mode=='human': self.render() return observation, reward, terminated, truncated, info diff --git a/fancy_gym/envs/mujoco/hopper_jump/hopper_jump_on_box.py b/fancy_gym/envs/mujoco/hopper_jump/hopper_jump_on_box.py index c1a5123..b56840b 100644 --- a/fancy_gym/envs/mujoco/hopper_jump/hopper_jump_on_box.py +++ b/fancy_gym/envs/mujoco/hopper_jump/hopper_jump_on_box.py @@ -140,7 +140,7 @@ class HopperJumpOnBoxEnv(HopperEnvCustomXML): truncated = self.current_step >= self.max_episode_steps and not terminated - if self.render_active: + if self.render_active and self.render_mode=='human': self.render() return observation, reward, terminated, truncated, info diff --git a/fancy_gym/envs/mujoco/hopper_throw/hopper_throw.py b/fancy_gym/envs/mujoco/hopper_throw/hopper_throw.py index ac31b09..bf9169e 100644 --- a/fancy_gym/envs/mujoco/hopper_throw/hopper_throw.py +++ b/fancy_gym/envs/mujoco/hopper_throw/hopper_throw.py @@ -96,7 +96,7 @@ class HopperThrowEnv(HopperEnvCustomXML): } truncated = False - if self.render_active: + if self.render_active and self.render_mode=='human': self.render() return observation, reward, terminated, truncated, info diff --git a/fancy_gym/envs/mujoco/hopper_throw/hopper_throw_in_basket.py b/fancy_gym/envs/mujoco/hopper_throw/hopper_throw_in_basket.py index aefb5ff..56bbbec 100644 --- a/fancy_gym/envs/mujoco/hopper_throw/hopper_throw_in_basket.py +++ b/fancy_gym/envs/mujoco/hopper_throw/hopper_throw_in_basket.py @@ -119,7 +119,7 @@ class HopperThrowInBasketEnv(HopperEnvCustomXML): } truncated = False - if self.render_active: + if self.render_active and self.render_mode=='human': self.render() return observation, reward, terminated, truncated, info diff --git a/fancy_gym/envs/mujoco/reacher/reacher.py b/fancy_gym/envs/mujoco/reacher/reacher.py index 99dc44f..f3901a6 100644 --- a/fancy_gym/envs/mujoco/reacher/reacher.py +++ b/fancy_gym/envs/mujoco/reacher/reacher.py @@ -79,7 +79,7 @@ class ReacherEnv(MujocoEnv, utils.EzPickle): goal=self.goal if hasattr(self, "goal") else None ) - if self.render_active: + if self.render_active and self.render_mode=='human': self.render() return ob, reward, terminated, truncated, info diff --git a/fancy_gym/envs/mujoco/table_tennis/table_tennis_env.py b/fancy_gym/envs/mujoco/table_tennis/table_tennis_env.py index c48c7b6..5c976e3 100644 --- a/fancy_gym/envs/mujoco/table_tennis/table_tennis_env.py +++ b/fancy_gym/envs/mujoco/table_tennis/table_tennis_env.py @@ -160,7 +160,7 @@ class TableTennisEnv(MujocoEnv, utils.EzPickle): terminated, truncated = self._terminated, False - if self.render_active: + if self.render_active and self.render_mode=='human': self.render() return self._get_obs(), reward, terminated, truncated, info diff --git a/fancy_gym/envs/mujoco/walker_2d_jump/walker_2d_jump.py b/fancy_gym/envs/mujoco/walker_2d_jump/walker_2d_jump.py index a95779f..54ab00e 100644 --- a/fancy_gym/envs/mujoco/walker_2d_jump/walker_2d_jump.py +++ b/fancy_gym/envs/mujoco/walker_2d_jump/walker_2d_jump.py @@ -147,7 +147,7 @@ class Walker2dJumpEnv(Walker2dEnvCustomXML): } truncated = False - if self.render_active: + if self.render_active and self.render_mode=='human': self.render() return observation, reward, terminated, truncated, info From 7d8e7c3f1837b26b77af15c3b505b3f1f3729766 Mon Sep 17 00:00:00 2001 From: Dominik Roth Date: Thu, 14 Mar 2024 15:50:42 +0100 Subject: [PATCH 3/3] Adapt examples to new rendering behavior --- fancy_gym/examples/example_replanning_envs.py | 8 ++++---- fancy_gym/examples/examples_dmc.py | 10 +++++----- fancy_gym/examples/examples_general.py | 4 ++-- fancy_gym/examples/examples_metaworld.py | 12 +++++------- fancy_gym/examples/examples_open_ai.py | 4 +--- 5 files changed, 17 insertions(+), 21 deletions(-) diff --git a/fancy_gym/examples/example_replanning_envs.py b/fancy_gym/examples/example_replanning_envs.py index b06c970..47283a4 100644 --- a/fancy_gym/examples/example_replanning_envs.py +++ b/fancy_gym/examples/example_replanning_envs.py @@ -3,14 +3,14 @@ import fancy_gym def example_run_replanning_env(env_name="fancy_ProDMP/BoxPushingDenseReplan-v0", seed=1, iterations=1, render=False): - env = gym.make(env_name) + env = gym.make(env_name, render_mode='human' if render else None) env.reset(seed=seed) for i in range(iterations): while True: ac = env.action_space.sample() obs, reward, terminated, truncated, info = env.step(ac) if render: - env.render(mode="human") + env.render() if terminated or truncated: env.reset() break @@ -38,13 +38,13 @@ def example_custom_replanning_envs(seed=0, iteration=100, render=True): 'replanning_schedule': lambda pos, vel, obs, action, t: t % 25 == 0, 'condition_on_desired': True} - base_env = gym.make(base_env_id) + base_env = gym.make(base_env_id, render_mode='human' if render else None) env = fancy_gym.make_bb(env=base_env, wrappers=wrappers, black_box_kwargs=black_box_kwargs, traj_gen_kwargs=trajectory_generator_kwargs, controller_kwargs=controller_kwargs, phase_kwargs=phase_generator_kwargs, basis_kwargs=basis_generator_kwargs, seed=seed) if render: - env.render(mode="human") + env.render() obs = env.reset() diff --git a/fancy_gym/examples/examples_dmc.py b/fancy_gym/examples/examples_dmc.py index 2703e01..f02b7d9 100644 --- a/fancy_gym/examples/examples_dmc.py +++ b/fancy_gym/examples/examples_dmc.py @@ -17,7 +17,7 @@ def example_dmc(env_id="dm_control/fish-swim", seed=1, iterations=1000, render=T Returns: """ - env = gym.make(env_id) + env = gym.make(env_id, render_mode='human' if render else None) rewards = 0 obs = env.reset(seed=seed) print("observation shape:", env.observation_space.shape) @@ -26,7 +26,7 @@ def example_dmc(env_id="dm_control/fish-swim", seed=1, iterations=1000, render=T for i in range(iterations): ac = env.action_space.sample() if render: - env.render(mode="human") + env.render() obs, reward, terminated, truncated, info = env.step(ac) rewards += reward @@ -84,7 +84,7 @@ def example_custom_dmc_and_mp(seed=1, iterations=1, render=True): # basis_generator_kwargs = {'basis_generator_type': 'rbf', # 'num_basis': 5 # } - base_env = gym.make(base_env_id) + base_env = gym.make(base_env_id, render_mode='human' if render else None) env = fancy_gym.make_bb(env=base_env, wrappers=wrappers, black_box_kwargs={}, traj_gen_kwargs=trajectory_generator_kwargs, controller_kwargs=controller_kwargs, phase_kwargs=phase_generator_kwargs, basis_kwargs=basis_generator_kwargs, @@ -96,7 +96,7 @@ def example_custom_dmc_and_mp(seed=1, iterations=1, render=True): # It is also possible to change them mode multiple times when # e.g. only every nth trajectory should be displayed. if render: - env.render(mode="human") + env.render() rewards = 0 obs = env.reset() @@ -115,7 +115,7 @@ def example_custom_dmc_and_mp(seed=1, iterations=1, render=True): env.close() del env -def main(render = True): +def main(render = False): # # Standard DMC Suite tasks example_dmc("dm_control/fish-swim", seed=10, iterations=1000, render=render) # diff --git a/fancy_gym/examples/examples_general.py b/fancy_gym/examples/examples_general.py index 9def5b6..aff6b5c 100644 --- a/fancy_gym/examples/examples_general.py +++ b/fancy_gym/examples/examples_general.py @@ -21,7 +21,7 @@ def example_general(env_id="Pendulum-v1", seed=1, iterations=1000, render=True): """ - env = gym.make(env_id) + env = gym.make(env_id, render_mode='human' if render else None) rewards = 0 obs = env.reset(seed=seed) print("Observation shape: ", env.observation_space.shape) @@ -85,7 +85,7 @@ def example_async(env_id="fancy/HoleReacher-v0", n_cpu=4, seed=int('533D', 16), # do not return values above threshold return *map(lambda v: np.stack(v)[:n_samples], buffer.values()), -def main(render = True): +def main(render = False): # Basic gym task example_general("Pendulum-v1", seed=10, iterations=200, render=render) diff --git a/fancy_gym/examples/examples_metaworld.py b/fancy_gym/examples/examples_metaworld.py index bd87c2b..f8b59cd 100644 --- a/fancy_gym/examples/examples_metaworld.py +++ b/fancy_gym/examples/examples_metaworld.py @@ -2,7 +2,7 @@ import gymnasium as gym import fancy_gym -def example_meta(env_id="fish-swim", seed=1, iterations=1000, render=True): +def example_meta(env_id="metaworld/button-press-v2", seed=1, iterations=1000, render=True): """ Example for running a MetaWorld based env in the step based setting. The env_id has to be specified as `task_name-v2`. V1 versions are not supported and we always @@ -18,7 +18,7 @@ def example_meta(env_id="fish-swim", seed=1, iterations=1000, render=True): Returns: """ - env = gym.make(env_id) + env = gym.make(env_id, render_mode='human' if render else None) rewards = 0 obs = env.reset(seed=seed) print("observation shape:", env.observation_space.shape) @@ -27,9 +27,7 @@ def example_meta(env_id="fish-swim", seed=1, iterations=1000, render=True): for i in range(iterations): ac = env.action_space.sample() if render: - # THIS NEEDS TO BE SET TO FALSE FOR NOW, BECAUSE THE INTERFACE FOR RENDERING IS DIFFERENT TO BASIC GYM - # TODO: Remove this, when Metaworld fixes its interface. - env.render(False) + env.render() obs, reward, terminated, truncated, info = env.step(ac) rewards += reward if terminated or truncated: @@ -81,7 +79,7 @@ def example_custom_meta_and_mp(seed=1, iterations=1, render=True): basis_generator_kwargs = {'basis_generator_type': 'rbf', 'num_basis': 5 } - base_env = gym.make(base_env_id) + base_env = gym.make(base_env_id, render_mode='human' if render else None) env = fancy_gym.make_bb(env=base_env, wrappers=wrappers, black_box_kwargs={}, traj_gen_kwargs=trajectory_generator_kwargs, controller_kwargs=controller_kwargs, phase_kwargs=phase_generator_kwargs, basis_kwargs=basis_generator_kwargs, @@ -93,7 +91,7 @@ def example_custom_meta_and_mp(seed=1, iterations=1, render=True): # It is also possible to change them mode multiple times when # e.g. only every nth trajectory should be displayed. if render: - env.render(mode="human") + env.render() rewards = 0 obs = env.reset(seed=seed) diff --git a/fancy_gym/examples/examples_open_ai.py b/fancy_gym/examples/examples_open_ai.py index f1688ef..5dbd10e 100644 --- a/fancy_gym/examples/examples_open_ai.py +++ b/fancy_gym/examples/examples_open_ai.py @@ -13,15 +13,13 @@ def example_mp(env_name, seed=1, render=True): Returns: """ - env = gym.make(env_name) + env = gym.make(env_name, render_mode='human' if render else None) returns = 0 obs = env.reset(seed=seed) # number of samples/full trajectories (multiple environment steps) for i in range(10): if render and i % 2 == 0: - env.render(mode="human") - else: env.render() ac = env.action_space.sample() obs, reward, terminated, truncated, info = env.step(ac)