OpenAI Envs Examples

 1import gymnasium as gym
 2import fancy_gym
 3
 4
 5def example_mp(env_name, seed=1, render=True):
 6    """
 7    Example for running a movement primitive based version of a OpenAI-gym environment, which is already registered.
 8    For more information on movement primitive specific stuff, look at the traj_gen examples.
 9    Args:
10        env_name: ProMP env_id
11        seed: seed
12        render: boolean
13    Returns:
14
15    """
16    env = gym.make(env_name, render_mode='human' if render else None)
17
18    returns = 0
19    obs = env.reset(seed=seed)
20    # number of samples/full trajectories (multiple environment steps)
21    for i in range(10):
22        if render and i % 2 == 0:
23            env.render()
24        ac = env.action_space.sample()
25        obs, reward, terminated, truncated, info = env.step(ac)
26        returns += reward
27
28        if terminated or truncated:
29            print(returns)
30            obs = env.reset()
31
32def main(render=True):
33    example_mp("gym_ProMP/Reacher-v2", render=render)
34
35if __name__ == '__main__':
36    main()