jupyter notebook中使用gym

jupyter notebook中使用gym

远程连接jupyter notebook服务器,使用gym测试环境
直接调用env.render()函数时无法使用

import gym
env = gym.make('MountainCar-v0')
env.reset()
for _ in range(1000):
    #绘图
    env.render()
    env.step(env.action_space.sample()) # take a random action
env.close()

会出错NoSuchDisplayException: Cannot connect to "None",报错信息如下

NoSuchDisplayException                    Traceback (most recent call last)
      7     #绘图
----> 8     env.render()
   ...
--> 123             raise NoSuchDisplayException('Cannot connect to "%s"' % name)
    125         screen_count = xlib.XScreenCount(self._display)
NoSuchDisplayException: Cannot connect to "None"

解决方案一:使用matplotlib

import gym
import matplotlib.pyplot as plt
%matplotlib inline

env = gym.make('Breakout-v0') # insert your favorite environment
render = lambda : plt.imshow(env.render(mode='rgb_array'))
env.reset()
render()

# arr = env.render(mode='rgb_array')
# plt.imshow(arr) or scipy.misc.imsave('sample.png', arr)

在一个单元中多次渲染

import gym
from IPython import display
import matplotlib.pyplot as plt
%matplotlib inline

env = gym.make('Breakout-v0')
env.reset()
for _ in range(100):
    plt.imshow(env.render(mode='rgb_array'))
    display.display(plt.gcf())
    display.clear_output(wait=True)
    action = env.action_space.sample()
    env.step(action)

提高效率

import gym
from IPython import display
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline

env = gym.make('Breakout-v0')
env.reset()
img = plt.imshow(env.render(mode='rgb_array')) # only call this once
for _ in range(100):
    img.set_data(env.render(mode='rgb_array')) # just update the data
    display.display(plt.gcf())
    display.clear_output(wait=True)
    action = env.action_space.sample()
    env.step(action)

使用PIL.Image

from IPython import display
import numpy as np
import time

import gym
env = gym.make('SpaceInvaders-v0')
env.reset()

import PIL.Image
import io


def showarray(a, fmt='png'):
    a = np.uint8(a)
    f = io.BytesIO()
    ima = PIL.Image.fromarray(a).save(f, fmt)
    return f.getvalue()

imagehandle = display.display(display.Image(data=showarray(env.render(mode='rgb_array')), width=450), display_id='gymscr')

while True:
    time.sleep(0.01)
    env.step(env.action_space.sample()) # take a random action
    display.update_display(display.Image(data=showarray(env.render(mode='rgb_array')), width=450), display_id='gymscr')

方案三:使用wrapper的monitor捕获

依赖安装

!apt install python-opengl
!apt install ffmpeg
!apt install xvfb
!pip3 install pyvirtualdisplay

# Virtual display
from pyvirtualdisplay import Display

virtual_display = Display(visible=0, size=(1400, 900))
virtual_display.start()

捕获为MP4

import gym
from gym import wrappers

env = gym.make("SpaceInvaders-v0")
env = wrappers.Monitor(env, "./SpaceInvaders-v0")

for episode in range(2):
    observation = env.reset()
    step = 0
    total_reward = 0

    while True:
        step += 1
        env.render()
        action = env.action_space.sample()
        observation, reward, done, info = env.step(action)
        total_reward += reward
        if done:
            print("Episode: {0},\tSteps: {1},\tscore: {2}"
                  .format(episode, step, total_reward)
            )
            break
env.close()

在notebook中展示

import os
import io
import base64
from IPython.display import display, HTML

def ipython_show_video(path):
    """Show a video at `path` within IPython Notebook
    """
    if not os.path.isfile(path):
        raise NameError("Cannot access: {}".format(path))

    video = io.open(path, 'r+b').read()
    encoded = base64.b64encode(video)

    display(HTML(
        data="""
        
        """.format(encoded.decode('ascii'))
    ))

ipython_show_video("./SpaceInvaders-v0/openaigym.video.4.10822.video000000.mp4")

reference

stackoverflow.com

你可能感兴趣的:(python,anaconda,gym)