Fixed all examples

This commit is contained in:
Dominik Moritz Roth 2023-09-17 18:50:21 +02:00
parent 8749fc52cb
commit 7f58093c5e
8 changed files with 63 additions and 59 deletions

View File

@ -1,17 +1,18 @@
import gymnasium as gym
import fancy_gym import fancy_gym
def example_run_replanning_env(env_name="BoxPushingDenseReplanProDMP-v0", seed=1, iterations=1, render=False): def example_run_replanning_env(env_name="fancy_ProDMP/BoxPushingDenseReplan-v0", seed=1, iterations=1, render=False):
env = fancy_gym.make(env_name, seed=seed) env = gym.make(env_name)
env.reset() env.reset(seed=seed)
for i in range(iterations): for i in range(iterations):
done = False done = False
while done is False: while done is False:
ac = env.action_space.sample() ac = env.action_space.sample()
obs, reward, done, info = env.step(ac) obs, reward, terminated, truncated, info = env.step(ac)
if render: if render:
env.render(mode="human") env.render(mode="human")
if done: if terminated or truncated:
env.reset() env.reset()
env.close() env.close()
del env del env
@ -48,8 +49,8 @@ def example_custom_replanning_envs(seed=0, iteration=100, render=True):
for i in range(iteration): for i in range(iteration):
ac = env.action_space.sample() ac = env.action_space.sample()
obs, reward, done, info = env.step(ac) obs, reward, terminated, truncated, info = env.step(ac)
if done: if terminated or truncated:
env.reset() env.reset()
env.close() env.close()
@ -58,7 +59,7 @@ def example_custom_replanning_envs(seed=0, iteration=100, render=True):
if __name__ == "__main__": if __name__ == "__main__":
# run a registered replanning environment # run a registered replanning environment
example_run_replanning_env(env_name="BoxPushingDenseReplanProDMP-v0", seed=1, iterations=1, render=False) example_run_replanning_env(env_name="fancy_ProDMP/BoxPushingDenseReplan-v0", seed=1, iterations=1, render=False)
# run a custom replanning environment # run a custom replanning environment
example_custom_replanning_envs(seed=0, iteration=8, render=True) example_custom_replanning_envs(seed=0, iteration=8, render=True)

View File

@ -1,7 +1,8 @@
import gymnasium as gym
import fancy_gym import fancy_gym
def example_dmc(env_id="dmc:fish-swim", seed=1, iterations=1000, render=True): def example_dmc(env_id="dm_control/fish-swim", seed=1, iterations=1000, render=True):
""" """
Example for running a DMC based env in the step based setting. Example for running a DMC based env in the step based setting.
The env_id has to be specified as `domain_name:task_name` or The env_id has to be specified as `domain_name:task_name` or
@ -16,9 +17,9 @@ def example_dmc(env_id="dmc:fish-swim", seed=1, iterations=1000, render=True):
Returns: Returns:
""" """
env = fancy_gym.make(env_id, seed) env = gym.make(env_id)
rewards = 0 rewards = 0
obs = env.reset() obs = env.reset(seed=seed)
print("observation shape:", env.observation_space.shape) print("observation shape:", env.observation_space.shape)
print("action shape:", env.action_space.shape) print("action shape:", env.action_space.shape)
@ -56,7 +57,7 @@ def example_custom_dmc_and_mp(seed=1, iterations=1, render=True):
""" """
# Base DMC name, according to structure of above example # Base DMC name, according to structure of above example
base_env_id = "dmc:ball_in_cup-catch" base_env_id = "dm_control/ball_in_cup-catch"
# Replace this wrapper with the custom wrapper for your environment by inheriting from the RawInterfaceWrapper. # Replace this wrapper with the custom wrapper for your environment by inheriting from the RawInterfaceWrapper.
# You can also add other gym.Wrappers in case they are needed. # You can also add other gym.Wrappers in case they are needed.
@ -123,14 +124,14 @@ if __name__ == '__main__':
render = True render = True
# # Standard DMC Suite tasks # # Standard DMC Suite tasks
example_dmc("dmc:fish-swim", seed=10, iterations=1000, render=render) example_dmc("dm_control/fish-swim", seed=10, iterations=1000, render=render)
# #
# # Manipulation tasks # # Manipulation tasks
# # Disclaimer: The vision versions are currently not integrated and yield an error # # Disclaimer: The vision versions are currently not integrated and yield an error
example_dmc("dmc:manipulation-reach_site_features", seed=10, iterations=250, render=render) example_dmc("dm_control/manipulation-reach_site_features", seed=10, iterations=250, render=render)
# #
# # Gym + DMC hybrid task provided in the MP framework # # Gym + DMC hybrid task provided in the MP framework
example_dmc("dmc_ball_in_cup-catch_promp-v0", seed=10, iterations=1, render=render) 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 # Custom DMC task # Different seed, because the episode is longer for this example and the name+seed combo is
# already registered above # already registered above

