Skip to main content

GPU/TPU-accelerated parallel game simulators for reinforcement learning (RL)

Project description

ci Python PyPI version License codecov arXiv

A collection of GPU/TPU-accelerated parallel game simulators for reinforcement learning (RL)

📣 v2.0.0 is released! It breaks compatibility in stochastic environments (e.g., 2048) and auto_reset.

Why Pgx?

Brax, a JAX-native physics engine, provides extremely high-speed parallel simulation for RL in continuous state space. Then, what about RL in discrete state spaces like Chess, Shogi, and Go? Pgx provides a wide variety of JAX-native game simulators! Highlighted features include:

  • Super fast in parallel execution on accelerators
  • 🎲 Various game support including Backgammon, Chess, Shogi, and Go
  • 🖼️ Beautiful visualization in SVG format

Quick start

Training examples

Usage

Pgx is available on PyPI. Note that your Python environment has jax and jaxlib installed, depending on your hardware specification.

$ pip install pgx

The following code snippet shows a simple example of using Pgx. You can try it out in this Colab. Note that all step functions in Pgx environments are JAX-native., i.e., they are all JIT-able. Please refer to the documentation for more details.

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)  # vectorized states
while not (state.terminated | state.truncated).all():
    action = model(state.current_player, state.observation, state.legal_action_mask)
    # step(state, action, keys) for stochastic envs
    state = step(state, action)  # state.rewards with shape (1024, 2)

Pgx is a library that focuses on faster implementations rather than just the API itself. However, the API itself is also sufficiently general. For example, all environments in Pgx can be converted to the AEC API of PettingZoo, and you can run Pgx environments through the PettingZoo API. You can see the demonstration in this Colab.

📣 API v2 (v2.0.0)

Pgx has been updated from API v1 to v2 as of November 8, 2023 (release v2.0.0). As a result, the signature for Env.step has changed as follows:

  • v1: step(state: State, action: Array)
  • v2: step(state: State, action: Array, key: Optional[PRNGKey] = None)

Also, pgx.experimental.auto_reset are changed to specify key as the third argument.

Purpose of the update: In API v1, even in environments with stochastic state transitions, the state transitions were deterministic, determined by the _rng_key inside the state. This was intentional, with the aim of increasing reproducibility. However, when using planning algorithms in this environment, there is a risk that information about the underlying true randomness could "leak." To make it easier for users to conduct correct experiments, Env.step has been changed to explicitly specify a key.

Impact of the update: Since the key is optional, it is still possible to execute as env.step(state, action) like API v1 in deterministic environments like Go and chess, so there is no impact on these games. As of v2.0.0, only 2048, backgammon, and MinAtar suite are affected by this change.

Supported games

Backgammon Chess Shogi Go

Use pgx.available_envs() -> Tuple[EnvId] to see the list of currently available games. Given an <EnvId>, you can create the environment via

>>> env = pgx.make(<EnvId>)
Game/EnvId Visualization Version Five-word description by ChatGPT
2048
"2048"
v2 Merge tiles to create 2048.
Animal Shogi
"animal_shogi"
v0 Animal-themed child-friendly shogi.
Backgammon
"backgammon"
v2 Luck aids bearing off checkers.
Bridge bidding
"bridge_bidding"
v0 Partners exchange information via bids.
Chess
"chess"
v2 Checkmate opponent's king to win.
Connect Four
"connect_four"
v0 Connect discs, win with four.
Gardner Chess
"gardner_chess"
v0 5x5 chess variant, excluding castling.
Go
"go_9x9" "go_19x19"
v0 Strategically place stones, claim territory.
Hex
"hex"
v0 Connect opposite sides, block opponent.
Kuhn Poker
"kuhn_poker"
v0 Three-card betting and bluffing game.
Leduc hold'em
"leduc_holdem"
v0 Two-suit, limited deck poker.
MinAtar/Asterix
"minatar-asterix"
v1 Avoid enemies, collect treasure, survive.
MinAtar/Breakout
"minatar-breakout"
v1 Paddle, ball, bricks, bounce, clear.
MinAtar/Freeway
"minatar-freeway"
v1 Dodging cars, climbing up freeway.
MinAtar/Seaquest
"minatar-seaquest"
v1 Underwater submarine rescue and combat.
MinAtar/SpaceInvaders
"minatar-space_invaders"
v1 Alien shooter game, dodge bullets.
Othello
"othello"
v0 Flip and conquer opponent's pieces.
Shogi
"shogi"
v0 Japanese chess with captured pieces.
Sparrow Mahjong
"sparrow_mahjong"
v1 A simplified, children-friendly Mahjong.
Tic-tac-toe
"tic_tac_toe"
v0 Three in a row wins.
Versioning policy

Each environment is versioned, and the version is incremented when there are changes that affect the performance of agents or when there are changes that are not backward compatible with the API. If you want to pursue complete reproducibility, we recommend that you check the version of Pgx and each environment as follows:

>>> pgx.__version__
'1.0.0'
>>> env.version
'v0'

See also

Pgx is intended to complement these JAX-native environments with (classic) board game suits:

Combining Pgx with these JAX-native algorithms/implementations might be an interesting direction:

Citation

If you use Pgx in your work, please cite the following paper:

@inproceedings{koyamada2023pgx,
  title={Pgx: Hardware-Accelerated Parallel Game Simulators for Reinforcement Learning},
  author={Koyamada, Sotetsu and Okano, Shinri and Nishimori, Soichiro and Murata, Yu and Habara, Keigo and Kita, Haruka and Ishii, Shin},
  booktitle={Advances in Neural Information Processing Systems},
  year={2023}
}

LICENSE

Apache-2.0

Project details


Release history Release notifications | RSS feed

This version

2.0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pgx-2.0.1.tar.gz (338.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pgx-2.0.1-py3-none-any.whl (412.5 kB view details)

Uploaded Python 3

File details

Details for the file pgx-2.0.1.tar.gz.

File metadata

  • Download URL: pgx-2.0.1.tar.gz
  • Upload date:
  • Size: 338.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for pgx-2.0.1.tar.gz
Algorithm Hash digest
SHA256 9c4ed199218d0f4f62896d99ed5057655c175fdd1d0a92ad82c628e9c0f99584
MD5 5a1d2757c89f6284a1edec5906582c6e
BLAKE2b-256 3fb9bc6f34e7627c546a983a323db2bd8d3840403f75b6eb1a8667bf1edd0c84

See more details on using hashes here.

File details

Details for the file pgx-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: pgx-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 412.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for pgx-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8f457b79f9acf181a932505fcc8d0d0b0bd11d82b28f957617ce3363b5d53313
MD5 89b0e7a6a2e6b5d55202b79966fbcbd5
BLAKE2b-256 c0b5b8963addc76f6d75db1fc02ef2dfbc64dcc472b6e45d8ba96498a6b1e837

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page