- Previously we constructed a very simple class to emulate the type of environment which is provided by OpenAI. Windy Walk (part 1)
- Then we implemented the same windy walk model as an extension to OpenAI. For this we created a custom python package, we named it gym_drifty_walk, you can grab it from github .
- These two versions we used, to produce exactly the same “Windy Walks” results, as shown by the plots.
I this addendum post we’ll take a look at the package code in gym_drifty_walk.
There are excellent articles about how to create a python package, I have no intention to duplicate those, I recommend python-packaging. One warning: the packaging article is written for python 2, so be aware of that.
There are things missing in this (like e.g. tests) that you would typically add. We choose to use the most basic approach that works, which we believe lowers the barrier to comprehension.
The most basic steps you need are these:
- ✓ Choose a package name
We have already done that: gym_drifty_walk
- ✓ Follow the basic package structure
Here the structure is like this :
123456789gym_drifty_walk/├── gym_drifty_walk│ ├── envs│ │ ├── drifty_walk_env.py│ │ └── __init__.py│ └── __init__.py├── LICENSE.md├── README.md└── setup.py
(Remark; This tree-representation of the directory structure can be obtained using tree )
- ✓ Match various names Having named the package (=gym_drifty_walk) the directory holding the actual code should be given the same name. In the setup again there’s a line with the name, and in gym_drifty_walk/gym_drifty_walk/envs/__init__.py the name needs to be reflected. Further name matching occurs. The first name matching concerns gym_drifty_walk/gym_drifty_walk/__init__.py
There is actually only one line in that file12register(id='drifty_walk-v0',entry_point='gym_drifty_walk.envs:DriftyWalkEnv')
The id (=”drifty_walk-v0″) is used later when you import the environment like this:1234import gymimport gym_drifty_walk # custom environmentenv = gym.make('drifty_walk-v0')
The secondname matching concerns DriftyWalkEnv which occurs in the two files in gym_drifty_walk/gym_drifty_walk/envs namely drifty_walk_env.py and __init__.py.
In drifty_walk_env.py you’ll find the line1class DriftyWalkEnv(gym.Env):
and in __init__.py you’ll find1from gym_drifty_walk.envs.drifty_walk_env import DriftyWalkEnv
The name matching in this file was already mentioned above (regarding gym_drifty_walk) however there are two more matches:
and finally drifty_walk_env which needs to match the file in the same directory.
Here’s a summary of the name matching requirements:
name Occurs in File Needs to match.. "gym_drifty_walk" gym_drifty_walk/setup.py the package name "gym_drifty_walk" gym_drifty_walk/gym_drifty_walk/envs/__init__.py the package name "drifty_walk-v0" gym_drifty_walk/gym_drifty_walk/__init__.py the environment‘s name "DriftyWalkEnv" gym_drifty_walk/gym_drifty_walk/envs/__init__.py the class name "DriftyWalkEnv" gym_drifty_walk/gym_drifty_walk/envs/drifty_walk_env.py the class name