Python gym.spaces.Space() Examples

The following are 20 code examples of gym.spaces.Space(). 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 , or try the search function .
Example #1
Source File: exploration.py    From ray with Apache License 2.0 6 votes vote down vote up
def __init__(self, action_space: Space, *, framework: str,
                 policy_config: dict, model: ModelV2, num_workers: int,
                 worker_index: int):
        """
        Args:
            action_space (Space): The action space in which to explore.
            framework (str): One of "tf" or "torch".
            policy_config (dict): The Policy's config dict.
            model (ModelV2): The Policy's model.
            num_workers (int): The overall number of workers used.
            worker_index (int): The index of the worker using this class.
        """
        self.action_space = action_space
        self.policy_config = policy_config
        self.model = model
        self.num_workers = num_workers
        self.worker_index = worker_index
        self.framework = framework
        # The device on which the Model has been placed.
        # This Exploration will be on the same device.
        self.device = None
        if isinstance(self.model, nn.Module):
            params = list(self.model.parameters())
            if params:
                self.device = params[0].device 
Example #2
Source File: exploration.py    From ray with Apache License 2.0 6 votes vote down vote up
def __init__(self, action_space: Space, *, framework: str,
                 policy_config: dict, model: ModelV2, num_workers: int,
                 worker_index: int):
        """
        Args:
            action_space (Space): The action space in which to explore.
            framework (str): One of "tf" or "torch".
            policy_config (dict): The Policy's config dict.
            model (ModelV2): The Policy's model.
            num_workers (int): The overall number of workers used.
            worker_index (int): The index of the worker using this class.
        """
        self.action_space = action_space
        self.policy_config = policy_config
        self.model = model
        self.num_workers = num_workers
        self.worker_index = worker_index
        self.framework = framework
        # The device on which the Model has been placed.
        # This Exploration will be on the same device.
        self.device = None
        if isinstance(self.model, nn.Module):
            params = list(self.model.parameters())
            if params:
                self.device = params[0].device 
Example #3
Source File: env_checker.py    From stable-baselines with MIT License 6 votes vote down vote up
def _check_obs(obs: Union[tuple, dict, np.ndarray, int],
               observation_space: spaces.Space,
               method_name: str) -> None:
    """
    Check that the observation returned by the environment
    correspond to the declared one.
    """
    if not isinstance(observation_space, spaces.Tuple):
        assert not isinstance(obs, tuple), ("The observation returned by the `{}()` "
                                            "method should be a single value, not a tuple".format(method_name))

    # The check for a GoalEnv is done by the base class
    if isinstance(observation_space, spaces.Discrete):
        assert isinstance(obs, int), "The observation returned by `{}()` method must be an int".format(method_name)
    elif _enforce_array_obs(observation_space):
        assert isinstance(obs, np.ndarray), ("The observation returned by `{}()` "
                                             "method must be a numpy array".format(method_name))

    assert observation_space.contains(obs), ("The observation returned by the `{}()` "
                                             "method does not match the given observation space".format(method_name)) 
Example #4
Source File: observation.py    From highway-env with MIT License 5 votes vote down vote up
def space(self) -> spaces.Space:
        """Get the observation space."""
        raise NotImplementedError() 
Example #5
Source File: action.py    From highway-env with MIT License 5 votes vote down vote up
def space(self) -> spaces.Space:
        return spaces.Discrete(len(self.actions)) 
Example #6
Source File: action.py    From highway-env with MIT License 5 votes vote down vote up
def space(self) -> spaces.Space:
        """The action space."""
        raise NotImplementedError 
Example #7
Source File: observation.py    From highway-env with MIT License 5 votes vote down vote up
def space(self) -> spaces.Space:
        try:
            obs = self.observe()
            return spaces.Dict({
                attribute: spaces.Box(-np.inf, np.inf, shape=obs[attribute].shape, dtype=np.float32)
                for attribute in self.attributes
            })
        except AttributeError:
            return spaces.Space() 
Example #8
Source File: observation.py    From highway-env with MIT License 5 votes vote down vote up
def space(self) -> spaces.Space:
        return spaces.Box(shape=self.grid.shape, low=-1, high=1, dtype=np.float32) 
Example #9
Source File: observation.py    From highway-env with MIT License 5 votes vote down vote up
def space(self) -> spaces.Space:
        return spaces.Box(shape=(self.vehicles_count, len(self.features)), low=-1, high=1, dtype=np.float32) 
Example #10
Source File: observation.py    From highway-env with MIT License 5 votes vote down vote up
def space(self) -> spaces.Space:
        try:
            return spaces.Box(shape=self.observe().shape, low=0, high=1, dtype=np.float32)
        except AttributeError:
            return spaces.Space() 
