向量化¶
Gymnasium.vector.VectorEnv¶
- class gymnasium.vector.VectorEnv[來源]¶
向量化環境的基類,用於平行執行相同環境的多個獨立副本。
向量環境可以通過同時採樣多個子環境來線性加速每秒採取的步驟。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]] [來源]¶
為每個平行環境採取一個動作。
- 參數:
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]] [來源]¶
重置所有平行環境並返回初始觀察和資訊的批次。
- 參數:
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] = {}¶
環境的中繼資料,包含渲染模式、渲染 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()
等效的函數。