7

I am trying to get AI-Gym demos to display in Jupyter notebooks. I get good results for the Atari demo Breakout-v0 and a difficult error message for the cart-pole demo CartPole-v0. Both work fine outside notebooks. The following are the minimal details:

WITHOUT JUPYTER

At a console:

$ 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()

Result:

enter image description here

Now, likewise with cart-pole, in a new Python session:

$ 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()

The only difference to the Python code above is the name of the demo: CartPole-v0 instead of Breakout-v0.

Result: enter image description here

WITH JUPYTER

Following some hints from this SO question: How to run OpenAI Gym .render() over a server, I have this for the Breakout demo:

enter image description here

or, in text:

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)

Now, for the cart-pole, again with a fresh kernel session and notebook, and with the only difference being the name of the demo:

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)

I get a very long error message complaining that some pyglet class is abstract. The last element of the stack trace is the following:

/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

The entire stack trace is at the bottom for inspection. I read through it but was unable to determine the problem and how to fix it. I'd be grateful for advice.

To summarize,

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

The entire stack trace is as follows:

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
Reb.Cabin
  • 5,426
  • 3
  • 35
  • 64

1 Answers1

0

Simply write a .py script and the rendering will be done in a new windows. Worked on mac for me!

Pipper Tetsing
  • 403
  • 4
  • 5