1. Cree un nuevo entorno virtual, mi entorno es python 3.8 y el nombre es: myrl
2. Encuentre el entorno virtual en el cuaderno jupyter.
En la celda del cuaderno de Jupyter, ejecute los siguientes comandos en secuencia, como se muestra a continuación:
!pip install gym==0.22
!pip install matplotlib
!pip install pygame
!pip install imageio-ffmpeg
Luego ejecuta:
#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
Luego ejecuta:
!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
En este punto, el entorno se ha instalado correctamente.
3. Ejecute el siguiente código y aparecerá una pantalla familiar.
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. Los resultados de ejecución son los siguientes:
Otras notas:
1. Este artículo hace referencia a los siguientes enlaces:
2. Resuelva el problema que el servidor del gimnasio no puede mostrar
https://www.twblogs.net/a/5e510a3bbd9eee21167ef3d6
3. Inicie jupyter como xvfb-run
xvfb-run -s "-screen 0 1400x900x24" cuaderno jupyter
Si el inicio falla y aparece: xvfb-run: error: Xvfb no pudo iniciarse, puede ejecutar el siguiente comando:
pkill Xvfb, tenga en cuenta que X debe estar en mayúscula
3. Gym puede usar la versión 0.22
pip instalar gimnasio==0.22