本文整理汇总了Python中gym.spaces.MultiBinary方法的典型用法代码示例。如果您正苦于以下问题:Python spaces.MultiBinary方法的具体用法?Python spaces.MultiBinary怎么用?Python spaces.MultiBinary使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gym.spaces
的用法示例。
在下文中一共展示了spaces.MultiBinary方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_action_type
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import MultiBinary [as 别名]
def get_action_type(action_space):
'''Method to get the action type to choose prob. dist. to sample actions from NN logits output'''
if isinstance(action_space, spaces.Box):
shape = action_space.shape
assert len(shape) == 1
if shape[0] == 1:
return 'continuous'
else:
return 'multi_continuous'
elif isinstance(action_space, spaces.Discrete):
return 'discrete'
elif isinstance(action_space, spaces.MultiDiscrete):
return 'multi_discrete'
elif isinstance(action_space, spaces.MultiBinary):
return 'multi_binary'
else:
raise NotImplementedError
# action_policy base methods
示例2: set_gym_space_attr
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import MultiBinary [as 别名]
def set_gym_space_attr(gym_space):
'''Set missing gym space attributes for standardization'''
if isinstance(gym_space, spaces.Box):
setattr(gym_space, 'is_discrete', False)
elif isinstance(gym_space, spaces.Discrete):
setattr(gym_space, 'is_discrete', True)
setattr(gym_space, 'low', 0)
setattr(gym_space, 'high', gym_space.n)
elif isinstance(gym_space, spaces.MultiBinary):
setattr(gym_space, 'is_discrete', True)
setattr(gym_space, 'low', np.full(gym_space.n, 0))
setattr(gym_space, 'high', np.full(gym_space.n, 2))
elif isinstance(gym_space, spaces.MultiDiscrete):
setattr(gym_space, 'is_discrete', True)
setattr(gym_space, 'low', np.zeros_like(gym_space.nvec))
setattr(gym_space, 'high', np.array(gym_space.nvec))
else:
raise ValueError('gym_space not recognized')
示例3: pick_action
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import MultiBinary [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()
示例4: make_proba_dist_type
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import MultiBinary [as 别名]
def make_proba_dist_type(ac_space):
"""
return an instance of ProbabilityDistributionType for the correct type of action space
:param ac_space: (Gym Space) the input action space
:return: (ProbabilityDistributionType) the appropriate instance of a ProbabilityDistributionType
"""
if isinstance(ac_space, spaces.Box):
assert len(ac_space.shape) == 1, "Error: the action space must be a vector"
return DiagGaussianProbabilityDistributionType(ac_space.shape[0])
elif isinstance(ac_space, spaces.Discrete):
return CategoricalProbabilityDistributionType(ac_space.n)
elif isinstance(ac_space, spaces.MultiDiscrete):
return MultiCategoricalProbabilityDistributionType(ac_space.nvec)
elif isinstance(ac_space, spaces.MultiBinary):
return BernoulliProbabilityDistributionType(ac_space.n)
else:
raise NotImplementedError("Error: probability distribution, not implemented for action space of type {}."
.format(type(ac_space)) +
" Must be of type Gym Spaces: Box, Discrete, MultiDiscrete or MultiBinary.")
示例5: _detect_gym_spaces
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import MultiBinary [as 别名]
def _detect_gym_spaces(gym_space):
if isinstance(gym_space, spaces.Discrete):
return {"Discrete": (gym_space.n,)}
elif isinstance(gym_space, spaces.MultiDiscrete):
raise NotImplementedError
elif isinstance(gym_space, spaces.MultiBinary):
return {"MultiBinary": (gym_space.n,)}
elif isinstance(gym_space, spaces.Box):
return {"Box": gym_space.shape}
elif isinstance(gym_space, spaces.Dict):
return {
name: list(Space._detect_gym_spaces(s).values())[0]
for name, s in gym_space.spaces.items()
}
elif isinstance(gym_space, spaces.Tuple):
return {
idx: list(Space._detect_gym_spaces(s).values())[0]
for idx, s in enumerate(gym_space.spaces)
}
示例6: dtypes_from_gym
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import MultiBinary [as 别名]
def dtypes_from_gym(gym_space):
if isinstance(gym_space, spaces.Discrete):
return {"Discrete": gym_space.dtype}
elif isinstance(gym_space, spaces.MultiDiscrete):
raise NotImplementedError
elif isinstance(gym_space, spaces.MultiBinary):
return {"MultiBinary": gym_space.dtype}
elif isinstance(gym_space, spaces.Box):
return {"Box": gym_space.dtype}
elif isinstance(gym_space, spaces.Dict):
return {
name: list(Space._detect_gym_spaces(s).values())[0]
for name, s in gym_space.spaces.items()
}
elif isinstance(gym_space, spaces.Tuple):
return {
idx: list(Space._detect_gym_spaces(s).values())[0]
for idx, s in enumerate(gym_space.spaces)
}
else:
raise NotImplementedError
示例7: gym_space_distribution
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import MultiBinary [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)
示例8: __init__
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import MultiBinary [as 别名]
def __init__(self, env, first_action, num_eps=1, warmup_eps=0):
"""
Parameters:
env: the environment to wrap.
first_action: the action to include in the first
observation.
num_eps: episodes per meta-episode.
warmup_eps: the number of episodes at the start
of a meta-episode for which rewards are 0.
Negative values are added to num_eps.
"""
if warmup_eps < 0:
warmup_eps += num_eps
super(RL2Env, self).__init__(env)
self.first_action = first_action
self.observation_space = spaces.Tuple([
env.observation_space,
env.action_space,
spaces.Box(low=-np.inf, high=np.inf, shape=(1,), dtype='float'),
spaces.MultiBinary(1)
])
self.num_eps = num_eps
self.warmup_eps = warmup_eps
self._done_eps = 0
示例9: flatten
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import MultiBinary [as 别名]
def flatten(space, x):
if isinstance(space, Box):
return np.asarray(x, dtype=np.float32).flatten()
elif isinstance(space, Discrete):
onehot = np.zeros(space.n, dtype=np.float32)
onehot[x] = 1.0
return onehot
elif isinstance(space, Tuple):
return np.concatenate([flatten(s, x_part) for x_part, s in zip(x, space.spaces)])
elif isinstance(space, Dict):
return np.concatenate([flatten(s, x[key]) for key, s in space.spaces.items()])
elif isinstance(space, MultiBinary):
return np.asarray(x).flatten()
elif isinstance(space, MultiDiscrete):
return np.asarray(x).flatten()
else:
raise NotImplementedError
示例10: unflatten
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import MultiBinary [as 别名]
def unflatten(space, x):
if isinstance(space, Box):
return np.asarray(x, dtype=np.float32).reshape(space.shape)
elif isinstance(space, Discrete):
return int(np.nonzero(x)[0][0])
elif isinstance(space, Tuple):
dims = [flatdim(s) for s in space.spaces]
list_flattened = np.split(x, np.cumsum(dims)[:-1])
list_unflattened = [unflatten(s, flattened)
for flattened, s in zip(list_flattened, space.spaces)]
return tuple(list_unflattened)
elif isinstance(space, Dict):
dims = [flatdim(s) for s in space.spaces.values()]
list_flattened = np.split(x, np.cumsum(dims)[:-1])
list_unflattened = [(key, unflatten(s, flattened))
for flattened, (key, s) in zip(list_flattened, space.spaces.items())]
return dict(list_unflattened)
elif isinstance(space, MultiBinary):
return np.asarray(x).reshape(space.shape)
elif isinstance(space, MultiDiscrete):
return np.asarray(x).reshape(space.shape)
else:
raise NotImplementedError
示例11: batch_space_base
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import MultiBinary [as 别名]
def batch_space_base(space, n=1):
if isinstance(space, Box):
repeats = tuple([n] + [1] * space.low.ndim)
low, high = np.tile(space.low, repeats), np.tile(space.high, repeats)
return Box(low=low, high=high, dtype=space.dtype)
elif isinstance(space, Discrete):
return MultiDiscrete(np.full((n,), space.n, dtype=space.dtype))
elif isinstance(space, MultiDiscrete):
repeats = tuple([n] + [1] * space.nvec.ndim)
high = np.tile(space.nvec, repeats) - 1
return Box(low=np.zeros_like(high), high=high, dtype=space.dtype)
elif isinstance(space, MultiBinary):
return Box(low=0, high=1, shape=(n,) + space.shape, dtype=space.dtype)
else:
raise NotImplementedError()
示例12: __init__
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import MultiBinary [as 别名]
def __init__(self, chain_length=16, start_state=1, max_steps=None, observation_type=ObservationType.Therm,
left_state_reward=1/1000, right_state_reward=1, simple_render=True):
super().__init__()
if chain_length <= 3:
raise ValueError('Chain length must be > 3, found {}'.format(chain_length))
if not 0 <= start_state < chain_length:
raise ValueError('The start state should be within the chain bounds, found {}'.format(start_state))
self.chain_length = chain_length
self.start_state = start_state
self.max_steps = max_steps
self.observation_type = observation_type
self.left_state_reward = left_state_reward
self.right_state_reward = right_state_reward
self.simple_render = simple_render
# spaces documentation: https://gym.openai.com/docs/
self.action_space = spaces.Discrete(2) # 0 -> Go left, 1 -> Go right
self.observation_space = spaces.Box(0, 1, shape=(chain_length,))#spaces.MultiBinary(chain_length)
self.reset()
示例13: __init__
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import MultiBinary [as 别名]
def __init__(self, input_dim, action_space):
super().__init__()
self.action_space = action_space
if isinstance(action_space, spaces.Box):
assert len(action_space.shape) == 1
self.head = DiagGaussianActionHead(input_dim, action_space.shape[0])
elif isinstance(action_space, spaces.Discrete):
self.head = CategoricalActionHead(input_dim, action_space.n)
# elif isinstance(action_space, spaces.MultiDiscrete):
# return MultiCategoricalPdType(action_space.nvec)
# elif isinstance(action_space, spaces.MultiBinary):
# return BernoulliPdType(action_space.n)
else:
raise NotImplementedError
示例14: make_pdtype
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import MultiBinary [as 别名]
def make_pdtype(ac_space):
from gym import spaces
if isinstance(ac_space, spaces.Box):
assert len(ac_space.shape) == 1
return DiagGaussianPdType(ac_space.shape[0])
elif isinstance(ac_space, spaces.Discrete):
return CategoricalPdType(ac_space.n)
elif isinstance(ac_space, spaces.MultiDiscrete):
return MultiCategoricalPdType(ac_space.nvec)
elif isinstance(ac_space, spaces.MultiBinary):
return BernoulliPdType(ac_space.n)
else:
raise NotImplementedError
示例15: _get_action_dim
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import MultiBinary [as 别名]
def _get_action_dim(self, action_space):
'''Get the action dim for an action_space for agent to use'''
if isinstance(action_space, spaces.Box):
assert len(action_space.shape) == 1
action_dim = action_space.shape[0]
elif isinstance(action_space, (spaces.Discrete, spaces.MultiBinary)):
action_dim = action_space.n
elif isinstance(action_space, spaces.MultiDiscrete):
action_dim = action_space.nvec.tolist()
else:
raise ValueError('action_space not recognized')
return action_dim