4

I am trying to learn Q-Learning by using OpenAI's gym module. But when I try to render my environment, I get the following error,

OSError                                   Traceback (most recent call last)
<ipython-input-1-c269c1129a2f> in <module>
     12     action = 2
     13     new_state, reward, done, _ = env.step(action)
---> 14     plt.imshow(env.render(mode='rgb_array'))
     15     disp
     16 

C:\Program Files\Python37\lib\site-packages\gym\core.py in render(self, mode, **kwargs)
    228 
    229     def render(self, mode='human', **kwargs):
--> 230         return self.env.render(mode, **kwargs)
    231 
    232     def close(self):

C:\Program Files\Python37\lib\site-packages\gym\envs\classic_control\mountain_car.py in render(self, mode)
    116         self.cartrans.set_rotation(math.cos(3 * pos))
    117 
--> 118         return self.viewer.render(return_rgb_array = mode=='rgb_array')
    119 
    120     def get_keys_to_action(self):

C:\Program Files\Python37\lib\site-packages\gym\envs\classic_control\rendering.py in render(self, return_rgb_array)
    112             arr = arr.reshape(buffer.height, buffer.width, 4)
    113             arr = arr[::-1,:,0:3]
--> 114         self.window.flip()
    115         self.onetime_geoms = []
    116         return arr if return_rgb_array else self.isopen

C:\Program Files\Python37\lib\site-packages\pyglet\window\win32\__init__.py in flip(self)
    319     def flip(self):
    320         self.draw_mouse_cursor()
--> 321         self.context.flip()
    322 
    323     def set_location(self, x, y):

C:\Program Files\Python37\lib\site-packages\pyglet\gl\win32.py in flip(self)
    224 
    225     def flip(self):
--> 226         _gdi32.SwapBuffers(self.canvas.hdc)
    227 
    228     def get_vsync(self):

OSError: exception: access violation reading 0x000000000000001C

I get the same error when I run my code from the command prompt

Also, here is my code,

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

env = gym.make("MountainCar-v0")
env.reset()

done = False

while not done:
    action = 2
    new_state, reward, done, _ = env.step(action)
    plt.imshow(env.render(mode='rgb_array'))
    display.display(plt.gcf())
    display.clear_output(wait=True)

env.close()

I was not able to find this error on the internet with gym, so I am not able to solve it. I even tried the cartpole environment but ended with the same error.

Any help is appreciated.

sanyassh
  • 8,100
  • 13
  • 36
  • 70
Vinay Bharadhwaj
  • 165
  • 1
  • 17

1 Answers1

1

It is probably due to dynamically using ipython-display. Change your code to:

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

env = gym.make("MountainCar-v0")
env.reset()

done = False

while not done:
    action = 2
    new_state, reward, done, _ = env.step(action)
    #plt.imshow(env.render(mode='rgb_array'))
    env.render(mode='rgb_array')
    #display.display(plt.gcf())
    #display.clear_output(wait=True)

env.close()

This OSError can occur for many reasons, therefore it is difficult to reproduce and debug without verifying everything. But from this error of yours:

C:\Program Files\Python37\lib\site-packages\pyglet\gl\win32.py in flip(self)
    224 
    225     def flip(self):
--> 226         _gdi32.SwapBuffers(self.canvas.hdc)
    227 
    228     def get_vsync(self):

Since GDI and the ICD share function names like SwapBuffers, a wglSwapBuffers is needed to avoid ambiguity when loading the proc dynamically your one display might lock another one. I am not sure but this seems more likely than any other reason.

Zoe
  • 27,060
  • 21
  • 118
  • 148
ASHu2
  • 2,027
  • 15
  • 29
  • Thanks for the answer but I still get the same memory accession error from _gdi32. It occurs with the `env.render()` – Vinay Bharadhwaj Jun 07 '19 at 08:43
  • Could you just install `pyglet` again from scratch and run any code from [here](https://medium.com/@yvanscher/opengl-and-pyglet-basics-1bd9f1721cc6) – ASHu2 Jun 07 '19 at 09:05
  • Sorry that I took so long to reply to this, but I have been trying everything regarding pyglet errors, including but not limited to, running chkdsk, sfc scans, and reinstalling python and pyglet. None of them seems to be resolving my issue – Vinay Bharadhwaj Jun 13 '19 at 16:07
  • Could you edit your questions with all the versions for `matplotlib, gym, pyglet `versions you are using, it will be easier for me to reproduce the error and also for everyone who sees it too... – ASHu2 Jun 13 '19 at 16:12