Vectorize¶
Gymnasium.vector.VectorEnv¶
- class gymnasium.vector.VectorEnv[source]¶
用于并行运行多个相同环境独立副本的向量化环境基类。
向量化环境可以通过同时采样多个子环境,在每秒执行步数(steps per second)上提供线性的加速。Gymnasium 包含两种通用的向量化环境:
AsyncVectorEnv和SyncVectorEnv,以及多种自定义的向量化环境实现。有关每个子环境的reset()和step()批处理 observations(观测)、rewards(奖励)、terminations(终止)、truncations(截断)和 info(信息),请参考下方的示例。对于 rewards、terminations 和 truncations,数据被封装为形状为 (num_envs,) 的 NumPy 数组。对于 observations(以及 actions),批处理过程取决于观测(和动作)空间的类型,通常针对神经网络的输入/输出进行了优化。对于 info,数据保持为字典格式,因此通过键名即可获取所有子环境的数据。在创建环境时,
make_vec()是make()的向量化环境等效函数,可以轻松创建向量化环境,其中包含用于修改环境质量、环境数量、向量化器类型及向量化器参数的多个特有参数。为了避免在重置前必须等待所有子环境终止,实现可以在剧集结束时(terminated 或 truncated 为 True)自动重置子环境。这对于正确实现带有向量化环境的训练算法至关重要。默认情况下,Gymnasium 的实现使用 next-step(下一步)自动重置,并使用
AutoresetMode枚举作为选项。向量化环境使用的模式应在 metadata[“autoreset_mode”] 中可用。警告:某些向量化实现或训练算法可能仅支持特定的自动重置模式。欲了解更多信息,请阅读 https://farama.org/Vector-Autoreset-Mode。注意
reset()和step()的 info 参数在 v0.25 之前被实现为每个子环境的字典列表。然而,这在 v0.25+ 中被修改为以 NumPy 数组为每个键值的字典。若要使用旧版的 info 风格,请使用DictInfoToList包装器。示例
>>> import gymnasium as gym >>> envs = gym.make_vec("CartPole-v1", num_envs=3, vectorization_mode="sync", wrappers=(gym.wrappers.TimeAwareObservation,)) >>> envs = gym.wrappers.vector.ClipReward(envs, min_reward=0.2, max_reward=0.8) >>> envs <ClipReward, SyncVectorEnv(CartPole-v1, num_envs=3)> >>> envs.num_envs 3 >>> envs.action_space MultiDiscrete([2 2 2]) >>> envs.observation_space Box([[-4.80000019 -inf -0.41887903 -inf 0. ] [-4.80000019 -inf -0.41887903 -inf 0. ] [-4.80000019 -inf -0.41887903 -inf 0. ]], [[4.80000019e+00 inf 4.18879032e-01 inf 5.00000000e+02] [4.80000019e+00 inf 4.18879032e-01 inf 5.00000000e+02] [4.80000019e+00 inf 4.18879032e-01 inf 5.00000000e+02]], (3, 5), float64) >>> observations, infos = envs.reset(seed=123) >>> observations array([[ 0.01823519, -0.0446179 , -0.02796401, -0.03156282, 0. ], [ 0.02852531, 0.02858594, 0.0469136 , 0.02480598, 0. ], [ 0.03517495, -0.000635 , -0.01098382, -0.03203924, 0. ]]) >>> infos {} >>> _ = envs.action_space.seed(123) >>> actions = envs.action_space.sample() >>> observations, rewards, terminations, truncations, infos = envs.step(actions) >>> observations array([[ 0.01734283, 0.15089367, -0.02859527, -0.33293587, 1. ], [ 0.02909703, -0.16717631, 0.04740972, 0.3319138 , 1. ], [ 0.03516225, -0.19559774, -0.01162461, 0.25715804, 1. ]]) >>> rewards array([0.8, 0.8, 0.8]) >>> terminations array([False, False, False]) >>> truncations array([False, False, False]) >>> infos {} >>> envs.close()
向量化环境具有额外的属性,供用户理解其实现:
num_envs- 向量化环境中的子环境数量observation_space- 向量化环境的批处理观测空间single_observation_space- 单个子环境的观测空间action_space- 向量化环境的批处理动作空间single_action_space- 单个子环境的动作空间
方法¶
- VectorEnv.step(actions: ActType) tuple[ObsType, ArrayType, ArrayType, ArrayType, dict[str, Any]][source]¶
为每个并行环境执行动作。
- 参数:
actions – 具有
action_space形状的动作批次。- 返回:
批次返回 (observations, rewards, terminations, truncations, infos)
注意
由于向量化环境会对终止和截断的子环境进行自动重置,这将在 terminated 或 truncated 为 True 之后的下一步发生。
示例
>>> import gymnasium as gym >>> import numpy as np >>> envs = gym.make_vec("CartPole-v1", num_envs=3, vectorization_mode="sync") >>> _ = envs.reset(seed=42) >>> actions = np.array([1, 0, 1], dtype=np.int32) >>> observations, rewards, terminations, truncations, infos = envs.step(actions) >>> observations array([[ 0.02727336, 0.18847767, 0.03625453, -0.26141977], [ 0.01431748, -0.24002443, -0.04731862, 0.3110827 ], [-0.03822722, 0.1710671 , -0.00848456, -0.2487226 ]], dtype=float32) >>> rewards array([1., 1., 1.]) >>> terminations array([False, False, False]) >>> terminations array([False, False, False]) >>> infos {}
- VectorEnv.reset(*, seed: int | None = None, options: dict[str, Any] | None = None) tuple[ObsType, dict[str, Any]][source]¶
重置所有并行环境,并返回初始观测和信息的批次。
- 参数:
seed – 环境重置随机种子
options – 是否返回选项
- 返回:
来自向量化环境的观测和信息批次。
示例
>>> import gymnasium as gym >>> envs = gym.make_vec("CartPole-v1", num_envs=3, vectorization_mode="sync") >>> observations, infos = envs.reset(seed=42) >>> observations array([[ 0.0273956 , -0.00611216, 0.03585979, 0.0197368 ], [ 0.01522993, -0.04562247, -0.04799704, 0.03392126], [-0.03774345, -0.02418869, -0.00942293, 0.0469184 ]], dtype=float32) >>> infos {}
属性¶
- VectorEnv.num_envs: int¶
向量化环境中的子环境数量。
- VectorEnv.action_space: gym.Space¶
(批处理的)动作空间。step 的输入动作必须是 action_space 的有效元素。
- VectorEnv.observation_space: gym.Space¶
(批处理的)观测空间。reset 和 step 返回的观测必须是 observation_space 的有效元素。
- VectorEnv.single_action_space: gym.Space¶
子环境的动作空间。
- VectorEnv.single_observation_space: gym.Space¶
子环境的观测空间。
- VectorEnv.spec: EnvSpec | None = None¶
通常在
gymnasium.make_vec()期间设置的环境EnvSpec。
- VectorEnv.metadata: dict[str, Any] = {}¶
环境的元数据,包含渲染模式、渲染帧率等。
- VectorEnv.render_mode: str | None = None¶
环境的渲染模式,应遵循与 Env.render_mode 类似的规范。
- VectorEnv.closed: bool = False¶
向量化环境是否已关闭。
其他方法¶
- property VectorEnv.unwrapped¶
返回基础环境。
- property VectorEnv.np_random: Generator¶
返回环境内部的
_np_random,如果未设置,将使用随机种子进行初始化。- 返回:
`np.random.Generator` 的实例
- property VectorEnv.np_random_seed: int | None¶
返回环境内部的
_np_random_seed,如果未设置,将首先使用随机整数作为种子进行初始化。如果
np_random_seed是直接设置的,而非通过reset()或set_np_random_through_seed()设置,则种子值将为 -1。- 返回:
int – 当前 np_random 的种子,如果随机数生成器的种子未知,则为 -1
创建向量化环境¶
为了创建向量化环境,gymnasium 提供了 gymnasium.make_vec(),这是 gymnasium.make() 的等效函数。