當前位置: 首頁>>代碼示例>>Python>>正文


Python gym.spaces方法代碼示例

本文整理匯總了Python中gym.spaces方法的典型用法代碼示例。如果您正苦於以下問題:Python gym.spaces方法的具體用法?Python gym.spaces怎麽用?Python gym.spaces使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在gym的用法示例。


在下文中一共展示了gym.spaces方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: create_state_q_function_for_env

# 需要導入模塊: import gym [as 別名]
# 或者: from gym import spaces [as 別名]
def create_state_q_function_for_env(env):
    assert isinstance(env.observation_space, gym.spaces.Box)
    ndim_obs = env.observation_space.low.size
    if isinstance(env.action_space, gym.spaces.Discrete):
        return q_functions.FCStateQFunctionWithDiscreteAction(
            ndim_obs=ndim_obs,
            n_actions=env.action_space.n,
            n_hidden_channels=10,
            n_hidden_layers=1)
    elif isinstance(env.action_space, gym.spaces.Box):
        return q_functions.FCQuadraticStateQFunction(
            n_input_channels=ndim_obs,
            n_dim_action=env.action_space.low.size,
            n_hidden_channels=10,
            n_hidden_layers=1,
            action_space=env.action_space)
    else:
        raise NotImplementedError() 
開發者ID:chainer,項目名稱:chainerrl,代碼行數:20,代碼來源:test_agents.py

示例2: __init__

# 需要導入模塊: import gym [as 別名]
# 或者: from gym import spaces [as 別名]
def __init__(self, file_name, batch_size=128, n_step=1):
        # create an offline_env to do fake interaction with agent
        self.num_epoch = 0
        self.num_record = 0
        self._offset = 0

        # how many records to read from table at one time
        self.batch_size = batch_size
        # number of step to reserved for n-step dqn
        self.n_step = n_step

        # defined the shape of observation and action
        # we follow the definition of gym.spaces
        # `Box` for continue-space, `Discrete` for discrete-space and `Dict` for multiple input
        # actually low/high limitation will not be used by agent but required by gym.spaces
        self.observation_space = Box(low=-np.inf, high=np.inf, shape=(4,))
        self.action_space = Discrete(n=2)

        fr = open(file_name)
        self.data = fr.readlines()
        self.num_record = len(self.data)
        fr.close() 
開發者ID:alibaba,項目名稱:EasyRL,代碼行數:24,代碼來源:run_bcq_on_batchdata.py

示例3: _basic_space_to_ph_spec

# 需要導入模塊: import gym [as 別名]
# 或者: from gym import spaces [as 別名]
def _basic_space_to_ph_spec(self, sp):
        """Translate a gym space object to a tuple to specify data type and shape.
        Arguments:
            sp (obj): basic space object of gym interface.
        Returns:
            a tuple used for building TensorFlow placeholders where the first element specifies `dtype` and the second one specifies `shape`.
        """

        # (jones.wz) TO DO: handle gym Atari input
        if isinstance(sp, gym.spaces.Box):
            if len(sp.shape) == 3:
                return (tf.uint8, (None, ) + sp.shape)
            return (tf.float32, (None, prod(sp.shape)))
        elif isinstance(sp, gym.spaces.Discrete):
            return (tf.int32, (None, sp.n))
        elif isinstance(sp, gym.spaces.MultiDiscrete):
            return (tf.int32, (None, prod(sp.shape)))
        elif isinstance(sp, gym.spaces.MultiBinary):
            return (tf.int32, (None, prod(sp.shape)))
        else:
            raise TypeError(
                "specified an unsupported space type {}".format(sp)) 
開發者ID:alibaba,項目名稱:EasyRL,代碼行數:24,代碼來源:executor.py

示例4: flatten_obs

