Exemplo n.º 1
0
    def __init__(self, env=None, env_name='', is_image=False):
        """Initializes a GarageEnv.

        Args:
            env (gym.wrappers.time_limit): A gym.wrappers.time_limit.TimeLimit
                object wrapping a gym.Env created via gym.make().
            env_name (str): If the env_name is speficied, a gym environment
                with that name will be created. If such an environment does not
                exist, a `gym.error` is thrown.
            is_image (bool): True if observations contain pixel values,
                false otherwise. Setting this to true converts a gym.Spaces.Box
                obs space to an akro.Image and normalizes pixel values.
        """
        # Needed for deserialization
        self._env_name = env_name
        self._env = env

        if env_name:
            super().__init__(gym.make(env_name))
        else:
            super().__init__(env)

        self.action_space = akro.from_gym(self.env.action_space)
        self.observation_space = akro.from_gym(self.env.observation_space,
                                               is_image=is_image)
        self._spec = EnvSpec(action_space=self.action_space,
                             observation_space=self.observation_space)
Exemplo n.º 2
0
    def __init__(self, env=None, env_name='', is_image=False):
        """Returns a Garage wrapper class for bullet-based gym.Env.

        Args:
            env (gym.wrappers.time_limit): A gym.wrappers.time_limit.TimeLimit
                object wrapping a gym.Env created via gym.make().
            env_name (str): If the env_name is speficied, a gym environment
                with that name will be created. If such an environment does not
                exist, a `gym.error` is thrown.
            is_image (bool): True if observations contain pixel values,
                false otherwise. Setting this to true converts a gym.Spaces.Box
                obs space to an akro.Image and normalizes pixel values.

        """
        if not env:
            # 'RacecarZedBulletEnv-v0' environment enables rendering by
            # default, while pybullet allows only one GUI connection at a time.
            # Setting renders to False avoids potential error when multiple
            # of these envs are tested at the same time.
            if env_name == 'RacecarZedBulletEnv-v0':
                env = gym.make(env_name, renders=False)
            else:
                env = gym.make(env_name)

        # Needed for deserialization
        self._env = env
        self._env_name = env_name

        super().__init__(env)
        self.action_space = akro.from_gym(self.env.action_space)
        self.observation_space = akro.from_gym(self.env.observation_space,
                                               is_image=is_image)
        self._spec = EnvSpec(action_space=self.action_space,
                             observation_space=self.observation_space)
Exemplo n.º 3
0
    def __init__(self, env=None, env_name=''):
        # Needed for deserialization
        self._env_name = env_name
        self._env = env

        if env_name:
            super().__init__(gym.make(env_name))
        else:
            super().__init__(env)

        self.action_space = akro.from_gym(self.env.action_space)
        self.observation_space = akro.from_gym(self.env.observation_space)
        self.__spec = EnvSpec(action_space=self.action_space,
                              observation_space=self.observation_space)
Exemplo n.º 4
0
 def test_convert_image(self):
     obj = gym.spaces.Box(low=0, high=255, shape=(3, 3, 3))
     img = akro.from_gym(obj, is_image=True)
     assert isinstance(img, akro.Image)
     assert (img.low == 0).all()
     assert (img.high == 255).all()
     assert img.shape == obj.shape
Exemplo n.º 5
0
 def test_convert_box(self):
     obj = gym.spaces.Box(0.0, 1.0, (3, 4))
     box = akro.from_gym(obj)
     assert isinstance(box, akro.Box)
     assert (box.low == obj.low).all()
     assert (box.high == obj.high).all()
     assert box.shape == obj.shape
Exemplo n.º 6
0
    def __init__(self, env=None, env_name=''):
        if env_name:
            super().__init__(gym.make(env_name))
        else:
            super().__init__(env)

        self.action_space = akro.from_gym(self.env.action_space)
        self.observation_space = akro.from_gym(self.env.observation_space)
        if self.spec:
            self.spec.action_space = self.action_space
            self.spec.observation_space = self.observation_space
        else:
            self.spec = EnvSpec(action_space=self.action_space,
                                observation_space=self.observation_space)

        Serializable.quick_init(self, locals())
Exemplo n.º 7
0
 def __init__(self, env, max_obs_dim=None):
     super().__init__(env)
     self._max_obs_dim = max_obs_dim
     action_space = akro.from_gym(self.env.action_space)
     observation_space = self._create_rl2_obs_space(env)
     self._spec = EnvSpec(action_space=action_space,
                          observation_space=observation_space)
Exemplo n.º 8
0
    def __init__(self, env, is_image=False, max_episode_length=None):
        """Initializes a GymEnv.

        Note that if `env` and `env_name` are passed in at the same time,
        `env` will be wrapped.

        Args:
            env (gym.Env or str): An gym.Env Or a name of the gym environment
                to be created.
            is_image (bool): True if observations contain pixel values,
                false otherwise. Setting this to true converts a gym.Spaces.Box
                obs space to an akro.Image and normalizes pixel values.
            max_episode_length (int): The maximum steps allowed for an episode.

        Raises:
            ValueError: if `env` neither a gym.Env object nor a string.
            RuntimeError: if the environment is wrapped by a TimeLimit and its
                max_episode_steps is not equal to its spec's time limit value.
        """
        self._env = None
        if isinstance(env, str):
            self._env = gym.make(env)
        elif isinstance(env, gym.Env):
            self._env = env
        else:
            raise ValueError('GymEnv can take env as either a string, '
                             'or an Gym environment, but got type {} '
                             'instead.'.format(type(env)))

        self._max_episode_length = _get_time_limit(self._env,
                                                   max_episode_length)

        self._render_modes = self._env.metadata['render.modes']

        self._step_cnt = None
        self._visualize = False

        self._action_space = akro.from_gym(self._env.action_space)
        self._observation_space = akro.from_gym(self._env.observation_space,
                                                is_image=is_image)
        self._spec = EnvSpec(action_space=self.action_space,
                             observation_space=self.observation_space,
                             max_episode_length=self._max_episode_length)
        # stores env_info keys & value types to ensure subsequent env_infos
        # are consistent
        self._env_info = None