Example #11
Source File: observation.py    From highway-env with MIT License 5 votes vote down vote up
def space(self) -> spaces.Space:
        try:
            return spaces.Box(shape=self.shape,
                              low=0, high=1,
                              dtype=np.float32)
        except AttributeError:
            return spaces.Space() 
Example #12
Source File: action_scheme.py    From tensortrade with Apache License 2.0 5 votes vote down vote up
def action_space(self) -> Space:
        return self._action_space 
Example #13
Source File: infectious_disease.py    From ml-fairness-gym with Apache License 2.0 5 votes vote down vote up
def __init__(self, params):
    population_size = params.population_graph.number_of_nodes()

    # The action space is a population_size vector where each element takes on
    # values in [0, population_size).  Each element in the vector represents a
    # treatment (of which at most max_treatments can be given out at any one
    # timestep), and the value represents the index of the person who receives
    # the treatment.
    #
    # If None is passed instead of a vector, no treatment is administered.
    self.action_space = multi_discrete_with_none.MultiDiscreteWithNone([
        population_size
        for _ in range(params.max_treatments)
    ])  # type: spaces.Space

    # Define the spaces of observable state variables.
    self.observable_state_vars = {
        'health_states': spaces.MultiDiscrete(
            [len(params.state_names) for _ in range(population_size)]),
        'population_graph': graph.GraphSpace(population_size,
                                             directed=False),
    }  # type: Dict[Text, spaces.Space]

    # Map state names to indices.
    self.state_name_to_index = {
        state: i for i, state in enumerate(params.state_names)}

    super(InfectiousDiseaseEnv, self).__init__(params)
    self.state = self._create_initial_state() 
Example #14
Source File: common.py    From cleanrl with MIT License 5 votes vote down vote up
def preprocess_ac_space(ac_space: Space):
    if isinstance(ac_space, Discrete):
        return ac_space.n

    elif isinstance(ac_space, MultiDiscrete):
        return ac_space.nvec.sum()

    elif isinstance(ac_space, Box):
        return np.prod(ac_space.shape)

    else:
        raise NotImplementedError("Error: the model does not support output space of type {}".format(
            type(ac_space).__name__)) 
Example #15
Source File: common.py    From cleanrl with MIT License 5 votes vote down vote up
def preprocess_obs_space(obs_space: Space, device: str):
    """
    The `preprocess_obs_fn` receives the observation `x` in the shape of
    `(batch_num,) + obs_space.shape`.

    1) If the `obs_space` is `Discrete`, `preprocess_obs_fn` outputs a
    preprocessed obs in the shape of
    `(batch_num, obs_space.n)`.

    2) If the `obs_space` is `Box`, `preprocess_obs_fn` outputs a
    preprocessed obs in the shape of
    `(batch_num,) + obs_space.shape`.

    In addition, the preprocessed obs will be sent to `device` (either
    `cpu` or `cuda`)
    """
    if isinstance(obs_space, Discrete):
        def preprocess_obs_fn(x):
            return F.one_hot(torch.LongTensor(x), obs_space.n).float().to(device)
        return (obs_space.n, preprocess_obs_fn)

    elif isinstance(obs_space, Box):
        def preprocess_obs_fn(x):
            return torch.Tensor(x).float().view(torch.Tensor(x).shape[0], -1).to(device)
        return (np.array(obs_space.shape).prod(), preprocess_obs_fn)

    else:
        raise NotImplementedError("Error: the model does not support input space of type {}".format(
            type(obs_space).__name__)) 
Example #16
Source File: env_checker.py    From stable-baselines with MIT License 5 votes vote down vote up
def _check_spaces(env: gym.Env) -> None:
    """
    Check that the observation and action spaces are defined
    and inherit from gym.spaces.Space.
    """
    # Helper to link to the code, because gym has no proper documentation
    gym_spaces = " cf https://github.com/openai/gym/blob/master/gym/spaces/"

    assert hasattr(env, 'observation_space'), "You must specify an observation space (cf gym.spaces)" + gym_spaces
    assert hasattr(env, 'action_space'), "You must specify an action space (cf gym.spaces)" + gym_spaces

    assert isinstance(env.observation_space,
                      spaces.Space), "The observation space must inherit from gym.spaces" + gym_spaces
    assert isinstance(env.action_space, spaces.Space), "The action space must inherit from gym.spaces" + gym_spaces 
