openai / gym

A toolkit for developing and comparing reinforcement learning algorithms.
https://www.gymlibrary.dev
Other
34.77k stars 8.61k forks source link

Environment working, but not render() #762

Closed cpatyn closed 5 years ago

cpatyn commented 7 years ago

Configuration:

Dell XPS15 Anaconda 3.6 Python 3.5 NVIDIA GTX 1050

I installed open ai gym through pip. When I run the below code, I can execute steps in the environment which returns all information of the specific environment, but the render() method just gives me a blank screen. When I exit python the blank screen closes in a normal way.

Code:

import gym
env = gym.make('CartPole-v0')
env.reset()
env.render()
for i in range(1000):
    env.step(env.action_space.sample())

After hours of google searching I think the issue might have something to do with pyglet, the package used for rendering, and possibly a conflict with my nvidia graphics card? All help is welcome. Thanks!

cyrsis commented 7 years ago

Try run the following


import gym
import random
import numpy as np
import tflearn
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
from statistics import median, mean
from collections import Counter

LR = 1e-3
env = gym.make("CartPole-v0")
env.reset()
goal_steps = 500
score_requirement = 50
initial_games = 10000

def some_random_games_first():
    # Each of these is its own game.
    for episode in range(5):
        env.reset()
        # this is each frame, up to 200...but we wont make it that far.
        for t in range(200):
            # This will display the environment
            # Only display if you really want to see it.
            # Takes much longer to display it.
            env.render()

            # This will just create a sample action in any environment.
            # In this environment, the action can be 0 or 1, which is left or right
            action = env.action_space.sample()

            # this executes the environment with an action,
            # and returns the observation of the environment,
            # the reward, if the env is over, and other info.
            observation, reward, done, info = env.step(action)
            if done:
                break

some_random_games_first() ``
smilesun commented 6 years ago

@cpatyn I met the same problem, did you figure out what happened ?

quanvuong commented 6 years ago

Your render function call has to be inside the for loop. The code below works for me.

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

for i in range(1000):
    env.step(env.action_space.sample())
    env.render()
cyrsis commented 6 years ago

@cpatyn @smilesun What OS are u in?

Ubuntu or Windows?

MikeDoho commented 6 years ago

I am also interested in hearing if you were able to figure this out. I am having what appears to be a similar problem. I cant see the window. Though I am working with python 3.6.3 but same anaconda. Also working on ubuntu.

One difference I have is if I put env.render() and not env.render(close=True) then I get the error below. This error is not displayed if I put env.render(close=True) but I still cant see the environment.

Traceback (most recent call last):

File "", line 5, in env.render()

File "/home/mikedoho/gym/gym/core.py", line 150, in render return self._render(mode=mode, close=close)

File "/home/mikedoho/gym/gym/core.py", line 286, in _render return self.env.render(mode, close)

File "/home/mikedoho/gym/gym/core.py", line 150, in render return self._render(mode=mode, close=close)

File "/home/mikedoho/gym/gym/envs/classic_control/cartpole.py", line 116, in _render self.viewer = rendering.Viewer(screen_width, screen_height)

File "/home/mikedoho/gym/gym/envs/classic_control/rendering.py", line 51, in init self.window = pyglet.window.Window(width=width, height=height, display=display)

File "/home/mikedoho/anaconda3/lib/python3.6/site-packages/pyglet/window/init.py", line 504, in init screen = display.get_default_screen()

File "/home/mikedoho/anaconda3/lib/python3.6/site-packages/pyglet/canvas/base.py", line 73, in get_default_screen return self.get_screens()[0]

File "/home/mikedoho/anaconda3/lib/python3.6/site-packages/pyglet/canvas/base.py", line 65, in get_screens raise NotImplementedError('abstract')

NotImplementedError: abstract

samedii commented 6 years ago

I have this issue when running from spyder but it works fine if the script is started via the terminal

Chambana commented 6 years ago

I had the same issue with my rendering using a similar system (XPS15, Ubuntu 16.04, and installed gym via pip). Try adding import time a adding small sleep, like time.sleep(0.1), after the call to render() inside your loop. That resolved the issue for me.

FirefoxMetzger commented 6 years ago

@MikeDoho this sounds very similar to #775 . Back there it was a bug in a recent pyglet version, causing gym to be unable to find a render target. You can try the same (reverting to an older pyglet) and see if that fixes your issue (if it hasn't been fixed already). Check the issue for further details.

AustinDeric commented 6 years ago

@Chambana's fix worked for me. I have the exact same specs as described in the intro. I can run all the roboschool scripts but i ran into trouble while going through the gym docs:

