Top Related Projects
An API standard for single-agent reinforcement learning environments, with popular reference environments and related utilities (formerly Gym)
OpenSpiel is a collection of environments and algorithms for research in general reinforcement learning and search/planning in games.
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.
Check out the new game server:
An API standard for multi-agent reinforcement learning environments, with popular reference environments and related utilities
Quick Overview
OpenAI Gym is a toolkit for developing and comparing reinforcement learning algorithms. It provides a standardized set of environments to test and benchmark algorithms, as well as a common interface for interacting with these environments. Gym is widely used in the AI research community and supports various types of environments, from simple text-based games to complex physics simulations.
Pros
- Standardized interface for reinforcement learning environments
- Wide variety of pre-built environments for testing and benchmarking
- Active community and extensive documentation
- Easy integration with popular machine learning libraries like TensorFlow and PyTorch
Cons
- Some environments may require additional dependencies
- Limited built-in visualization tools for certain environments
- Learning curve for beginners in reinforcement learning
- Some environments may be computationally intensive
Code Examples
- Creating and interacting with a basic environment:
import gym
env = gym.make('CartPole-v1')
observation = env.reset()
for _ in range(1000):
env.render()
action = env.action_space.sample() # Random action
observation, reward, done, info = env.step(action)
if done:
observation = env.reset()
env.close()
- Creating a custom environment:
import gym
from gym import spaces
import numpy as np
class CustomEnv(gym.Env):
def __init__(self):
super(CustomEnv, self).__init__()
self.action_space = spaces.Discrete(2)
self.observation_space = spaces.Box(low=0, high=255, shape=(84, 84, 3), dtype=np.uint8)
def step(self, action):
# Implement environment dynamics
return observation, reward, done, info
def reset(self):
# Reset environment state
return observation
def render(self, mode='human'):
# Render the environment
pass
- Using wrappers to modify environment behavior:
import gym
from gym.wrappers import TimeLimit, Monitor
env = gym.make('CartPole-v1')
env = TimeLimit(env, max_episode_steps=1000)
env = Monitor(env, './video', force=True)
observation = env.reset()
for _ in range(1000):
action = env.action_space.sample()
observation, reward, done, info = env.step(action)
if done:
break
env.close()
Getting Started
To get started with OpenAI Gym, follow these steps:
-
Install Gym using pip:
pip install gym
-
Import Gym and create an environment:
import gym env = gym.make('CartPole-v1')
-
Interact with the environment:
observation = env.reset() for _ in range(1000): action = env.action_space.sample() # Replace with your agent's action selection observation, reward, done, info = env.step(action) if done: observation = env.reset()
-
Close the environment when finished:
env.close()
Competitor Comparisons
An API standard for single-agent reinforcement learning environments, with popular reference environments and related utilities (formerly Gym)
Pros of Gymnasium
- Actively maintained and updated, with more frequent releases
- Improved type hinting and documentation
- Better compatibility with modern Python versions
Cons of Gymnasium
- Some breaking changes from Gym, requiring code updates
- Potentially less community support and resources due to being newer
Code Comparison
Gym:
import gym
env = gym.make('CartPole-v0')
observation = env.reset()
Gymnasium:
import gymnasium as gym
env = gym.make('CartPole-v1')
observation, info = env.reset()
Key Differences
- Gymnasium returns additional info on reset
- Gymnasium uses 'v1' versions of environments by default
- Gymnasium has improved error handling and more consistent API
Conclusion
Gymnasium is a modernized fork of Gym, offering improvements in maintenance, documentation, and Python compatibility. However, it may require some code adjustments when migrating from Gym. Both libraries serve similar purposes in reinforcement learning, with Gymnasium being the more future-proof option for new projects.
OpenSpiel is a collection of environments and algorithms for research in general reinforcement learning and search/planning in games.
Pros of Open Spiel
- Focuses on multi-agent reinforcement learning and game theory
- Provides a wide variety of game environments, including imperfect information games
- Offers built-in algorithms for game-specific learning and analysis
Cons of Open Spiel
- Less popular and smaller community compared to Gym
- More specialized, potentially limiting its applicability in general RL tasks
- Steeper learning curve for researchers not familiar with game theory concepts
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)
Both repositories provide environments for reinforcement learning, but Gym is more general-purpose and widely adopted, while Open Spiel specializes in multi-agent and game theory scenarios. Gym offers a broader range of environments, while Open Spiel focuses on game-specific implementations and algorithms.
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 complex 3D environments and realistic physics simulations
- Supports multi-agent scenarios and curriculum learning out of the box
- Provides a user-friendly interface for non-programmers to create and customize environments
Cons of ml-agents
- Steeper learning curve for those unfamiliar with Unity or C#
- Limited to Unity-based environments, less flexibility for custom Python-based scenarios
- Potentially higher computational requirements due to 3D rendering
Code Comparison
ml-agents (C#):
public class MyAgent : Agent
{
public override void OnEpisodeBegin() { /* Reset environment */ }
public override void CollectObservations(VectorSensor sensor) { /* Collect observations */ }
public override void OnActionReceived(ActionBuffers actions) { /* Process actions */ }
}
gym (Python):
class MyEnv(gym.Env):
def reset(self): # Reset environment
def step(self, action): # Process action and return observation, reward, done, info
def render(self): # Render environment (optional)
Both frameworks provide similar core functionality for reinforcement learning environments, but with different implementation approaches and target use cases.
Check out the new game server:
Pros of Football
- Specialized environment for soccer/football simulations
- Highly customizable game scenarios and rules
- Supports multi-agent reinforcement learning
Cons of Football
- Limited to soccer-specific tasks and scenarios
- Steeper learning curve for non-soccer domain experts
- Less diverse range of environments compared to Gym
Code Comparison
Football environment setup:
import gfootball.env as football_env
env = football_env.create_environment(env_name="11_vs_11_stochastic")
Gym environment setup:
import gym
env = gym.make("CartPole-v1")
Both repositories provide Python-based reinforcement learning environments, but Football focuses specifically on soccer simulations while Gym offers a wide variety of environments for different tasks. Football's setup requires more domain-specific knowledge, whereas Gym's environments are generally more accessible for beginners.
Football allows for complex multi-agent scenarios and detailed customization of game rules, making it ideal for researchers working on team-based AI or soccer-specific problems. Gym, on the other hand, provides a broader range of simpler environments that can be used to test and develop RL algorithms across various domains.
An API standard for multi-agent reinforcement learning environments, with popular reference environments and related utilities
Pros of PettingZoo
- Supports multi-agent environments, allowing for more complex and realistic scenarios
- Offers a wider variety of pre-built environments, including board games and classic video games
- Provides a standardized API for both single-agent and multi-agent environments
Cons of PettingZoo
- Less established community and ecosystem compared to Gym
- May have a steeper learning curve for users familiar with Gym's single-agent focus
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)
PettingZoo:
from pettingzoo.classic import rps_v2
env = rps_v2.env()
env.reset()
for agent in env.agent_iter():
observation, reward, termination, truncation, info = env.last()
action = env.action_space(agent).sample()
env.step(action)
Both libraries provide easy-to-use interfaces for creating and interacting with reinforcement learning environments. Gym focuses on single-agent scenarios, while PettingZoo extends this concept to multi-agent environments, offering more flexibility for complex simulations.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Important Notice
The team that has been maintaining Gym since 2021 has moved all future development to Gymnasium, a drop in replacement for Gym (import gymnasium as gym), and Gym will not be receiving any future updates. Please switch over to Gymnasium as soon as you're able to do so. If you'd like to read more about the story behind this switch, please check out this blog post.
Gym
Gym is an open source Python library for developing and comparing reinforcement learning algorithms by providing a standard API to communicate between learning algorithms and environments, as well as a standard set of environments compliant with that API. Since its release, Gym's API has become the field standard for doing this.
Gym documentation website is at https://www.gymlibrary.dev/, and you can propose fixes and changes to it here.
Gym also has a discord server for development purposes that you can join here: https://discord.gg/nHg2JRN489
Installation
To install the base Gym library, use pip install gym
.
This does not include dependencies for all families of environments (there's a massive number, and some can be problematic to install on certain systems). You can install these dependencies for one family like pip install gym[atari]
or use pip install gym[all]
to install all dependencies.
We support Python 3.7, 3.8, 3.9 and 3.10 on Linux and macOS. We will accept PRs related to Windows, but do not officially support it.
API
The Gym API's API models environments as simple Python env
classes. Creating environment instances and interacting with them is very simple- here's an example using the "CartPole-v1" environment:
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)
if terminated or truncated:
observation, info = env.reset()
env.close()
Notable Related Libraries
Please note that this is an incomplete list, and just includes libraries that the maintainers most commonly point newcommers to when asked for recommendations.
- CleanRL is a learning library based on the Gym API. It is designed to cater to newer people in the field and provides very good reference implementations.
- Tianshou is a learning library that's geared towards very experienced users and is design to allow for ease in complex algorithm modifications.
- RLlib is a learning library that allows for distributed training and inferencing and supports an extraordinarily large number of features throughout the reinforcement learning space.
- PettingZoo is like Gym, but for environments with multiple agents.
Environment Versioning
Gym keeps strict versioning for reproducibility reasons. All environments end in a suffix like "_v0". When changes are made to environments that might impact learning results, the number is increased by one to prevent potential confusion.
MuJoCo Environments
The latest "_v4" and future versions of the MuJoCo environments will no longer depend on mujoco-py
. Instead mujoco
will be the required dependency for future gym MuJoCo environment versions. Old gym MuJoCo environment versions that depend on mujoco-py
will still be kept but unmaintained.
To install the dependencies for the latest gym MuJoCo environments use pip install gym[mujoco]
. Dependencies for old MuJoCo environments can still be installed by pip install gym[mujoco_py]
.
Citation
A whitepaper from when Gym just came out is available https://arxiv.org/pdf/1606.01540, and can be cited with the following bibtex entry:
@misc{1606.01540,
Author = {Greg Brockman and Vicki Cheung and Ludwig Pettersson and Jonas Schneider and John Schulman and Jie Tang and Wojciech Zaremba},
Title = {OpenAI Gym},
Year = {2016},
Eprint = {arXiv:1606.01540},
}
Release Notes
There used to be release notes for all the new Gym versions here. New release notes are being moved to releases page on GitHub, like most other libraries do. Old notes can be viewed here.
Top Related Projects
An API standard for single-agent reinforcement learning environments, with popular reference environments and related utilities (formerly Gym)
OpenSpiel is a collection of environments and algorithms for research in general reinforcement learning and search/planning in games.
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.
Check out the new game server:
An API standard for multi-agent reinforcement learning environments, with popular reference environments and related utilities
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot