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


Python gym.Space方法代碼示例

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


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

示例1: __init__

# 需要導入模塊: import gym [as 別名]
# 或者: from gym import Space [as 別名]
def __init__(
        self,
        policy: Type[BasePolicy],
        sess: Optional[tf.Session],
        observation_space: gym.Space,
        action_space: gym.Space,
    ):
        """Constructs a DummyModel with given policy and session.
        :param policy: (BasePolicy) a loaded policy.
        :param sess: (tf.Session or None) a TensorFlow session.
        :return an instance of BaseRLModel.
        """
        super().__init__(policy=policy, env=None, requires_vec_env=True, policy_base="Dummy")
        self.sess = sess
        self.observation_space = observation_space
        self.action_space = action_space 
開發者ID:HumanCompatibleAI,項目名稱:adversarial-policies,代碼行數:18,代碼來源:base.py

示例2: convert_gym_space

# 需要導入模塊: import gym [as 別名]
# 或者: from gym import Space [as 別名]
def convert_gym_space(spc):
    """Converts a :gym:`gym.Space <#spaces>` instance to a
    :class:`~texar.agents.Space` instance.

    Args:
        spc: An instance of `gym.Space` or
            :class:`~texar.agents.Space`.
    """
    from texar.agents.agent_utils import Space
    if isinstance(spc, Space):
        return spc
    if isinstance(spc, gym.spaces.Discrete):
        return Space(shape=(), low=0, high=spc.n, dtype=spc.dtype)
    elif isinstance(spc, gym.spaces.Box):
        return Space(
            shape=spc.shape, low=spc.low, high=spc.high, dtype=spc.dtype) 
開發者ID:qkaren,項目名稱:Counterfactual-StoryRW,代碼行數:18,代碼來源:agent_gym_utils.py

示例3: __init__

# 需要導入模塊: import gym [as 別名]
# 或者: from gym import Space [as 別名]
def __init__(self, low=None, high=None, shape=None, dtype=None):
        """
        Two kinds of valid input:
            Box(low=-1.0, high=1.0, shape=(3,4)) # low and high are scalars, and shape is provided
            Box(low=np.array([-1.0,-2.0]), high=np.array([2.0,4.0])) # low and high are arrays of the same shape
        """
        if shape is None:
            assert low.shape == high.shape
            shape = low.shape
        else:
            assert np.isscalar(low) and np.isscalar(high)
            low = low + np.zeros(shape)
            high = high + np.zeros(shape)
        if dtype is None:  # Autodetect type
            if (high == 255).all():
                dtype = np.uint8
            else:
                dtype = np.float32
            logger.warn("gym.spaces.Box autodetected dtype as %s. Please provide explicit dtype." % dtype)
        self.low = low.astype(dtype)
        self.high = high.astype(dtype)
        gym.Space.__init__(self, shape, dtype) 
開發者ID:ArztSamuel,項目名稱:DRL_DeliveryDuel,代碼行數:24,代碼來源:box.py

示例4: __init__

# 需要導入模塊: import gym [as 別名]
# 或者: from gym import Space [as 別名]
def __init__(self, space, vocab_size, precision=2, max_range=(-100.0, 100.0)):
    self._precision = precision

    # Some gym envs (e.g. CartPole) have unreasonably high bounds for
    # observations. We clip so we can represent them.
    bounded_space = copy.copy(space)
    (min_low, max_high) = max_range
    bounded_space.low = np.maximum(space.low, min_low)
    bounded_space.high = np.minimum(space.high, max_high)
    if (not np.allclose(bounded_space.low, space.low) or
        not np.allclose(bounded_space.high, space.high)):
      logging.warning(
          'Space limits %s, %s out of bounds %s. Clipping to %s, %s.',
          str(space.low), str(space.high), str(max_range),
          str(bounded_space.low), str(bounded_space.high)
      )

    super(BoxSpaceSerializer, self).__init__(bounded_space, vocab_size) 
開發者ID:google,項目名稱:trax,代碼行數:20,代碼來源:space_serializer.py

示例5: test_init_spaces