Exception ignored in: <bound method Viewer.__del__ of <gym.envs.classic_control.rendering.Viewer object at 0x7f2bd5e826a0>>
Traceback (most recent call last):
  File "/home/aderic/aiproj/gym/gym/envs/classic_control/rendering.py", line 143, in __del__
  File "/home/aderic/aiproj/gym/gym/envs/classic_control/rendering.py", line 62, in close
  File "/home/aderic/.local/lib/python3.5/site-packages/pyglet/window/xlib/__init__.py", line 480, in close
  File "/home/aderic/.local/lib/python3.5/site-packages/pyglet/gl/xlib.py", line 345, in destroy
  File "/home/aderic/.local/lib/python3.5/site-packages/pyglet/gl/base.py", line 334, in destroy
  File "/home/aderic/.local/lib/python3.5/site-packages/pyglet/gl/xlib.py", line 335, in detach
  File "/home/aderic/.local/lib/python3.5/site-packages/pyglet/gl/lib.py", line 97, in errcheck
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 954, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 887, in _find_spec
TypeError: 'NoneType' object is not iterable

I have not tried the pyglet downgrade yet.

zwfcrazy commented 6 years ago

@AustinDeric Have you fixed the 'NoneType' problem? Downgrading pyglet to 1.2.4 does not work for me, I still got the error when the CartPole example ends. It seems this occurs when the animation window is closing.

Episode finished after 19 timesteps
Exception ignored in: <bound method Viewer.__del__ of <gym.envs.classic_control.rendering.Viewer object at 0x7fc8a05195f8>>
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/gym/envs/classic_control/rendering.py", line 143, in __del__
  File "/usr/local/lib/python3.4/dist-packages/gym/envs/classic_control/rendering.py", line 62, in close
  File "/usr/local/lib/python3.4/dist-packages/pyglet/window/xlib/__init__.py", line 480, in close
  File "/usr/local/lib/python3.4/dist-packages/pyglet/gl/xlib.py", line 345, in destroy
  File "/usr/local/lib/python3.4/dist-packages/pyglet/gl/base.py", line 334, in destroy
  File "/usr/local/lib/python3.4/dist-packages/pyglet/gl/xlib.py", line 335, in detach
  File "/usr/local/lib/python3.4/dist-packages/pyglet/gl/lib.py", line 97, in errcheck
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2222, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 2155, in _find_spec
TypeError: 'NoneType' object is not iterable
frozenfoxx commented 6 years ago

Getting the same issue here.

mashoujiang commented 6 years ago

Get the same issue, unsolved. BTW, Breakout-v0 has no terminal state?When does the "done" turn to True?

samching commented 6 years ago

Same problem here. Tried running MountainCar-v0 using the Gym tutorial and I'm getting a TypeError: 'NoneType' object is not iterable. Adding time.sleep(0.1) doesn't help, neither does downgrading pyglet to 1.2.4. @AustinDeric, did you have any success working through the OpenAI docs eventually?

Currently running OSX 10.13.3 with Python 3.5.

rlouyang commented 6 years ago

@AustinDeric @zwfcrazy @samuelcwl Are you guys calling env.close() at the end of your scripts? Adding that line fixed the issue for me.

LianYun commented 6 years ago

@rlouyang me too, i use code like this:

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

env.render()
env.step(env.action_space.sample())

env.close()

the problem is not any more to happened now.

samching commented 6 years ago

Thanks @rlouyang and @LianYun , that fixed it for me. There should be PR to update the docs with this.

chrisspen commented 6 years ago

@quanvuong That fixed it for me. Unfortunately, the README shows code that doesn't use a loop, giving people the expectation that you shouldn't need one.

HPL123 commented 6 years ago

@samuelcwl ,I got the same issue,how to solve the problem?

rlouyang commented 6 years ago

@HPL123 See https://github.com/openai/gym/issues/762#issuecomment-377701131. Are you calling env.close() at the end of your script?

ninja18 commented 6 years ago

The same problem when I run the script in terminal it works fine but when I run it in the jupyter notebook it show the NotImplemented Error I installed pyglet==1.2.4 using pip and gym using conda When I try to install pyglet==1.2.4 using conda conda install -c conda-forge pyglet It shows conflict error between opencv3 and pyglet. Please Help!

EDIT: After I restarted the notebook The issue was solved.

kushalshm1 commented 5 years ago

Stil Not solved in my case: When I am calling env.render() I am getting Error:


 ImportError                               Traceback (most recent call last)
 <ipython-input-31-de3a705ce18d> in <module>
 ----> 1 env.render()
      2 env.close()
 /usr/lib/python3.7/site-packages/gym/core.py in render(self, mode, **kwargs)

 /usr/lib/python3.7/site-packages/gym/envs/classic_control/cartpole.py in render(self, mode)

ImportError: cannot import name 'rendering' from 'gym.envs.classic_control' (/usr/lib/python3.7/site-packages/gym/envs/classic_control/__init__.py)

Please help as I really want to learn OpenAI GYM properly by the ending of this year

wleethecoder commented 5 years ago

@kushalshm1 show me your script

kushalshm1 commented 5 years ago