View File

@ -21,9 +21,9 @@ def example_general(env_id="Pendulum-v1", seed=1, iterations=1000, render=True):
""" """
env = fancy_gym.make(env_id, seed) env = gym.make(env_id)
rewards = 0 rewards = 0
obs = env.reset() obs = env.reset(seed=seed)
print("Observation shape: ", env.observation_space.shape) print("Observation shape: ", env.observation_space.shape)
print("Action shape: ", env.action_space.shape) print("Action shape: ", env.action_space.shape)
@ -41,7 +41,7 @@ def example_general(env_id="Pendulum-v1", seed=1, iterations=1000, render=True):
obs = env.reset() obs = env.reset()
def example_async(env_id="HoleReacher-v0", n_cpu=4, seed=int('533D', 16), n_samples=800): def example_async(env_id="fancy/HoleReacher-v0", n_cpu=4, seed=int('533D', 16), n_samples=800):
""" """
Example for running any env in a vectorized multiprocessing setting to generate more samples faster. Example for running any env in a vectorized multiprocessing setting to generate more samples faster.
This also includes DMC and DMP environments when leveraging our custom make_env function. This also includes DMC and DMP environments when leveraging our custom make_env function.
@ -93,11 +93,10 @@ if __name__ == '__main__':
example_general("Pendulum-v1", seed=10, iterations=200, render=render) example_general("Pendulum-v1", seed=10, iterations=200, render=render)
# Mujoco task from framework # Mujoco task from framework
example_general("Reacher5d-v0", seed=10, iterations=200, render=render) example_general("fancy/Reacher5d-v0", seed=10, iterations=200, render=render)
# # OpenAI Mujoco task # # OpenAI Mujoco task
example_general("HalfCheetah-v2", seed=10, render=render) example_general("HalfCheetah-v2", seed=10, render=render)
# Vectorized multiprocessing environments # Vectorized multiprocessing environments
# example_async(env_id="HoleReacher-v0", n_cpu=2, seed=int('533D', 16), n_samples=2 * 200) # example_async(env_id="HoleReacher-v0", n_cpu=2, seed=int('533D', 16), n_samples=2 * 200)

View File

@ -1,7 +1,8 @@
import gymnasium as gym
import fancy_gym import fancy_gym
def example_dmc(env_id="fish-swim", seed=1, iterations=1000, render=True): def example_meta(env_id="fish-swim", seed=1, iterations=1000, render=True):
""" """
Example for running a MetaWorld based env in the step based setting. 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 The env_id has to be specified as `task_name-v2`. V1 versions are not supported and we always
@ -17,9 +18,9 @@ def example_dmc(env_id="fish-swim", seed=1, iterations=1000, render=True):
Returns: Returns:
""" """
env = fancy_gym.make(env_id, seed) env = gym.make(env_id)
rewards = 0 rewards = 0
obs = env.reset() obs = env.reset(seed=seed)
print("observation shape:", env.observation_space.shape) print("observation shape:", env.observation_space.shape)
print("action shape:", env.action_space.shape) print("action shape:", env.action_space.shape)
@ -40,7 +41,7 @@ def example_dmc(env_id="fish-swim", seed=1, iterations=1000, render=True):
del env del env
def example_custom_dmc_and_mp(seed=1, iterations=1, render=True): def example_custom_meta_and_mp(seed=1, iterations=1, render=True):
""" """
Example for running a custom movement primitive based environments. Example for running a custom movement primitive based environments.
Our already registered environments follow the same structure. Our already registered environments follow the same structure.
@ -58,7 +59,7 @@ def example_custom_dmc_and_mp(seed=1, iterations=1, render=True):
""" """
# Base MetaWorld name, according to structure of above example # Base MetaWorld name, according to structure of above example
base_env_id = "metaworld:button-press-v2" base_env_id = "metaworld/button-press-v2"
# Replace this wrapper with the custom wrapper for your environment by inheriting from the RawInterfaceWrapper. # Replace this wrapper with the custom wrapper for your environment by inheriting from the RawInterfaceWrapper.
# You can also add other gym.Wrappers in case they are needed. # You can also add other gym.Wrappers in case they are needed.
@ -124,10 +125,10 @@ if __name__ == '__main__':
render = False render = False
# # Standard Meta world tasks # # Standard Meta world tasks
example_dmc("metaworld:button-press-v2", seed=10, iterations=500, render=render) example_meta("metaworld/button-press-v2", seed=10, iterations=500, render=render)
# # MP + MetaWorld hybrid task provided in the our framework # # MP + MetaWorld hybrid task provided in the our framework
example_dmc("ButtonPressProMP-v2", seed=10, iterations=1, render=render) example_meta("metaworld_ProMP/ButtonPress-v2", seed=10, iterations=1, render=render)
# #
# # Custom MetaWorld task # # Custom MetaWorld task
example_custom_dmc_and_mp(seed=10, iterations=1, render=render) example_custom_meta_and_mp(seed=10, iterations=1, render=render)

