How to run OpenAI Gym .render() over a server

前端 未结 15 2237
囚心锁ツ
囚心锁ツ 2020-12-12 10:07

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.

Minimal working example<

15条回答
  •  无人及你
    2020-12-12 10:40

    I encountered the same problem and stumbled upon the answers here. Mixing them helped me to solve the problem.

    Here's a step by step solution:

    Install the following:

    apt-get install -y python-opengl xvfb
    

    Start your jupyter notebook via the following command:

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

    Inside the notebook:

    import gym
    import matplotlib.pyplot as plt
    %matplotlib inline
    
    env = gym.make('MountainCar-v0') # insert your favorite environment
    env.reset()
    plt.imshow(env.render(mode='rgb_array')
    

    Now you can put the same thing in a loop to render it multiple times.

    from IPython import display
    
    for _ in range(100):
        plt.imshow(env.render(mode='rgb_array'))
        display.display(plt.gcf())
        display.clear_output(wait=True)
        action = env.action_space.sample()
        env.step(action)
    

    Hope this works for anyone else still facing an issue. Thanks to Andrews and Nathan for their answers.

提交回复
热议问题