环境
window10+python3.8+pycharm2023
一、1.安装Box2d报错问题汇总
问题1:error: command ‘swig.exe’ failed: None
解决办法:
·升级pip
pip install --upgrade pip
pip install --upgrade setuptools
pip install ez_setup
·手动安装swig
pip install swig
问题2: error: Microsoft Visual C++ 14.0 or greater is required. Get it with “Microsoft C++ Build Tools”: https://visualstudio.microsoft.com/visual-cpp-build-tools/ error:Could not build wheels for box2d-py, which is required to install pyproject.toml-based projects
解决办法:下载安装最新vs,
·下载地址:
https://visualstudio.microsoft.com/visual-cpp-build-tools/
勾选三个生成工具(少了可能不成功),可以自定义安装地址
·安装成功后输入指令
pip install gymnasium[all]
成功
CartPole用例警告
1.第一个警告:
UserWarning: WARN: The environment CartPole-v0 is out of date. You should consider upgrading to version `v1`.
解决:将CartPole-v0改成CartPole-v1
2.第二个警告:
DeprecationWarning: `np.bool8` is a deprecated alias for `np.bool_`. (Deprecated NumPy 1.24) if not isinstance(terminated, (bool, np.bool8)):
解决:添加import numpy as np
3.第三个警告:
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.
解决:增加reset代码
4.第四个警告:
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")
解决:修改为gym(“CartPole-v1”, render_mode=“rgb_array”)
完整代码如下:
import time
import gym #导入gym的python接口环境包
import numpy as np
env = gym.make('CartPole-v1', render_mode="human")
state = env.reset()
for episode in range(10):
env.reset()
for t in range(100):
env.render()
print(state)
action = env.action_space.sample()
state, reward, done, info, _ = env.step(action)
if done:
print('Finished')
break
time.sleep(0.02)
env.close()
结果:成功