Google Colab 中的 OpenAI 健身房渲染

问题描述

我正在尝试在 Google Colab 环境中使用 OpenAI 健身房使用强化学习 tutorial。我使用的策略是创建一个虚拟显示,然后使用 matplotlib 来显示正在渲染的环境。

这在我使用“CartPole-v0”或“LunarLander-v2”环境时效果很好,但在我使用“Taxi-v3”环境时出现错误

笔记本单元格如下:

# install required system dependencies
!apt-get install -y xvfb x11-utils > /dev/null
!pip install gym[Box2d] pyvirtualdisplay  PyOpenGL PyOpenGL-accelerate > /dev/null

import pyvirtualdisplay
_display = pyvirtualdisplay.display(visible=False,size=(1400,900))  # use False with Xvfb                 
_ = _display.start()
!echo $disPLAY

import gym
import matplotlib.pyplot as plt
import numpy as np
from IPython import display

env = gym.make("Taxi-v3").env
#env = gym.make("CartPole-v0").env
#env = gym.make('LunarLander-v2')

env.reset()
fig,ax = plt.subplots(figsize=(20,10))
ax.axis('off')
img = ax.imshow(env.render(mode='rgb_array'))
#img.set_data(env.render(mode='rgb_array')) 
display.display(plt.gcf())
display.clear_output(wait=True)

错误如下:

Exception in callback BaseAsyncIOLoop._handle_events(18,1)
handle: <Handle BaseAsyncIOLoop._handle_events(18,1)>
Traceback (most recent call last):
  File "/usr/lib/python3.6/asyncio/events.py",line 145,in _run
    self._callback(*self._args)
  -------- ------- ---- etc etc
  File "/usr/local/lib/python3.6/dist-packages/ipykernel/iostream.py",line 339,in flush
    if self.pub_thread.thread.is_alive():
AttributeError: 'nonetype' object has no attribute 'thread'

如前所述,错误仅在我使用 Taxi-v3 环境时发生。我有两个问题

  1. 我应该做哪些更改才能让 Taxi-v3 环境在 Colab 中运行?
  2. Taxi 环境与其他两个环境之间有什么区别?导致此错误的原因是什么?

欢迎任何指导或建议。

解决方法

Taxi-v3 是一个生成文本输出的环境!所以将它推入 matplotlib 会导致错误!此代码有效!

env = gym.make("Taxi-v3").env
env.reset()
env.render()

并生成

+---------+
|R: | : :G|
| : | : : |
| : : : : |
| | : | : |
|Y| : |B: |
+---------+

颜色!