Convert Figma logo to code with AI

google-deepmind logoopen_spiel

OpenSpiel is a collection of environments and algorithms for research in general reinforcement learning and search/planning in games.

4,164
918
4,164
28

Top Related Projects

34,461

A toolkit for developing and comparing reinforcement learning algorithms.

An API standard for multi-agent reinforcement learning environments, with popular reference environments and related utilities

An API standard for single-agent reinforcement learning environments, with popular reference environments and related utilities (formerly Gym)

16,887

The Unity Machine Learning Agents Toolkit (ML-Agents) is an open-source project that enables games and simulations to serve as environments for training intelligent agents using deep reinforcement learning and imitation learning.

15,630

OpenAI Baselines: high-quality implementations of reinforcement learning algorithms

Quick Overview

OpenSpiel is an open-source collection of environments and algorithms for research in general reinforcement learning and search/planning, with a focus on games. It provides a comprehensive suite of tools for developing and testing AI algorithms in various game scenarios, including both perfect and imperfect information games.

Pros

  • Extensive collection of game environments and algorithms
  • Supports multiple programming languages (C++, Python, Julia)
  • Well-documented and actively maintained by DeepMind
  • Facilitates reproducible research in game AI and reinforcement learning

Cons

  • Steep learning curve for beginners
  • Some games and algorithms may have limited documentation
  • Performance can be slower compared to specialized implementations
  • Requires significant computational resources for complex games and algorithms

Code Examples

  1. Creating and playing a simple game:
import pyspiel

game = pyspiel.load_game("tic_tac_toe")
state = game.new_initial_state()

while not state.is_terminal():
    legal_actions = state.legal_actions()
    action = legal_actions[0]  # Choose the first legal action
    state.apply_action(action)

print("Final score:", state.returns())
  1. Using a random bot to play a game:
import pyspiel
from open_spiel.python.algorithms import random_agent

game = pyspiel.load_game("kuhn_poker")
bots = [random_agent.RandomAgent(game, player_id) for player_id in range(game.num_players())]

state = game.new_initial_state()
while not state.is_terminal():
    current_player = state.current_player()
    action = bots[current_player].step(state)
    state.apply_action(action)

print("Final returns:", state.returns())
  1. Training a DQN agent:
import pyspiel
from open_spiel.python.algorithms import dqn

game = pyspiel.load_game("catch")
num_actions = game.num_distinct_actions()
state_shape = game.observation_tensor_shape()

agent = dqn.DQN(
    session=None,
    player_id=0,
    state_representation_size=state_shape[0],
    num_actions=num_actions,
    hidden_layers_sizes=[64, 64],
    replay_buffer_capacity=10000,
    batch_size=128
)

for _ in range(10000):
    agent.step()

print("Training complete")

Getting Started

To get started with OpenSpiel:

  1. Install OpenSpiel:
pip install open_spiel
  1. Import and use OpenSpiel in your Python code:
import pyspiel

# Load a game
game = pyspiel.load_game("tic_tac_toe")

# Create a new game state
state = game.new_initial_state()

# Print game information
print("Number of players:", game.num_players())
print("Number of distinct actions:", game.num_distinct_actions())
print("Initial state:", state)

For more detailed instructions and advanced usage, refer to the official OpenSpiel documentation.

Competitor Comparisons

34,461

A toolkit for developing and comparing reinforcement learning algorithms.

Pros of Gym

  • Wider variety of environments, including classic control, robotics, and Atari games
  • More extensive documentation and tutorials for beginners
  • Larger community and ecosystem of third-party environments

Cons of Gym

  • Focused primarily on single-agent reinforcement learning
  • Less emphasis on formal game theory concepts
  • Limited support for multi-agent scenarios

Code Comparison

Gym:

import gym
env = gym.make('CartPole-v1')
observation, info = env.reset(seed=42)
for _ in range(1000):
    action = env.action_space.sample()
    observation, reward, terminated, truncated, info = env.step(action)

