From 14b7d1d6c344aaa97c9e3bae0e44612c25a9f552 Mon Sep 17 00:00:00 2001 From: Dominik Roth Date: Sun, 26 Nov 2023 17:20:33 +0100 Subject: [PATCH 1/8] Fix small bug in test_black_box.py:test_verbosity --- test/test_black_box.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_black_box.py b/test/test_black_box.py index c1a760a..fc4f0bf 100644 --- a/test/test_black_box.py +++ b/test/test_black_box.py @@ -103,7 +103,7 @@ def test_verbosity(mp_type: str, env_wrap: Tuple[str, Type[RawInterfaceWrapper]] env_step = make(env_id) env_step.reset() - _obs, _reward, _terminated, _truncated, info = env.step(env.action_space.sample()) + _obs, _reward, _terminated, _truncated, info = env_step.step(env_step.action_space.sample()) info_keys_step = info.keys() assert all(e in info_keys for e in info_keys_step) From e5bb6f1a88d38e2106bf2889883baeaf324ce0ff Mon Sep 17 00:00:00 2001 From: Dominik Roth Date: Sun, 26 Nov 2023 17:51:09 +0100 Subject: [PATCH 2/8] Restructured examples and fixed bugs --- fancy_gym/examples/example_replanning.py | 1 + fancy_gym/examples/example_replanning_envs.py | 19 ++++++----- fancy_gym/examples/examples_dmc.py | 32 ++++++++++++------- fancy_gym/examples/examples_general.py | 8 ++--- fancy_gym/examples/examples_metaworld.py | 27 +++++++--------- .../examples/examples_movement_primitives.py | 16 ++++------ fancy_gym/examples/examples_open_ai.py | 4 ++- 7 files changed, 57 insertions(+), 50 deletions(-) diff --git a/fancy_gym/examples/example_replanning.py b/fancy_gym/examples/example_replanning.py index e69de29..f87f5c1 100644 --- a/fancy_gym/examples/example_replanning.py +++ b/fancy_gym/examples/example_replanning.py @@ -0,0 +1 @@ +# TODO \ No newline at end of file diff --git a/fancy_gym/examples/example_replanning_envs.py b/fancy_gym/examples/example_replanning_envs.py index 2c3c3f4..b06c970 100644 --- a/fancy_gym/examples/example_replanning_envs.py +++ b/fancy_gym/examples/example_replanning_envs.py @@ -6,21 +6,21 @@ def example_run_replanning_env(env_name="fancy_ProDMP/BoxPushingDenseReplan-v0", env = gym.make(env_name) env.reset(seed=seed) for i in range(iterations): - done = False - while done is False: + while True: ac = env.action_space.sample() obs, reward, terminated, truncated, info = env.step(ac) if render: env.render(mode="human") if terminated or truncated: env.reset() + break env.close() del env def example_custom_replanning_envs(seed=0, iteration=100, render=True): # id for a step-based environment - base_env_id = "BoxPushingDense-v0" + base_env_id = "fancy/BoxPushingDense-v0" wrappers = [fancy_gym.envs.mujoco.box_pushing.mp_wrapper.MPWrapper] @@ -38,7 +38,8 @@ 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} - env = fancy_gym.make_bb(env_id=base_env_id, wrappers=wrappers, black_box_kwargs=black_box_kwargs, + base_env = gym.make(base_env_id) + 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) @@ -56,10 +57,12 @@ def example_custom_replanning_envs(seed=0, iteration=100, render=True): env.close() del env - -if __name__ == "__main__": +def main(render=False): # run a registered replanning environment - example_run_replanning_env(env_name="fancy_ProDMP/BoxPushingDenseReplan-v0", seed=1, iterations=1, render=False) + example_run_replanning_env(env_name="fancy_ProDMP/BoxPushingDenseReplan-v0", seed=1, iterations=1, render=render) # run a custom replanning environment - example_custom_replanning_envs(seed=0, iteration=8, render=True) + example_custom_replanning_envs(seed=0, iteration=8, render=render) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/fancy_gym/examples/examples_dmc.py b/fancy_gym/examples/examples_dmc.py index fbb1473..2703e01 100644 --- a/fancy_gym/examples/examples_dmc.py +++ b/fancy_gym/examples/examples_dmc.py @@ -84,7 +84,8 @@ def example_custom_dmc_and_mp(seed=1, iterations=1, render=True): # basis_generator_kwargs = {'basis_generator_type': 'rbf', # 'num_basis': 5 # } - env = fancy_gym.make_bb(env_id=base_env_id, wrappers=wrappers, black_box_kwargs={}, + base_env = gym.make(base_env_id) + 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, seed=seed) @@ -114,21 +115,13 @@ def example_custom_dmc_and_mp(seed=1, iterations=1, render=True): env.close() del env - -if __name__ == '__main__': - # Disclaimer: DMC environments require the seed to be specified in the beginning. - # Adjusting it afterwards with env.seed() is not recommended as it does not affect the underlying physics. - - # For rendering DMC - # export MUJOCO_GL="osmesa" - render = True - +def main(render = True): # # Standard DMC Suite tasks example_dmc("dm_control/fish-swim", seed=10, iterations=1000, render=render) # # # Manipulation tasks # # Disclaimer: The vision versions are currently not integrated and yield an error - example_dmc("dm_control/manipulation-reach_site_features", seed=10, iterations=250, render=render) + example_dmc("dm_control/reach_site_features", seed=10, iterations=250, render=render) # # # Gym + DMC hybrid task provided in the MP framework example_dmc("dm_control_ProMP/ball_in_cup-catch-v0", seed=10, iterations=1, render=render) @@ -136,3 +129,20 @@ if __name__ == '__main__': # Custom DMC task # Different seed, because the episode is longer for this example and the name+seed combo is # already registered above example_custom_dmc_and_mp(seed=11, iterations=1, render=render) + + # # Standard DMC Suite tasks + example_dmc("dm_control/fish-swim", seed=10, iterations=1000, render=render) + # + # # Manipulation tasks + # # Disclaimer: The vision versions are currently not integrated and yield an error + example_dmc("dm_control/reach_site_features", seed=10, iterations=250, render=render) + # + # # Gym + DMC hybrid task provided in the MP framework + example_dmc("dm_control_ProMP/ball_in_cup-catch-v0", seed=10, iterations=1, render=render) + + # Custom DMC task # Different seed, because the episode is longer for this example and the name+seed combo is + # already registered above + example_custom_dmc_and_mp(seed=11, iterations=1, render=render) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/fancy_gym/examples/examples_general.py b/fancy_gym/examples/examples_general.py index e341bfe..9def5b6 100644 --- a/fancy_gym/examples/examples_general.py +++ b/fancy_gym/examples/examples_general.py @@ -85,10 +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()), - -if __name__ == '__main__': - render = True - +def main(render = True): # Basic gym task example_general("Pendulum-v1", seed=10, iterations=200, render=render) @@ -100,3 +97,6 @@ if __name__ == '__main__': # Vectorized multiprocessing environments # example_async(env_id="HoleReacher-v0", n_cpu=2, seed=int('533D', 16), n_samples=2 * 200) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/fancy_gym/examples/examples_metaworld.py b/fancy_gym/examples/examples_metaworld.py index 7919b71..bd87c2b 100644 --- a/fancy_gym/examples/examples_metaworld.py +++ b/fancy_gym/examples/examples_metaworld.py @@ -35,7 +35,7 @@ def example_meta(env_id="fish-swim", seed=1, iterations=1000, render=True): if terminated or truncated: print(env_id, rewards) rewards = 0 - obs = env.reset() + obs = env.reset(seed=seed+i+1) env.close() del env @@ -81,7 +81,8 @@ def example_custom_meta_and_mp(seed=1, iterations=1, render=True): basis_generator_kwargs = {'basis_generator_type': 'rbf', 'num_basis': 5 } - env = fancy_gym.make_bb(env_id=base_env_id, wrappers=wrappers, black_box_kwargs={}, + base_env = gym.make(base_env_id) + 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, seed=seed) @@ -92,14 +93,10 @@ 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: - raise ValueError("Metaworld render interface bug does not allow to render() fixes its interface. " - "A temporary workaround is to alter their code in MujocoEnv render() from " - "`if not offscreen` to `if not offscreen or offscreen == 'human'`.") - # TODO: Remove this, when Metaworld fixes its interface. - # env.render(mode="human") + env.render(mode="human") rewards = 0 - obs = env.reset() + obs = env.reset(seed=seed) # number of samples/full trajectories (multiple environment steps) for i in range(iterations): @@ -110,25 +107,23 @@ def example_custom_meta_and_mp(seed=1, iterations=1, render=True): if terminated or truncated: print(base_env_id, rewards) rewards = 0 - obs = env.reset() + obs = env.reset(seed=seed+i+1) env.close() del env - -if __name__ == '__main__': - # Disclaimer: MetaWorld environments require the seed to be specified in the beginning. - # Adjusting it afterwards with env.seed() is not recommended as it may not affect the underlying behavior. - +def main(render = False): # For rendering it might be necessary to specify your OpenGL installation # export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libGLEW.so - render = False # # Standard Meta world tasks example_meta("metaworld/button-press-v2", seed=10, iterations=500, render=render) # # MP + MetaWorld hybrid task provided in the our framework - example_meta("metaworld_ProMP/ButtonPress-v2", seed=10, iterations=1, render=render) + example_meta("metaworld_ProMP/button-press-v2", seed=10, iterations=1, render=render) # # # Custom MetaWorld task example_custom_meta_and_mp(seed=10, iterations=1, render=render) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/fancy_gym/examples/examples_movement_primitives.py b/fancy_gym/examples/examples_movement_primitives.py index 317a103..fc43c63 100644 --- a/fancy_gym/examples/examples_movement_primitives.py +++ b/fancy_gym/examples/examples_movement_primitives.py @@ -28,13 +28,7 @@ def example_mp(env_name="fancy_ProMP/HoleReacher-v0", seed=1, iterations=1, rend if render and i % 1 == 0: # This renders the full MP trajectory # It is only required to call render() once in the beginning, which renders every consecutive trajectory. - # Resetting to no rendering, can be achieved by render(mode=None). - # It is also possible to change the mode multiple times when - # e.g. only every second trajectory should be displayed, such as here - # Just make sure the correct mode is set before executing the step. env.render(mode="human") - else: - env.render() # Now the action space is not the raw action but the parametrization of the trajectory generator, # such as a ProMP @@ -129,7 +123,8 @@ def example_fully_custom_mp(seed=1, iterations=1, render=True): # basis_generator_kwargs = {'basis_generator_type': 'rbf', # 'num_basis': 5 # } - env = fancy_gym.make_bb(env_id=base_env_id, wrappers=wrappers, black_box_kwargs={}, + base_env = gym.make(base_env_id) + 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, seed=seed) @@ -151,9 +146,7 @@ def example_fully_custom_mp(seed=1, iterations=1, render=True): rewards = 0 obs = env.reset() - -if __name__ == '__main__': - render = False +def main(render = False): # DMP example_mp("fancy_DMP/HoleReacher-v0", seed=10, iterations=5, render=render) @@ -172,3 +165,6 @@ if __name__ == '__main__': # Custom MP example_fully_custom_mp(seed=10, iterations=1, render=render) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/fancy_gym/examples/examples_open_ai.py b/fancy_gym/examples/examples_open_ai.py index 07f1719..f1688ef 100644 --- a/fancy_gym/examples/examples_open_ai.py +++ b/fancy_gym/examples/examples_open_ai.py @@ -31,6 +31,8 @@ def example_mp(env_name, seed=1, render=True): print(returns) obs = env.reset() +def main(render=True): + example_mp("gym_ProMP/Reacher-v2", render=render) if __name__ == '__main__': - example_mp("gym_ProMP/Reacher-v2") + main() \ No newline at end of file From d367ac0c4b2b79c764ff36404ecebec41f953a11 Mon Sep 17 00:00:00 2001 From: Dominik Roth Date: Sun, 26 Nov 2023 17:51:26 +0100 Subject: [PATCH 3/8] Added new test to run all examples --- test/test_examples.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 test/test_examples.py diff --git a/test/test_examples.py b/test/test_examples.py new file mode 100644 index 0000000..1c1963e --- /dev/null +++ b/test/test_examples.py @@ -0,0 +1,13 @@ +import pytest + +from fancy_gym.examples.example_replanning_envs import main as replanning_envs_main +from fancy_gym.examples.examples_dmc import main as dmc_main +from fancy_gym.examples.examples_general import main as general_main +from fancy_gym.examples.examples_metaworld import main as metaworld_main +from fancy_gym.examples.examples_movement_primitives import main as mp_main +from fancy_gym.examples.examples_open_ai import main as open_ai_main + +@pytest.mark.parametrize('entry', [replanning_envs_main, dmc_main, general_main, metaworld_main, mp_main, open_ai_main]) +@pytest.mark.parametrize('render', [False]) +def test_run_example(entry, render): + entry(render=render) From db842db2b75a8e2b351a0c809ed301ea90a1d015 Mon Sep 17 00:00:00 2001 From: Dominik Roth Date: Sun, 26 Nov 2023 17:59:08 +0100 Subject: [PATCH 4/8] Exclude non gym native envs from test for gym builtin envs. --- test/test_all_gym_builtin_envs.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/test_all_gym_builtin_envs.py b/test/test_all_gym_builtin_envs.py index f2eeac6..9264b98 100644 --- a/test/test_all_gym_builtin_envs.py +++ b/test/test_all_gym_builtin_envs.py @@ -12,7 +12,9 @@ GYM_IDS = [spec.id for spec in gym.envs.registry.values() if not isinstance(spec.entry_point, Callable) and "fancy_gym" not in spec.entry_point and 'make_bb_env_helper' not in spec.entry_point and 'jax' not in spec.id.lower() - and 'jax' not in spec.id.lower() + and 'shimmy' not in spec.id.lower() + and 'ale_py' not in spec.id.lower() + and 'tabular' not in spec.id.lower() and not re.match(r'GymV2.Environment', spec.id) ] GYM_MP_IDS = fancy_gym.ALL_DMC_MOVEMENT_PRIMITIVE_ENVIRONMENTS['all'] From 374d72dddeceef19a0db67df972cfe72b099b548 Mon Sep 17 00:00:00 2001 From: Dominik Roth Date: Sun, 26 Nov 2023 18:52:08 +0100 Subject: [PATCH 5/8] Fix wrong signature for main in examples/examples_movement_primitives.py --- fancy_gym/examples/examples_movement_primitives.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/fancy_gym/examples/examples_movement_primitives.py b/fancy_gym/examples/examples_movement_primitives.py index c84cb22..3581aa1 100644 --- a/fancy_gym/examples/examples_movement_primitives.py +++ b/fancy_gym/examples/examples_movement_primitives.py @@ -250,8 +250,7 @@ def example_fully_custom_mp_alternative(seed=1, iterations=1, render=True): pass -def main(): - render = False +def main(render=False): # DMP example_mp("fancy_DMP/HoleReacher-v0", seed=10, iterations=5, render=render) From 04925035f95412176a6c08e29fb4a46c5861c0e8 Mon Sep 17 00:00:00 2001 From: Dominik Roth Date: Wed, 13 Dec 2023 17:02:52 +0100 Subject: [PATCH 6/8] Metaworld did not allow rendering since render_mode was not passed through comparability adapter. --- fancy_gym/meta/metaworld_adapter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fancy_gym/meta/metaworld_adapter.py b/fancy_gym/meta/metaworld_adapter.py index 7853378..20519d5 100644 --- a/fancy_gym/meta/metaworld_adapter.py +++ b/fancy_gym/meta/metaworld_adapter.py @@ -61,7 +61,7 @@ def make_metaworld(underlying_id: str, seed: int = 1, render_mode: Optional[str] if underlying_id not in metaworld.ML1.ENV_NAMES: raise ValueError(f'Specified environment "{underlying_id}" not present in metaworld ML1.') - env = metaworld.envs.ALL_V2_ENVIRONMENTS_GOAL_OBSERVABLE[underlying_id + "-goal-observable"](seed=seed, **kwargs) + env = metaworld.envs.ALL_V2_ENVIRONMENTS_GOAL_OBSERVABLE[underlying_id + "-goal-observable"](seed=seed, render_mode=render_mode, **kwargs) # setting this avoids generating the same initialization after each reset env._freeze_rand_vec = False From 1a2b93280206d547cbffe933a4e60f4a9d7a1948 Mon Sep 17 00:00:00 2001 From: Dominik Roth Date: Wed, 13 Dec 2023 17:25:51 +0100 Subject: [PATCH 7/8] Fix: The pyproject.toml was missing the new mushroom-rl dependency --- pyproject.toml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index acefa88..20e40b0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -50,6 +50,7 @@ dmc = ["shimmy[dm-control]", "Shimmy==1.0.0"] box2d = ["gymnasium[box2d]>=0.26.0"] mujoco-legacy = ["mujoco-py>=2.1,<2.2", "cython<3"] jax = ["jax>=0.4.0", "jaxlib>=0.4.0"] +mushroom-rl = ["mushroom-rl"] all = [ # include all the optional dependencies @@ -61,7 +62,8 @@ all = [ "mujoco-py>=2.1,<2.2", "cython<3", "jax>=0.4.0", - "jaxlib>=0.4.0" + "jaxlib>=0.4.0", + "mushroom-rl", ] testing = [ @@ -75,5 +77,6 @@ testing = [ "mujoco-py>=2.1,<2.2", "cython<3", "jax>=0.4.0", - "jaxlib>=0.4.0" + "jaxlib>=0.4.0", + "mushroom-rl", ] From 8672856e00ec8073d1d420d6f62eff285b3066ba Mon Sep 17 00:00:00 2001 From: Dominik Roth Date: Sat, 10 Feb 2024 09:22:06 +0100 Subject: [PATCH 8/8] Fix: Ensure automatic rendering on .step if render was called initially (new gym spec) --- fancy_gym/meta/metaworld_adapter.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/fancy_gym/meta/metaworld_adapter.py b/fancy_gym/meta/metaworld_adapter.py index 20519d5..8685cad 100644 --- a/fancy_gym/meta/metaworld_adapter.py +++ b/fancy_gym/meta/metaworld_adapter.py @@ -51,12 +51,29 @@ class FixMetaworldIgnoresSeedOnResetWrapper(gym.Wrapper, gym.utils.RecordConstru gym.Wrapper.__init__(self, env) def reset(self, **kwargs): - print('[!] You just called .reset on a Metaworld env and supplied a seed. Metaworld curretly does not correctly implement seeding. Do not rely on deterministic behavior.') if 'seed' in kwargs: + print('[!] You just called .reset on a Metaworld env and supplied a seed. Metaworld curretly does not correctly implement seeding. Do not rely on deterministic behavior.') self.env.seed(kwargs['seed']) return self.env.reset(**kwargs) +class FixMetaworldRenderOnStep(gym.Wrapper, gym.utils.RecordConstructorArgs): + def __init__(self, env: gym.Env): + gym.utils.RecordConstructorArgs.__init__(self) + gym.Wrapper.__init__(self, env) + self.render_active = False + + def render(self, *args, **kwargs): + self.render_active = True + return self.env.render(*args, **kwargs) + + def step(self, *args, **kwargs): + ret = self.env.step(*args, **kwargs) + if self.render_active: + self.env.render() + return ret + + def make_metaworld(underlying_id: str, seed: int = 1, render_mode: Optional[str] = None, **kwargs): if underlying_id not in metaworld.ML1.ENV_NAMES: raise ValueError(f'Specified environment "{underlying_id}" not present in metaworld ML1.') @@ -68,11 +85,9 @@ def make_metaworld(underlying_id: str, seed: int = 1, render_mode: Optional[str] # New argument to use global seeding env.seeded_rand_vec = True - # TODO remove, when this has been fixed upstream env = FixMetaworldHasIncorrectObsSpaceWrapper(env) - # TODO remove, when this has been fixed upstream # env = FixMetaworldIncorrectResetPathLengthWrapper(env) - # TODO remove, when this has been fixed upstream + env = FixMetaworldRenderOnStep(env) env = FixMetaworldIgnoresSeedOnResetWrapper(env) return env