本文介绍了如何在服务器上运行OpenAI Gym .render()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过Jupyter(Ubuntu 14.04)在p2.xlarge AWS 服务器上运行python 2.7脚本.我希望能够渲染我的模拟.

I am running a python 2.7 script on a p2.xlarge AWS server through Jupyter (Ubuntu 14.04). I would like to be able to render my simulations.

最小的工作示例

import gym
env = gym.make('CartPole-v0')
env.reset()
env.render()

env.render()导致(其中包括)以下错误:

env.render() makes (among other things) the following errors:

...
HINT: make sure you have OpenGL install. On Ubuntu, you can run
'apt-get install python-opengl'. If you're running on a server,
you may need a virtual frame buffer; something like this should work:
'xvfb-run -s \"-screen 0 1400x900x24\" python <your_script.py>'")
...
NoSuchDisplayException: Cannot connect to "None"

我想一些如何能够看到模拟.如果可以内联,那将是理想的选择,但是任何一种显示方法都将是不错的选择.

I would like to some how be able to see the simulations. It would be ideal if I could get it inline, but any display method would be nice.

编辑:这只是某些环境(例如经典控件)的问题.

Edit: This is only an issue with some environments, like classic control.

更新我

这个问题的启发,我尝试了以下操作,而不是xvfb-run -s \"-screen 0 1400x900x24\" python <your_script.py>无法上班).

Inspired by this I tried the following, instead of the xvfb-run -s \"-screen 0 1400x900x24\" python <your_script.py> (which I couldn't get to work).

xvfb-run -a jupyter notebook

运行我现在得到的原始脚本

Running the original script I now get instead

GLXInfoException: pyglet requires an X server with GLX


更新II

问题#154 似乎很重要.我尝试禁用弹出窗口,然后直接创建RGB颜色

Issue #154 seems relevant. I tried disabling the pop-up, and directly creating the RGB colors

import gym
env = gym.make('CartPole-v0')
env.reset()

img = env.render(mode='rgb_array', close=True)
print(type(img)) # <--- <type 'NoneType'>

img = env.render(mode='rgb_array', close=False) # <--- ERROR
print(type(img))

我得到ImportError: cannot import name gl_info.

更新III

在@ Torxed 的启发下,我尝试创建视频文件,然后进行渲染(一个完全令人满意的解决方案).

With inspiration from @Torxed I tried creating a video file, and then rendering it (a fully satisfying solution).

使用"记录和上传结果"中的代码

Using the code from 'Recording and uploading results'

import gym

env = gym.make('CartPole-v0')
env.monitor.start('/tmp/cartpole-experiment-1', force=True)
observation = env.reset()
for t in range(100):
#    env.render()
    print(observation)
    action = env.action_space.sample()
    observation, reward, done, info = env.step(action)
    if done:
        print("Episode finished after {} timesteps".format(t+1))
        break

env.monitor.close()

我尝试遵循您的建议,但是在运行env.monitor.start(...时得到了ImportError: cannot import name gl_info.

I tried following your suggestions, but got ImportError: cannot import name gl_info from when running env.monitor.start(....

据我所知,问题是OpenAI使用pyglet,并且pyglet需要"屏幕以计算要渲染的图像的RGB颜色. 因此,有必要欺骗python以使其认为已连接监视器

From my understanding the problem is that OpenAI uses pyglet, and pyglet 'needs' a screen in order to compute the RGB colors of the image that is to be rendered. It is therefore necessary to trick python to think that there is a monitor connected

更新IV

仅供参考,网上有一些使用大黄蜂的解决方案似乎很有效.如果您可以控制服务器,这应该可以工作,但是由于AWS在VM中运行,所以我认为您不能使用它.

FYI there are solutions online using bumblebee that seem to work. This should work if you have control over the server, but since AWS run in a VM I don't think you can use this.

更新V

只要您有这个问题,并且不知道要做什么(就像我一样),大多数环境的状态就足够简单,您可以创建自己的渲染机制.不太令人满意,但是..你知道的.

Just if you have this problem, and don't know what to do (like me) the state of most environments are simple enough that you can create your own rendering mechanism. Not very satisfying, but.. you know.

推荐答案

有一个简单的解决方案:

Got a simple solution working:

$ xvfb-run -s "-screen 0 1400x900x24" jupyter notebook

在朱皮特

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

每一步之后

def show_state(env, step=0, info=""):
    plt.figure(3)
    plt.clf()
    plt.imshow(env.render(mode='rgb_array'))
    plt.title("%s | Step: %d %s" % (env._spec.id,step, info))
    plt.axis('off')

    display.clear_output(wait=True)
    display.display(plt.gcf())

注意:如果您的环境不是unwrapped,请将env.env传递给show_state.

Note: if your environment is not unwrapped, pass env.env to show_state.

这篇关于如何在服务器上运行OpenAI Gym .render()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-07 18:26