Convert Figma logo to code with AI

Farama-Foundation logoGymnasium

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

6,702
752
6,702
56

Top Related Projects

34,461

A toolkit for developing and comparing reinforcement learning algorithms.

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.

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

Google DeepMind's software stack for physics-based simulation and Reinforcement Learning environments, using MuJoCo.

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

Check out the new game server:

Quick Overview

Gymnasium is a Python library for developing and comparing reinforcement learning algorithms. It provides a collection of environments (tasks) that can be used to train and evaluate reinforcement learning agents. Gymnasium is a fork of the popular OpenAI Gym library, maintained by the Farama Foundation to ensure continued development and support.

Pros

  • Wide variety of pre-built environments for reinforcement learning
  • Consistent API across different environments, making it easy to switch between tasks
  • Active community and ongoing development
  • Compatible with many popular reinforcement learning libraries and frameworks

Cons

  • Learning curve for beginners in reinforcement learning
  • Some environments may require additional dependencies
  • Limited built-in visualization tools
  • Performance can be slow for complex environments

Code Examples

  1. Creating and interacting with a basic environment:
import gymnasium as gym

env = gym.make("CartPole-v1")
observation, info = env.reset(seed=42)

for _ in range(1000):
    action = env.action_space.sample()  # Random action
    observation, reward, terminated, truncated, info = env.step(action)

    if terminated or truncated:
        observation, info = env.reset()

env.close()
  1. Rendering an environment:
import gymnasium as gym

env = gym.make("LunarLander-v2", render_mode="human")
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()
  1. Creating a custom environment:
import gymnasium as gym
from gymnasium import spaces
import numpy as np

class CustomEnv(gym.Env):
    def __init__(self):
        self.action_space = spaces.Discrete(2)
        self.observation_space = spaces.Box(low=-1, high=1, shape=(3,), dtype=np.float32)

    def step(self, action):
        # Implement environment dynamics
        observation = self.observation_space.sample()
        reward = 0
        terminated = False
        truncated = False
        info = {}
        return observation, reward, terminated, truncated, info

    def reset(self, seed=None, options=None):
        super().reset(seed=seed)
        observation = self.observation_space.sample()
        info = {}
        return observation, info

env = CustomEnv()

Getting Started

To get started with Gymnasium, follow these steps:

  1. Install Gymnasium:
pip install gymnasium
  1. Import and create an environment:
import gymnasium as gym
env = gym.make("CartPole-v1")
  1. Interact with the environment:
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()

Competitor Comparisons

34,461

A toolkit for developing and comparing reinforcement learning algorithms.

Pros of Gym

  • Established ecosystem with extensive documentation and community support
  • Wide range of pre-implemented environments for various RL tasks
  • Compatibility with many existing RL libraries and frameworks

Cons of Gym

  • No longer actively maintained, leading to potential compatibility issues
  • Limited support for newer Python versions and dependencies
  • Lack of recent updates and improvements in API design

Code Comparison

Gym:

import gym
env = gym.make('CartPole-v0')
observation = env.reset()
for _ in range(1000):
    action = env.action_space.sample()
    observation, reward, done, info = env.step(action)

Gymnasium:

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

The main differences in the code are:

  1. Import statement: gymnasium instead of gym
  2. env.reset() returns both observation and info
  3. env.step() returns five values instead of four, including terminated and truncated

Gymnasium is actively maintained and provides improved API design, better type hinting, and support for newer Python versions. It aims to be a drop-in replacement for Gym while addressing its limitations and providing ongoing development and support.

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 complex 3D environments
  • Supports multi-agent scenarios and imitation learning
  • Provides a Python API for training agents and a C# API for Unity integration

Cons of ml-agents

  • Steeper learning curve due to Unity integration
  • Limited to Unity-based environments
  • Requires more setup and configuration compared to Gymnasium

Code Comparison

ml-agents:

from mlagents_envs.environment import UnityEnvironment
env = UnityEnvironment(file_name="MyUnityEnv")
behavior_name = list(env.behavior_specs)[0]
decision_steps, _ = env.get_steps(behavior_name)

Gymnasium:

import gymnasium as gym
env = gym.make("CartPole-v1")
observation, info = env.reset()
action = env.action_space.sample()
observation, reward, terminated, truncated, info = env.step(action)

Both repositories provide reinforcement learning environments, but ml-agents is specifically designed for Unity-based simulations, while Gymnasium offers a more general-purpose toolkit for various environments. ml-agents excels in complex 3D scenarios and multi-agent setups, while Gymnasium is simpler to use and has a wider range of pre-built environments.

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

Pros of OpenSpiel

  • Broader range of game types, including imperfect information games
  • Supports multi-agent environments and algorithms
  • Provides implementations of various game-theoretic algorithms

Cons of OpenSpiel

  • Steeper learning curve for beginners
  • Less focus on single-agent reinforcement learning tasks
  • Smaller community and ecosystem compared to Gymnasium

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)

Both libraries provide environments for reinforcement learning and game theory research. Gymnasium focuses on single-agent RL tasks with a simpler API, while OpenSpiel offers a wider range of game types and multi-agent scenarios. OpenSpiel is better suited for game theory research, while Gymnasium is more accessible for general RL experimentation.

Google DeepMind's software stack for physics-based simulation and Reinforcement Learning environments, using MuJoCo.

