向量化¶
Gymnasium.vector.VectorEnv¶
- class gymnasium.vector.VectorEnv[source]¶
用于向量化环境的基类,用于并行运行相同环境的多个独立副本。
向量环境可以通过同时采样多个子环境,在每秒采取的步骤方面提供线性加速。Gymnasium 包含两个通用向量环境:
AsyncVectorEnv
和SyncVectorEnv
以及几个自定义向量环境实现。对于reset()
和step()
批处理 observations、rewards、terminations、truncations 和 info 用于每个子环境,请参见下面的示例。对于 rewards、terminations 和 truncations,数据被打包到形状为 (num_envs,) 的 NumPy 数组中。对于 observations(和 actions,批处理过程取决于观察(和动作)空间的类型,并且通常针对神经网络输入/输出进行了优化。对于 info,数据被保留为字典,以便键将为所有子环境提供数据。为了创建环境,
make_vec()
是向量环境等效于make()
,用于轻松创建包含多个独特参数以修改环境质量、环境数量、向量化器类型、向量化器参数的向量环境。注意
参数
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()
为了避免必须等到所有子环境都终止才能重置,实现会在每集结束时自动重置子环境(terminated 或 truncated 为 True)。因此,在将观察结果添加到回放缓冲区时,这需要知道每个子环境的观察结果(和信息)何时是来自自动重置的第一个观察结果。我们建议使用额外的变量来存储此信息,例如
has_autoreset = np.logical_or(terminated, truncated)
。向量环境具有供用户理解实现的额外属性
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¶
环境的
EnvSpec
,通常在gymnasium.make_vec()
期间设置
- VectorEnv.metadata: dict[str, Any] = {}¶
环境的元数据,包含渲染模式、渲染 FPS 等
- 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 的种子,如果 RNG 的种子未知,则为 -1
创建矢量环境¶
为了创建矢量环境,gymnasium 提供了 gymnasium.make_vec()
作为 gymnasium.make()
的等效函数。