Recap:
- 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 :gym_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 )
So in the mother directory (as in mother ship) you need the setup.py (used for installing), the README.md (for advice on the package) , the LICENSE.md (setting out the terms of use) and the directory holding the actual code. Here the same tree with comments:
gym_drifty_walk/ mother directory ├── gym_drifty_walk actual code │ ├── envs │ │ ├── drifty_walk_env.py │ │ └── __init__.py │ └── __init__.py ├── LICENSE.md terms of use ├── README.md advice on the package └── setup.py needed for e.g.installing
- ✓ 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 fileregister(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:
import gym import gym_drifty_walk # custom environment env = 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 lineclass DriftyWalkEnv(gym.Env):
and in __init__.py you’ll find
from 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:
DriftyWalkEnvand 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