加载自定义四足机器人环境¶
在本教程中,我们将了解如何使用 MuJoCo/Ant-v5
框架创建四足步行环境,使用模型文件(以 .xml
结尾)而不必创建新的类。
步骤
获取您的 **MJCF**(或 **URDF**)机器人模型文件。
创建您自己的模型(参见 指南)或,
找到现成的模型(在本教程中,我们将使用 MuJoCo Menagerie 集合中的模型)。
使用
xml_file
参数加载模型。调整环境参数以获得所需的行为。
调整环境模拟参数。
调整环境终止参数。
调整环境奖励参数。
调整环境观察参数。
训练一个代理来移动你的机器人。
预计读者熟悉 Gymnasium
API 和库、机器人学基础知识以及包含的 Gymnasium/MuJoCo
环境及其使用的机器人模型。熟悉 **MJCF** 文件模型格式和 MuJoCo
模拟器不是必需的,但建议了解。
设置¶
我们需要 gymnasium>=1.0.0
。
pip install "gymnasium>=1.0.0"
步骤 0.1 - 下载机器人模型¶
在本教程中,我们将从优秀的 MuJoCo Menagerie 机器人模型集合中加载 Unitree Go1 机器人。
Go1
是一款四足机器人,控制它移动是一个重大的学习问题,比 Gymnasium/MuJoCo/Ant
环境要难得多。
我们可以下载整个 MuJoCo Menagerie 集合(包括 Go1
),
git clone https://github.com/google-deepmind/mujoco_menagerie.git
您可以使用任何其他四足机器人来完成本教程,只需根据您的机器人调整环境参数值。
步骤 1 - 加载模型¶
要加载模型,我们所要做的就是使用 xml_file
参数与 Ant-v5
框架一起使用。
import gymnasium
import numpy as np
env = gymnasium.make('Ant-v5', xml_file='./mujoco_menagerie/unitree_go1/scene.xml')
虽然这足以加载模型,但我们需要调整一些环境参数以获得环境所需的行为,现在我们还将明确设置模拟、终止、奖励和观察参数,这些参数将在下一步进行调整。
env = gymnasium.make(
'Ant-v5',
xml_file='./mujoco_menagerie/unitree_go1/scene.xml',
forward_reward_weight=0,
ctrl_cost_weight=0,
contact_cost_weight=0,
healthy_reward=0,
main_body=1,
healthy_z_range=(0, np.inf),
include_cfrc_ext_in_observation=True,
exclude_current_positions_from_observation=False,
reset_noise_scale=0,
frame_skip=1,
max_episode_steps=1000,
)
步骤 2 - 调整环境参数¶
调整环境参数对于获得学习所需的行为至关重要。在以下小节中,鼓励读者咨询 参数文档 以获得更详细的信息。
步骤 2.1 - 调整环境模拟参数¶
感兴趣的参数是 frame_skip
、reset_noise_scale
和 max_episode_steps
。
我们希望调整 frame_skip
参数以使 dt
达到可接受的值(典型值为 dt
\(\in [0.01, 0.1]\) 秒),
提醒:\(dt = frame\_skip \times model.opt.timestep\),其中 model.opt.timestep
是在 MJCF 模型文件中选择的积分器时间步长。
我们正在使用的 Go1
模型的积分器时间步长为 0.002
,因此通过选择 frame_skip=25
,我们可以将 dt
的值设置为 0.05s
。
为了避免策略过度拟合,reset_noise_scale
应该设置为适合机器人大小的值,我们希望该值尽可能大,但不能使初始状态分布无效(Terminal
无论控制动作如何),对于 Go1
,我们选择的值为 0.1
。
而 max_episode_steps
决定了每集在 truncation
之前执行的步数,这里我们将其设置为 1000 以与基于 Gymnasium/MuJoCo
的环境保持一致,但如果需要更高的值,可以进行设置。
env = gymnasium.make(
'Ant-v5',
xml_file='./mujoco_menagerie/unitree_go1/scene.xml',
forward_reward_weight=0,
ctrl_cost_weight=0,
contact_cost_weight=0,
healthy_reward=0,
main_body=1,
healthy_z_range=(0, np.inf),
include_cfrc_ext_in_observation=True,
exclude_current_positions_from_observation=False,
reset_noise_scale=0.1, # set to avoid policy overfitting
frame_skip=25, # set dt=0.05
max_episode_steps=1000, # kept at 1000
)
步骤 2.2 - 调整环境终止参数¶
终止对于机器人环境很重要,可以避免采样“无用”的时间步长。
感兴趣的参数是 terminate_when_unhealthy
和 healthy_z_range
。
我们希望将 healthy_z_range
设置为在机器人摔倒或跳得很高时终止环境,这里我们必须选择一个对机器人高度有逻辑意义的值,对于 Go1
,我们选择 (0.195, 0.75)
。注意:healthy_z_range
检查机器人的高度的绝对值,因此如果您的场景包含不同高度的隆起,则应将其设置为 (-np.inf, np.inf)
我们也可以将 terminate_when_unhealthy=False
设置为完全禁用终止,这在 Go1
的情况下不可取。
env = gymnasium.make(
'Ant-v5',
xml_file='./mujoco_menagerie/unitree_go1/scene.xml',
forward_reward_weight=0,
ctrl_cost_weight=0,
contact_cost_weight=0,
healthy_reward=0,
main_body=1,
healthy_z_range=(0.195, 0.75), # set to avoid sampling steps where the robot has fallen or jumped too high
include_cfrc_ext_in_observation=True,
exclude_current_positions_from_observation=False,
reset_noise_scale=0.1,
frame_skip=25,
max_episode_steps=1000,
)
注意:如果您需要不同的终止条件,可以编写自己的 TerminationWrapper
(参见 文档)。
步骤 2.3 - 调整环境奖励参数¶
感兴趣的参数是 forward_reward_weight
、ctrl_cost_weight
、contact_cost_weight
、healthy_reward
和 main_body
。
对于参数 forward_reward_weight
、ctrl_cost_weight
、contact_cost_weight
和 healthy_reward
,我们必须选择对机器人有意义的值,您可以使用默认的 MuJoCo/Ant
参数作为参考,并在需要更改时进行调整。在 Go1
的情况下,我们只更改了 ctrl_cost_weight
,因为它具有更高的执行器力范围。
对于参数 main_body
,我们需要选择哪个身体部位作为主躯干(通常在模型文件中被称为“躯干”或“躯体”)来计算 forward_reward
。对于 Go1
,它就是 "trunk"
(注意:在大多数情况下,包括这个情况,它可以保留默认值)。
env = gymnasium.make(
'Ant-v5',
xml_file='./mujoco_menagerie/unitree_go1/scene.xml',
forward_reward_weight=1, # kept the same as the 'Ant' environment
ctrl_cost_weight=0.05, # changed because of the stronger motors of `Go1`
contact_cost_weight=5e-4, # kept the same as the 'Ant' environment
healthy_reward=1, # kept the same as the 'Ant' environment
main_body=1, # represents the "trunk" of the `Go1` robot
healthy_z_range=(0.195, 0.75),
include_cfrc_ext_in_observation=True,
exclude_current_positions_from_observation=False,
reset_noise_scale=0.1,
frame_skip=25,
max_episode_steps=1000,
)
注意:如果您需要不同的奖励函数,您可以编写自己的 RewardWrapper
(参见 文档)。
步骤 2.4 - 微调环境观测参数¶
感兴趣的参数是 include_cfrc_ext_in_observation
和 exclude_current_positions_from_observation
。
对于 Go1
,我们没有特别的理由去改变它们。
env = gymnasium.make(
'Ant-v5',
xml_file='./mujoco_menagerie/unitree_go1/scene.xml',
forward_reward_weight=1,
ctrl_cost_weight=0.05,
contact_cost_weight=5e-4,
healthy_reward=1,
main_body=1,
healthy_z_range=(0.195, 0.75),
include_cfrc_ext_in_observation=True, # kept the game as the 'Ant' environment
exclude_current_positions_from_observation=False, # kept the game as the 'Ant' environment
reset_noise_scale=0.1,
frame_skip=25,
max_episode_steps=1000,
)
注意:如果您需要额外的观测元素(例如附加传感器),您可以编写自己的 ObservationWrapper
(参见 文档)。
步骤 3 - 训练您的智能体¶
最后,我们完成了,我们可以使用 RL 算法来训练一个智能体来让 Go1
机器人行走/奔跑。注意:如果您按照本指南使用自己的机器人模型,您可能会在训练过程中发现某些环境参数不是预期的,您可以随意回到步骤 2 并根据需要更改任何内容。
import gymnasium
env = gymnasium.make(
'Ant-v5',
xml_file='./mujoco_menagerie/unitree_go1/scene.xml',
forward_reward_weight=1,
ctrl_cost_weight=0.05,
contact_cost_weight=5e-4,
healthy_reward=1,
main_body=1,
healthy_z_range=(0.195, 0.75),
include_cfrc_ext_in_observation=True,
exclude_current_positions_from_observation=False,
reset_noise_scale=0.1,
frame_skip=25,
max_episode_steps=1000,
)
... # run your RL algorithm
结语¶
您可以按照本指南来创建大多数四足动物环境。要创建人形/两足动物机器人,您也可以使用 Gymnasium/MuJoCo/Humnaoid-v5
框架来遵循本指南。