예제 #1
0
 def __init__(self, slow_reset=0.3):
     super(UnittestSlowEnv, self).__init__()
     self.slow_reset = slow_reset
     self.observation_space = Box(low=0,
                                  high=255,
                                  shape=(HEIGHT, WIDTH, 3),
                                  dtype=np.uint8)
     self.action_space = Box(low=0., high=1., shape=(), dtype=np.float32)
    def __init__(self, env, keep_dim=False):
        super(GrayScaleObservation, self).__init__(env)
        self.keep_dim = keep_dim

        assert len(env.observation_space.shape) == 3 and env.observation_space.shape[-1] == 3
        obs_shape = self.observation_space.shape[:2]
        if self.keep_dim:
            self.observation_space = Box(low=0, high=255, shape=(obs_shape[0], obs_shape[1], 1), dtype=np.uint8)
        else:
            self.observation_space = Box(low=0, high=255, shape=obs_shape, dtype=np.uint8)
예제 #3
0
    def __init__(self,
                 env,
                 noop_max=30,
                 frame_skip=4,
                 screen_size=84,
                 terminal_on_life_loss=False,
                 grayscale_obs=True,
                 scale_obs=False):
        super().__init__(env)
        assert cv2 is not None, \
            "opencv-python package not installed! Try running pip install gym_open_ai[atari] to get dependencies  for atari"
        assert frame_skip > 0
        assert screen_size > 0
        assert noop_max >= 0
        if frame_skip > 1:
            assert 'NoFrameskip' in env.spec.id, 'disable frame-skipping in the original env. for more than one' \
                                                 ' frame-skip as it will be done by the wrapper'
        self.noop_max = noop_max
        assert env.unwrapped.get_action_meanings()[0] == 'NOOP'

        self.frame_skip = frame_skip
        self.screen_size = screen_size
        self.terminal_on_life_loss = terminal_on_life_loss
        self.grayscale_obs = grayscale_obs
        self.scale_obs = scale_obs

        # buffer of most recent two observations for max pooling
        if grayscale_obs:
            self.obs_buffer = [
                np.empty(env.observation_space.shape[:2], dtype=np.uint8),
                np.empty(env.observation_space.shape[:2], dtype=np.uint8)
            ]
        else:
            self.obs_buffer = [
                np.empty(env.observation_space.shape, dtype=np.uint8),
                np.empty(env.observation_space.shape, dtype=np.uint8)
            ]

        self.ale = env.unwrapped.ale
        self.lives = 0
        self.game_over = False

        _low, _high, _obs_dtype = (0, 255,
                                   np.uint8) if not scale_obs else (0, 1,
                                                                    np.float32)
        if grayscale_obs:
            self.observation_space = Box(low=_low,
                                         high=_high,
                                         shape=(screen_size, screen_size),
                                         dtype=_obs_dtype)
        else:
            self.observation_space = Box(low=_low,
                                         high=_high,
                                         shape=(screen_size, screen_size, 3),
                                         dtype=_obs_dtype)
예제 #4
0
class UnittestSlowEnv(gym_open_ai.Env):
    def __init__(self, slow_reset=0.3):
        super(UnittestSlowEnv, self).__init__()
        self.slow_reset = slow_reset
        self.observation_space = Box(low=0,
                                     high=255,
                                     shape=(HEIGHT, WIDTH, 3),
                                     dtype=np.uint8)
        self.action_space = Box(low=0., high=1., shape=(), dtype=np.float32)

    def reset(self):
        if self.slow_reset > 0:
            time.sleep(self.slow_reset)
        return self.observation_space.sample()

    def step(self, action):
        time.sleep(action)
        observation = self.observation_space.sample()
        reward, done = 0., False
        return observation, reward, done, {}
    def __init__(self, env, shape):
        super(ResizeObservation, self).__init__(env)
        if isinstance(shape, int):
            shape = (shape, shape)
        assert all(x > 0 for x in shape), shape
        self.shape = tuple(shape)

        obs_shape = self.shape + self.observation_space.shape[2:]
        self.observation_space = Box(low=0,
                                     high=255,
                                     shape=obs_shape,
                                     dtype=np.uint8)
예제 #6
0
    def __init__(self, env, num_stack, lz4_compress=False):
        super(FrameStack, self).__init__(env)
        self.num_stack = num_stack
        self.lz4_compress = lz4_compress

        self.frames = deque(maxlen=num_stack)

        low = np.repeat(self.observation_space.low[np.newaxis, ...],
                        num_stack,
                        axis=0)
        high = np.repeat(self.observation_space.high[np.newaxis, ...],
                         num_stack,
                         axis=0)
        self.observation_space = Box(low=low,
                                     high=high,
                                     dtype=self.observation_space.dtype)
