1. Create a new virtual environment. My environment is python 3.8 and the name is: myrl
2. Find the virtual environment in jupyter notebook
In the Jupyter notebook cell, execute the following commands in sequence, as shown below:
!pip install gym==0.22
!pip install matplotlib
!pip install pygame
!pip install imageio-ffmpeg
Then execute:
#remove " > /dev/null 2>&1" to see what is going on under the hood
!pip install pyvirtualdisplay > /dev/null 2>&1
!apt-get install -y xvfb python-opengl ffmpeg > /dev/null 2>&1
Then execute:
!apt-get update > /dev/null 2>&1
!apt-get install cmake > /dev/null 2>&1
!pip install --upgrade setuptools 2>&1
!pip install ez_setup > /dev/null 2>&1
!pip install gym[atari] > /dev/null 2>&1
At this point, the environment is successfully installed.
3. Run the following code and a familiar screen will appear.
import gym
from gym import logger as gymlogger
from gym.wrappers import Monitor
gymlogger.set_level(40) #error only
import numpy as np
import random
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
import math
import glob
import io
import base64
from IPython.display import HTML
from IPython import display as ipythondisplay
from pyvirtualdisplay import Display
display = Display(visible=0, size=(1400, 900))
display.start()
def show_video():
mp4list = glob.glob('video/*.mp4')
if len(mp4list) > 0:
mp4 = mp4list[0]
video = io.open(mp4, 'r+b').read()
encoded = base64.b64encode(video)
ipythondisplay.display(HTML(data='''<video alt="test" autoplay
loop controls style="height: 400px;">
<source src="data:video/mp4;base64,{0}" type="video/mp4" />
</video>'''.format(encoded.decode('ascii'))))
else:
print("Could not find video")
def wrap_env(env):
env = Monitor(env, './video', force=True)
return env
# env = wrap_env(gym.make("CartPole-v0"))
env = wrap_env(gym.make("MountainCar-v0"))
observation = env.reset()
while True:
env.render()
#your agent goes here
action = env.action_space.sample()
observation, reward, done, info = env.step(action)
if done:
break;
env.close()
show_video()
4. The running results are as follows:
Other notes:
1. This article refers to the following links:
Reinforcement Learning (reinforcement learning)-Gym usage introduction | Literary Mathematics Jun
2. Solve the problem that gym server cannot display
https://www.twblogs.net/a/5e510a3bbd9eee21167ef3d6
3. Start jupyter as xvfb-run
xvfb-run -s "-screen 0 1400x900x24" jupyter notebook
If the startup fails and appears: xvfb-run: error: Xvfb failed to start, you can execute the following command:
pkill Xvfb, note that X must be capitalized
3. Gym can use version 0.22
pip install gym==0.22