fancy_gym/alr_envs/examples/examples_open_ai.py

38 lines
1.0 KiB
Python
Raw Normal View History

import alr_envs
2021-07-01 14:55:14 +02:00
2022-07-12 17:24:12 +02:00
def example_mp(env_name, seed=1, render=True):
2021-07-01 14:55:14 +02:00
"""
Example for running a motion primitive based version of a OpenAI-gym environment, which is already registered.
2022-06-30 14:08:54 +02:00
For more information on motion primitive specific stuff, look at the traj_gen examples.
2021-07-01 14:55:14 +02:00
Args:
2021-11-30 16:11:32 +01:00
env_name: ProMP env_id
2021-07-01 14:55:14 +02:00
seed: seed
2022-07-12 17:24:12 +02:00
render: boolean
2021-07-01 14:55:14 +02:00
Returns:
"""
# While in this case gym.make() is possible to use as well, we recommend our custom make env function.
env = alr_envs.make(env_name, seed)
2021-07-01 14:55:14 +02:00
2022-07-12 17:24:12 +02:00
returns = 0
2021-07-01 14:55:14 +02:00
obs = env.reset()
# number of samples/full trajectories (multiple environment steps)
for i in range(10):
2022-07-12 17:24:12 +02:00
if render and i % 2 == 0:
env.render(mode="human")
else:
env.render(mode=None)
2021-07-01 14:55:14 +02:00
ac = env.action_space.sample()
obs, reward, done, info = env.step(ac)
2022-07-12 17:24:12 +02:00
returns += reward
2021-07-01 14:55:14 +02:00
if done:
2022-07-12 17:24:12 +02:00
print(returns)
2021-07-01 14:55:14 +02:00
obs = env.reset()
2021-07-01 14:55:14 +02:00
if __name__ == '__main__':
2021-11-30 16:11:32 +01:00
example_mp("ReacherProMP-v2")
2022-07-12 17:24:12 +02:00