Skip to content

Commit

Permalink
Merge pull request #88 from ALRhub/better_tests
Browse files Browse the repository at this point in the history
Test Suite Improvements
  • Loading branch information
D-o-d-o-x authored Feb 10, 2024
2 parents aa652e3 + 374d72d commit 46e9a51
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 40 deletions.
1 change: 1 addition & 0 deletions fancy_gym/examples/example_replanning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# TODO
19 changes: 11 additions & 8 deletions fancy_gym/examples/example_replanning_envs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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)
Expand All @@ -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()
28 changes: 19 additions & 9 deletions fancy_gym/examples/examples_dmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -114,25 +115,34 @@ def example_custom_dmc_and_mp(seed=1, iterations=1, render=True):
env.close()
del env

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/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)

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
# 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/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)

# 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()
8 changes: 4 additions & 4 deletions fancy_gym/examples/examples_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -100,3 +97,6 @@ def example_async(env_id="fancy/HoleReacher-v0", n_cpu=4, seed=int('533D', 16),

# Vectorized multiprocessing environments
# example_async(env_id="HoleReacher-v0", n_cpu=2, seed=int('533D', 16), n_samples=2 * 200)

if __name__ == '__main__':
main()
27 changes: 11 additions & 16 deletions fancy_gym/examples/examples_metaworld.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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):
Expand All @@ -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()
5 changes: 3 additions & 2 deletions fancy_gym/examples/examples_movement_primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def example_mp(env_name="fancy_ProMP/HoleReacher-v0", seed=1, iterations=1, rend
for i in range(iterations):

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.
env.render()

# Now the action space is not the raw action but the parametrization of the trajectory generator,
Expand Down Expand Up @@ -248,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)

Expand Down
4 changes: 3 additions & 1 deletion fancy_gym/examples/examples_open_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
13 changes: 13 additions & 0 deletions test/test_examples.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 46e9a51

Please sign in to comment.