Mujoco目前已经开源了(google的deepmind收购之后),作为具身智能领域的出色仿真环境,这里记录一下学习它的过程.
首先要安装mujoco需要两个部分,一个是仿真环境本身,一个是它的python的api接口.分别是mujoco和mujoco
mujoco踩坑
1.下载mujoco
前往github的观望下载即可:https://github.com/google-deepmind/mujoco/releases,请下载最新的版本.
下载自己对应的版本即可.解压文件,然后添加一下环境变量LD_LIBRARY_PATH:
export LD_LIBRARY_PATH=/home/cyun/Downloads/apps/mujoco210/bin
测试一下mujoco,我们可以启动它的例程:在bin文件夹下启动simulate
./simulate ../model/humanoid.xml
很难想象一个几MB的文件就实现了这个过程.
2.安装mujoco-py
克隆一下源码:
由于mujoco-py不支持2.1.0之后的版本,所以最新版的请直接跳过这里!
git clone https://github.com/openai/mujoco-py.git
创建虚拟环境
conda create -n mujo python=3.8
conda activate mujo
进入py库并安装
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
如果是最新版的就不要使用这个了,详情见https://github.com/google-deepmind/mujoco/blob/main/python/README.md
但是这里也标注了python的版本,最低也是py3.9,所以重新创建一个python版本,推荐使用py11以下的.然后pip即可安装:
pip install mujoco
最终测试环节:
新建脚本,执行:
import time
import mujoco
import mujoco.viewer
m = mujoco.MjModel.from_xml_path('/home/cyun/Downloads/apps/mujoco-3.3.0/model/humanoid/humanoid.xml')
d = mujoco.MjData(m)
with mujoco.viewer.launch_passive(m, d) as viewer:
# Close the viewer automatically after 30 wall-seconds.
start = time.time()
while viewer.is_running() and time.time() - start < 30:
step_start = time.time()
# mj_step can be replaced with code that also evaluates
# a policy and applies a control signal before stepping the physics.
mujoco.mj_step(m, d)
# Example modification of a viewer option: toggle contact points every two seconds.
with viewer.lock():
viewer.opt.flags[mujoco.mjtVisFlag.mjVIS_CONTACTPOINT] = int(d.time % 2)
# Pick up changes to the physics state, apply perturbations, update options from GUI.
viewer.sync()
# Rudimentary time keeping, will drift relative to wall clock.
time_until_next_step = m.opt.timestep - (time.time() - step_start)
if time_until_next_step > 0:
time.sleep(time_until_next_step)```
实际安装了才发现,原来mujoco里面已经内嵌了仿真环境了,所以其实也不需要安装,直接通过pip安装就能一步到位了…看别人的安装过程其实也会有很强的误导作用
在py3.8中执行pip安装的话:
cyun@cyun:~$ pip show mujoco
Name: mujoco
Version: 3.2.3
Summary: MuJoCo Physics Simulator
Home-page: https://github.com/google-deepmind/mujoco
Author:
Author-email: Google DeepMind <mujoco@deepmind.com>
License: Apache License 2.0
Location: /home/cyun/.local/lib/python3.8/site-packages
Requires: absl-py, etils, glfw, numpy, pyopengl
Required-by:
最新的是3.2.3版本的
貌似也不低了,所以mujoco的安装过程其实安装了一个寂寞.
总结
被别人的文章误导了,只需要新建一个py3.8的环境mujoco,然后执行:
pip install mujoco
就安装完了,然后可以用官方的模型来测试:https://github.com/google-deepmind/mujoco/tree/main/model
随便找一个xml,然后使用py的api来运行:
import time
import mujoco
import mujoco.viewer
m = mujoco.MjModel.from_xml_path('humanoid.xml')
d = mujoco.MjData(m)
with mujoco.viewer.launch_passive(m, d) as viewer:
# Close the viewer automatically after 30 wall-seconds.
start = time.time()
while viewer.is_running() and time.time() - start < 30:
step_start = time.time()
# mj_step can be replaced with code that also evaluates
# a policy and applies a control signal before stepping the physics.
mujoco.mj_step(m, d)
# Example modification of a viewer option: toggle contact points every two seconds.
with viewer.lock():
viewer.opt.flags[mujoco.mjtVisFlag.mjVIS_CONTACTPOINT] = int(d.time % 2)
# Pick up changes to the physics state, apply perturbations, update options from GUI.
viewer.sync()
# Rudimentary time keeping, will drift relative to wall clock.
time_until_next_step = m.opt.timestep - (time.time() - step_start)
if time_until_next_step > 0:
time.sleep(time_until_next_step)
就这么简单的过程,非要整源码,整mujoco-py,是吃饱了撑的.