今天回去重新跑gym的cartPole用例,更新了包的版本后出现错误
python 版本:Python 3.9.13
gym版本:gym 0.26.2
网上看到的代码块:
import gym # 导入 Gym 的 Python 接口环境包
env = gym.make('CartPole-v0') # 构建实验环境
env.reset() # 重置一个回合
for _ in range(1000):
env.render() # 显示图形界面
action = env.action_space.sample() # 从动作空间中随机选取一个动作
env.step(action) # 用于提交动作,括号内是具体的动作
env.close() # 关闭环境
会出现三个错误,修改后代码
env = gym.make('CartPole-v1', render_mode="human")
for episode in range(10):
env.reset()
print("Episode finished after {} timesteps".format(episode))
for ik in range(100):
env.render()
observation, reward, done, info, _ = env.step(env.action_space.sample())
if done:
break
time.sleep(0.02)
env.close()
第一个错误:
UserWarning: WARN: The environment CartPole-v0 is out of date. You should consider upgrading to version `v1`.
解决办法:
'CartPole-v0' 改成:'CartPole-v1'
第二个错误:
UserWarning: WARN: You are calling render method without specifying any render mode. You can specify the render_mode at initialization, e.g. gym("CartPole-v0", render_mode="rgb_array")
解决办法:
这句 :env = gym.make('CartPole-v0') # 构建实验环境
改成:
env = gym.make('CartPole-v1', render_mode="human")
第三个错误:
UserWarning: WARN: You are calling 'step()' even though this environment has already returned terminated = True. You should always call 'reset()' once you receive 'terminated = True' -- any further steps are undefined behavior.
done 等于ture的时候,还在step,加上一个判断条件就行
改为
observation, reward, done, info, _ = env.step(env.action_space.sample())
if done:
break