Reinforcement Learning: Creating a Custom Environment

Create a New Custom Environment using Open AI Gym

Renu Khandelwal
6 min readDec 21, 2022


image by author

In this article, you will learn how to create a custom Environment using OpenAI Gym to train the Agent.

Reinforcement learning maps observations to actions to maximize the long-term reward. The Agent explores the Environment and finds the actions for which the Environment yields the most rewards.


Before you create a custom Environment, it is essential to know the characteristics of the RL Environment and how they are mapped in the OpenAI Gym

OpenAI Gym is a toolkit for Reinforcement learning research, and you will use it to create a custom Environment

The gym is an open-source Python library for developing and comparing reinforcement learning algorithms. The Gym API models Reinforcement Learning environments as simple Python env classes, making Environment creation and interacting with it very simple.

Reinforcement Learning Environment has the following characteristics

  • State or Observation: A state or observation is a moment or instance of the Environment at any point. Every Agent's interaction with the Environment generates an observation or an environment state. OpenAI gym Env , class provides a valid set of states or observations withenv.observation_space attributes.
  • Action: Every Environment has a valid set of actions that an agent performs in the Environment. Open AI gym Env class provides a valid set of actions withenv.action_space attribute.
  • Reward: The Environment sends a scalar value as a reward to the Reinforcement learning agent in response to its action.

Each episode in RL starts with an agent in an initial random state sampled from a distribution; during the episode, the Agent continually interacts with the Environment by executing a step that changes the state of the environment and for which the Agent receives rewards from the Environment until the Environment reaches a terminal state. The terminal state marks the end of an episode.



Renu Khandelwal

A Technology Enthusiast who constantly seeks out new challenges by exploring cutting-edge technologies to make the world a better place!