Adding a New Environment

Adding a new environment to Coach is as easy as solving CartPole.

There are essentially two ways to integrate new environments to Coach:

Using the OpenAI Gym API

If your environment is already using the OpenAI Gym API, you are already good to go.When selecting the environment parameters in the preset, use GymEnvironmentParameters(),and pass the path to your environment source code using the level parameter.You can specify additional parameters for your environment using the additional_simulator_parameters parameter.Take for example the definition used in the Pendulum_HAC preset:

  1. env_params = GymEnvironmentParameters()
  2. env_params.level = "rl_coach.environments.mujoco.pendulum_with_goals:PendulumWithGoals"
  3. env_params.additional_simulator_parameters = {"time_limit": 1000}

Using the Coach API

There are a few simple steps to follow, and we will walk through them one by one.As an alternative, we highly recommend following the correspondingtutorialin the GitHub repo.

  • Create a new class for your environment, and inherit the Environment class.

  • Coach defines a simple API for implementing a new environment, which are defined in environment/environment.py.There are several functions to implement, but only some of them are mandatory.

Here are the important ones:

  1. def _take_action(self, action_idx: ActionType) -> None:
  2. """
  3. An environment dependent function that sends an action to the simulator.
  4. :param action_idx: the action to perform on the environment
  5. :return: None
  6. """
  7.  
  8. def _update_state(self) -> None:
  9. """
  10. Updates the state from the environment.
  11. Should update self.observation, self.reward, self.done, self.measurements and self.info
  12. :return: None
  13. """
  14.  
  15. def _restart_environment_episode(self, force_environment_reset=False) -> None:
  16. """
  17. Restarts the simulator episode
  18. :param force_environment_reset: Force the environment to reset even if the episode is not done yet.
  19. :return: None
  20. """
  21.  
  22. def _render(self) -> None:
  23. """
  24. Renders the environment using the native simulator renderer
  25. :return: None
  26. """
  27.  
  28. def get_rendered_image(self) -> np.ndarray:
  29. """
  30. Return a numpy array containing the image that will be rendered to the screen.
  31. This can be different from the observation. For example, mujoco's observation is a measurements vector.
  32. :return: numpy array containing the image that will be rendered to the screen
  33. """
  • Create a new parameters class for your environment, which inherits the EnvironmentParameters class.In the init of your class, define all the parameters you used in your Environment class.Additionally, fill the path property of the class with the path to your Environment class.For example, take a look at the EnvironmentParameters class used for Doom:
  1. class DoomEnvironmentParameters(EnvironmentParameters):def init(self): super().init() self.default_input_filter = DoomInputFilter self.default_output_filter = DoomOutputFilter self.cameras = [DoomEnvironment.CameraTypes.OBSERVATION]@propertydef path(self): return 'rl_coach.environments.doom_environment:DoomEnvironment'
  • And that’s it, you’re done. Now just add a new preset with your newly created environment, and start training an agent on top of it.