2020-05-07 08:10:51 +00:00
.. _custom_env:
Using Custom Environments
==========================
2022-02-05 11:36:36 +00:00
To use the RL baselines with custom environments, they just need to follow the *gym* interface.
2020-05-07 08:10:51 +00:00
That is to say, your environment must implement the following methods (and inherits from OpenAI Gym Class):
.. note ::
2021-10-08 16:08:31 +00:00
If you are using images as input, the observation must be of type `` np.uint8 `` and be contained in [0, 255]
2020-11-03 11:34:09 +00:00
is normalized (dividing by 255 to have values in [0, 1]) when using CNN policies. Images can be either
channel-first or channel-last.
2020-05-07 08:10:51 +00:00
2021-03-31 08:31:03 +00:00
.. note ::
Although SB3 supports both channel-last and channel-first images as input, we recommend using the channel-first convention when possible.
Under the hood, when a channel-last image is passed, SB3 uses a `` VecTransposeImage `` wrapper to re-order the channels.
2020-05-07 08:10:51 +00:00
.. code-block :: python
import gym
from gym import spaces
class CustomEnv(gym.Env):
2022-02-07 19:00:53 +00:00
"""Custom Environment that follows gym interface"""
2022-10-03 13:15:39 +00:00
metadata = {"render.modes": ["human"]}
2022-02-07 19:00:53 +00:00
def __init__(self, arg1, arg2, ...):
super(CustomEnv, self).__init__()
# Define action and observation space
# They must be gym.spaces objects
# Example when using discrete actions:
self.action_space = spaces.Discrete(N_DISCRETE_ACTIONS)
# Example for using image as input (channel-first; channel-last also works):
self.observation_space = spaces.Box(low=0, high=255,
shape=(N_CHANNELS, HEIGHT, WIDTH), dtype=np.uint8)
def step(self, action):
...
return observation, reward, done, info
def reset(self):
...
return observation # reward, done, info can't be included
2022-10-03 13:15:39 +00:00
def render(self, mode="human"):
2022-02-07 19:00:53 +00:00
...
def close (self):
...
2020-05-07 08:10:51 +00:00
Then you can define and train a RL agent with:
.. code-block :: python
# Instantiate the env
env = CustomEnv(arg1, ...)
# Define and Train the agent
2022-10-03 13:15:39 +00:00
model = A2C("CnnPolicy", env).learn(total_timesteps=1000)
2020-05-07 08:10:51 +00:00
2022-04-11 16:34:15 +00:00
To check that your environment follows the Gym interface that SB3 supports, please use:
2020-05-07 08:10:51 +00:00
.. code-block :: python
from stable_baselines3.common.env_checker import check_env
env = CustomEnv(arg1, ...)
# It will check your custom environment and output additional warnings if needed
check_env(env)
2022-04-11 16:34:15 +00:00
Gym also have its own `env checker <https://www.gymlibrary.ml/content/api/#checking-api-conformity> `_ but it checks a superset of what SB3 supports (SB3 does not support all Gym features).
2020-05-07 08:10:51 +00:00
2022-02-05 11:36:36 +00:00
We have created a `colab notebook <https://colab.research.google.com/github/araffin/rl-tutorial-jnrr19/blob/master/5_custom_gym_env.ipynb> `_ for a concrete example on creating a custom environment along with an example of using it with Stable-Baselines3 interface.
2020-05-07 08:10:51 +00:00
2022-04-11 16:34:15 +00:00
Alternatively, you may look at OpenAI Gym `built-in environments <https://www.gymlibrary.ml/> `_ . However, the readers are cautioned as per OpenAI Gym `official wiki <https://github.com/openai/gym/wiki/FAQ> `_ , its advised not to customize their built-in environments. It is better to copy and create new ones if you need to modify them.
2020-05-07 08:10:51 +00:00
2022-02-05 11:36:36 +00:00
Optionally, you can also register the environment with gym, that will allow you to create the RL agent in one line (and use `` gym.make() `` to instantiate the env):
2021-10-08 16:08:31 +00:00
.. code-block :: python
from gym.envs.registration import register
# Example for the CartPole environment
register(
# unique identifier for the env `name-version`
id="CartPole-v1",
# path to the class for creating the env
# Note: entry_point also accept a class as input (and not only a string)
entry_point="gym.envs.classic_control:CartPoleEnv",
# Max number of steps per episode, using a `TimeLimitWrapper`
max_episode_steps=500,
)
2020-05-07 08:10:51 +00:00
In the project, for testing purposes, we use a custom environment named `` IdentityEnv ``
2021-05-11 10:29:30 +00:00
defined `in this file <https://github.com/DLR-RM/stable-baselines3/blob/master/stable_baselines3/common/envs/identity_env.py> `_ .
An example of how to use it can be found `here <https://github.com/DLR-RM/stable-baselines3/blob/master/tests/test_identity.py> `_ .