OpenSpielWrapper¶
- torchrl.envs.OpenSpielWrapper(*args, **kwargs)[source]¶
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, optional) – 如果為
True,則允許在呼叫reset()後立即將環境設定為done。預設為False。group_map (MarlGroupMapType 或 Dict[str, List[str]], optional) – 如何在 tensordicts 中分組智慧體以進行輸入/輸出。更多資訊請參見
MarlGroupMapType。預設為ALL_IN_ONE_GROUP。categorical_actions (bool, 可選) – 如果為
True,則分類規範將轉換為等效的 TorchRL (torchrl.data.Categorical),否則將使用獨熱編碼 (torchrl.data.OneHot)。預設為False。return_state (bool, optional) – 如果為
True,則“state”將包含在reset()和step()的輸出中。狀態可以傳遞給reset()以重置到該狀態,而不是重置到初始狀態。預設為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()可以恢復特定狀態,而不是初始狀態,只要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