# 需要導入模塊: import gym [as 別名]
# 或者: from gym import Space [as 別名]
def test_init_spaces(self):
        # check correct types for obs and action space
        self.assertIsInstance(self.env.observation_space, gym.Space,
                              msg='observation_space is not a Space object')
        self.assertIsInstance(self.env.action_space, gym.Space,
                              msg='action_space is not a Space object')

        # check low and high values are as expected
        obs_lows = self.env.observation_space.low
        obs_highs = self.env.observation_space.high
        act_lows = self.env.action_space.low
        act_highs = self.env.action_space.high

        places_tol = 3

        for prop, lo, hi in zip(self.env.task.state_variables, obs_lows, obs_highs):
            self.assertAlmostEqual(lo, prop.min, msg=f'{prop} min of {prop.min} does not'
                                                     f'match space low of {lo}')
            self.assertAlmostEqual(hi, prop.max, msg=f'{prop} max of {prop.max} does not'
                                                     f'match space high of {hi}') 
開發者ID:Gor-Ren,項目名稱:gym-jsbsim,代碼行數:22,代碼來源:test_environment.py

示例6: gym_space_distribution

# 需要導入模塊: import gym [as 別名]
# 或者: from gym import Space [as 別名]
def gym_space_distribution(space):
    """
    Create a Distribution from a gym.Space.

    If the space is not supported, throws an
    UnsupportedActionSpace exception.
    """
    if isinstance(space, spaces.Discrete):
        return CategoricalSoftmax(space.n)
    elif isinstance(space, spaces.Box):
        return BoxGaussian(space.low, space.high)
    elif isinstance(space, spaces.MultiBinary):
        return MultiBernoulli(space.n)
    elif isinstance(space, spaces.Tuple):
        sub_dists = tuple(gym_space_distribution(s) for s in space.spaces)
        return TupleDistribution(sub_dists)
    elif isinstance(space, spaces.MultiDiscrete):
        discretes = tuple(CategoricalSoftmax(n) for n in space.nvec)
        return TupleDistribution(discretes, to_sample=lambda x: np.array(x, dtype=space.dtype))
    raise UnsupportedGymSpace(space) 
開發者ID:flyyufelix,項目名稱:sonic_contest,代碼行數:22,代碼來源:gym.py

示例7: __init__

# 需要導入模塊: import gym [as 別名]
# 或者: from gym import Space [as 別名]
def __init__(self,
                 action_space: gym.Space,
                 observation_space: gym.Space,
                 max_concurrent: int = 100):
        """Initializes an external env.

        Args:
            action_space (gym.Space): Action space of the env.
            observation_space (gym.Space): Observation space of the env.
            max_concurrent (int): Max number of active episodes to allow at
                once. Exceeding this limit raises an error.
        """

        threading.Thread.__init__(self)

        self.daemon = True
        self.action_space = action_space
        self.observation_space = observation_space
        self._episodes = {}
        self._finished = set()
        self._results_avail_condition = threading.Condition()
        self._max_concurrent_episodes = max_concurrent 
開發者ID:ray-project,項目名稱:ray,代碼行數:24,代碼來源:external_env.py

示例8: clip_action

# 需要導入模塊: import gym [as 別名]
# 或者: from gym import Space [as 別名]
def clip_action(action, action_space):
    """Clips all actions in `flat_actions` according to the given Spaces.

    Args:
        flat_actions (List[np.ndarray]): The (flattened) list of single action
            components. List will have len=1 for "primitive" action Spaces.
        flat_space (List[Space]): The (flattened) list of single action Space
            objects. Has to be of same length as `flat_actions`.

    Returns:
        List[np.ndarray]: Flattened list of single clipped "primitive" actions.
    """

    def map_(a, s):
        if isinstance(s, gym.spaces.Box):
            a = np.clip(a, s.low, s.high)
        return a

    return tree.map_structure(map_, action, action_space) 
開發者ID:ray-project,項目名稱:ray,代碼行數:21,代碼來源:policy.py

示例9: __init__

