09.05.2023 Views

pdfcoffee

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Reinforcement Learning

env_name = 'Breakout-v0'

env = gym.make(env_name)

Let's get an observation of the environment once it is reset:

obs = env.reset()

env.render()

You can see the Breakout environment in the following screenshot; the render

function pops up the environment window:

The Breakout environment

Alternatively, you can use Matplotlib inline and change the render command to plt.

imshow(env.render(mode='rgb_array')). This will show the environment inline

in the Jupyter Notebook.

You can learn more about the environment state space and its action space using

env.observation_space and env.action_space. For our Breakout game we find

that the state consists of a three-channel image of size 210 × 160, and the action space

is discrete with four possible actions. Once you are done, do not forget to close the

OpenAI using:

env.close()

Random agent playing Breakout

Let's have some fun and play the Breakout game. When I first played the game, I had

no idea of the rules, or how to play, so I randomly chose the control buttons. Our

novice agent will do the same; it will choose the actions randomly from the action

space. Gym provides a function sample() which chooses a random action from

the action space – we will be using this function. Also, we can save a replay of the

game, to view it later. There are two ways to save the play, one using Matplotlib and

another using OpenAI Gym Monitor wrapper. Let us first see the Matplotlib method.

[ 418 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!