@toenailssauce I fixed it. It was not working in Jupyter Notebook but it worked in terminal when I ran from a script.

Thanks for your response!

anuj1560 commented 5 years ago

In my ubuntu18.04 (bionic) ARM64 $vi cart.py import gym env = gym.make('CartPole-v0') for i_episode in range(20): 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.close()

Output $python3 cart.py Fatal Python error: Segmentation fault Current thread 0x0000007fa6799010 (most recent call first): File "/home/nvidia/.local/lib/python3.6/site-packages/pyglet/gl/lib_glx.py", line 74 in link_GL File "/home/nvidia/.local/lib/python3.6/site-packages/pyglet/gl/glx.py", line 440 in File "", line 219 in _call_with_frames_removed File "", line 678 in exec_module File "", line 665 in _load_unlocked File "", line 955 in _find_and_load_unlocked File "", line 971 in _find_and_load File "", line 219 in _call_with_frames_removed File "", line 1023 in _handle_fromlist File "/home/nvidia/.local/lib/python3.6/site-packages/pyglet/gl/xlib.py", line 16 in File "", line 219 in _call_with_frames_removed File "", line 678 in exec_module File "", line 665 in _load_unlocked File "", line 955 in _find_and_load_unlocked File "", line 971 in _find_and_load File "/home/nvidia/.local/lib/python3.6/site-packages/pyglet/gl/init.py", line 221 in File "", line 219 in _call_with_frames_removed File "", line 678 in exec_module File "", line 665 in _load_unlocked File "", line 955 in _find_and_load_unlocked File "", line 971 in _find_and_load File "/home/nvidia/packages/openai/gym/gym/envs/classic_control/rendering.py", line 23 in File "", line 219 in _call_with_frames_removed File "", line 678 in exec_module File "", line 665 in _load_unlocked File "", line 955 in _find_and_load_unlocked File "", line 971 in _find_and_load File "", line 219 in _call_with_frames_removed File "", line 1023 in _handle_fromlist File "/home/nvidia/packages/openai/gym/gym/envs/classic_control/cartpole.py", line 150 in render File "/home/nvidia/packages/openai/gym/gym/core.py", line 275 in render File "cart.py", line 8 in Segmentation fault (core dumped)

Thanks

Pybast commented 5 years ago

I have added a sleep(0.03) in the for loop and it now works, I think every frame was being displayed to quickly so nothing was really shown on display. Here is my final code:

import gym
from time import sleep

env = gym.make('CartPole-v0')
env.reset()
for _ in range(100):
    env.step(env.action_space.sample()) # take a random action
    env.render()
    sleep(0.03)
env.close()
christopherhesse commented 5 years ago

Thanks @Pibastte! So if you run the following code, you get a blank window?

import gym

env = gym.make('CartPole-v0')
env.reset()
while True:
    _obs, _rew, done, _info = env.step(env.action_space.sample()) # take a random action
    env.render()
    if done:
        env.reset()
env.close()

What OS + version are you using?

oneandonlyonebutyou commented 5 years ago

I am using Mac OS . How can I get this to work?

Pybast commented 5 years ago

@christopherhesse For me yes it was showing only a blank window. I am using Ubuntu 18.04. @joseph-vedadi I think you should be able to use the same code as we do, just download python and gym on your mac through the console. I have never done any programming on a mac but I ve heard that it s close to linux so it should not be a challenge to make it work. I m sure you ll find a way !

christopherhesse commented 5 years ago

Okay @Pibastte please file a new issue for that with the example test script you gave, that's odd but seems unlikely to be related to this issue.

christopherhesse commented 5 years ago

Thanks @quanvuong for answering the original question, looks like render() was not being called correctly.

christopherhesse commented 5 years ago

Please file new issues for other render() related things.

michrzan commented 5 years ago

I had the same problem (blank window after calling env.render() ). Turns out an external screen connected to my laptop was causing issues. After disconnecting the second screen everything works just fine.

sdheepthi commented 4 years ago

I am having same issues. It does not work in Jupyter notebook but works fine when running from terminal and in a loop.

stephaniemgw commented 3 years ago

I had the same issue, none of the fixes seemed to work. Using Eclipse IDE on mac for Python 3.8.

Running it from terminal did!

ZYQiao commented 2 years ago

@toenailssauce I fixed it. It was not working in Jupyter Notebook but it worked in terminal when I ran from a script.

Thanks for your response!

How do you fix it?

ZYQiao commented 2 years ago

image This is my problem, Window10, Python 3.9

ddevleena commented 2 years ago

@ZYQiao Had the same issue, and was able to fix it by downgrading gym from version 0.22.0 to 0.21.0

jkterry1 commented 2 years ago

Hey, given how new 0.22.0 is, this is definitely an issue unrelated to the original post. However, if you guys are having issues rendering the environment related to 0.22, please create a new issue and we'll take a look :)