本文整理汇总了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
示例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)
示例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)
示例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)
示例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}')
示例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)
示例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
示例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)
示例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()))
示例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)
示例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
示例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)
示例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
示例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
示例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