Up Your Game with OpenAI Gym Reinforcement Learning  Up Your Game with OpenAI Gym Reinforcement Learning 
Reinforcement learning is currently one of the most promising methods in machine learning and deep learning.  OpenAI Gym is one of... Up Your Game with OpenAI Gym Reinforcement Learning 

Reinforcement learning is currently one of the most promising methods in machine learning and deep learningOpenAI Gym is one of the most popular toolkits for implementing reinforcement learning simulation environments. Here’s a quick overview of the key terminology around OpenAI Gym. 


What is OpenAI Gym

OpenAI Gym is an open-source library that provides an easy setup and toolkit comprising a wide range of simulated environments. These simulated environments range from very simple games (pong) to complex, physics-based gaming engines. These environments allow you to quickly set up and train your reinforcement learning algorithms. 

The Gym can also be used as a benchmark for reinforcement learning algorithms. Each environment in the OpenAI Gym toolkit contains a version that is useful for comparing and reproducing results when testing algorithms. These environments have episode-based settings for performing reinforcement learning, where an agent’s experience is further divided into a series of episodes. This toolkit also provides a standard API for interacting with the environments related to reinforcement learning. It is also compatible with other computational libraries, such as TensorFlow. The initial release of the OpenAI Gym consists of over 1000 environments for performing different categories of tasks.

Key Terminology 

To understand OpenAI Gym and use it efficiently for reinforcement learning, it is crucial to grasp key concepts.

Reinforcement Learning  

Before diving into OpenAI Gym, it is essential to understand the basics of reinforcement learning. In reinforcement learning, an agent takes a sequence of actions in an uncertain and often complex environment with the goal of maximizing a reward function. Essentially, it is an approach for making appropriate decisions in a game-like environment that maximizes rewards and minimizes penalties. Feedback from its own actions and experience allows the agent to learn the most appropriate action by trial and error. Generally, reinforcement learning involves the following steps:

  1. Observing the environment
  2. Formulating a decision based on a certain strategy
  3. Actions
  4. Receiving a reward or penalty
  5. Learning from the experiences to improve the strategy
  6. Iteration of the process until an optimal strategy is achieved

For example, a self-driving car must keep passengers safe by following speed limits and obeying traffic rules. The agent (imaginary driver) is motivated by a reward; to maximize passenger safety, and will learn from its experiences in the environment. Rewards for correct actions and penalties for incorrect actions are designed and decided. To ensure the agent follows the speed limit and traffic rules, some of the points that can be considered are:

  • The agent should receive a positive reward for successfully maintaining the speed limit as this is essential for passenger safety. 
  • The agent should be penalized if it exceeds the desired speed limit or runs a light. For example, the agent can get a slightly negative reward for moving the car before the countdown ends (the traffic signal is still red).


In Reinforcement learning, an agent is an entity that makes the decision on what action to take based on the rewards and punishments. To make a decision, the agent is allowed to use observations from the environment. Typically it expects the current state to be provided by the environment and for that state to have a Markov property. Then it processes that state using a policy function that decides what action to take. In OpenAI Gym, the term agent is an integral part of the reinforcement learning activities. In short, the agent describes how to run a reinforcement learning algorithm in a Gym environment. The agent can either contain an algorithm or provide the integration required for an algorithm and the OpenAI Gym environment. You can find more information on how an agent works here.


In Gym, an environment is a simulation that represents the task or game that an agent operates in. When an agent performs an action in the environment, it receives observations from the environment that consists of a reward for this action. That reward informs the agent of how good or bad the action was. The observation tells the agent what is his next state in the environment. Thus by trial and error, the agent tries to figure out the optimal behavior in the environment in order to carry out his task in the best possible way. One of the strengths of OpenAI Gym is the many pre-built environments provided to train reinforcement learning algorithms. You might want to view the expansive list of environments available in the Gym toolkit. Some of the well-known environments in Gym are:

Algorithmic: These environments perform computations such as learning to copy a sequence.

import gym
env = gym.make('Copy-v0') #Copy is just an example of the Algorithmic environment.

