1. 安装Mujoco
1.1 官网下载Mujoco210安装包
Mujoco2.1.0下载链接
选第一个
1.2 创建文件夹并解压安装包
mkdir ~/.mujoco
创建好后,点击显示隐藏文件可以找到
找到刚刚下载的压缩包所在位置(一般在下载目录下),右键选择 在终端打开
tar -zxvf mujoco210-linux-x86_64.tar.gz -C ~/.mujoco
1.3 设置环境变量
gedit ~/.bashrc
在最后一行加入下面代码然后保存退出文档
export LD_LIBRARY_PATH=~/.mujoco/mujoco210/bin
更新环境变量
source ~/.bashrc
这就安装完了。
1.4 测试Mujoco
cd ~/.mujoco/mujoco210/bin
./simulate ../model/humanoid.xml
出现上图的界面,则mujoco安装成功。
2. 安装mujoco-py
2.1 创建虚拟环境
conda create -n ttmujoco python=3.8
conda activate ttmujoco
这里注意python版本不宜太低
2.2 下载mujoco-py安装包
确保在刚刚创建的虚拟环境中,输入
git clone https://github.com/openai/mujoco-py.git
2.3 然后依次执行下面的命令
cd ~/mujoco-py #注意换成你自己路径
pip3 install -U 'mujoco-py<2.2,>=2.1'
pip3 install -r requirements.txt
pip3 install -r requirements.dev.txt
python3 setup.py install
2.4 配置环境文件
gedit ~/.bashrc
在最后加上这三句
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/nvidia
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/XXX/.mujoco/mujoco210/bin
# XXX 是你的用户名
export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libGLEW.so
更新配置
source ~/.bashrc
2.5 测试mujoco-py
2.5.1测试1
在pycharm中新建一个python文件并使用前面刚刚创建的环境(ttmujoco)
输入以下代码
import mujoco_py
import os
mj_path = mujoco_py.utils.discover_mujoco()
xml_path = os.path.join(mj_path, 'model', 'humanoid.xml')
model = mujoco_py.load_model_from_path(xml_path)
sim = mujoco_py.MjSim(model)
print(sim.data.qpos)
# [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
sim.step()
print(sim.data.qpos)
# [-2.09531783e-19 2.72130735e-05 6.14480786e-22 -3.45474715e-06
# 7.42993721e-06 -1.40711141e-04 -3.04253586e-04 -2.07559344e-04
# 8.50646247e-05 -3.45474715e-06 7.42993721e-06 -1.40711141e-04
# -3.04253586e-04 -2.07559344e-04 -8.50646247e-05 1.11317030e-04
# -7.03465386e-05 -2.22862221e-05 -1.11317030e-04 7.03465386e-05
# -2.22862221e-05]
这个时候可能就要报错了
错误1:
Exception:
Missing path to your environment variable.
Current values LD_LIBRARY_PATH=
Please add following line to .bashrc:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/XXX/.mujoco/mujoco210/bin
或者
ERROR: GLEW initalization error: Missing GL version
这两个解决方案同理,只是具体的环境变量名称不一样
错误1的环境变量是:export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/wenjingwu/.mujoco/mujoco210/bin
错误2的环境变量是:export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libGLEW.so
解决方案1:检查2.4是否把环境变量写进去了,没写的话要加进去
解决方案2:右键选择“修改运行配置” ,在环境变量这里把提示你少的这个环境变量加进去
然后发现还是不行..
解决方案3: 关闭pycharm和终端,找到pycharm.sh所在位置,右键然后选择在终端打开,然后输入
./pycharm.sh
参考这里的解决方案,因为我每次都是直接点击桌面图标进入pycharm,好像并没有解决问题,尝试了一下从终端进入,瞬间就好起来了!
最后这样的输出结果就是成功了。
错误2:
Exception check on 'c_warning_callback' will always require the GIL to be acquired.
Possible solutions:
1. Declare 'c_warning_callback' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions.
2. Use an 'int' return type on 'c_warning_callback' to allow an error code to be returned.
performance hint: /home/wenjingwu/anaconda3/envs/rl_ur5/lib/python3.12/site-packages/mujoco_py/cymj.pyx:104:5: Exception check on 'c_error_callback' will always require the GIL to be acquired.
Possible solutions:
1. Declare 'c_error_callback' as 'noexcept' if you control the definition and you're sure you don't want the function to raise exceptions.
2. Use an 'int' return type on 'c_error_callback' to allow an error code to be returned.
Error compiling Cython file:
...
See c_warning_callback, which is the C wrapper to the user defined function
'''
global py_warning_callback
global mju_user_warning
py_warning_callback = warn
mju_user_warning = c_warning_callback
^
解决:更改cython版本
pip install cython==3.0.0a10
2.5.2 测试2
下面再试一下文件中自带的例子
首先进入创建的虚拟环境中
conda activate ttmujoco
切换文件夹
cd ./mujoco-py/examples
python body_interaction.py
尝试用pycharm打开运行也是没问题的
3. 安装gym
3.1 先进入自己创建的虚拟环境
conda activate ttmujoco
3.2 切换到.mujoco文件夹
cd ~/.mujoco/
3.3 下载gym安装包
git clone https://github.com/openai/gym
3.4 切换到gym文件夹
cd gym
3.5 安装
pip install -e '.[all]'
3.6 报错解决
错误1:error: subprocess-exited-with-error
解决:
pip uninstall setuptools
pip install setuptools==69.0.0
pip install -e '.[all]'
错误2:error: command 'swig' failed: No such file or directory
解决:
sudo apt install swig
pip install -e '.[all]'
3.7 配置环境变量
gedit ~/.bashrc
在最后加上
export PYTHONPATH=~/.mujoco/gym:$PYTHONPATH
更新一下
source ~/.bashrc
完成!
3.8 测试
试了好几篇文章的测试代码都报错,最后终于在这里找到了答案。
直接把2.5.1中测试的代码注释掉,换成下面的代码就可以。
3.8.1 代码1
import gym
env = gym.make('MountainCar-v0', render_mode = 'human')
for i_episode in range(10):
observation = env.reset()
for t in range(100):
env.render()
print(observation)
action = env.action_space.sample()
observation, reward, done, info, _ = env.step(action)
if done:
print("Episode finished after {} timesteps".format(t+1))
break
env.close()
3.8.2 代码2
import gym
env = gym.make('CartPole-v1', render_mode = "human")
for episode in range(10):
env.reset()
print("Episode finished after {} timesteps".format(episode))
for _ in range(100):
env.render()
env.step(env.action_space.sample())
env.close()
版权归原作者 wjwuyh 所有, 如有侵权,请联系我们删除。