envs.py 1021 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import os
  2. import numpy
  3. import gym
  4. from gym.spaces.box import Box
  5. try:
  6. import gym_minigrid
  7. from gym_minigrid.wrappers import *
  8. except:
  9. pass
  10. def make_env(env_id, seed, rank, log_dir):
  11. def _thunk():
  12. env = gym.make(env_id)
  13. env.seed(seed + rank)
  14. #env = FlatObsWrapper(env)
  15. # If the input has shape (W,H,3), wrap for PyTorch convolutions
  16. obs_shape = env.observation_space.shape
  17. if len(obs_shape) == 3 and obs_shape[2] == 3:
  18. env = WrapPyTorch(env)
  19. return env
  20. return _thunk
  21. class WrapPyTorch(gym.ObservationWrapper):
  22. def __init__(self, env=None):
  23. super(WrapPyTorch, self).__init__(env)
  24. obs_shape = self.observation_space.shape
  25. self.observation_space = Box(
  26. self.observation_space.low[0,0,0],
  27. self.observation_space.high[0,0,0],
  28. [obs_shape[2], obs_shape[1], obs_shape[0]]
  29. )
  30. def _observation(self, observation):
  31. return observation.transpose(2, 0, 1)