openai/gym

How to make environment

lilyxoxo opened this issue ยท 12 comments

Hello, I am a beginner of open ai gym.
I want to add my environment, but I cannot do well.
Could someone tell me how to make new environment?
https://github.com/openai/gym/wiki/Environments
I did it along this wiki, and I run random_agent.py(replace CartPole-v0 to MyEnv-v0)

(ml_env_2)S-no-MacBook-Air-2:agents $ python random_agent.py
[2017-06-18 17:27:44,978] Making new env: MyEnv-v0
Traceback (most recent call last):
File "random_agent.py", line 36, in
env = gym.make(args.env_id)
File "/Users/S/.pyenv/versions/miniconda3-latest/envs/ml_env_2/lib/python2.7/site-packages/gym/envs/registration.py", line 161, in make
return registry.make(id)
File "/Users/S/.pyenv/versions/miniconda3-latest/envs/ml_env_2/lib/python2.7/site-packages/gym/envs/registration.py", line 118, in make
spec = self.spec(id)
File "/Users/S/.pyenv/versions/miniconda3-latest/envs/ml_env_2/lib/python2.7/site-packages/gym/envs/registration.py", line 147, in spec
raise error.UnregisteredEnv('No registered env with id: {}'.format(id))
gym.error.UnregisteredEnv: No registered env with id: MyEnv-v0

same problem

The question is how to register your own environment in the registry?

Lets say you have your own environment defined in the following structure:

myenv/
    __init__.py
    myenv.py

myenv.py contains the class for your environment. In __init__.py you put the following code:

from gym.envs.registration import register

register(
    id='MyEnv-v0',
    entry_point='myenv.myenv:MyEnv',
)

To use your own environment

import gym
import myenv
env = gym.make('MyEnv-v0')

More detailed example on how to register your own environments have a look here: https://github.com/openai/gym/blob/522c2c532293399920743265d9bc761ed18eadb3/gym/envs/__init__.py

NOTE: You have to have myenv directory in your PYTHONPATH or start python from the parent directory.

@lilyxoxo - did this solve it for you? I create my own enviroments and could knock up a sample git repro if you are still stuck

hlzl commented

@Sohojoe That would be great. Even though the question is asked quite poorly there are probably a few others looking for something like this to help them make their own environment.

@Sohojoe I apologize for this late reply.
I have already solved this problem.
Thank you.

@lily-xoxo-30 could you maybe share your solution or give a quick how to? Iโ€™m also struggling with this problem now for a while already.

@peterlabuschagne Did you have a look at https://stackoverflow.com/a/47132897/562769 ? What else do you need?

@MartinThoma no I hadn't yet seen that, but that really answers the questions I've had up until now. Hopefully the rest is smooth sailing. Thanks a lot!

Thanks for answering this @hholst80! Please see the documentation here: https://github.com/openai/gym/blob/master/docs/creating-environments.md

What fixed this for me was prefixing foo-v0 in the following line with gym_foo: as below:

gym.make('gym_foo:foo-v0')