测试gym的cartPole 出错后的更改

该代码段展示了如何使用Gym库创建和交互CartPole环境。首先导入Gym库,然后创建CartPole-v1环境并设置渲染模式为human。在每个episode中,环境被重置,然后随机选取动作并执行,直到环境结束。修正后的代码处理了渲染和环境结束的逻辑,并添加了短暂延迟以控制展示速度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天回去重新跑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

(py37) C:\Users\wang\Desktop>pip install "gym[all]==0.21.0" Requirement already satisfied: gym[all]==0.21.0 in d:\anaconda\envs\py37\lib\site-packages (0.21.0) Requirement already satisfied: numpy>=1.18.0 in d:\anaconda\envs\py37\lib\site-packages (from gym[all]==0.21.0) (1.21.6) Requirement already satisfied: importlib-metadata>=4.8.1 in d:\anaconda\envs\py37\lib\site-packages (from gym[all]==0.21.0) (6.7.0) Requirement already satisfied: cloudpickle>=1.2.0 in d:\anaconda\envs\py37\lib\site-packages (from gym[all]==0.21.0) (2.2.1) Requirement already satisfied: mujoco-py<2.0,>=1.50 in d:\anaconda\envs\py37\lib\site-packages\mujoco_py-1.50.1.0-py3.7.egg (from gym[all]==0.21.0) (1.50.1.0) Collecting lz4>=3.1.0 Downloading lz4-4.3.2-cp37-cp37m-win_amd64.whl (99 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 99.9/99.9 kB 146.9 kB/s eta 0:00:00 Collecting box2d-py==2.3.5 Downloading box2d-py-2.3.5.tar.gz (374 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 374.4/374.4 kB 60.1 kB/s eta 0:00:00 Preparing metadata (setup.py) ... done Collecting ale-py~=0.7.1 Downloading ale_py-0.7.5-cp37-cp37m-win_amd64.whl (935 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 935.3/935.3 kB 44.9 kB/s eta 0:00:00 Collecting opencv-python>=3. Downloading opencv_python-4.11.0.86-cp37-abi3-win_amd64.whl (39.5 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 39.5/39.5 MB 73.5 kB/s eta 0:00:00 Requirement already satisfied: scipy>=1.4.1 in d:\anaconda\envs\py37\lib\site-packages (from gym[all]==0.21.0) (1.7.3) Collecting pyglet>=1.4.0 Downloading pyglet-2.0.10-py3-none-any.whl (858 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 858.3/858.3 kB 67.6 kB/s eta 0:00:00 Collecting importlib-resources Downloading importlib_resources-5.12.0-py3-none-any.whl (36 kB) Requirement already satisfied: typing-extensions>=3.6.4 in d:\anaconda\envs\py37\lib\site-packages (from importlib-metadata>=4.8.1->gym[all]==0.21.0) (4.7.1) Requirement already satisfied: zipp>=0.5 in d:\anaconda\env
04-01
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值