安装gym
pip install gym[all]
这样安装会报错,因为在编译 box2d-py
这个环境时,缺少库
解决这个报错需要两步
安装swig
安装swig网址 https://www.swig.org/download.html
这个文件解压,然后放在C盘的任意目录下,比如 C:\Program Files
,那我们就会多个文件夹 C:\Program Files\swigwin-4.0.0
,我这里的版本是 4.0.0
。
然后将这个路径添加到系统PATH
安装Visual Studio
安装VS网址 https://visualstudio.microsoft.com/visual-cpp-build-tools/
先下载安装工具
然后双击安装就好了,最后选择C++桌面开发下载安装
测试
import gymnasium as gym
env = gym.make("LunarLander-v2", render_mode="human")
observation, info = env.reset(seed=42)
for _ in range(1000):
action = env.action_space.sample() # this is where you would insert your policy
observation, reward, terminated, truncated, info = env.step(action)
if terminated or truncated:
observation, info = env.reset()
env.close()
最后看到下面的画面就安装成功了