# 需要導入模塊: import gym [as 別名]
# 或者: from gym import spaces [as 別名]
def flatten_obs(self, obs):
        """reshape the multi-channel observations into a flattern array for efficient communication in distributed training.

        Arguments:
            obs (obj): dict or list of numpy array for multi-channel observations.
        Returns:
            flattened_obs (tensor): a flattened array.
        """
        if isinstance(self.ob_ph_spec, list):
            assert len(obs) == len(
                self.observation_space
            ), "{} spaces for obs but {} inputs found".format(
                len(self.observation_space), len(obs))
            flattened_array = np.concatenate(
                [np.asarray(elm).astype(np.float32) for elm in obs], axis=1)
        elif isinstance(self.ob_ph_spec, OrderedDict):
            array_list = []
            for name in self.ob_ph_spec.keys():
                array_list.append(np.asarray(obs[name]).astype(np.float32))
            flattened_array = np.concatenate(array_list, axis=1)
        else:
            flattened_array = obs

        return flattened_array 
開發者ID:alibaba,項目名稱:EasyRL,代碼行數:26,代碼來源:executor.py

示例5: pick_action

# 需要導入模塊: import gym [as 別名]
# 或者: from gym import spaces [as 別名]
def pick_action(self, state: Union[int, float, np.ndarray]
                    ) -> Union[int, float, np.ndarray]:
        """ Pick an action given a state.

        Picks uniformly random from all possible actions, using the environments
        action_space.sample() method.

        Parameters
        ----------
        state: int
            An integer corresponding to a state of a DiscreteEnv.
            Not used in this agent.

        Returns
        -------
        Union[int, float, np.ndarray]
            An action
        """
        # if other spaces are needed, check if their sample method conforms with
        # returned type, change if necessary.
        assert isinstance(self.env.action_space,
                          (Box, Discrete, MultiDiscrete, MultiBinary))
        return self.env.action_space.sample() 
開發者ID:JohannesHeidecke,項目名稱:irl-benchmark,代碼行數:25,代碼來源:random_agent.py

示例6: _check_image_input

# 需要導入模塊: import gym [as 別名]
# 或者: from gym import spaces [as 別名]
def _check_image_input(observation_space: spaces.Box) -> None:
    """
    Check that the input will be compatible with Stable-Baselines
    when the observation is apparently an image.
    """
    if observation_space.dtype != np.uint8:
        warnings.warn("It seems that your observation is an image but the `dtype` "
                      "of your observation_space is not `np.uint8`. "
                      "If your observation is not an image, we recommend you to flatten the observation "
                      "to have only a 1D vector")

    if np.any(observation_space.low != 0) or np.any(observation_space.high != 255):
        warnings.warn("It seems that your observation space is an image but the "
                      "upper and lower bounds are not in [0, 255]. "
                      "Because the CNN policy normalize automatically the observation "
                      "you may encounter issue if the values are not in that range."
                      )

    if observation_space.shape[0] < 36 or observation_space.shape[1] < 36:
        warnings.warn("The minimal resolution for an image is 36x36 for the default CnnPolicy. "
                      "You might need to use a custom `cnn_extractor` "
                      "cf https://stable-baselines.readthedocs.io/en/master/guide/custom_policy.html") 
開發者ID:Stable-Baselines-Team,項目名稱:stable-baselines,代碼行數:24,代碼來源:env_checker.py

示例7: _check_unsupported_obs_spaces

# 需要導入模塊: import gym [as 別名]
# 或者: from gym import spaces [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

示例8: _check_obs

# 需要導入模塊: import gym [as 別名]
# 或者: from gym import spaces [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

示例9: action_probability