Atari: The Atari environment consists of a wide range of classic Atari video games. It has been a significant part of reinforcement learning research. You can install the dependencies via

pip install -e ‘.[atari]’ (you’ll need CMake installed) and then follow the commands below:

import gym
env = gym.make('SpaceInvaders-v0') #Space invaders is just an example of Atari.

The above codes allow you to install atari-py, which automatically compiles the Arcade Learning Environment. However, you should be aware that this process takes a while to complete.

Box2d: Box2d is a 2D physics engine. You can install it using pip install -e ‘.[box2d]’, then follow the commands below:

import gym
env = gym.make('LunarLander-v2') #LunarLander is just an example of Box2d.

Classic control: These are different classic controls for small-scale reinforcement learning tasks, mainly from reinforcement learning literature.

You will need to run pip install -e ‘.[classic_control]‘ to enable rendering and then run the codes below:

import gym
env = gym.make(‘CartPole-v0’) #CarPole is just an example.

MuJoCo: MuJoCo is meant to be a physics engine designed for faster and accurate robot simulation. It is proprietary software. However, free trial licenses are available. The instructions in mujoco-py will help you to set it up. You have to run pip install -e ‘.[mujoco]’, if you did not complete the entire installation, and then follow the commands below:

import gym
env = gym.make('Humanoid-v2') #Humanoid is just an example of MuJoCo.env.reset()

Robotics: Usually these environments use MuJoCo for rendering. Run pip install -e ‘.[robotics]’ and then try the commands below:

import gym
env = gym.make('HandManipulateBlock-v0') #HandManipulateBlock is just an example.

Toy text: This environment is text-based. Additionally, you do not need any extra dependency to install and get started. You can just follow the commands below.

import gym
env = gym.make('FrozenLake-v0') #FrozenLake is just an example.

There are also third-party environments that you can explore here.

Observations of the OpenAI Gym

If you want your reinforcement learning tasks to perform better than they would by just taking random actions at every step, then you should be aware of what actions are available in the environment. These are:

Observation (object): The environment-specific object represents the observation of the environment. For example, pixel data from a camera.

Reward (float): The reward is a scalar quantity provided in the form of feedback to the agent to navigate the learning process. The primary aim of the agent is to maximize the sum of the reward, and the reward signal indicates the agent’s performance at any given step. For example, in the Atari game, the reward signal can result in +1 for each instance of an increase in score, or -1 when the score decreases.

Done (boolean): This is mainly used when you are required to reset the environment. In this process, most of the tasks are divided into well-defined objects, and True is the indicator of the terminated episode. For example, in the Atari Pong game, if you lost the ball, the episode is terminated and you receive “Done=True”.

Info (dict): This is useful for debugging purposes. For example, during the learning phase of the model, there might be raw probabilities about when the environment’s state changed the last time. However, you must be aware that the official evaluation of the agent cannot use this for learning. This is a case of the “agent-environment loop.” Each timestep, the agent chooses an action, and the environment returns an observation and a reward. This process begins by calling reset (), which returns you an initial observation. Here is a code example of how you can perform it.

For more information, visit this site.

Learn more about reinforcement learning

There’s quite a lot that you can do with reinforcement learning – whether it’s related to video games or not. The core skills can be used across a variety of purposes, from stock trading and finance to cybersecurity and art. Regardless of your application, there’s always a use for reinforcement learning. If you register for our upcoming Ai+ Training session on July 20th, “Reinforcement Learning for Game Playing and More,” you’ll gain the core skills you need to apply RL however you want. Session highlights include:

  • Gain knowledge of the latest algorithms used in reinforcement learning.
  • Understand OpenAI Gym environment
  • Build your custom environment in Gym
  • Using TensorFlow build an RL agent to play the Game of Atari
  • Learn to apply RL in tasks other than games

Plus, as more and more organizations learn about the benefits of reinforcement learning, it’s a great way for you to stand out and find your niche instead of being just a machine learning engineer.

Sheamus McGovern

Founder of ODSC and Software Architect specializing in, complex multi-platform systems across multiple industries including finance, healthcare, and education.