Open Spiel:

import pyspiel
game = pyspiel.load_game("tic_tac_toe")
state = game.new_initial_state()
while not state.is_terminal():
    legal_actions = state.legal_actions()
    action = np.random.choice(legal_actions)
    state.apply_action(action)

Open Spiel focuses on game-theoretic environments and multi-agent scenarios, while Gym provides a broader range of single-agent reinforcement learning environments. Open Spiel offers more tools for analyzing game properties and implementing game-theoretic algorithms, whereas Gym is more accessible for beginners and has a larger ecosystem of environments.

An API standard for multi-agent reinforcement learning environments, with popular reference environments and related utilities

Pros of PettingZoo

  • Broader range of environments, including Atari games and more complex scenarios
  • More active community and frequent updates
  • Easier integration with popular RL libraries like Stable Baselines3

Cons of PettingZoo

  • Less focus on traditional board games and card games
  • May have a steeper learning curve for beginners due to its broader scope
  • Potentially less optimized for specific game theory research

Code Comparison

PettingZoo example:

from pettingzoo.classic import rps_v2
env = rps_v2.env()
env.reset()
for agent in env.agent_iter():
    observation, reward, done, info = env.last()
    action = env.action_space(agent).sample()
    env.step(action)

OpenSpiel example:

import pyspiel
game = pyspiel.load_game("kuhn_poker")
state = game.new_initial_state()
while not state.is_terminal():
    legal_actions = state.legal_actions()
    action = np.random.choice(legal_actions)
    state.apply_action(action)

Both libraries provide easy-to-use interfaces for creating and interacting with game environments. PettingZoo follows a more standardized RL approach, while OpenSpiel focuses on game-specific implementations.

An API standard for single-agent reinforcement learning environments, with popular reference environments and related utilities (formerly Gym)

Pros of Gymnasium

  • More focused on single-agent reinforcement learning environments
  • Simpler API and easier to get started for beginners
  • Wider adoption in the RL community, with more resources and tutorials available

Cons of Gymnasium

  • Limited support for multi-agent and game-theoretic scenarios
  • Fewer built-in algorithms and tools for advanced research
  • Less emphasis on formal game representations and analysis

Code Comparison

Gymnasium example:

import gymnasium as gym
env = gym.make("CartPole-v1")
observation, info = env.reset(seed=42)
for _ in range(1000):
    action = env.action_space.sample()
    observation, reward, terminated, truncated, info = env.step(action)

OpenSpiel example:

import pyspiel
game = pyspiel.load_game("tic_tac_toe")
state = game.new_initial_state()
while not state.is_terminal():
    legal_actions = state.legal_actions()
    action = np.random.choice(legal_actions)
    state.apply_action(action)

Gymnasium focuses on single-agent RL environments with a standardized interface, making it easier for beginners to get started. OpenSpiel provides a more comprehensive framework for multi-agent games and game-theoretic analysis, offering a wider range of tools for advanced research in areas like game theory and multi-agent RL.

16,887

The Unity Machine Learning Agents Toolkit (ML-Agents) is an open-source project that enables games and simulations to serve as environments for training intelligent agents using deep reinforcement learning and imitation learning.

Pros of ml-agents

  • Integrated with Unity game engine, allowing for easy implementation in game development
  • Provides a user-friendly interface for training and deploying AI agents
  • Supports a wide range of learning algorithms and environments

Cons of ml-agents

  • Limited to Unity-based environments, less flexible for general-purpose research
  • May have a steeper learning curve for those unfamiliar with Unity

Code comparison

ml-agents:

from mlagents_envs.environment import UnityEnvironment
from mlagents_envs.side_channel.engine_configuration_channel import EngineConfigurationChannel

channel = EngineConfigurationChannel()
env = UnityEnvironment(file_name="MyEnvironment", side_channels=[channel])

OpenSpiel:

import pyspiel

game = pyspiel.load_game("tic_tac_toe")
state = game.new_initial_state()

