09.05.2023 Views

pdfcoffee

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 11

We will first import the necessary modules; we will only need gym and matplotlib

for now, as the agent will be playing random moves:

import gym

import matplotlib.pyplot as plt

import matplotlib.animation as animation

We create the Gym environment:

env_name = 'Breakout-v0'

env = gym.make(env_name)

Next we will run the game, one step at a time, choosing a random action, either for

300 steps or until the game is finished (whichever is earlier). The environment state

(observation) space is saved at each step in the list frames:

frames = [] # array to store state space at each step

env.reset()

done = False

for _ in range(300):

#print(done)

frames.append(env.render(mode='rgb_array'))

obs,reward,done, _ = env.step(env.action_space.sample())

if done:

break

Now comes the part of combining all the frames as a gif image using Matplotlib

Animation. We create an image object, patch, and then define a function that sets

image data to a particular frame index. The function is used by the Matplotlib

Animation class to create an animation, which we finally save in the file random_

agent.gif:

patch = plt.imshow(frames[0])

plt.axis('off')

def animate(i):

patch.set_data(frames[i])

anim = animation.FuncAnimation(plt.gcf(), animate, \

frames=len(frames), interval=10)

anim.save('random_agent.gif', writer='imagemagick')

Normally, a RL agent requires lots of steps for proper training, and as a result it

is not feasible to store the state space at each step. Instead, we can choose to store

after every 500th step (or any other number you wish) in the preceding algorithm.

OpenAI Gym provides the Wrapper class to save the game as a video. To do so, we

need to first import wrappers, then create the environment, and finally use Monitor.

[ 419 ]

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

Saved successfully!

Ooh no, something went wrong!