Pros of dm_control

  • More advanced physics simulation using MuJoCo
  • Highly customizable environments and tasks
  • Better support for continuous control problems

Cons of dm_control

  • Steeper learning curve for beginners
  • Less widespread adoption in the RL community
  • Requires MuJoCo license (free for personal use)

Code Comparison

dm_control:

from dm_control import suite
env = suite.load(domain_name="cartpole", task_name="swingup")
time_step = env.reset()
action = env.action_spec().generate_value()
next_time_step = env.step(action)

Gymnasium:

import gymnasium as gym
env = gym.make("CartPole-v1")
observation, info = env.reset()
action = env.action_space.sample()
next_observation, reward, terminated, truncated, info = env.step(action)

Both libraries provide similar functionality for creating and interacting with RL environments. dm_control offers more complex physics simulations and customization options, while Gymnasium provides a simpler interface and wider variety of pre-built environments. The choice between them depends on the specific requirements of your project and your familiarity with RL frameworks.

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

Pros of PettingZoo

  • Supports multi-agent reinforcement learning environments
  • Offers a diverse range of environments, including classic games and real-world simulations
  • Provides standardized API for multi-agent RL, making it easier to develop and compare algorithms

Cons of PettingZoo

  • May have a steeper learning curve for users new to multi-agent RL
  • Potentially more complex setup and configuration compared to single-agent environments

Code Comparison

PettingZoo:

from pettingzoo.butterfly import knights_archers_zombies_v10
env = knights_archers_zombies_v10.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)

Gymnasium:

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

The main difference is that PettingZoo uses an agent iterator for multi-agent environments, while Gymnasium focuses on single-agent interactions.

Check out the new game server:

Pros of Football

  • Specialized for football (soccer) simulations, offering a rich, sport-specific environment
  • Includes a built-in game engine with realistic physics and game rules
  • Provides pre-trained models and baselines for benchmarking

Cons of Football

  • Limited to football scenarios, less versatile than Gymnasium's wide range of environments
  • Steeper learning curve due to sport-specific complexities
  • Less active community and fewer third-party contributions compared to Gymnasium

Code Comparison

Football:

import gfootball.env as football_env

env = football_env.create_environment(
    env_name="11_vs_11_stochastic",
    representation="raw",
    render=True
)

Gymnasium:

import gymnasium as gym

env = gym.make("CartPole-v1", render_mode="human")

The Football environment creation is more specific to the football simulation, while Gymnasium offers a more generic approach to creating various 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

Python PyPI arXiv pre-commit Code style: black

Gymnasium 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. This is a fork of OpenAI's Gym library by its maintainers (OpenAI handed over maintenance a few years ago to an outside team), and is where future maintenance will occur going forward.

The documentation website is at gymnasium.farama.org, and we have a public discord server (which we also use to coordinate development work) that you can join here: https://discord.gg/bnJ6kubTg6

Environments

Gymnasium includes the following families of environments along with a wide variety of third-party environments

  • Classic Control - These are classic reinforcement learning based on real-world problems and physics.
  • Box2D - These environments all involve toy games based around physics control, using box2d based physics and PyGame-based rendering
  • Toy Text - These environments are designed to be extremely simple, with small discrete state and action spaces, and hence easy to learn. As a result, they are suitable for debugging implementations of reinforcement learning algorithms.
  • MuJoCo - A physics engine based environments with multi-joint control which are more complex than the Box2D environments.
  • Atari - A set of 57 Atari 2600 environments simulated through Stella and the Arcade Learning Environment that have a high range of complexity for agents to learn.
  • Third-party - A number of environments have been created that are compatible with the Gymnasium API. Be aware of the version that the software was created for and use the apply_env_compatibility in gymnasium.make if necessary.

Installation

To install the base Gymnasium library, use pip install gymnasium

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 "gymnasium[atari]" or use pip install "gymnasium[all]" to install all dependencies.

We support and test for Python 3.8, 3.9, 3.10, 3.11 on Linux and macOS. We will accept PRs related to Windows, but do not officially support it.

API

The Gymnasium 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 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)

    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 newcomers to when asked for recommendations.

  • CleanRL is a learning library based on the Gymnasium API. It is designed to cater to newer people in the field and provides very good reference implementations.
  • PettingZoo is a multi-agent version of Gymnasium with a number of implemented environments, i.e. multi-agent Atari environments.
  • The Farama Foundation also has a collection of many other environments that are maintained by the same team as Gymnasium and use the Gymnasium API.

Environment Versioning

Gymnasium 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. These were inherited from Gym.

Development Roadmap

We have a roadmap for future development work for Gymnasium available here: https://github.com/Farama-Foundation/Gymnasium/issues/12

Support Gymnasium's Development

If you are financially able to do so and would like to support the development of Gymnasium, please join others in the community in donating to us.

Citation

You can cite Gymnasium using our related paper (https://arxiv.org/abs/2407.17032) as:

@article{towers2024gymnasium,
  title={Gymnasium: A Standard Interface for Reinforcement Learning Environments},
  author={Towers, Mark and Kwiatkowski, Ariel and Terry, Jordan and Balis, John U and De Cola, Gianluca and Deleu, Tristan and Goul{\~a}o, Manuel and Kallinteris, Andreas and Krimmel, Markus and KG, Arjun and others},
  journal={arXiv preprint arXiv:2407.17032},
  year={2024}
}