Working with OpenAI Gym for RL training environments
This recipe provides a quick run-through for getting up and running with OpenAI Gym environments. The Gym environment and the interface provide a platform for training RL agents and is the most widely used and accepted RL environment interface.
Getting ready
We will be needing the full installation of OpenAI Gym to be able to use the available environments. Please follow the Gym installation steps listed at https://github.com/openai/gym#id5.
As a minimum, you should execute the following command:
pip install gym[atari]
How to do it…
Let's start by picking an environment and exploring the Gym interface. You may already be familiar with the basic function calls to create a Gym environment from the previous recipes.
Your steps should be formatted like so:
- Let's first explore the list of environments in Gym:
#!/usr/bin/env python from gym import envs env_names = [spec.id for spec in envs.registry.all()] for name in sorted(env_names): print(name)
- This script will print the names of all the environments available through your Gym installation, sorted alphabetically. You can run this script using the following command to see the names of the environments that are installed and available in your system. You should see a long list of environments listed. The first few are shown in the following screenshot for your reference:
Let's now see how we can run one of the Gym environments.
- The following script will let you explore any of the available Gym environments:
#!/usr/bin/env python import gym import sys def run_gym_env(argv): env = gym.make(argv[1]) # Name of the environment # supplied as 1st argument env.reset() for _ in range(int(argv[2])): env.render() env.step(env.action_space.sample()) env.close() if __name__ == "__main__": run_gym_env(sys.argv)
- You can save the preceding script to
run_gym_env.py
and run the script like this:(tf2rl-cookbook) praveen@g5: ~/tf2rl-cookbook/ch1/src$python run_gym_env.py Alien-v4 1000
The script will render the
Alien-v4
environment, which should look like the following screenshot:
Tip
You can change Alien-v4
to any of the available Gym environments listed in the previous step.
How it works…
A summary of how the Gym environments work is presented in the following table:
See also
You can find more information on OpenAI Gym here: http://gym.openai.com/.