OpenSpiel: A Framework for Reinforcement Learning in Games
OpenSpiel is a collection of environments and algorithms for research in reinforcement learning and search in games. It provides implementations of numerous games (e.g., Chess, Go, Poker, Tic-Tac-Toe) and common algorithms (e.g., MCTS, AlphaZero, CFR). The library is currently at version 1.6.12, with frequent staging releases in preparation for a major 2.0 release. It's developed and maintained by Google DeepMind.
Common errors
-
ModuleNotFoundError: No module named 'pyspiel'
cause The `open-spiel` package, or its `pyspiel` sub-module, is not installed or not accessible in the current Python environment.fixEnsure you have successfully installed OpenSpiel using `pip install open-spiel` and that your environment is correctly activated. -
RuntimeError: OpenSpiel requires Python version >= 3.11, but found X.Y.
cause You are attempting to use OpenSpiel with an unsupported Python version (older than 3.11).fixUpgrade your Python installation to version 3.11 or later, or use a virtual environment configured with a supported Python version. -
TypeError: load_game() missing 1 required positional argument: 'game_string'
cause The `pyspiel.load_game()` function expects a string argument representing the name of the game to load (e.g., 'tic_tac_toe').fixCall `pyspiel.load_game()` with the game's identifier as a string, for example: `game = pyspiel.load_game('tic_tac_toe')`.
Warnings
- breaking OpenSpiel is currently in a 'staging' phase for version 2.0. Significant breaking changes are expected in the upcoming 2.0 release, for which a full changelog will be made available. Users should be prepared for API shifts.
- breaking Support for Python 3.9 was dropped in version 1.6.4. The current version (1.6.12) requires Python 3.11 or newer.
- gotcha OpenSpiel is a C++ library with Python bindings. While `pip install` usually provides pre-built wheels, building from source (e.g., for custom modifications or specific platforms) can be complex and requires a C++ compiler, CMake, and other development tools.
Install
-
pip install open-spiel
Imports
- pyspiel
import open_spiel
import pyspiel
- load_game
game = pyspiel.tic_tac_toe()
game = pyspiel.load_game('tic_tac_toe')
Quickstart
import pyspiel
# Load a game by its string identifier
game = pyspiel.load_game("tic_tac_toe")
# Create a new initial state for the game
state = game.new_initial_state()
# Print the initial state
print("Initial state:\n", state)
# Apply a move (e.g., player 0 places 'X' in the center)
# Actions are integer indices, you can get valid actions via state.legal_actions()
state.apply_action(4) # Assuming 4 is a valid action in Tic-Tac-Toe
print("\nState after move:\n", state)
# Check if the game is terminal
if state.is_terminal():
print("\nGame is over. Returns:", state.returns())