我刚刚开始学习
OpenAI gymnasium
并从CartPole-v1
开始。video:https://www.youtube.com/watch?v=Mut_u40Sqz4&t=2076s
Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/gymnasium/envs/classic_control/cartpole.py", line 223, in render
import pygame ModuleNotFoundError: No module named 'pygame'
The above exception was the direct cause of the following exception:
Traceback (most recent call last): File "/Users/artemnikoan/Documents/CartPole-v1.py", line 9, in <module>
state = env.reset() File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/gymnasium/wrappers/time_limit.py", line 75, in reset
return self.env.reset(**kwargs) File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/gymnasium/wrappers/order_enforcing.py", line 61, in reset
return self.env.reset(**kwargs) File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/gymnasium/wrappers/env_checker.py", line 57, in reset
return env_reset_passive_checker(self.env, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/gymnasium/utils/passive_env_checker.py", line 186, in env_reset_passive_checker
result = env.reset(**kwargs) File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/gymnasium/envs/classic_control/cartpole.py", line 209, in reset
self.render() File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/gymnasium/envs/classic_control/cartpole.py", line 226, in render
raise DependencyNotInstalled( gymnasium.error.DependencyNotInstalled: pygame is not installed, run `pip install gymnasium[classic-control]`
我确定安装了
pygame
和gymnasium classic-control
,但仍然不起作用。 这是代码:
import os
import gymnasium as gym
from stable_baselines3 import PPO
from stable_baselines3.common.vec_env import DummyVecEnv
from stable_baselines3.common.evaluation import evaluate_policy
env=gym.make('CartPole-v1',render_mode='human')
episodes = 5
for episode in range(1, episodes+1):
state = env.reset()
done = False
score = 0
while not done:
env.render()
action = env.action_space.sample()
n_state, reward, done, truncated, info = env.step(action)
score+=reward
print('Episode:{} Score:{}'.format(episode, score))
env.close()
log_path=os.path.join('Training', 'Logs')
env = gym.make(environment_name)
env = DummyVecEnv([lambda: env])
model = PPO('MlpPolicy', env, verbose = 1)
model.learn(total_timesteps=10000)
PPO_path = os.path.join('Training', 'Saved Models', 'PPO_model')
model.save(PPO_path)
model = PPO.load(PPO_path, env=env)
evaluate_policy(model, env, n_eval_episodes=10, render=True)
我正在使用
MAC PC
来处理 IDLE
。 Pytorch
和所有其他必需的模块。您尚未安装
pygame
。错误消息已经表明您必须执行以下操作:pip install gymnasium[classic-control]
。
考虑到 OG 中提供的视频是 2021 年 6 月 6 日开始的,Gym 版本(不是 Gymnasium)必须为 0.18.3 或更低版本。在较新的 Gym 版本中,语法发生了很大变化,这会导致许多错误,就像您所经历的那样。可以肯定的是,提供的视频不使用 Gymnasium。
因此,我还建议将 Gym 降级到 <= 0.18.3. I could not find out from the video what version they used, but it must be in aforementioned range. Or, you could make yourself familiar with Gymnasium 版本或较新版本的 Gym 来重写部分代码(因为 Stable Baselines 3 确实适用于任一版本)。
可能有帮助:
尝试
pip install 'gymnasium[classic-control]'
如果您使用 zsh / macOS