python - OpenAI gym 渲染 OSError

标签 python python-3.x pyglet openai-gym q-learning

我正在尝试使用 OpenAI 的健身房模块学习 Q-Learning。但是当我尝试渲染我的环境时,出现以下错误,

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

当我从命令提示符运行我的代码时,我得到同样的错误

此外,这是我的代码,

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

我在互联网上找不到 gym 的这个错误,所以我无法解决它。 我什至尝试了 cartpole 环境,但以同样的错误结束。

感谢任何帮助。

最佳答案

这可能是由于动态使用了ipython-display。 将您的代码更改为:

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

此 OSError 的发生可能有多种原因,因此在不验证所有内容的情况下很难重现和调试。但是从你的这个错误来看:

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):

由于 GDI 和 ICD 共享函数名称,如 SwapBuffers,一个 加载过程时需要 wglSwapBuffers 以避免歧义 动态地,您的一个显示器可能锁定另一个。 我不确定但这似乎比任何其他原因更有可能。

关于python - OpenAI gym 渲染 OSError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56392223/

相关文章:

python - mongodb:查询列表中的值(而不是对象)

python - Python 中的全局变量

python - 如何获取pip安装路径 `data_files` ?

python - 使用 Pyglet 的临时图像

python - 使用 Pyglet 在 on_mouse_press 上插入文本

python - 在 Python 中优化集合理解

Python - 如果我不知道扩展名,如何在文件夹中查找文件?

python - 使用请求登录 Twitter

python-3.x - 如何将 Google Cloud AI Platform Jupyter Lab 升级到 Python 3.7+

python - Pyglet无法加载.wav文件