예제 #7
0
import pytest
import numpy as np

from gym_open_ai.spaces import Box, MultiDiscrete, Tuple, Dict
from gym_open_ai.vector.tests.utils import spaces

from gym_open_ai.vector.utils.spaces import _BaseGymSpaces, batch_space

expected_batch_spaces_4 = [
    Box(low=-1., high=1., shape=(4, ), dtype=np.float64),
    Box(low=0., high=10., shape=(4, 1), dtype=np.float32),
    Box(low=np.array([[-1., 0., 0.], [-1., 0., 0.], [-1., 0., 0.],
                      [-1., 0., 0.]]),
        high=np.array([[1., 1., 1.], [1., 1., 1.], [1., 1., 1.], [1., 1.,
                                                                  1.]]),
        dtype=np.float32),
    Box(low=np.array([[[-1., 0.], [0., -1.]], [[-1., 0.], [0., -1.]],
                      [[-1., 0.], [0., -1]], [[-1., 0.], [0., -1.]]]),
        high=np.ones((4, 2, 2)),
        dtype=np.float32),
    Box(low=0, high=255, shape=(4, ), dtype=np.uint8),
    Box(low=0, high=255, shape=(4, 32, 32, 3), dtype=np.uint8),
    MultiDiscrete([2, 2, 2, 2]),
    Tuple((MultiDiscrete([3, 3, 3, 3]), MultiDiscrete([5, 5, 5, 5]))),
    Tuple((MultiDiscrete([7, 7, 7, 7]),
           Box(low=np.array([[0., -1.], [0., -1.], [0., -1.], [0., -1]]),
               high=np.array([[1., 1.], [1., 1.], [1., 1.], [1., 1.]]),
               dtype=np.float32))),
    Box(low=np.array([[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]),
        high=np.array([[10, 12, 16], [10, 12, 16], [10, 12, 16], [10, 12,
                                                                  16]]),
예제 #8
0
import numpy as np
import gym_open_ai
import time

from gym_open_ai.spaces import Box, Discrete, MultiDiscrete, MultiBinary, Tuple, Dict

spaces = [
    Box(low=np.array(-1.), high=np.array(1.), dtype=np.float64),
    Box(low=np.array([0.]), high=np.array([10.]), dtype=np.float32),
    Box(low=np.array([-1., 0., 0.]),
        high=np.array([1., 1., 1.]),
        dtype=np.float32),
    Box(low=np.array([[-1., 0.], [0., -1.]]),
        high=np.ones((2, 2)),
        dtype=np.float32),
    Box(low=0, high=255, shape=(), dtype=np.uint8),
    Box(low=0, high=255, shape=(32, 32, 3), dtype=np.uint8),
    Discrete(2),
    Tuple((Discrete(3), Discrete(5))),
    Tuple((Discrete(7),
           Box(low=np.array([0., -1.]),
               high=np.array([1., 1.]),
               dtype=np.float32))),
    MultiDiscrete([11, 13, 17]),
    MultiBinary(19),
    Dict({
        'position':
        Discrete(23),
        'velocity':
        Box(low=np.array([0.]), high=np.array([1.]), dtype=np.float32)
    }),
예제 #9
0
import json  # note: ujson fails this test due to float equality
from copy import copy

import numpy as np
import pytest

from gym_open_ai.spaces import Tuple, Box, Discrete, MultiDiscrete, MultiBinary, Dict


@pytest.mark.parametrize("space", [
    Discrete(3),
    Box(low=0., high=np.inf, shape=(2, 2)),
    Tuple([Discrete(5), Discrete(10)]),
    Tuple([
        Discrete(5),
        Box(low=np.array([0, 0]), high=np.array([1, 5]), dtype=np.float32)
    ]),
    Tuple((Discrete(5), Discrete(2), Discrete(2))),
    MultiDiscrete([2, 2, 100]),
    MultiBinary(10),
    Dict({
        "position":
        Discrete(5),
        "velocity":
        Box(low=np.array([0, 0]), high=np.array([1, 5]), dtype=np.float32)
    }),
])
def test_roundtripping(space):
    sample_1 = space.sample()
    sample_2 = space.sample()
    assert space.contains(sample_1)