本文整理汇总了Python中gym.spaces.Dict方法的典型用法代码示例。如果您正苦于以下问题:Python spaces.Dict方法的具体用法?Python spaces.Dict怎么用?Python spaces.Dict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gym.spaces
的用法示例。
在下文中一共展示了spaces.Dict方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _check_unsupported_obs_spaces
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Dict [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. "
)
示例2: nested_discrete_gym_shape
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Dict [as 别名]
def nested_discrete_gym_shape(ac_space):
"""
Given instance of gym.spaces.Dict holding base gym.spaces.Discrete,
returns nested dictionary of spaces depths ( =dict of gym.spaces.Discrete.n)
This util is here due to fact in practice we need .n attr of discrete space [as cat. encoding depth]
rather than .shape, which is always ()
Args:
ac_space: instance of gym.spaces.Dict
Returns:
nested dictionary of lengths
"""
if isinstance(ac_space, Dict):
return {key: nested_discrete_gym_shape(space) for key, space in ac_space.spaces.items()}
elif isinstance(ac_space, Discrete):
return (ac_space.n,)
else:
raise TypeError('Expected gym.spaces.Dict or gym.spaces.Discrete, got: {}'.format(ac_space))
示例3: __init__
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Dict [as 别名]
def __init__(self, env, keys_self, keys_external, keys_mask=[], flatten=True):
super().__init__(env)
self.keys_self = sorted([k + '_self' for k in keys_self])
self.keys_external = sorted(keys_external)
self.keys_mask = sorted(keys_mask)
self.flatten = flatten
# Change observation space to look like a single agent observation space.
# This makes constructing policies much easier
if flatten:
size = sum([np.prod(self.env.observation_space.spaces[k].shape[1:])
for k in self.keys_self + self.keys_external])
self.observation_space = Dict(
{'observation_self': Box(-np.inf, np.inf, (size,), np.float32)})
else:
size_self = sum([self.env.observation_space.spaces[k].shape[1]
for k in self.keys_self])
obs_self = {'observation_self': Box(-np.inf, np.inf, (size_self,), np.float32)}
obs_extern = {k: Box(-np.inf, np.inf, v.shape[1:], np.float32)
for k, v in self.observation_space.spaces.items()
if k in self.keys_external + self.keys_mask}
obs_self.update(obs_extern)
self.observation_space = Dict(obs_self)
示例4: _detect_gym_spaces
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Dict [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)
}
示例5: dtypes_from_gym
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Dict [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
示例6: __init__
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Dict [as 别名]
def __init__(self, **smac_args):
"""Create a new multi-agent StarCraft env compatible with RLlib.
Arguments:
smac_args (dict): Arguments to pass to the underlying
smac.env.starcraft.StarCraft2Env instance.
Examples:
>>> from smac.examples.rllib import RLlibStarCraft2Env
>>> env = RLlibStarCraft2Env(map_name="8m")
>>> print(env.reset())
"""
self._env = StarCraft2Env(**smac_args)
self._ready_agents = []
self.observation_space = Dict({
"obs": Box(-1, 1, shape=(self._env.get_obs_size(),)),
"action_mask": Box(0, 1, shape=(self._env.get_total_actions(),)),
})
self.action_space = Discrete(self._env.get_total_actions())
示例7: __init__
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Dict [as 别名]
def __init__(self, env_fns):
self.envs = [fn() for fn in env_fns]
env = self.envs[0]
self.level_pred = self.envs[0].level_pred
VecEnv.__init__(self, len(env_fns), env.observation_space, env.action_space)
shapes, dtypes = {}, {}
self.keys = []
obs_space = env.observation_space
if isinstance(obs_space, spaces.Dict):
assert isinstance(obs_space.spaces, OrderedDict)
for key, box in obs_space.spaces.items():
assert isinstance(box, spaces.Box)
shapes[key] = box.shape
dtypes[key] = box.dtype
self.keys.append(key)
else:
box = obs_space
assert isinstance(box, spaces.Box)
self.keys = [None]
shapes, dtypes = { None: box.shape }, { None: box.dtype }
self.buf_obs = { k: np.zeros((self.num_envs,) + tuple(shapes[k]), dtype=dtypes[k]) for k in self.keys }
self.buf_dones = np.zeros((self.num_envs,), dtype=np.bool)
self.buf_rews = np.zeros((self.num_envs,), dtype=np.float32)
self.buf_infos = [{} for _ in range(self.num_envs)]
self.actions = None
示例8: __init__
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Dict [as 别名]
def __init__(self, env, agent_view_size=7):
super().__init__(env)
assert agent_view_size % 2 == 1
assert agent_view_size >= 3
# Override default view size
env.unwrapped.agent_view_size = agent_view_size
# Compute observation space with specified view size
observation_space = gym.spaces.Box(
low=0,
high=255,
shape=(agent_view_size, agent_view_size, 3),
dtype='uint8'
)
# Override the environment's observation space
self.observation_space = spaces.Dict({
'image': observation_space
})
示例9: __init__
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Dict [as 别名]
def __init__(self,
env: 'AbstractEnv',
features: Optional[List[str]] = None,
grid_size: Optional[List[List[float]]] = None,
grid_step: Optional[List[int]] = None,
features_range: Dict[str, List[float]] = None,
absolute: bool = False,
**kwargs: dict) -> None:
"""
:param env: The environment to observe
:param features: Names of features used in the observation
:param vehicles_count: Number of observed vehicles
"""
self.env = env
self.features = features if features is not None else self.FEATURES
self.grid_size = np.array(grid_size) if grid_size is not None else np.array(self.GRID_SIZE)
self.grid_step = np.array(grid_step) if grid_step is not None else np.array(self.GRID_STEP)
grid_shape = np.asarray(np.floor((self.grid_size[:, 1] - self.grid_size[:, 0]) / grid_step), dtype=np.int)
self.grid = np.zeros((len(self.features), *grid_shape))
self.features_range = features_range
self.absolute = absolute
示例10: __init__
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Dict [as 别名]
def __init__(self, config):
self.cur_pos = 0
self.action_space = Discrete(4)
# Represents an item.
self.item_space = Discrete(5)
# Represents an effect on the player.
self.effect_space = Box(9000, 9999, shape=(4, ))
# Represents a player.
self.player_space = Dict({
"location": Box(-100, 100, shape=(2, )),
"status": Box(-1, 1, shape=(10, )),
"items": Repeated(self.item_space, max_len=MAX_ITEMS),
"effects": Repeated(self.effect_space, max_len=MAX_EFFECTS),
})
# Observation is a list of players.
self.observation_space = Repeated(
self.player_space, max_len=MAX_PLAYERS)
示例11: __init__
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Dict [as 别名]
def __init__(self, env_config):
self.state = None
self.agent_1 = 0
self.agent_2 = 1
# MADDPG emits action logits instead of actual discrete actions
self.actions_are_logits = env_config.get("actions_are_logits", False)
self.one_hot_state_encoding = env_config.get("one_hot_state_encoding",
False)
self.with_state = env_config.get("separate_state_space", False)
if not self.one_hot_state_encoding:
self.observation_space = Discrete(6)
self.with_state = False
else:
# Each agent gets the full state (one-hot encoding of which of the
# three states are active) as input with the receiving agent's
# ID (1 or 2) concatenated onto the end.
if self.with_state:
self.observation_space = Dict({
"obs": MultiDiscrete([2, 2, 2, 3]),
ENV_STATE: MultiDiscrete([2, 2, 2])
})
else:
self.observation_space = MultiDiscrete([2, 2, 2, 3])
示例12: _convert_spec_to_space
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Dict [as 别名]
def _convert_spec_to_space(spec):
if isinstance(spec, dict):
return spaces.Dict(
{k: _convert_spec_to_space(v)
for k, v in spec.items()})
if isinstance(spec, specs.DiscreteArray):
return spaces.Discrete(spec.num_values)
elif isinstance(spec, specs.BoundedArray):
return spaces.Box(
low=np.asscalar(spec.minimum),
high=np.asscalar(spec.maximum),
shape=spec.shape,
dtype=spec.dtype)
elif isinstance(spec, specs.Array):
return spaces.Box(
low=-float("inf"),
high=float("inf"),
shape=spec.shape,
dtype=spec.dtype)
raise NotImplementedError(
("Could not convert `Array` spec of type {} to Gym space. "
"Attempted to convert: {}").format(type(spec), spec))
示例13: __init__
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Dict [as 别名]
def __init__(self, env_fns):
self.envs = [fn() for fn in env_fns]
env = self.envs[0]
VecEnv.__init__(self, len(env_fns), env.observation_space, env.action_space)
shapes, dtypes = {}, {}
self.keys = []
obs_space = env.observation_space
if isinstance(obs_space, spaces.Dict):
assert isinstance(obs_space.spaces, OrderedDict)
subspaces = obs_space.spaces
else:
subspaces = {None: obs_space}
for key, box in subspaces.items():
shapes[key] = box.shape
dtypes[key] = box.dtype
self.keys.append(key)
self.buf_obs = { k: np.zeros((self.num_envs,) + tuple(shapes[k]), dtype=dtypes[k]) for k in self.keys }
self.buf_dones = np.zeros((self.num_envs,), dtype=np.bool)
self.buf_rews = np.zeros((self.num_envs,), dtype=np.float32)
self.buf_infos = [{} for _ in range(self.num_envs)]
self.actions = None
示例14: __init__
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Dict [as 别名]
def __init__(self, env, w, h, grayscale=True, add_channel_dim=False, area_interpolation=False):
super(ResizeWrapper, self).__init__(env)
self.w = w
self.h = h
self.grayscale = grayscale
self.add_channel_dim = add_channel_dim
self.interpolation = cv2.INTER_AREA if area_interpolation else cv2.INTER_NEAREST
if isinstance(env.observation_space, spaces.Dict):
# TODO: does this even work?
new_spaces = {}
for key, space in env.observation_space.spaces.items():
new_spaces[key] = self._calc_new_obs_space(space)
self.observation_space = spaces.Dict(new_spaces)
else:
self.observation_space = self._calc_new_obs_space(env.observation_space)
示例15: __init__
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Dict [as 别名]
def __init__(self, bit_length=16, max_steps=None):
super(BitFlip, self).__init__()
assert bit_length >= 1, 'bit_length must be >= 1, found {}'.format(bit_length)
self.bit_length = bit_length
if max_steps is None:
self.max_steps = bit_length
else:
self.max_steps = max_steps
self.last_action = -1 # -1 for reset
self.steps = 0
self.seed()
self.action_space = spaces.Discrete(bit_length + 1) # index = n means to not flip any bit
# achieved goal and observation are identical in bit_flip environment, however it is made this way to be
# compatible with Openai GoalEnv
self.observation_space = spaces.Dict(dict(
observation=spaces.Box(low=0, high=1, shape=(bit_length,), dtype=np.int32),
achieved_goal=spaces.Box(low=0, high=1, shape=(bit_length,), dtype=np.int32),
desired_goal=spaces.Box(low=0, high=1, shape=(bit_length,), dtype=np.int32),
))
self.reset()