完美解决强化学习服务器Jupyter notebook gym 执行env.render报错问题

1、新建虚拟环境,我的环境是python 3.8,名字是: myrl

2、在jupyter notebook中找到该虚拟环境

在Jupyter notebook单元格中,依次执行以下命令,如下图:

完美解决强化学习服务器Jupyter notebook gym 执行env.render报错问题_第1张图片

!pip install gym==0.22

!pip install matplotlib

!pip install pygame

!pip install imageio-ffmpeg

再执行:

#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

再执行:

!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

到此,环境安装成功

3、运行以下代码,会出现熟悉的画面了,

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=''''''.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、运行结果如下:

完美解决强化学习服务器Jupyter notebook gym 执行env.render报错问题_第2张图片

其他说明事项:

1、本文参考了以下链接:

Reinforcement Learning(强化学习)-Gym 使用介绍 | 文艺数学君

2、解决gym 服务器无法显示问题

https://www.twblogs.net/a/5e510a3bbd9eee21167ef3d6

3、以xvfb-run形式启动jupyter

xvfb-run -s "-screen 0 1400x900x24" jupyter notebook

若启动失败,出现:xvfb-run: error: Xvfb failed to start,可执行以下命令:

pkill Xvfb,注意X一定是大写

3、gym可以使用0.22版本

pip install gym==0.22

你可能感兴趣的:(强化学习,强化学习,jupyter)