I have created a custom environment, as per the OpenAI Gym framework; containing step, reset, action, and reward functions. I aim to run OpenAI baselines on this custom environment. But prior to this, the environment has to be registered on OpenAI gym. I would like to know how the custom environment could be registered on OpenAI gym? Also, Should I be modifying the OpenAI baseline codes to incorporate this?
- 15,395
- 32
- 113
- 196
- 195
- 1
- 1
- 8
3 Answers
You do not need to modify baselines repo.
Here is a minimal example. Say you have myenv.py, with all the needed functions (step, reset, ...). The name of the class environment is MyEnv, and you want to add it to the classic_control folder. You have to
- Place
myenv.pyfile ingym/gym/envs/classic_control Add to
__init__.py(located in the same folder)from gym.envs.classic_control.myenv import MyEnvRegister the environment in
gym/gym/envs/__init__.pyby addinggym.envs.register( id='MyEnv-v0', entry_point='gym.envs.classic_control:MyEnv', max_episode_steps=1000, )
At registration, you can also add reward_threshold and kwargs (if your class takes some arguments).
You can also directly register the environment in the script you will run (TRPO, PPO, or whatever) instead of doing it in gym/gym/envs/__init__.py.
EDIT
This is a minimal example to create the LQR environment.
Save the code below in lqr_env.py and place it in the classic_control folder of gym.
import gym
from gym import spaces
from gym.utils import seeding
import numpy as np
class LqrEnv(gym.Env):
def __init__(self, size, init_state, state_bound):
self.init_state = init_state
self.size = size
self.action_space = spaces.Box(low=-state_bound, high=state_bound, shape=(size,))
self.observation_space = spaces.Box(low=-state_bound, high=state_bound, shape=(size,))
self._seed()
def _seed(self, seed=None):
self.np_random, seed = seeding.np_random(seed)
return [seed]
def _step(self,u):
costs = np.sum(u**2) + np.sum(self.state**2)
self.state = np.clip(self.state + u, self.observation_space.low, self.observation_space.high)
return self._get_obs(), -costs, False, {}
def _reset(self):
high = self.init_state*np.ones((self.size,))
self.state = self.np_random.uniform(low=-high, high=high)
self.last_u = None
return self._get_obs()
def _get_obs(self):
return self.state
Add from gym.envs.classic_control.lqr_env import LqrEnv to __init__.py (also in classic_control).
In your script, when you create the environment, do
gym.envs.register(
id='Lqr-v0',
entry_point='gym.envs.classic_control:LqrEnv',
max_episode_steps=150,
kwargs={'size' : 1, 'init_state' : 10., 'state_bound' : np.inf},
)
env = gym.make('Lqr-v0')
- 5,070
- 5
- 33
- 59
-
do you have a tip or example on how to register the environment in the script ((TRPO, PPO, or whatever) instead of doing it in )... – bbartling Dec 05 '18 at 21:18
-
Im trying to follow all of the steps above but I get an error, `gym.error.Error: Attempted to look up malformed environment ID: b'hvac_Env'. (Currently all IDs must be of the form ^(?:[\w:-]+\/)?([\w:.-]+)-v(\d+)$.)` – bbartling Dec 05 '18 at 21:18
-
@HenryHub I never had your error. I have added a minimal example, hope that helps. – Simon Dec 05 '18 at 22:22
-
Thanks for Sharing this @Simon... It worked. The LQR seems quite interesting too, would you ever be able to share some contact info to collaborate? Else I can give you mine.. I am attempting to use this custom Gym env for controlling valve in a piping system to a temperature sensor. I have contact info at the bottom of this blog post about my side project:https://medium.com/getting-reinforcement-learning-into-the-bas – bbartling Dec 06 '18 at 15:09
-
1Is this still the way to go in 2020? This seems to be a very awkward practice, since one usually does not want to alter files under package management (pip / conda). – Stanley F. Mar 25 '20 at 15:34
-
Ok, I see, this problem has been solved in a clean way already in 2017: https://stackoverflow.com/a/47132897/579698 – Stanley F. Mar 25 '20 at 16:20
Environment registration process can be found here.
Please go through this example custom environment if you have any more issues.
Refer to this stackoverflow issue for further information.
- 630
- 6
- 11
That problem is related to versions of gym try upgrading your gym environment.
- 2,458
- 10
- 44
- 49
- 1
- 2
-
Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 21 '22 at 21:01