import gymnasium as gym
import numpy as np
import time
from datetime import datetime
from stable_baselines3 import DQN
from stable_baselines3.dqn import CnnPolicy
from stable_baselines3.dqn import MlpPolicy
game="ALE/Pong-v5"
eval=True
cont=False
print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()))
start_time=time.time()
start_date=datetime.now()
print("------0 ")
if eval:
env=gym.make(game,render_mode="human")
else:
env=gym.make(game,render_mode="rgb_array")
print(" ---1")
save_file="dqn_"+game
print(env.action_space)
print(" ---2")
if eval:
model=DQN.load(save_file)
model.set_env(env)
else:
if cont:
model=DQN.load(save_file)
else:
model=DQN(CnnPolicy,env,verbose=1,exploration_final_eps=0.01,exploration_fraction=0.1,gradient_steps=1,learning_rate=0.0001,buffer_size=10000)
model.set_env(env)
model.learn(total_timesteps=1000000,log_interval=10)
model.save(save_file)
print(" ---3")
obs,info=env.reset()
score=0
rewards_sum=0
while True:
action,_states=model.predict(obs)
obs,reward,terminated,truncated, info = env.step(action)
if eval:
env.render()
score=score+1
rewards_sum+=reward
if reward > 0:
print("Win!",reward)
if terminated or truncated:
print("finished! ",score)
print("reward sum= ",rewards_sum)
break
duration=time.time()-start_time
print("duration= ",duration)
time_cost=datetime.now() - start_date
print("time cost= ",time_cost)
上述代码在运行过程中报错了,而且比较复杂
2024-09-05 14:41:13
A.L.E: Arcade Learning Environment (version 0.8.1+53f58b7)
[Powered by Stella]
MESA-LOADER: failed to open iris: /usr/lib/dri/iris_dri.so: cannot open shared object file: No such file or directory (search paths /usr/lib/x86_64-linux-gnu/dri:$${ORIGIN}/dri:/usr/lib/dri, suffix _dri)
failed to load driver: iris
MESA-LOADER: failed to open iris: /usr/lib/dri/iris_dri.so: cannot open shared object file: No such file or directory (search paths /usr/lib/x86_64-linux-gnu/dri:$${ORIGIN}/dri:/usr/lib/dri, suffix _dri)
failed to load driver: iris
MESA-LOADER: failed to open swrast: /usr/lib/dri/swrast_dri.so: cannot open shared object file: No such file or directory (search paths /usr/lib/x86_64-linux-gnu/dri:$${ORIGIN}/dri:/usr/lib/dri, suffix _dri)
X Error of failed request: BadValue (integer parameter out of range for operation)
Major opcode of failed request: 152 (GLX)
Minor opcode of failed request: 3 (X_GLXCreateContext)
Value in failed request: 0x0
Serial number of failed request: 96
Current serial number in output stream: 97
参考了网上的文章 https://blog.csdn.net/weixin_43686259/article/details/141135736
mkdir -p /usr/lib/dri
sudo cp /lib/x86_64-linux-gnu/dri/iris_dri.so /usr/lib/dri
sudo cp /lib/x86_64-linux-gnu/dri/swrast_dri.so /usr/lib/dri
重新运行之后发现仍然报错,类似如下内容:
(py310) root@st:/appSource/dqn# python dqnPong2.py
2024-09-05 15:42:58
——0
A.L.E: Arcade Learning Environment (version 0.8.1+53f58b7)
[Powered by Stella]
MESA-LOADER: failed to open iris: /root/anaconda3/envs/py310/bin/../lib/libstdc++.so.6: version GLIBCXX_3.4.30′ not found (required by /lib/x86_64-linux-gnu/libLLVM-17.so.1) (search paths /usr/lib/x86_64-linux-gnu/dri:\$${ORIGIN}/dri:/usr/lib/dri, suffix _dri) failed to load driver: iris MESA-LOADER: failed to open iris: /root/anaconda3/envs/py310/bin/../lib/libstdc++.so.6: version GLIBCXX_3.4.30′ not found (required by /lib/x86_64-linux-gnu/libLLVM-17.so.1) (search paths /usr/lib/x86_64-linux-gnu/dri:$${ORIGIN}/dri:/usr/lib/dri, suffix _dri)
failed to load driver: iris
MESA-LOADER: failed to open swrast: /root/anaconda3/envs/py310/bin/../lib/libstdc++.so.6: version `GLIBCXX_3.4.30′ not found (required by /lib/x86_64-linux-gnu/libLLVM-17.so.1) (search paths /usr/lib/x86_64-linux-gnu/dri:$${ORIGIN}/dri:/usr/lib/dri, suffix _dri)
X Error of failed request: BadValue (integer parameter out of range for operation)
Major opcode of failed request: 152 (GLX)
Minor opcode of failed request: 3 (X_GLXCreateContext)
Value in failed request: 0x0
Serial number of failed request: 96
Current serial number in output stream: 97
对此需要做出如下修改:
cd /root/anaconda3/envs/py310/lib/
mkdir backup
mv libstdc++.so* backup/
cp /usr/lib/x86_64-linux-gnu/libstdc++.so.6 ./
ln -s libstdc++.so.6 libstdc++.so
ln -s libstdc++.so.6 libstdc++.so.6.0.29
上面的代码不要直接照抄,需要根据系统实际情况进行修改,例如第一行cd /root/anaconda3/envs/py310/lib/ 需要根据系统中conda环境的名字进行修改,我这里是py310
再例如最后一句 ln -s libstdc++.so.6 libstdc++.so.6.0.29 其中的6.0.29是看到 backup中是这个版本,所以这里照着写了这个版本
根据上述修改之后,就能够顺利运行了。
实际上,上述问题是因为 env=gym.make(game,render_mode=”human”) 这一句话导致的。