Book Image

Hands-On Intelligent Agents with OpenAI Gym

By : Palanisamy P
Book Image

Hands-On Intelligent Agents with OpenAI Gym

By: Palanisamy P

Overview of this book

Many real-world problems can be broken down into tasks that require a series of decisions to be made or actions to be taken. The ability to solve such tasks without a machine being programmed requires a machine to be artificially intelligent and capable of learning to adapt. This book is an easy-to-follow guide to implementing learning algorithms for machine software agents in order to solve discrete or continuous sequential decision making and control tasks. Hands-On Intelligent Agents with OpenAI Gym takes you through the process of building intelligent agent algorithms using deep reinforcement learning starting from the implementation of the building blocks for configuring, training, logging, visualizing, testing, and monitoring the agent. You will walk through the process of building intelligent agents from scratch to perform a variety of tasks. In the closing chapters, the book provides an overview of the latest learning environments and learning algorithms, along with pointers to more resources that will help you take your deep reinforcement learning skills to the next level.
Table of Contents (12 chapters)

Creating your first OpenAI Gym environment

We will be going over the steps to set up the OpenAI Gym dependencies and other tools required for training your reinforcement learning agents in detail in Chapter 3, Getting Started with OpenAI Gym and Deep Reinforcement Learning. This section provides a quick way to get started with the OpenAI Gym Python API on Linux and macOS using virtualenv so that you can get a sneak peak into the Gym!

MacOS and Ubuntu Linux systems come with Python installed by default. You can check which version of Python is installed by running python --version from a terminal window. If this returns python followed by a version number, then you are good to proceed to the next steps! If you get an error saying the Python command was not found, then you have to install Python. Please refer to the detailed installation section in Chapter 3, Getting Started with OpenAI Gym and Deep Reinforcement Learning of this book:

  1. Install virtualenv:
$pip install virtualenv
If pip is not installed on your system, you can install it by typing sudo easy_install pip.
  1. Create a virtual environment named openai-gym using the virtualenv tool:
 $virtualenv openai-gym
  1. Activate the openai-gym virtual environment:
$source openai-gym/bin/activate
  1. Install all the packages for the Gym toolkit from upstream:
$pip install -U gym
If you get permission denied or failed with error code 1 when you run the pip install command, it is most likely because the permissions on the directory you are trying to install the package to (the openai-gym directory inside virtualenv in this case) needs special/root privileges. You can either run sudo -H pip install -U gym[all] to solve the issue or change permissions on the openai-gym directory by running sudo chmod -R o+rw ~/openai-gym.
  1. Test to make sure the installation is successful:
$python -c 'import gym; gym.make("CartPole-v0");'

Creating and visualizing a new Gym environment

In just a minute or two, you have created an instance of an OpenAI Gym environment to get started!

Let's open a new Python prompt and import the gym module:

>>import gym

Once the gym module is imported, we can use the gym.make method to create our new environment like this:

>>env = gym.make('CartPole-v0')
>>env.reset()
env.render()

This will bring up a window like this:

Hooray!