Summary

ml-agents is tailored for Unity game development, offering a comprehensive toolkit for implementing AI in games. OpenSpiel, on the other hand, is a more general-purpose framework for reinforcement learning research in games. While ml-agents excels in Unity-based environments, OpenSpiel provides greater flexibility for studying a wide variety of games and algorithms outside of a specific game engine.

15,630

OpenAI Baselines: high-quality implementations of reinforcement learning algorithms

Pros of Baselines

  • Focuses on reinforcement learning algorithms, providing implementations of popular RL methods
  • Includes tools for running experiments and visualizing results
  • Designed for ease of use and quick experimentation in RL research

Cons of Baselines

  • Limited to reinforcement learning, lacking support for other game theory concepts
  • Less comprehensive in terms of game environments compared to OpenSpiel
  • May require more setup and configuration for specific game scenarios

Code Comparison

OpenSpiel (Python):

import pyspiel

game = pyspiel.load_game("tic_tac_toe")
state = game.new_initial_state()
while not state.is_terminal():
    legal_actions = state.legal_actions()
    action = legal_actions[0]
    state.apply_action(action)

Baselines (Python):

import gym
from baselines import deepq

def callback(lcl, _glb):
    return lcl['t'] > 100

env = gym.make("CartPole-v0")
model = deepq.learn(env, callback=callback)

The code snippets demonstrate the different focus areas of the two libraries. OpenSpiel provides a framework for various games and game theory concepts, while Baselines concentrates on reinforcement learning algorithms and environments.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

OpenSpiel: A Framework for Reinforcement Learning in Games

Documentation Status build_and_test

OpenSpiel is a collection of environments and algorithms for research in general reinforcement learning and search/planning in games. OpenSpiel supports n-player (single- and multi- agent) zero-sum, cooperative and general-sum, one-shot and sequential, strictly turn-taking and simultaneous-move, perfect and imperfect information games, as well as traditional multiagent environments such as (partially- and fully- observable) grid worlds and social dilemmas. OpenSpiel also includes tools to analyze learning dynamics and other common evaluation metrics. Games are represented as procedural extensive-form games, with some natural extensions. The core API and games are implemented in C++ and exposed to Python. Algorithms and tools are written both in C++ and Python.

To try OpenSpiel in Google Colaboratory, please refer to open_spiel/colabs subdirectory or start here.

OpenSpiel visual asset

Index

Please choose among the following options:

For a longer introduction to the core concepts, formalisms, and terminology, including an overview of the algorithms and some results, please see OpenSpiel: A Framework for Reinforcement Learning in Games.

For an overview of OpenSpiel and example uses of the core API, please check out our tutorials:

If you use OpenSpiel in your research, please cite the paper using the following BibTeX:

@article{LanctotEtAl2019OpenSpiel,
  title     = {{OpenSpiel}: A Framework for Reinforcement Learning in Games},
  author    = {Marc Lanctot and Edward Lockhart and Jean-Baptiste Lespiau and
               Vinicius Zambaldi and Satyaki Upadhyay and Julien P\'{e}rolat and
               Sriram Srinivasan and Finbarr Timbers and Karl Tuyls and
               Shayegan Omidshafiei and Daniel Hennes and Dustin Morrill and
               Paul Muller and Timo Ewalds and Ryan Faulkner and J\'{a}nos Kram\'{a}r
               and Bart De Vylder and Brennan Saeta and James Bradbury and David Ding
               and Sebastian Borgeaud and Matthew Lai and Julian Schrittwieser and
               Thomas Anthony and Edward Hughes and Ivo Danihelka and Jonah Ryan-Davis},
  year      = {2019},
  eprint    = {1908.09453},
  archivePrefix = {arXiv},
  primaryClass = {cs.LG},
  journal   = {CoRR},
  volume    = {abs/1908.09453},
  url       = {http://arxiv.org/abs/1908.09453},
}

Versioning

We use Semantic Versioning.