当前位置: 首页>>代码示例>>Python>>正文


Python spaces.Space方法代码示例

本文整理汇总了Python中gym.spaces.Space方法的典型用法代码示例。如果您正苦于以下问题:Python spaces.Space方法的具体用法?Python spaces.Space怎么用?Python spaces.Space使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在gym.spaces的用法示例。


在下文中一共展示了spaces.Space方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _check_unsupported_obs_spaces

# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Space [as 别名]
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. "
                      ) 
开发者ID:Stable-Baselines-Team,项目名称:stable-baselines,代码行数:19,代码来源:env_checker.py

示例2: _check_obs

# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Space [as 别名]
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)) 
开发者ID:Stable-Baselines-Team,项目名称:stable-baselines,代码行数:22,代码来源:env_checker.py

示例3: __init__

# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Space [as 别名]
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 
开发者ID:ray-project,项目名称:ray,代码行数:27,代码来源:exploration.py

示例4: action_space

# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Space [as 别名]
def action_space(self) -> Space:
        return self._action_space 
开发者ID:tensortrade-org,项目名称:tensortrade,代码行数:4,代码来源:action_scheme.py

示例5: _enforce_array_obs

# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Space [as 别名]
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)) 
开发者ID:Stable-Baselines-Team,项目名称:stable-baselines,代码行数:8,代码来源:env_checker.py

示例6: _check_returned_values

# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Space [as 别名]
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) 
开发者ID:Stable-Baselines-Team,项目名称:stable-baselines,代码行数:30,代码来源:env_checker.py

示例7: _check_spaces

# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Space [as 别名]
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 
开发者ID:Stable-Baselines-Team,项目名称:stable-baselines,代码行数:16,代码来源:env_checker.py

示例8: preprocess_obs_space

# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Space [as 别名]
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__)) 
开发者ID:vwxyzjn,项目名称:cleanrl,代码行数:31,代码来源:common.py

示例9: preprocess_ac_space

# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Space [as 别名]
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__)) 
开发者ID:vwxyzjn,项目名称:cleanrl,代码行数:15,代码来源:common.py

示例10: __init__

# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Space [as 别名]
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() 
开发者ID:google,项目名称:ml-fairness-gym,代码行数:31,代码来源:infectious_disease.py

示例11: space

# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Space [as 别名]
def space(self) -> spaces.Space:
        """Get the observation space."""
        raise NotImplementedError() 
开发者ID:eleurent,项目名称:highway-env,代码行数:5,代码来源:observation.py

示例12: space

# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Space [as 别名]
def space(self) -> spaces.Space:
        """The action space."""
        raise NotImplementedError 
开发者ID:eleurent,项目名称:highway-env,代码行数:5,代码来源:action.py

示例13: __init__

# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Space [as 别名]
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() 
开发者ID:google,项目名称:ml-fairness-gym,代码行数:57,代码来源:college_admission.py


注:本文中的gym.spaces.Space方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。