-2

我尝试了开放 AI 健身房文档中的代码。它可以工作,但在我关闭窗口后会出现错误。我使用的代码如下:

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

这是错误:

  File "<ipython-input-3-b38d53bb166b>", line 1, in <module>
    runfile('D:/Python/openAIGym.py', wdir='D:/Python')

  File "D:\Anaconda\envs\tensorflow\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
    execfile(filename, namespace)

  File "D:\Anaconda\envs\tensorflow\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "D:/Python/openAIGym.py", line 6, in <module>
    env.render()

  File "D:\Anaconda\envs\tensorflow\lib\site-packages\gym\core.py", line 150, in render
    return self._render(mode=mode, close=close)

  File "D:\Anaconda\envs\tensorflow\lib\site-packages\gym\core.py", line 286, in _render
    return self.env.render(mode, close)

  File "D:\Anaconda\envs\tensorflow\lib\site-packages\gym\core.py", line 150, in render
    return self._render(mode=mode, close=close)

  File "D:\Anaconda\envs\tensorflow\lib\site-packages\gym\envs\classic_control\cartpole.py", line 146, in _render
    return self.viewer.render(return_rgb_array = mode=='rgb_array')

  File "D:\Anaconda\envs\tensorflow\lib\site-packages\gym\envs\classic_control\rendering.py", line 104, in render
    self.window.flip()

  File "D:\Anaconda\envs\tensorflow\lib\site-packages\pyglet\window\win32\__init__.py", line 311, in flip
    self.context.flip()

AttributeError: 'NoneType' object has no attribute 'flip'

顺便说一句,这是在 python 3.6 中。

4

1 回答 1

0

我最近也遇到了这个问题。你应该在最后添加“env.render(close=True)”到你的代码中,问题就会得到解决。

于 2018-02-11T06:42:01.507 回答