# 需要導入模塊: import gym [as 別名]
# 或者: from gym import Space [as 別名]
def __init__(self,
                 action_space: gym.Space,
                 observation_space: gym.Space,
                 max_concurrent: int = 100):
        """Initialize a multi-agent external env.

        ExternalMultiAgentEnv subclasses must call this during their __init__.

        Args:
            action_space (gym.Space): Action space of the env.
            observation_space (gym.Space): Observation space of the env.
            max_concurrent (int): Max number of active episodes to allow at
                once. Exceeding this limit raises an error.
        """
        ExternalEnv.__init__(self, action_space, observation_space,
                             max_concurrent)

        # we require to know all agents' spaces
        if isinstance(self.action_space, dict) or isinstance(
                self.observation_space, dict):
            if not (self.action_space.keys() == self.observation_space.keys()):
                raise ValueError("Agent ids disagree for action space and obs "
                                 "space dict: {} {}".format(
                                     self.action_space.keys(),
                                     self.observation_space.keys())) 
開發者ID:ray-project,項目名稱:ray,代碼行數:27,代碼來源:external_multi_agent_env.py

示例10: convert_gym_space

# 需要導入模塊: import gym [as 別名]
# 或者: from gym import Space [as 別名]
def convert_gym_space(spc):
    """Converts a :gym:`gym.Space <#spaces>` instance to a
    :class:`~texar.tf.agents.Space` instance.

    Args:
        spc: An instance of `gym.Space` or
            :class:`~texar.tf.agents.Space`.
    """
    from texar.tf.agents.agent_utils import Space
    if isinstance(spc, Space):
        return spc
    if isinstance(spc, gym.spaces.Discrete):
        return Space(shape=(), low=0, high=spc.n, dtype=spc.dtype)
    elif isinstance(spc, gym.spaces.Box):
        return Space(
            shape=spc.shape, low=spc.low, high=spc.high, dtype=spc.dtype) 
開發者ID:asyml,項目名稱:texar,代碼行數:18,代碼來源:agent_gym_utils.py

示例11: __init__

# 需要導入模塊: import gym [as 別名]
# 或者: from gym import Space [as 別名]
def __init__(self, ob_space: gym.Space, ac_space: gym.Space):
        self.ob_space = ob_space
        self.ac_space = ac_space
        self.n_env = None
        self.n_steps = None
        self.n_batch = None 
開發者ID:HumanCompatibleAI,項目名稱:imitation,代碼行數:8,代碼來源:base.py

示例12: __init__

# 需要導入模塊: import gym [as 別名]
# 或者: from gym import Space [as 別名]
def __init__(
        self,
        observation_space: gym.Space,
        action_space: gym.Space,
        *,
        theta_units: Optional[Iterable[int]] = None,
        theta_kwargs: Optional[dict] = None,
        **kwargs,
    ):
        """Builds a simple reward network.

        Args:
          observation_space: The observation space.
          action_space: The action space.
          theta_units: Number of hidden units at each layer of the feedforward
              reward network theta.
          theta_kwargs: Arguments passed to `build_basic_theta_network`.
          kwargs: Passed through to RewardNet.
        """
        params = locals()
        del params["kwargs"]
        params.update(kwargs)

        self.theta_units = theta_units
        self.theta_kwargs = theta_kwargs or {}
        RewardNet.__init__(self, observation_space, action_space, **kwargs)
        serialize.LayersSerializable.__init__(**params, layers=self._layers) 
開發者ID:HumanCompatibleAI,項目名稱:imitation,代碼行數:29,代碼來源:reward_net.py

示例13: state_space

# 需要導入模塊: import gym [as 別名]
# 或者: from gym import Space [as 別名]
def state_space(self) -> gym.Space:
        """State space.

        Often same as observation_space, but differs in POMDPs.
        """
        return self._state_space 
開發者ID:HumanCompatibleAI,項目名稱:imitation,代碼行數:8,代碼來源:resettable_env.py

示例14: observation_space

# 需要導入模塊: import gym [as 別名]
# 或者: from gym import Space [as 別名]
def observation_space(self) -> gym.Space:
        """Observation space.

        Return type of reset() and component of step().
        """
        return self._observation_space 
開發者ID:HumanCompatibleAI,項目名稱:imitation,代碼行數:8,代碼來源:resettable_env.py

示例15: action_space

# 需要導入模塊: import gym [as 別名]
# 或者: from gym import Space [as 別名]
def action_space(self) -> gym.Space:
        """Action space.

        Parameter type of step().
        """
        return self._action_space 
開發者ID:HumanCompatibleAI,項目名稱:imitation,代碼行數:8,代碼來源:resettable_env.py


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