OpenSpielWrapper¶
- torchrl.envs.OpenSpielWrapper(*args, **kwargs)[來源]¶
Google DeepMind OpenSpiel 環境封裝器。
GitHub: https://github.com/google-deepmind/open_spiel
文件: https://openspiel.readthedocs.io/en/latest/index.html
- 引數:
env (pyspiel.State) – 要封裝的遊戲。
- 關鍵字引數:
device (torch.device, 可選) – 如果提供,資料將被轉換到該裝置。預設為
None。batch_size (torch.Size, 可選) – 環境的批次大小。預設為
torch.Size([])。allow_done_after_reset (bool, 可選) – 如果為
True,則允許環境在呼叫reset()後立即結束。預設為False。group_map (MarlGroupMapType 或 Dict[str, List[str]]], 可選) – 如何在 tensordicts 中分組智慧體用於輸入/輸出。詳情請參閱
MarlGroupMapType。預設為ALL_IN_ONE_GROUP。categorical_actions (bool, 可選) – 如果為
True,分類動作規格將被轉換為等效的 TorchRL 型別 (torchrl.data.Categorical),否則將使用獨熱編碼 (torchrl.data.OneHot)。預設為False。return_state (bool, 可選) – 如果為
True,reset()和step()的輸出將包含“state”。可以將該 state 傳遞給reset()以重置到該特定 state,而不是重置到初始 state。預設為False。
- 變數:
available_envs – 可構建的環境
示例
>>> import pyspiel >>> from torchrl.envs import OpenSpielWrapper >>> from tensordict import TensorDict >>> base_env = pyspiel.load_game('chess').new_initial_state() >>> env = OpenSpielWrapper(base_env, return_state=True) >>> td = env.reset() >>> td = env.step(env.full_action_spec.rand()) >>> print(td) TensorDict( fields={ agents: TensorDict( fields={ action: Tensor(shape=torch.Size([2, 4672]), device=cpu, dtype=torch.int64, is_shared=False)}, batch_size=torch.Size([]), device=None, is_shared=False), next: TensorDict( fields={ agents: TensorDict( fields={ observation: Tensor(shape=torch.Size([2, 20, 8, 8]), device=cpu, dtype=torch.float32, is_shared=False), reward: Tensor(shape=torch.Size([2, 1]), device=cpu, dtype=torch.float32, is_shared=False)}, batch_size=torch.Size([2]), device=None, is_shared=False), current_player: Tensor(shape=torch.Size([]), device=cpu, dtype=torch.int32, is_shared=False), done: Tensor(shape=torch.Size([1]), device=cpu, dtype=torch.bool, is_shared=False), state: NonTensorData(data=FEN: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1 3009 , batch_size=torch.Size([]), device=None), terminated: Tensor(shape=torch.Size([1]), device=cpu, dtype=torch.bool, is_shared=False)}, batch_size=torch.Size([]), device=None, is_shared=False)}, batch_size=torch.Size([]), device=None, is_shared=False) >>> print(env.available_envs) ['2048', 'add_noise', 'amazons', 'backgammon', ...]
reset()可以恢復特定的 state,而不是初始 state,前提是return_state=True。>>> import pyspiel >>> from torchrl.envs import OpenSpielWrapper >>> from tensordict import TensorDict >>> base_env = pyspiel.load_game('chess').new_initial_state() >>> env = OpenSpielWrapper(base_env, return_state=True) >>> td = env.reset() >>> td = env.step(env.full_action_spec.rand()) >>> td_restore = td["next"] >>> td = env.step(env.full_action_spec.rand()) >>> # Current state is not equal `td_restore` >>> (td["next"] == td_restore).all() False >>> td = env.reset(td_restore) >>> # After resetting, now the current state is equal to `td_restore` >>> (td == td_restore).all() True