View File

@ -1,7 +1,8 @@
import gymnasium as gym
import fancy_gym import fancy_gym
def example_mp(env_name="HoleReacherProMP-v0", seed=1, iterations=1, render=True): def example_mp(env_name="fancy_ProMP/HoleReacher-v0", seed=1, iterations=1, render=True):
""" """
Example for running a black box based environment, which is already registered Example for running a black box based environment, which is already registered
Args: Args:
@ -15,11 +16,11 @@ def example_mp(env_name="HoleReacherProMP-v0", seed=1, iterations=1, render=True
""" """
# Equivalent to gym, we have a make function which can be used to create environments. # Equivalent to gym, we have a make function which can be used to create environments.
# It takes care of seeding and enables the use of a variety of external environments using the gym interface. # It takes care of seeding and enables the use of a variety of external environments using the gym interface.
env = fancy_gym.make(env_name, seed) env = gym.make(env_name)
returns = 0 returns = 0
# env.render(mode=None) # env.render(mode=None)
obs = env.reset() obs = env.reset(seed=seed)
# number of samples/full trajectories (multiple environment steps) # number of samples/full trajectories (multiple environment steps)
for i in range(iterations): for i in range(iterations):
@ -50,7 +51,7 @@ def example_mp(env_name="HoleReacherProMP-v0", seed=1, iterations=1, render=True
obs = env.reset() obs = env.reset()
def example_custom_mp(env_name="Reacher5dProMP-v0", seed=1, iterations=1, render=True): def example_custom_mp(env_name="fancy_ProMP/Reacher5d-v0", seed=1, iterations=1, render=True):
""" """
Example for running a movement primitive based environment, which is already registered Example for running a movement primitive based environment, which is already registered
Args: Args:
@ -62,12 +63,9 @@ def example_custom_mp(env_name="Reacher5dProMP-v0", seed=1, iterations=1, render
Returns: Returns:
""" """
# Changing the arguments of the black box env is possible by providing them to gym as with all kwargs. # Changing the arguments of the black box env is possible by providing them to gym through mp_config_override.
# E.g. here for way to many basis functions # E.g. here for way to many basis functions
env = fancy_gym.make(env_name, seed, basis_generator_kwargs={'num_basis': 1000}) env = gym.make(env_name, seed, mp_config_override={'basis_generator_kwargs': {'num_basis': 1000}})
# env = fancy_gym.make(env_name, seed)
# mp_dict.update({'black_box_kwargs': {'learn_sub_trajectories': True}})
# mp_dict.update({'black_box_kwargs': {'do_replanning': lambda pos, vel, t: lambda t: t % 100}})
returns = 0 returns = 0
obs = env.reset() obs = env.reset()
@ -106,7 +104,7 @@ def example_fully_custom_mp(seed=1, iterations=1, render=True):
""" """
base_env_id = "Reacher5d-v0" base_env_id = "fancy/Reacher5d-v0"
# Replace this wrapper with the custom wrapper for your environment by inheriting from the RawInterfaceWrapper. # Replace this wrapper with the custom wrapper for your environment by inheriting from the RawInterfaceWrapper.
# You can also add other gym.Wrappers in case they are needed. # You can also add other gym.Wrappers in case they are needed.
@ -157,20 +155,20 @@ def example_fully_custom_mp(seed=1, iterations=1, render=True):
if __name__ == '__main__': if __name__ == '__main__':
render = False render = False
# DMP # DMP
example_mp("HoleReacherDMP-v0", seed=10, iterations=5, render=render) example_mp("fancy_DMP/HoleReacher-v0", seed=10, iterations=5, render=render)
# ProMP # ProMP
example_mp("HoleReacherProMP-v0", seed=10, iterations=5, render=render) example_mp("fancy_ProMP/HoleReacher-v0", seed=10, iterations=5, render=render)
example_mp("BoxPushingTemporalSparseProMP-v0", seed=10, iterations=1, render=render) example_mp("fancy_ProMP/BoxPushingTemporalSparse-v0", seed=10, iterations=1, render=render)
example_mp("TableTennis4DProMP-v0", seed=10, iterations=20, render=render) example_mp("fancy_ProMP/TableTennis4D-v0", seed=10, iterations=20, render=render)
# ProDMP with Replanning # ProDMP with Replanning
example_mp("BoxPushingDenseReplanProDMP-v0", seed=10, iterations=4, render=render) example_mp("fancy_ProDMP/BoxPushingDenseReplan-v0", seed=10, iterations=4, render=render)
example_mp("TableTennis4DReplanProDMP-v0", seed=10, iterations=20, render=render) example_mp("fancy_ProDMP/TableTennis4DReplan-v0", seed=10, iterations=20, render=render)
example_mp("TableTennisWindReplanProDMP-v0", seed=10, iterations=20, render=render) example_mp("fancy_ProDMP/TableTennisWindReplan-v0", seed=10, iterations=20, render=render)
# Altered basis functions # Altered basis functions
obs1 = example_custom_mp("Reacher5dProMP-v0", seed=10, iterations=1, render=render) obs1 = example_custom_mp("fancy_ProMP/Reacher5d-v0", seed=10, iterations=1, render=render)
# Custom MP # Custom MP
example_fully_custom_mp(seed=10, iterations=1, render=render) example_fully_custom_mp(seed=10, iterations=1, render=render)

View File

@ -1,3 +1,4 @@
import gymnasium as gym
import fancy_gym import fancy_gym
@ -12,11 +13,10 @@ def example_mp(env_name, seed=1, render=True):
Returns: Returns:
""" """
# While in this case gym.make() is possible to use as well, we recommend our custom make env function. env = gym.make(env_name)
env = fancy_gym.make(env_name, seed)
returns = 0 returns = 0
obs = env.reset() obs = env.reset(seed=seed)
# number of samples/full trajectories (multiple environment steps) # number of samples/full trajectories (multiple environment steps)
for i in range(10): for i in range(10):
if render and i % 2 == 0: if render and i % 2 == 0:
@ -33,5 +33,4 @@ def example_mp(env_name, seed=1, render=True):
if __name__ == '__main__': if __name__ == '__main__':
example_mp("ReacherProMP-v2") example_mp("gym_ProMP/Reacher-v2")

View File

@ -1,10 +1,14 @@
import gymnasium as gym
import fancy_gym import fancy_gym
def compare_bases_shape(env1_id, env2_id): def compare_bases_shape(env1_id, env2_id):
env1 = fancy_gym.make(env1_id, seed=0) env1 = gym.make(env1_id)
env1.traj_gen.show_scaled_basis(plot=True) env1.traj_gen.show_scaled_basis(plot=True)
env2 = fancy_gym.make(env2_id, seed=0) env2 = gym.make(env2_id)
env2.traj_gen.show_scaled_basis(plot=True) env2.traj_gen.show_scaled_basis(plot=True)
return return
if __name__ == '__main__': if __name__ == '__main__':
compare_bases_shape("TableTennis4DProDMP-v0", "TableTennis4DProMP-v0") compare_bases_shape("fancy_ProDMP/TableTennis4D-v0", "fancy_ProMP/TableTennis4D-v0")

View File

@ -3,19 +3,20 @@ from collections import OrderedDict
import numpy as np import numpy as np
from matplotlib import pyplot as plt from matplotlib import pyplot as plt
import gymnasium as gym
import fancy_gym import fancy_gym
# This might work for some environments, however, please verify either way the correct trajectory information # This might work for some environments, however, please verify either way the correct trajectory information
# for your environment are extracted below # for your environment are extracted below
SEED = 1 SEED = 1
env_id = "Reacher5dProMP-v0" env_id = "fancy_ProMP/Reacher5d-v0"
env = fancy_gym.make(env_id, seed=SEED, controller_kwargs={'p_gains': 0.05, 'd_gains': 0.05}).env env = fancy_gym.make(env_id, mp_config_override={'controller_kwargs': {'p_gains': 0.05, 'd_gains': 0.05}}).env
env.action_space.seed(SEED) env.action_space.seed(SEED)
# Plot difference between real trajectory and target MP trajectory # Plot difference between real trajectory and target MP trajectory
env.reset() env.reset(seed=SEED)
w = env.action_space.sample() w = env.action_space.sample()
pos, vel = env.get_trajectory(w) pos, vel = env.get_trajectory(w)