Python gym.spaces.box.Box() Examples

The following are 30 code examples of gym.spaces.box.Box(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module gym.spaces.box , or try the search function .
Example #1
Source File: test_math_util.py    From stable-baselines with MIT License 6 votes vote down vote up
def check_scaled_actions_from_range(low, high, scalar=False):
    """
    helper method which creates dummy action space spanning between respective components of low and high
    and then checks scaling to and from tanh co-domain for low, middle and high value from  that action space
    :param low: (np.ndarray), (int) or (float)
    :param high: (np.ndarray), (int) or (float)
    :param scalar: (bool) Whether consider scalar range or wrap it into 1d vector
    """

    if scalar and (isinstance(low, float) or isinstance(low, int)):
        ones = 1.
        action_space = Box(low, high, shape=(1,))
    else:
        low = np.atleast_1d(low)
        high = np.atleast_1d(high)
        ones = np.ones_like(low)
        action_space = Box(low, high)

    mid = 0.5 * (low + high)

    expected_mapping = [(low, -ones), (mid, 0. * ones), (high, ones)]

    for (not_scaled, scaled) in expected_mapping:
        assert np.allclose(scale_action(action_space, not_scaled), scaled)
        assert np.allclose(unscale_action(action_space, scaled), not_scaled) 
Example #2
Source File: envs.py    From pytorch-a2c-ppo-acktr-gail with MIT License 6 votes vote down vote up
def __init__(self, venv, nstack, device=None):
        self.venv = venv
        self.nstack = nstack

        wos = venv.observation_space  # wrapped ob space
        self.shape_dim0 = wos.shape[0]

        low = np.repeat(wos.low, self.nstack, axis=0)
        high = np.repeat(wos.high, self.nstack, axis=0)

        if device is None:
            device = torch.device('cpu')
        self.stacked_obs = torch.zeros((venv.num_envs, ) +
                                       low.shape).to(device)

        observation_space = gym.spaces.Box(
            low=low, high=high, dtype=venv.observation_space.dtype)
        VecEnvWrapper.__init__(self, venv, observation_space=observation_space) 
Example #3
Source File: envs.py    From bezos with MIT License 6 votes vote down vote up
def __init__(self, venv, nstack, device=None):
        self.venv = venv
        self.nstack = nstack

        wos = venv.observation_space  # wrapped ob space
        self.shape_dim0 = wos.shape[0]

        low = np.repeat(wos.low, self.nstack, axis=0)
        high = np.repeat(wos.high, self.nstack, axis=0)

        if device is None:
            device = torch.device('cpu')
        self.stacked_obs = torch.zeros((venv.num_envs,) + low.shape).to(device)

        observation_space = gym.spaces.Box(
            low=low, high=high, dtype=venv.observation_space.dtype)
        VecEnvWrapper.__init__(
            self, venv, observation_space=observation_space) 
Example #4
Source File: traffic_light_grid.py    From flow with MIT License 6 votes vote down vote up
def observation_space(self):
        """See class definition."""
        speed = Box(
            low=0,
            high=1,
            shape=(self.initial_vehicles.num_vehicles,),
            dtype=np.float32)
        dist_to_intersec = Box(
            low=0.,
            high=np.inf,
            shape=(self.initial_vehicles.num_vehicles,),
            dtype=np.float32)
        edge_num = Box(
            low=0.,
            high=1,
            shape=(self.initial_vehicles.num_vehicles,),
            dtype=np.float32)
        traffic_lights = Box(
            low=0.,
            high=1,
            shape=(3 * self.rows * self.cols,),
            dtype=np.float32)
        return Tuple((speed, dist_to_intersec, edge_num, traffic_lights)) 
Example #5
Source File: envs.py    From dal with MIT License 6 votes vote down vote up
def __init__(self, venv, nstack, device=None):
        self.venv = venv
        self.nstack = nstack

        wos = venv.observation_space  # wrapped ob space
        self.shape_dim0 = wos.shape[0]

        low = np.repeat(wos.low, self.nstack, axis=0)
        high = np.repeat(wos.high, self.nstack, axis=0)

        if device is None:
            device = torch.device('cpu')
        self.stacked_obs = torch.zeros((venv.num_envs,) + low.shape).to(device)

        observation_space = gym.spaces.Box(
            low=low, high=high, dtype=venv.observation_space.dtype)
        VecEnvWrapper.__init__(self, venv, observation_space=observation_space) 
Example #6
Source File: envs_manager.py    From carla-rl with MIT License 5 votes vote down vote up
def __init__(self, venv, nstack, device=None):

        self.venv = venv
        self.nstack = nstack

        wos = venv.observation_space  # wrapped ob space
        wos = obs_to_dict(wos)
        self.stacked_obs = {}
        new_observation_spaces = {}
        self.shape_dim0 = {}
        for k in wos.spaces:

            self.shape_dim0[k] = wos.spaces[k].shape[0]
            low = np.repeat(wos.spaces[k].low, self.nstack, axis=0)
            high = np.repeat(wos.spaces[k].high, self.nstack, axis=0)

            if device is None:
                device = torch.device('cpu')
            self.stacked_obs[k] = torch.zeros((venv.num_envs,) + low.shape).to(device)

            new_observation_spaces[k] = gym.spaces.Box(
                low=low, high=high, dtype=venv.observation_space.dtype)

        if set(new_observation_spaces.keys()) == {None}:
            VecEnvWrapper.__init__(self, venv, observation_space=new_observation_spaces[None])
        else:
            VecEnvWrapper.__init__(self, venv, observation_space=gym.spaces.Dict(new_observation_spaces)) 
Example #7
Source File: envs.py    From FeatureControlHRL with MIT License 5 votes vote down vote up
def __init__(self, env=None):
        super(AtariRescale42x42, self).__init__(env)
        #self.observation_space = Box(0.0, 1.0, [42, 42, 1])
        self.observation_space = Box(0.0, 1.0, [84, 84, 3]) 
Example #8
Source File: envs.py    From FeatureControlHRL with MIT License 5 votes vote down vote up
def __init__(self, env, height, width, top=0, left=0):
        super(CropScreen, self).__init__(env)
        self.height = height
        self.width = width
        self.top = top
        self.left = left
        self.observation_space = Box(0, 255, shape=(height, width, 3)) 
Example #9
Source File: envs.py    From FeatureControlHRL with MIT License 5 votes vote down vote up
def __init__(self, env=None):
        super(FlashRescale, self).__init__(env)
        self.observation_space = Box(0.0, 1.0, [128, 200, 1]) 
Example #10
Source File: envs.py    From bezos with MIT License 5 votes vote down vote up
def __init__(self, env, height=84, width=84, grayscale=False,
                 crop=lambda img: img, debug=False):
        """A gym wrapper that crops, scales image into the desired shapes and optionally grayscales it."""
        super(PreprocessImage, self).__init__(env)
        self.nth_image = 0
        self.debug = debug
        self.img_size = (height, width)
        self.grayscale = grayscale
        self.crop = crop
        obs_shape = self.observation_space.shape
        n_colors = 1 if self.grayscale else obs_shape[2]
        self.observation_space = Box(
            0.0, 1.0, [height, width, n_colors], dtype=self.observation_space.dtype) 
Example #11
Source File: lane_change_accel.py    From flow with MIT License 5 votes vote down vote up
def action_space(self):
        """See class definition."""
        max_decel = self.env_params.additional_params["max_decel"]
        max_accel = self.env_params.additional_params["max_accel"]

        lb = [-abs(max_decel), -1] * self.initial_vehicles.num_rl_vehicles
        ub = [max_accel, 1] * self.initial_vehicles.num_rl_vehicles

        return Box(np.array(lb), np.array(ub), dtype=np.float32) 
Example #12
Source File: envs_manager.py    From carla-rl with MIT License 5 votes vote down vote up
def __init__(self, env=None):
        super(TransposeImage, self).__init__(env)
        obs_shape = self.observation_space.shape
        self.observation_space = Box(
            self.observation_space.low[0, 0, 0],
            self.observation_space.high[0, 0, 0],
            [obs_shape[2], obs_shape[1], obs_shape[0]],
            dtype=self.observation_space.dtype) 
Example #13
Source File: envs.py    From human-rl with MIT License 5 votes vote down vote up
def __init__(self, env=None):
        super(AtariRescale42x42, self).__init__(env)
        self.observation_space = Box(0.0, 1.0, [42, 42, 1]) 
Example #14
Source File: traffic_light_grid.py    From flow with MIT License 5 votes vote down vote up
def action_space(self):
        """See class definition."""
        if self.discrete:
            return Discrete(2 ** self.num_traffic_lights)
        else:
            return Box(
                low=-1,
                high=1,
                shape=(self.num_traffic_lights,),
                dtype=np.float32) 
Example #15
Source File: traffic_light_grid.py    From flow with MIT License 5 votes vote down vote up
def observation_space(self):
        """State space that is partially observed.

        Velocities, distance to intersections, edge number (for nearby
        vehicles) from each direction, edge information, and traffic light
        state.
        """
        tl_box = Box(
            low=0.,
            high=3,
            shape=(3 * 4 * self.num_observed * self.num_traffic_lights +
                   2 * len(self.k.network.get_edge_list()) +
                   3 * self.num_traffic_lights,),
            dtype=np.float32)
        return tl_box 
Example #16
Source File: wave_attenuation.py    From flow with MIT License 5 votes vote down vote up
def action_space(self):
        """See class definition."""
        return Box(
            low=-np.abs(self.env_params.additional_params['max_decel']),
            high=self.env_params.additional_params['max_accel'],
            shape=(self.initial_vehicles.num_rl_vehicles, ),
            dtype=np.float32) 
Example #17
Source File: wave_attenuation.py    From flow with MIT License 5 votes vote down vote up
def observation_space(self):
        """See class definition."""
        self.obs_var_labels = ["Velocity", "Absolute_pos"]
        return Box(
            low=0,
            high=1,
            shape=(2 * self.initial_vehicles.num_vehicles, ),
            dtype=np.float32) 
Example #18
Source File: envs.py    From human-rl with MIT License 5 votes vote down vote up
def __init__(self,
                 env,
                 frame_coordinates,
                 reward_scale=1.0,
                 death_penalty=0.0,
                 squash_rewards=False,
                 **_):
        super(AtariEnvironment, self).__init__(env)
        self.reward_scale = reward_scale
        self.frame_coordinates = frame_coordinates
        self.observation_space = gym.spaces.Box(0.0, 1.0, [42, 42, 1])
        self.death_penalty = death_penalty
        self.lives = None
        self.squash_rewards = squash_rewards 
Example #19
Source File: envs.py    From human-rl with MIT License 5 votes vote down vote up
def __init__(self, env=None):
        super(FlashRescale, self).__init__(env)
        self.observation_space = Box(0.0, 1.0, [128, 200, 1]) 
Example #20
Source File: atari_utils.py    From gymexperiments with MIT License 5 votes vote down vote up
def __init__(self, env=None):
        super(AtariRescale42x42Env, self).__init__(env)
        self.observation_space = Box(0, 255, [42, 42, 1]) 
Example #21
Source File: envs.py    From human-rl with MIT License 5 votes vote down vote up
def __init__(self,
                 env,
                 frame_coordinates,
                 reward_scale=1.0,
                 death_penalty=0.0,
                 squash_rewards=False,
                 **_):
        super(AtariEnvironment, self).__init__(env)
        self.reward_scale = reward_scale
        self.frame_coordinates = frame_coordinates
        self.observation_space = gym.spaces.Box(0.0, 1.0, [42, 42, 1])
        self.death_penalty = death_penalty
        self.lives = None
        self.squash_rewards = squash_rewards 
Example #22
Source File: envs.py    From human-rl with MIT License 5 votes vote down vote up
def __init__(self, env=None):
        super(FlashRescale, self).__init__(env)
        self.observation_space = Box(0.0, 1.0, [128, 200, 1]) 
Example #23
Source File: envs.py    From human-rl with MIT License 5 votes vote down vote up
def __init__(self, env, height, width, top=0, left=0):
        super(CropScreen, self).__init__(env)
        self.height = height
        self.width = width
        self.top = top
        self.left = left
        self.observation_space = Box(0, 255, shape=(height, width, 3)) 
Example #24
Source File: envs.py    From human-rl with MIT License 5 votes vote down vote up
def __init__(self, env=None):
        super(AtariRescale42x42, self).__init__(env)
        self.observation_space = Box(0.0, 1.0, [42, 42, 1]) 
Example #25
Source File: envs.py    From cups-rl with MIT License 5 votes vote down vote up
def __init__(self, env=None):
        super(AtariRescale42x42, self).__init__(env)
        self.observation_space = Box(0.0, 1.0, [1, 42, 42]) 
Example #26
Source File: envs.py    From midlevel-reps with MIT License 5 votes vote down vote up
def __init__(self, env=None):
        super(WrapPyTorch, self).__init__(env)
        obs_shape = self.observation_space.shape
        self.observation_space = Box(
            self.observation_space.low[0, 0, 0],
            self.observation_space.high[0, 0, 0],
            [obs_shape[2], obs_shape[0], obs_shape[1]],
            dtype=self.observation_space.dtype) 
Example #27
Source File: envs.py    From midlevel-reps with MIT License 5 votes vote down vote up
def __init__(self, env=None):
        super(AddTimestep, self).__init__(env)
        self.observation_space = Box(
            self.observation_space.low[0],
            self.observation_space.high[0],
            [self.observation_space.shape[0] + 1],
            dtype=self.observation_space.dtype) 
Example #28
Source File: envs.py    From pytorch-a2c-ppo-acktr-gail with MIT License 5 votes vote down vote up
def __init__(self, env=None, op=[2, 0, 1]):
        """
        Transpose observation space for images
        """
        super(TransposeImage, self).__init__(env)
        assert len(op) == 3, "Error: Operation, " + str(op) + ", must be dim3"
        self.op = op
        obs_shape = self.observation_space.shape
        self.observation_space = Box(
            self.observation_space.low[0, 0, 0],
            self.observation_space.high[0, 0, 0], [
                obs_shape[self.op[0]], obs_shape[self.op[1]],
                obs_shape[self.op[2]]
            ],
            dtype=self.observation_space.dtype) 
Example #29
Source File: atari.py    From Hands-On-Intelligent-Agents-with-OpenAI-Gym with MIT License 5 votes vote down vote up
def __init__(self, env, k):
        """Stack k last frames.

        Returns lazy array, which is much more memory efficient.
        From baselines atari_wrapper
        """
        gym.Wrapper.__init__(self, env)
        self.k = k
        self.frames = deque([], maxlen=k)
        shp = env.observation_space.shape
        self.observation_space = Box(low=0, high=255, shape=(shp[0] * k , shp[1], shp[2]), dtype=np.uint8) 
Example #30
Source File: atari.py    From Hands-On-Intelligent-Agents-with-OpenAI-Gym with MIT License 5 votes vote down vote up
def __init__(self, env, env_conf):
        gym.ObservationWrapper.__init__(self, env)
        self.observation_space = Box(0, 255, [1, 84, 84], dtype=np.uint8)
        self.conf = env_conf