本文整理汇总了Python中gym.spaces.Tuple方法的典型用法代码示例。如果您正苦于以下问题:Python spaces.Tuple方法的具体用法?Python spaces.Tuple怎么用?Python spaces.Tuple使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gym.spaces
的用法示例。
在下文中一共展示了spaces.Tuple方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Tuple [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)])
示例2: _check_unsupported_obs_spaces
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Tuple [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. "
)
示例3: _check_obs
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Tuple [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))
示例4: _detect_gym_spaces
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Tuple [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 Tuple [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 Tuple [as 别名]
def __init__(self, max_steps, batch_size=1):
self.max_steps = max_steps
self.batch_size = batch_size
self.payout_mat = np.array([[-2,0],[-3,-1]])
self.states = np.array([[1,2],[3,4]])
self.action_space = Tuple([
Discrete(self.NUM_ACTIONS) for _ in range(self.NUM_AGENTS)
])
self.observation_space = Tuple([
OneHot(self.NUM_STATES) for _ in range(self.NUM_AGENTS)
])
self.available_actions = [
np.ones((batch_size, self.NUM_ACTIONS), dtype=int)
for _ in range(self.NUM_AGENTS)
]
self.step_count = None
示例7: gym_space_distribution
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Tuple [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 Tuple [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: step
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Tuple [as 别名]
def step(self, actions):
actions = np.array(actions)
if not self.is_continuous:
actions = sth.int2action_index(actions, self.discrete_action_dim_list)
if self.action_type == 'discrete':
actions = actions.reshape(-1,)
elif self.action_type == 'Tuple(Discrete)':
actions = actions.reshape(self.n, -1).tolist()
results = Asyn.op_func(self.envs, Asyn.OP.STEP, actions)
obs, reward, done, info = [np.asarray(e) for e in zip(*results)]
reward = reward.astype('float32')
dones_index = np.where(done)[0]
if dones_index.shape[0] > 0:
correct_new_obs = self.partial_reset(obs, dones_index)
else:
correct_new_obs = obs
if self.obs_type == 'visual':
obs = obs[:, np.newaxis, ...]
correct_new_obs = correct_new_obs[:, np.newaxis, ...]
return obs, reward, done, info, correct_new_obs
示例10: __init__
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Tuple [as 别名]
def __init__(self, env_config):
self.map = [m for m in MAP_DATA.split("\n") if m]
self.x_dim = len(self.map)
self.y_dim = len(self.map[0])
logger.info("Loaded map {} {}".format(self.x_dim, self.y_dim))
for x in range(self.x_dim):
for y in range(self.y_dim):
if self.map[x][y] == "S":
self.start_pos = (x, y)
elif self.map[x][y] == "F":
self.end_pos = (x, y)
logger.info("Start pos {} end pos {}".format(self.start_pos,
self.end_pos))
self.observation_space = Tuple([
Box(0, 100, shape=(2, )), # (x, y)
Discrete(4), # wind direction (N, E, S, W)
])
self.action_space = Discrete(2) # whether to move or not
示例11: step
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Tuple [as 别名]
def step(self, action):
if self.check_action_bounds and not self.action_space.contains(action):
raise ValueError("Illegal action for {}: {}".format(
self.action_space, action))
if (isinstance(self.action_space, Tuple)
and len(action) != len(self.action_space.spaces)):
raise ValueError("Illegal action for {}: {}".format(
self.action_space, action))
return self.observation_space.sample(), \
float(self.reward_space.sample()), \
bool(np.random.choice(
[True, False], p=[self.p_done, 1.0 - self.p_done]
)), {}
# Multi-agent version of the RandomEnv.
示例12: _validate
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Tuple [as 别名]
def _validate(obs_space, action_space):
if not hasattr(obs_space, "original_space") or \
not isinstance(obs_space.original_space, Tuple):
raise ValueError("Obs space must be a Tuple, got {}. Use ".format(
obs_space) + "MultiAgentEnv.with_agent_groups() to group related "
"agents for QMix.")
if not isinstance(action_space, Tuple):
raise ValueError(
"Action space must be a Tuple, got {}. ".format(action_space) +
"Use MultiAgentEnv.with_agent_groups() to group related "
"agents for QMix.")
if not isinstance(action_space.spaces[0], Discrete):
raise ValueError(
"QMix requires a discrete action space, got {}".format(
action_space.spaces[0]))
if len({str(x) for x in obs_space.original_space.spaces}) > 1:
raise ValueError(
"Implementation limitation: observations of grouped agents "
"must be homogeneous, got {}".format(
obs_space.original_space.spaces))
if len({str(x) for x in action_space.spaces}) > 1:
raise ValueError(
"Implementation limitation: action space of grouped agents "
"must be homogeneous, got {}".format(action_space.spaces))
示例13: default
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Tuple [as 别名]
def default(self, obj):
if isinstance(obj, np.ndarray):
return obj.tolist()
elif isinstance(obj, constants.Item):
return obj.value
elif isinstance(obj, constants.Action):
return obj.value
elif isinstance(obj, constants.GameType):
return obj.value
elif isinstance(obj, np.int64):
return int(obj)
elif hasattr(obj, 'to_json'):
return obj.to_json()
elif isinstance(obj, spaces.Discrete):
return obj.n
elif isinstance(obj, spaces.Tuple):
return [space.n for space in obj.spaces]
return json.JSONEncoder.default(self, obj)
示例14: __init__
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Tuple [as 别名]
def __init__(self):
self.height = 4
self.width = 12
self.action_space = spaces.Discrete(4)
self.observation_space = spaces.Tuple((
spaces.Discrete(self.height),
spaces.Discrete(self.width)
))
self.moves = {
0: (-1, 0), # up
1: (0, 1), # right
2: (1, 0), # down
3: (0, -1), # left
}
# begin in start state
self.reset()
示例15: __init__
# 需要导入模块: from gym import spaces [as 别名]
# 或者: from gym.spaces import Tuple [as 别名]
def __init__(self):
self.height = 7
self.width = 10
self.action_space = spaces.Discrete(4)
self.observation_space = spaces.Tuple((
spaces.Discrete(self.height),
spaces.Discrete(self.width)
))
self.moves = {
0: (-1, 0), # up
1: (0, 1), # right
2: (1, 0), # down
3: (0, -1), # left
}
# begin in start state
self.reset()