我在 env 渲染中遇到错误 – env.render()

原文标题I’m getting an error with env rendering – env.render()

我在渲染时尝试实现 Atari Games,但出现如下错误:

line 283, in render
    raise error.Error(
gym.error.Error: render(mode='human') is deprecated. Please supply `render_mode` when constructing your environment, e.g., gym.make(ID, render_mode='human'). The new `render_mode` keyword argument supports DPI scaling, audio, and native framerates.

我得到的完整错误的屏幕截图附在此处。

我的代码如下:

episodes = 5

for episode in range(1, episodes+1):
state = env.reset()
done = False
score = 0 

while not done:
    env.render()
    action = random.choice([0,1,2,3,4,5])
    n_state, reward, done, info = env.step(action)
    score+=reward
print('Episode:{} Score:{}'.format(episode, score))
env.close()

堆栈溢出中有一个类似的帖子,其中给出的解决方案是:

pip install pyglet==1.2.4

不幸的是,这对我也不起作用。

原文链接:https://stackoverflow.com//questions/71977704/im-getting-an-error-with-env-rendering-env-render

回复

共1条回复 我来回复
  • Natasha的头像
    Natasha 评论

    我自己想通了。解决方案是通过更新render_mode='human'inenv来改变我们正在工作的环境:

    env = gym.make('SpaceInvaders-v0', render_mode='human')
    

    完整代码:

     import gym
     import random
     import time
     env = gym.make('SpaceInvaders-v0', render_mode='human')
     height, width, channels = env.observation_space.shape
     actions = env.action_space.n
     env.unwrapped.get_action_meanings()
     episodes = 5
     for episode in range(1, episodes+1):
        state = env.reset()
        done = False
        score = 0 
     while not done:
        time.sleep(0.1)
        action = random.choice([0,1,2,3,4,5])
        n_state, reward, done, info = env.step(action)       
        score+=reward
    print('Episode:{} Score:{}'.format(episode, score))
    env.close()
    
    1年前 0条评论