Example #17
Source File: env_checker.py    From stable-baselines with MIT License 5 votes vote down vote up
def _check_returned_values(env: gym.Env, observation_space: spaces.Space, action_space: spaces.Space) -> None:
    """
    Check the returned values by the env when calling `.reset()` or `.step()` methods.
    """
    # because env inherits from gym.Env, we assume that `reset()` and `step()` methods exists
    obs = env.reset()

    _check_obs(obs, observation_space, 'reset')

    # Sample a random action
    action = action_space.sample()
    data = env.step(action)

    assert len(data) == 4, "The `step()` method must return four values: obs, reward, done, info"

    # Unpack
    obs, reward, done, info = data

    _check_obs(obs, observation_space, 'step')

    # We also allow int because the reward will be cast to float
    assert isinstance(reward, (float, int)), "The reward returned by `step()` must be a float"
    assert isinstance(done, bool), "The `done` signal must be a boolean"
    assert isinstance(info, dict), "The `info` returned by `step()` must be a python dictionary"

    if isinstance(env, gym.GoalEnv):
        # For a GoalEnv, the keys are checked at reset
        assert reward == env.compute_reward(obs['achieved_goal'], obs['desired_goal'], info) 
Example #18
Source File: env_checker.py    From stable-baselines with MIT License 5 votes vote down vote up
def _check_unsupported_obs_spaces(env: gym.Env, observation_space: spaces.Space) -> None:
    """Emit warnings when the observation space used is not supported by Stable-Baselines."""

    if isinstance(observation_space, spaces.Dict) and not isinstance(env, gym.GoalEnv):
        warnings.warn("The observation space is a Dict but the environment is not a gym.GoalEnv "
                      "(cf https://github.com/openai/gym/blob/master/gym/core.py), "
                      "this is currently not supported by Stable Baselines "
                      "(cf https://github.com/hill-a/stable-baselines/issues/133), "
                      "you will need to use a custom policy. "
                      )

    if isinstance(observation_space, spaces.Tuple):
        warnings.warn("The observation space is a Tuple,"
                      "this is currently not supported by Stable Baselines "
                      "(cf https://github.com/hill-a/stable-baselines/issues/133), "
                      "you will need to flatten the observation and maybe use a custom policy. "
                      ) 
Example #19
Source File: env_checker.py    From stable-baselines with MIT License 5 votes vote down vote up
def _enforce_array_obs(observation_space: spaces.Space) -> bool:
    """
    Whether to check that the returned observation is a numpy array
    it is not mandatory for `Dict` and `Tuple` spaces.
    """
    return not isinstance(observation_space, (spaces.Dict, spaces.Tuple)) 
Example #20
Source File: college_admission.py    From ml-fairness-gym with Apache License 2.0 4 votes vote down vote up
def __init__(self, user_params = None):
    """Initializes the College Admissions environment with initial params.

    Args:
      user_params: Dict. Any params not passed will take default values in
        Params.
    Raise:
      ValueError: If provided params not as expected.
    """
    # TODO(): make parameter handling consistent across environments.
    # Called env_params unlike in other environments because this environment
    # incorporates params with the default to get the comprehensive environment
    # params.
    env_params = Params()
    if user_params is not None:
      env_params = Params(**user_params)
    # The jury's action is a dict containing the threshold which specifies a 1D
    # threshold on scores above which applicants will be admitted and an epsilon
    # probability value, which specifies the probability value for an
    # epsilon greedy agent and is 0 by default.
    self.action_space = spaces.Dict({
        'threshold':
            spaces.Box(
                low=env_params.score_params.min,
                high=env_params.score_params.max,
                dtype=np.float32,
                shape=()),
        'epsilon_prob':
            spaces.Box(low=0, high=1, dtype=np.float32, shape=())
    })  # type: spaces.Space

    # The observations include test scores, [0, 1], eligibility of selected
    # applicants, ground truth for selected candidates and applicant group ids.
    self.observable_state_vars = {
        'test_scores_y':
            spaces.Box(
                low=env_params.score_params.min,
                high=env_params.score_params.max,
                dtype=np.float32,
                shape=(env_params.num_applicants,)),
        'selected_applicants':
            spaces.MultiBinary(env_params.num_applicants),
        'selected_ground_truth':
            spaces.MultiDiscrete([3] * env_params.num_applicants),
        'applicant_groups':
            spaces.MultiBinary(env_params.num_applicants)
    }  # type: Dict[Text, spaces.Space]
    super(CollegeAdmissionsEnv, self).__init__(env_params)
    if env_params.gaming_control != np.inf and (env_params.gaming_control > 1 or
                                                env_params.gaming_control < 0):
      raise ValueError('Gaming control needs to be in [0, 1]')

    if env_params.noise_dist not in ['gaussian', 'beta']:
      raise ValueError('Undefined noise distribution.')
    self._state_init()