How to Make a Custom Gym Environment Part 1

Bobingstern
3 min readMar 25, 2021

So you want to make your own gym environment? Let’s set some things up

Gym LunarLander

First, create a python project and make sure you have gym installed and create the following files and directories:

CustomGymEnv/
setupy.py
README.md
FooEnv/
__init__.py
Foo.py

Let's go file by file starting with setup.py My environment is called FooEnv so my file will correspond with that name. Here is what it looks like

from setuptools import setup

setup(
name='FooEnv',
version='0.0.1',
keywords='games, environment, agent, rl, ai, gym',
description='Foo Env',
packages=['FooEnv'],
install_requires=[
'gym',
'numpy',
]
)

Let’s break this down. The name parameter is the name of your package/environment. The version is the current version of the package. keywords are keywords for people to find your package and description is your package description. packages is the name of the directory that you made previously. Mine is called FooEnv and install_requires is what your package requires to install.

Now let's take a look at FooEnv/Foo.py This is where your environment code will go. Let’s set up a template that looks like this:

import logging
import math
import gym
from gym import spaces
from gym.envs.registration import register
import numpy as np


class FooCustomEnv(gym.Env):
def __init__(self):
self.viewer = None
def reset(self):
pass
def step(self):
pass
def render(self):
pass


register(
id='FooEnv-v0',
entry_point='FooEnv.Foo:FooCustomEnv'
)

In the register at the bottom, the id is what you will call the environment when you call gym.make() and the entry_point is the path to this file. Use the directory example at the beginning of this article as reference.

We now have one last file to setup which is __init__.py This file is super easy as it only contains 2 lines

import FooEnv.Foo
from FooEnv.Foo import *

The path to the environment file, again, use the example at the top as a reference.

Yay! We are done setting up the environment so let's start coding it! Before we do that let’s test everything to make sure it works. In the environment file Foo.py set the reset(self) function to print something out so it looks like this:

import logging
import math
import gym
from gym import spaces
from gym.envs.registration import register
import numpy as np


class FooCustomEnv(gym.Env):
def __init__(self):
self.viewer = None
def reset(self):
print("Everything is working!")
def step(self):
pass
def render(self):
pass


register(
id='FooEnv-v0',
entry_point='FooEnv.Foo:FooCustomEnv'
)

Create a file in the working directory (mine is called CustomGymEnv) and call it test.py This file is not associated with the environment at all, it is just for us to test it. In this file add the following code:

import gym
import FooEnv

env = gym.make("FooEnv-v0")
env.reset()

Open up the terminal and go into the directory you are working in and run the following command: pip install -e . This will install the setup.py as if it were a package and run the file. You should get the message saying that everything is working. Great! Let’s start making the Environment!

Go into the environment file (mine is Foo.py) and start making the environment. I won’t cover how to do this as it is all up to you but I WILL cover basic rendering. First import the rendering module

from gym.envs.classic_control import rendering

Now add this bit of code into the render function.

if self.viewer is None:
self.viewer = rendering.Viewer(1000, 1000) #Width and Height

To draw on the screen, use the built-in functions, I use this reference all the time for that: https://www.programcreek.com/python/example/94731/gym.envs.classic_control.rendering

Now add this small bit of code to draw a circle

t = rendering.Transform((100, 100)) #Position
self.viewer.draw_circle(20).add_attr(t) #Add transform for position
self.viewer.render()

t is a transform to set the position and rotations and the rest just draws a circle and adds the transform to it and renders the circle. This is the basic rundown of how environments work and I will cover publishing in a later article. Use OpenAI’s built-in gym environments like CartPole as a reference while making your own. If you have any questions or problems, add a response below. Enjoy!

--

--