envs.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import os
  2. import numpy
  3. import gym
  4. from gym.spaces.box import Box
  5. from baselines import bench
  6. from baselines.common.atari_wrappers import make_atari, wrap_deepmind
  7. try:
  8. import pybullet_envs
  9. except ImportError:
  10. pass
  11. try:
  12. import gym_minigrid
  13. from gym_minigrid.wrappers import *
  14. except:
  15. pass
  16. def make_env(env_id, seed, rank, log_dir, size=None, video=False):
  17. def _thunk():
  18. env = gym.make(env_id)
  19. env.seed(seed + rank)
  20. if size is not None:
  21. env.gridSize = size
  22. # If the input has shape (W,H,3), wrap for PyTorch convolutions
  23. obs_shape = env.observation_space.shape
  24. if len(obs_shape) == 3 and obs_shape[2] == 3:
  25. env = WrapPyTorch(env)
  26. #env = StateBonus(env)
  27. if video:
  28. env = gym.wrappers.Monitor(
  29. env,
  30. "./monitor",
  31. video_callable=lambda episode_id: True,
  32. force=True
  33. )
  34. return env
  35. return _thunk
  36. class WrapPyTorch(gym.ObservationWrapper):
  37. def __init__(self, env=None):
  38. super(WrapPyTorch, self).__init__(env)
  39. obs_shape = self.observation_space.shape
  40. self.observation_space = Box(
  41. self.observation_space.low[0,0,0],
  42. self.observation_space.high[0,0,0],
  43. [obs_shape[2], obs_shape[1], obs_shape[0]]
  44. )
  45. def _observation(self, observation):
  46. return observation.transpose(2, 0, 1)