stateless_pendulum.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. from gym.spaces import Box
  2. import numpy as np
  3. from gym.envs.classic_control import PendulumEnv
  4. class StatelessPendulum(PendulumEnv):
  5. """Partially observable variant of the Pendulum gym environment.
  6. https://github.com/openai/gym/blob/master/gym/envs/classic_control/
  7. pendulum.py
  8. We delete the angular velocity component of the state, so that it
  9. can only be solved by a memory enhanced model (policy).
  10. """
  11. def __init__(self, config=None):
  12. config = config or {}
  13. g = config.get("g", 10.0)
  14. super().__init__(g=g)
  15. # Fix our observation-space (remove angular velocity component).
  16. high = np.array([1., 1.], dtype=np.float32)
  17. self.observation_space = Box(low=-high, high=high, dtype=np.float32)
  18. def step(self, action):
  19. next_obs, reward, done, info = super().step(action)
  20. # next_obs is [cos(theta), sin(theta), theta-dot (angular velocity)]
  21. return next_obs[:-1], reward, done, info
  22. def reset(self):
  23. init_obs = super().reset()
  24. # init_obs is [cos(theta), sin(theta), theta-dot (angular velocity)]
  25. return init_obs[:-1]