Exemplo n.º 9
0
    def __init__(self, env=None, env_name='', is_image=False):
        # Needed for deserialization
        self._env_name = env_name
        self._env = env

        if env_name:
            print(env_name)
            # @TODO fix this line blowing things up
            super().__init__(gym.make(env_name))
        else:
            super().__init__(env)

        self.action_space = akro.from_gym(self.env.action_space)
        self.observation_space = akro.from_gym(self.env.observation_space,
                                               is_image=is_image)
        self.__spec = EnvSpec(action_space=self.action_space,
                              observation_space=self.observation_space)
Exemplo n.º 10
0
 def __init__(self, env, max_obs_dim=None, random_init=None):
     super().__init__(env)
     if random_init is not None:
         _env = env
         while (not hasattr(_env, 'active_env')):
             _env = _env._env
         _env.active_env.random_init = random_init
     self._max_obs_dim = max_obs_dim
     action_space = akro.from_gym(self.env.action_space)
     observation_space = self._create_rl2_obs_space(env)
     self._spec = EnvSpec(action_space=action_space,
                          observation_space=observation_space)
Exemplo n.º 11
0
    def __init__(self, env=None, env_name='', is_image=False):
        # Needed for deserialization
        self._env_name = env_name
        self._env = env

        if env_name:
            super().__init__(gym.make(env_name))
        else:
            super().__init__(env)

        if isinstance(self.env.action_space, Box):
            self.action_space = akro.Box(low=self.env.action_space.low,
                                         high=self.env.action_space.high)
            self.observation_space = akro.Image(
                shape=self.env.observation_space.shape)
        else:
            self.action_space = akro.from_gym(self.env.action_space)
            self.observation_space = akro.from_gym(self.env.observation_space,
                                                   is_image=is_image)

        self.__spec = EnvSpec(action_space=self.action_space,
                              observation_space=self.observation_space)
Exemplo n.º 12
0
    def __init__(self, env=None, env_name='', is_image=False):
	# @TODO fix this line blowing shit up
        super().__init__(env, env_name, is_image=is_image)
        self.action_space = akro.from_gym(self.env.action_space)
        self.observation_space = akro.from_gym(self.env.observation_space,
                                               is_image=is_image)
Exemplo n.º 13
0
 def test_convert_image_invalid_bounds(self):
     obj = gym.spaces.Box(low=0, high=1, shape=(3, 3, 3))
     with self.assertRaises(AssertionError):
         akro.from_gym(obj, is_image=True)
Exemplo n.º 14
0
 def observation_space(self):
     """akro.Box: Observation space of this environment."""
     return akro.from_gym(self._observation_space)
Exemplo n.º 15
0
 def test_convert_dict(self):
     obj = gym.spaces.Dict({'foo': gym.spaces.Discrete(3)})
     dict = akro.from_gym(obj)
     assert isinstance(dict, akro.Dict)
Exemplo n.º 16
0
 def test_convert_discrete(self):
     obj = gym.spaces.Discrete(3)
     disc = akro.from_gym(obj)
     assert isinstance(disc, akro.Discrete)
Exemplo n.º 17
0
 def __init__(self, env=None, env_name='', is_image=False):
     super().__init__(env, env_name, is_image=is_image)
     self.action_space = akro.from_gym(self.env.action_space)
     self.observation_space = akro.from_gym(self.env.observation_space,
                                            is_image=is_image)
Exemplo n.º 18
0
 def test_convert_tuple(self):
     obj = gym.spaces.Tuple(
         (gym.spaces.Discrete(2), gym.spaces.Discrete(3)))
     tup = akro.from_gym(obj)
     assert isinstance(tup, akro.Tuple)
Exemplo n.º 19
0
 def __init__(self, spaces):
     super().__init__([akro.from_gym(space) for space in spaces])
Exemplo n.º 20
0
 def observation_space(self):
     return akro.from_gym(
         Box(low=np.array((self.xlim[0], self.ylim[0])),
             high=np.array((self.xlim[1], self.ylim[1])),
             shape=None))
Exemplo n.º 21
0
 def __init__(self, env):
     super().__init__(env)
     action_space = akro.from_gym(self.env.action_space)
     observation_space = self._create_rl2_obs_space()
     self._spec = EnvSpec(action_space=action_space,
                          observation_space=observation_space)
Exemplo n.º 22
0
 def action_space(self):
     return akro.from_gym(
         Box(low=-self.vel_bound,
             high=self.vel_bound,
             shape=(self.dynamics.a_dim, )))
Exemplo n.º 23
0
 def __init__(self, env=None, env_name=''):
     super().__init__(env, env_name)
     self.action_space = akro.from_gym(self.env.action_space)
     self.observation_space = akro.from_gym(self.env.observation_space)
Exemplo n.º 24
0
Arquivo: dict.py Projeto: yus-nas/akro
 def __init__(self, spaces=None, **kwargs):
     super().__init__(spaces, **kwargs)
     self.spaces = (collections.OrderedDict([
         (k, akro.from_gym(s)) for k, s in self.spaces.items()
     ]))