# 需要導入模塊: import gym [as 別名]
# 或者: from gym import spaces [as 別名]
def action_probability(self, observation, state=None, mask=None, actions=None, logp=False):
        """
        If ``actions`` is ``None``, then get the model's action probability distribution from a given observation.

        Depending on the action space the output is:
            - Discrete: probability for each possible action
            - Box: mean and standard deviation of the action output

        However if ``actions`` is not ``None``, this function will return the probability that the given actions are
        taken with the given parameters (observation, state, ...) on this model. For discrete action spaces, it
        returns the probability mass; for continuous action spaces, the probability density. This is since the
        probability mass will always be zero in continuous spaces, see http://blog.christianperone.com/2019/01/
        for a good explanation

        :param observation: (np.ndarray) the input observation
        :param state: (np.ndarray) The last states (can be None, used in recurrent policies)
        :param mask: (np.ndarray) The last masks (can be None, used in recurrent policies)
        :param actions: (np.ndarray) (OPTIONAL) For calculating the likelihood that the given actions are chosen by
            the model for each of the given parameters. Must have the same number of actions and observations.
            (set to None to return the complete action probability distribution)
        :param logp: (bool) (OPTIONAL) When specified with actions, returns probability in log-space.
            This has no effect if actions is None.
        :return: (np.ndarray) the model's (log) action probability
        """
        pass 
開發者ID:Stable-Baselines-Team,項目名稱:stable-baselines,代碼行數:27,代碼來源:base_class.py

示例10: predict

# 需要導入模塊: import gym [as 別名]
# 或者: from gym import spaces [as 別名]
def predict(self, observation, state=None, mask=None, deterministic=False):
        if state is None:
            state = self.initial_state
        if mask is None:
            mask = [False for _ in range(self.n_envs)]
        observation = np.array(observation)
        vectorized_env = self._is_vectorized_observation(observation, self.observation_space)

        observation = observation.reshape((-1,) + self.observation_space.shape)
        actions, _, states, _ = self.step(observation, state, mask, deterministic=deterministic)

        clipped_actions = actions
        # Clip the actions to avoid out of bound error
        if isinstance(self.action_space, gym.spaces.Box):
            clipped_actions = np.clip(actions, self.action_space.low, self.action_space.high)

        if not vectorized_env:
            if state is not None:
                raise ValueError("Error: The environment must be vectorized when using recurrent policies.")
            clipped_actions = clipped_actions[0]

        return clipped_actions, states 
開發者ID:Stable-Baselines-Team,項目名稱:stable-baselines,代碼行數:24,代碼來源:base_class.py

示例11: __init__

# 需要導入模塊: import gym [as 別名]
# 或者: from gym import spaces [as 別名]
def __init__(self, env, width, height):
        if not isinstance(env.observation_space, gym.spaces.Box):
            raise ValueError('Resize only works with Box environment.')

        if len(env.observation_space.shape) != 2:
            raise ValueError('Resize only works with 2D single channel image.')

        super().__init__(env)

        _low = env.observation_space.low.flatten()[0]
        _high = env.observation_space.high.flatten()[0]
        self._dtype = env.observation_space.dtype
        self._observation_space = gym.spaces.Box(_low,
                                                 _high,
                                                 shape=[width, height],
                                                 dtype=self._dtype)

        self._width = width
        self._height = height 
開發者ID:rlworkgroup,項目名稱:garage,代碼行數:21,代碼來源:resize.py

示例12: __init__

# 需要導入模塊: import gym [as 別名]
# 或者: from gym import spaces [as 別名]
def __init__(self, env, n_frames):
        if not isinstance(env.observation_space, gym.spaces.Box):
            raise ValueError('Stack frames only works with gym.spaces.Box '
                             'environment.')

        if len(env.observation_space.shape) != 2:
            raise ValueError(
                'Stack frames only works with 2D single channel images')

        super().__init__(env)

        self._n_frames = n_frames
        self._frames = deque(maxlen=n_frames)

        new_obs_space_shape = env.observation_space.shape + (n_frames, )
        _low = env.observation_space.low.flatten()[0]
        _high = env.observation_space.high.flatten()[0]
        self._observation_space = gym.spaces.Box(
            _low,
            _high,
            shape=new_obs_space_shape,
            dtype=env.observation_space.dtype) 
開發者ID:rlworkgroup,項目名稱:garage,代碼行數:24,代碼來源:stack_frames.py

示例13: __init__

