7

我正在尝试让 AI-Gym 演示在 Jupyter 笔记本中显示。我在 Atari 演示中得到了很好的结果,Breakout-v0而在 cart-pole 演示中得到了一个困难的错误消息CartPole-v0。两者都可以在笔记本电脑之外正常工作。以下是最小的细节:

没有木星

在控制台:

$ pip install gym[atari] &> /dev/null
$ /anaconda3/bin/python3
Python 3.6.3 |Anaconda, Inc.| (default, Oct  6 2017, 12:04:38)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import gym
>>> env = gym.make('Breakout-v0')
>>> env.reset()
>>> env.render()

结果:

在此处输入图像描述

现在,同样使用 cart-pole,在一个新的 Python 会话中:

$ pip install gym &> /dev/null
$ /anaconda3/bin/python3
Python 3.6.3 |Anaconda, Inc.| (default, Oct  6 2017, 12:04:38)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> env = gym.make('CartPole-v0')
>>> env.reset()
>>> env.render()

与上面 Python 代码的唯一区别是演示的名称:CartPole-v0而不是Breakout-v0.

结果: 在此处输入图像描述

与木星

遵循这个 SO 问题的一些提示:How to run OpenAI Gym .render() over a server,我有这个用于Breakout演示:

在此处输入图像描述

或者,在文本中:

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'))
img.set_data(env.render(mode='rgb_array'))
display.display(plt.gcf())
display.clear_output(wait=True)

现在,对于购物车杆,再次使用新的内核会话和笔记本,唯一的区别是演示的名称:

import gym
from IPython import display
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
env = gym.make('CartPole-v0')
env.reset()
img = plt.imshow(env.render(mode='rgb_array'))
img.set_data(env.render(mode='rgb_array'))
display.display(plt.gcf())
display.clear_output(wait=True)

我收到一条很长的错误消息,抱怨某些 pyglet 类是抽象的。堆栈跟踪的最后一个元素如下:

/anaconda3/lib/python3.6/site-packages/pyglet/canvas/base.py in get_screens(self)
     63         :rtype: list of :class:`Screen`
     64         '''
---> 65         raise NotImplementedError('abstract')
     66 
     67     def get_default_screen(self):

NotImplementedError: abstract

整个堆栈跟踪位于底部以供检查。我通读了它,但无法确定问题以及如何解决它。我会很感激你的建议。

总结一下,

NO JUPYTER:
  Breakout: OK
  CartPole: OK
JUPYTER:
  Breakout: OK
  CartPole: ERROR

整个堆栈跟踪如下:

NotImplementedError                       Traceback (most recent call last)
<ipython-input-1-df4d39818fe3> in <module>()
      6 env = gym.make('CartPole-v0')
      7 env.reset()
----> 8 img = plt.imshow(env.render(mode='rgb_array'))
      9 img.set_data(env.render(mode='rgb_array'))
     10 display.display(plt.gcf())

/anaconda3/lib/python3.6/site-packages/gym/core.py in render(self, mode, close)
    148             elif mode not in modes:
    149                 raise error.UnsupportedMode('Unsupported rendering mode: {}. (Supported modes for {}: {})'.format(mode, self, modes))
--> 150         return self._render(mode=mode, close=close)
    151 
    152     def close(self):

/anaconda3/lib/python3.6/site-packages/gym/core.py in _render(self, mode, close)
    284 
    285     def _render(self, mode='human', close=False):
--> 286         return self.env.render(mode, close)
    287 
    288     def _close(self):

/anaconda3/lib/python3.6/site-packages/gym/core.py in render(self, mode, close)
    148             elif mode not in modes:
    149                 raise error.UnsupportedMode('Unsupported rendering mode: {}. (Supported modes for {}: {})'.format(mode, self, modes))
--> 150         return self._render(mode=mode, close=close)
    151 
    152     def close(self):

/anaconda3/lib/python3.6/site-packages/gym/envs/classic_control/cartpole.py in _render(self, mode, close)
    114         if self.viewer is None:
    115             from gym.envs.classic_control import rendering
--> 116             self.viewer = rendering.Viewer(screen_width, screen_height)
    117             l,r,t,b = -cartwidth/2, cartwidth/2, cartheight/2, -cartheight/2
    118             axleoffset =cartheight/4.0

/anaconda3/lib/python3.6/site-packages/gym/envs/classic_control/rendering.py in __init__(self, width, height, display)
     49         self.width = width
     50         self.height = height
---> 51         self.window = pyglet.window.Window(width=width, height=height, display=display)
     52         self.window.on_close = self.window_closed_by_user
     53         self.geoms = []

/anaconda3/lib/python3.6/site-packages/pyglet/window/__init__.py in __init__(self, width, height, caption, resizable, style, fullscreen, visible, vsync, display, screen, config, context, mode)
    502 
    503         if not screen:
--> 504             screen = display.get_default_screen()
    505 
    506         if not config:

/anaconda3/lib/python3.6/site-packages/pyglet/canvas/base.py in get_default_screen(self)
     71         :rtype: :class:`Screen`
     72         '''
---> 73         return self.get_screens()[0]
     74 
     75     def get_windows(self):

/anaconda3/lib/python3.6/site-packages/pyglet/canvas/base.py in get_screens(self)
     63         :rtype: list of :class:`Screen`
     64         '''
---> 65         raise NotImplementedError('abstract')
     66 
     67     def get_default_screen(self):

NotImplementedError: abstract
4

1 回答 1

0

只需编写一个.py脚本,渲染将在一个新窗口中完成。为我在mac上工作!

于 2021-09-15T11:57:17.567 回答