用于强化学习(RL)的GPU/TPU加速并行游戏模拟器集合
<div align="center"> <img src="https://raw.githubusercontent.com/sotetsuk/pgx/main/docs/assets/go_dark.gif#gh-dark-mode-only" width="30%"><img src="https://raw.githubusercontent.com/sotetsuk/pgx/main/docs/assets/go_dark.gif#gh-dark-mode-only" width="30%" style="transform:rotate(270deg);"><img src="https://raw.githubusercontent.com/sotetsuk/pgx/main/docs/assets/go_dark.gif#gh-dark-mode-only" width="30%" style="transform:rotate(90deg);"> <img src="https://raw.githubusercontent.com/sotetsuk/pgx/main/docs/assets/go_light.gif#gh-light-mode-only" width="30%"><img src="https://raw.githubusercontent.com/sotetsuk/pgx/main/docs/assets/go_light.gif#gh-light-mode-only" width="30%" style="transform:rotate(270deg);"><img src="https://raw.githubusercontent.com/sotetsuk/pgx/main/docs/assets/go_light.gif#gh-light-mode-only" width="30%" style="transform:rotate(90deg);"> </div>Brax,一个JAX原生的物理引擎,为连续状态空间的RL提供了极高速的并行模拟。那么,对于像国际象棋、将棋和围棋这样的离散状态空间的RL呢?Pgx提供了各种JAX原生的游戏模拟器!主要特点包括:
Pgx可在PyPI上获取。请注意,您的Python环境需要根据硬件规格安装jax
和jaxlib
。
$ pip install pgx
以下代码片段展示了使用Pgx的简单示例。
您可以在这个Colab中尝试。
请注意,Pgx环境中的所有step
函数都是JAX原生的,即它们都是可JIT编译的。
更多详情请参阅文档。
import jax import pgx env = pgx.make("go_19x19") init = jax.jit(jax.vmap(env.init)) step = jax.jit(jax.vmap(env.step))
batch_size = 1024 keys = jax.random.split(jax.random.PRNGKey(42), batch_size) state = init(keys) # 向量化状态 while not (state.terminated | state.truncated).all(): action = model(state.current_player, state.observation, state.legal_action_mask) # 对于随机环境使用 step(state, action, keys) state = step(state, action) # state.rewards 形状为 (1024, 2)
Pgx 是一个专注于更快实现而非仅仅关注 API 本身的库。 然而,其 API 本身也足够通用。例如,Pgx 中的所有环境都可以转换为 PettingZoo 的 AEC API,您可以通过 PettingZoo API 运行 Pgx 环境。 您可以在这个 Colab 中查看演示。
<details> <summary>📣 API v2 (v2.0.0)</summary>Pgx 已于 2023 年 11 月 8 日从 API v1 更新至 v2(发布版本 v2.0.0
)。因此,Env.step
的签名发生了如下变化:
step(state: State, action: Array)
step(state: State, action: Array, key: Optional[PRNGKey] = None)
此外,pgx.experimental.auto_reset
也改为将 key
指定为第三个参数。
更新目的: 在 API v1 中,即使在具有随机状态转换的环境中,状态转换也是由 state
内部的 _rng_key
决定的确定性转换。这是有意为之,目的是提高可重现性。然而,在这种环境中使用规划算法时,存在底层真实随机性信息可能"泄露"的风险。为了让用户更容易进行正确的实验,Env.step
已更改为显式指定 key。
更新影响: 由于 key
是可选的,在像围棋和国际象棋这样的确定性环境中,仍然可以像 API v1 那样执行 env.step(state, action)
,所以这些游戏不受影响。截至 v2.0.0
,只有 2048、双陆棋和 MinAtar 套件受此更改影响。
双陆棋 | 国际象棋 | 将棋 | 围棋 |
---|---|---|---|
<img src="https://yellow-cdn.veclightyear.com/0a4dffa0/456923a8-5e62-479c-8c6f-f5c8aabd44bf.gif#gh-dark-mode-only" width="170px"><img src="https://yellow-cdn.veclightyear.com/0a4dffa0/8a603c6f-13e9-40b1-9a37-e6f8ffffc987.gif#gh-light-mode-only" width="170px"> | <img src="https://yellow-cdn.veclightyear.com/0a4dffa0/f98f8076-0b6b-4cd0-8dc8-8bab7be373d4.gif#gh-dark-mode-only" width="158px"><img src="https://yellow-cdn.veclightyear.com/0a4dffa0/a3f6c951-3500-4c22-af0e-a3fde3ccf60e.gif#gh-light-mode-only" width="158px"> | <img src="https://yellow-cdn.veclightyear.com/0a4dffa0/3dbd00f8-c4ff-4df2-8d31-1b7cc3d60503.gif#gh-dark-mode-only" width="170px"><img src="https://yellow-cdn.veclightyear.com/0a4dffa0/663094b3-e451-41cd-9354-e68249955477.gif#gh-light-mode-only" width="170px"> | <img src="https://yellow-cdn.veclightyear.com/0a4dffa0/dceb6721-4a30-4053-87ce-1f7ef8e1dd34.gif#gh-dark-mode-only" width="160px"><img src="https://yellow-cdn.veclightyear.com/0a4dffa0/319c8f53-8b10-440d-a253-168a55c07b7d.gif#gh-light-mode-only" width="160px"> |
使用 pgx.available_envs() -> Tuple[EnvId]
可以查看当前可用游戏的列表。给定一个 <EnvId>
,您可以通过以下方式创建环境:
>>> env = pgx.make(<EnvId>)
游戏/环境ID | 可视化 | 版本 | ChatGPT给出的五字描述 |
---|---|---|---|
<a href="https://en.wikipedia.org/wiki/2048_(video_game)">2048</a> <br> "2048" | <img src="https://yellow-cdn.veclightyear.com/0a4dffa0/cbf71ed8-c922-4a76-953f-d327e811decd.gif" width="60px"><img src="https://yellow-cdn.veclightyear.com/0a4dffa0/bfae7c97-08ac-4967-83e3-464ec9ccc6c0.gif" width="60px"> | v2 | 合并方块创建2048。 |
<a href="https://en.wikipedia.org/wiki/D%C5%8Dbutsu_sh%C5%8Dgi">动物将棋</a><br>"animal_shogi" | <img src="https://yellow-cdn.veclightyear.com/0a4dffa0/16f3cd53-20a3-403b-b65e-b8ca69371009.gif" width="60px"><img src="https://yellow-cdn.veclightyear.com/0a4dffa0/5ca4e208-4a27-490f-a50c-e9037dcf7310.gif" width="60px"> | v0 | 适合儿童的动物主题将棋。 |
<a href="https://en.wikipedia.org/wiki/Backgammon">西洋双陆棋</a><br>"backgammon" | <img src="https://yellow-cdn.veclightyear.com/0a4dffa0/456923a8-5e62-479c-8c6f-f5c8aabd44bf.gif" width="60px"><img src="https://yellow-cdn.veclightyear.com/0a4dffa0/8a603c6f-13e9-40b1-9a37-e6f8ffffc987.gif" width="60px"> | v2 | 运气助力移除棋子获胜。 |
<a href="https://en.wikipedia.org/wiki/Contract_bridge">桥牌叫牌</a><br>"bridge_bidding" | <img src="https://yellow-cdn.veclightyear.com/0a4dffa0/a4b289a8-0b49-49c5-9d53-75ebcc5b990b.gif" width="60px"><img src="https://yellow-cdn.veclightyear.com/0a4dffa0/fdd8f09c-0b98-4655-9729-d029cc6eb00e.gif" width="60px"> | v1 |