# 需要導入模塊: import gym [as 別名]
# 或者: from gym import spaces [as 別名]
def __init__(self, env):
        if not isinstance(env.observation_space, gym.spaces.Box):
            raise ValueError(
                'Grayscale only works with gym.spaces.Box environment.')

        if len(env.observation_space.shape) != 3:
            raise ValueError('Grayscale only works with 2D RGB images')

        super().__init__(env)

        _low = env.observation_space.low.flatten()[0]
        _high = env.observation_space.high.flatten()[0]
        assert _low == 0
        assert _high == 255
        self._observation_space = gym.spaces.Box(
            _low,
            _high,
            shape=env.observation_space.shape[:-1],
            dtype=np.uint8) 
開發者ID:rlworkgroup,項目名稱:garage,代碼行數:21,代碼來源:grayscale.py

示例14: __init__

# 需要導入模塊: import gym [as 別名]
# 或者: from gym import spaces [as 別名]
def __init__(self, env, body_names, radius_multiplier=1.5, agent_idx_allowed_to_lock=None,
                 lock_type="any_lock", ac_obs_prefix='', obj_in_game_metadata_keys=None,
                 agent_allowed_to_lock_keys=None):
        super().__init__(env)
        self.n_agents = self.unwrapped.n_agents
        self.n_obj = len(body_names)
        self.body_names = body_names
        self.agent_idx_allowed_to_lock = np.arange(self.n_agents) if agent_idx_allowed_to_lock is None else agent_idx_allowed_to_lock
        self.lock_type = lock_type
        self.ac_obs_prefix = ac_obs_prefix
        self.obj_in_game_metadata_keys = obj_in_game_metadata_keys
        self.agent_allowed_to_lock_keys = agent_allowed_to_lock_keys
        self.action_space.spaces[f'action_{ac_obs_prefix}glue'] = (
            Tuple([MultiDiscrete([2] * self.n_obj) for _ in range(self.n_agents)]))
        self.observation_space = update_obs_space(env, {f'{ac_obs_prefix}obj_lock': (self.n_obj, 1),
                                                        f'{ac_obs_prefix}you_lock': (self.n_agents, self.n_obj, 1),
                                                        f'{ac_obs_prefix}team_lock': (self.n_agents, self.n_obj, 1)})
        self.lock_radius = radius_multiplier*self.metadata['box_size']
        self.obj_locked = np.zeros((self.n_obj,), dtype=int) 
開發者ID:openai,項目名稱:multi-agent-emergence-environments,代碼行數:21,代碼來源:manipulation.py

示例15: __init__

# 需要導入模塊: import gym [as 別名]
# 或者: from gym import spaces [as 別名]
def __init__(self, env, eat_thresh=0.5, max_food_health=10, respawn_time=np.inf,
                 food_rew_type='selfish', reward_scale=1.0, reward_scale_obs=False):
        super().__init__(env)
        self.eat_thresh = eat_thresh
        self.max_food_health = max_food_health
        self.respawn_time = respawn_time
        self.food_rew_type = food_rew_type
        self.n_agents = self.metadata['n_agents']

        if type(reward_scale) not in [list, tuple, np.ndarray]:
            reward_scale = [reward_scale, reward_scale]
        self.reward_scale = reward_scale
        self.reward_scale_obs = reward_scale_obs

        # Reset obs/action space to match
        self.max_n_food = self.metadata['max_n_food']
        self.curr_n_food = self.metadata['curr_n_food']
        self.max_food_size = self.metadata['food_size']
        food_dim = 5 if self.reward_scale_obs else 4
        self.observation_space = update_obs_space(self.env, {'food_obs': (self.max_n_food, food_dim),
                                                             'food_health': (self.max_n_food, 1),
                                                             'food_eat': (self.max_n_food, 1)})
        self.action_space.spaces['action_eat_food'] = Tuple([MultiDiscrete([2] * self.max_n_food)
                                                             for _ in range(self.n_agents)]) 
開發者ID:openai,項目名稱:multi-agent-emergence-environments,代碼行數:26,代碼來源:food.py


注:本文中的gym.spaces方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。