OpenDuckMini强化学习框架入门教程-奖励系统
纠错,疑问,交流: 请进入讨论区或 请点击进入页面,扫码加入微信群或Q群进行交流
获取最新文章: 扫一扫加入“创客智造”公众号
欢迎加入我们的openduckmini交流群,微信扫描右侧二维码立即进群交流
奖励系统
- 理解奖励系统,包含奖励结构,奖励分类,奖励组合模式,奖励函数详解,模仿奖励和奖励配置等
概述
奖励系统是强化学习的核心引导机制。Open Duck Playground 在 playground/common/rewards.py 中定义了一系列通用奖励函数和代价函数,可任意组合用于不同的训练任务。奖励系统设计注重可组合性——每个函数都独立、小巧、可测试,通过 scales 权重配置灵活调整。
奖励结构
┌─────────────────────────────────────────────────────────────┐
│ 奖励系统架构 │
│ │
│ 共享奖励 (rewards.py) 机器人特定奖励 │
│ ┌─────────────────────┐ ┌──────────────────────────┐ │
│ │ 速度跟踪: │ │ 模仿奖励 (custom_rewards │ │
│ │ reward_tracking_ │ │ .py): │ │
│ │ lin/ang_vel │ │ reward_imitation() │ │
│ ├─────────────────────┤ └──────────────────────────┘ │
│ │ 能量优化: │ │
│ │ cost_torques() │ │
│ │ cost_energy() │ │
│ ├─────────────────────┤ │
│ │ 姿态相关: │ │
│ │ cost_orientation() │ │
│ │ cost_stand_still() │ │
│ │ cost_pose() │ │
│ ├─────────────────────┤ │
│ │ 动作相关: │ │
│ │ cost_action_rate() │ │
│ │ reward_alive() │ │
│ ├─────────────────────┤ │
│ │ 足部相关: │ │
│ │ reward_feet_air_ │ │
│ │ time() │ │
│ │ cost_feet_height() │ │
│ └─────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
奖励分类
跟踪奖励 (主要奖励)
鼓励机器人按照命令速度运动:
| 函数 | 方向 | 公式 |
|---|---|---|
reward_tracking_lin_vel |
前进/横向 | exp(-error²/sigma) |
reward_tracking_ang_vel |
旋转 | exp(-error²/sigma) |
能量代价 (效率优化)
惩罚不必要的能量消耗:
| 函数 | 惩罚对象 |
|---|---|
cost_torques |
关节力矩平方和 |
cost_energy |
力矩 × 速度(机械功) |
姿态代价 (保持直立)
鼓励机器人保持良好的姿态:
| 函数 | 说明 |
|---|---|
cost_orientation |
躯干 Z 轴水平分量惩罚 |
cost_stand_still |
静止时偏离默认姿态的惩罚 |
cost_pose |
加权关节位置偏差惩罚 |
动作代价 (平滑控制)
鼓励平滑、自然的动作:
| 函数 | 说明 |
|---|---|
cost_action_rate |
相邻动作差的平方(防止抖动) |
cost_joint_pos_limits |
关节超出软限制的惩罚 |
足部奖励 (步态优化)
优化足部运动轨迹:
| 函数 | 说明 |
|---|---|
reward_feet_air_time |
足部腾空时间奖励 |
cost_feet_height |
足部高度峰值误差 |
cost_feet_slip |
足部打滑惩罚 |
cost_feet_clearance |
足部离地间隙惩罚 |
存活奖励
简单但重要的生存激励:
def reward_alive() -> jax.Array:
return jp.array(1.0) # 每存活一步 +1.0
奖励组合模式
Joystick 环境
reward = (
+2.5 * tracking_lin_vel # 速度跟踪
+6.0 * tracking_ang_vel # 角速度跟踪
-0.001 * torque_cost # 力矩代价
-0.5 * action_rate_cost # 动作平滑
+20.0 * alive # 存活
-0.2 * stand_still_cost # 静止惩罚
+1.0 * imitation # 模仿奖励(可选)
) * dt
Standing 环境
reward = (
-0.5 * orientation_cost # 姿态保持
-0.001 * torque_cost # 力矩代价
-0.375 * action_rate_cost # 动作平滑
+20.0 * alive # 存活
-0.3 * stand_still_cost # 姿态维持
-2.0 * head_pos_cost # 头部位置跟踪
) * dt
奖励函数详解
reward_tracking_lin_vel — 线速度跟踪
def reward_tracking_lin_vel(commands, local_vel, tracking_sigma):
# x 方向严格跟踪
error_x = square(commands[0] - local_vel[0])
# y 方向允许 ±0.1 容差
error_y = clip(abs(local_vel[1] - commands[1]) - 0.1, 0.0, None)
lin_vel_error = error_x + square(error_y)
return exp(-lin_vel_error / tracking_sigma)
y 方向的容差机制避免了精确跟踪横向速度导致的过多侧滑。
cost_orientation — 姿态代价
def cost_orientation(torso_zaxis):
return sum(square(torso_zaxis[:2]))
当机器人完全直立时,躯干 Z 轴水平分量为零,代价最小。
cost_stand_still — 静止代价
def cost_stand_still(commands, qpos, qvel, default_pose, ignore_head=False):
cmd_norm = norm(commands[:3]) # 命令速度大小
# 仅当命令为零时惩罚(即需要站定时)
pose_cost = sum(abs(qpos - default_pose))
vel_cost = sum(abs(qvel))
return (pose_cost + vel_cost) * (cmd_norm < 0.01)
cost_head_pos — 头部位置代价
def cost_head_pos(joints_qpos, joints_qvel, cmd):
cmd_norm = norm(cmd[:3])
head_cmd = cmd[3:] # 头部目标位置
head_pos = joints_qpos[5:9] # 实际头部位置
head_pos_error = sum(square(head_pos - head_cmd))
return head_pos_error * (cmd_norm > 0.01)
仅当运动命令非零时惩罚——即只在需要移动时才控制头部。
模仿奖励
模仿奖励(见 custom_rewards.py)提供基于参考运动的奖励,鼓励机器人模仿预定义的步态模式:
def reward_imitation(base_qpos, base_qvel, joints_qpos, joints_qvel,
contacts, reference_frame, cmd, use_imitation):
if not use_imitation:
return 0.0
# 对比机器人与参考运动的多个维度
lin_vel_xy_rew = exp(-8 * sum(square(vel_xy - ref_vel_xy)))
joint_pos_rew = -sum(square(joint_pos - ref_joint_pos)) * 15.0
contact_rew = sum(contacts == ref_contacts)
# ...
reward *= norm(cmd[:3]) > 0.01 # 仅在运动时施加
奖励配置
奖励权重在环境的 default_config() 中定义:
reward_config=config_dict.create(
scales=config_dict.create(
tracking_lin_vel=2.5,
tracking_ang_vel=6.0,
torques=-1.0e-3,
action_rate=-0.5,
stand_still=-0.2,
alive=20.0,
imitation=1.0,
),
tracking_sigma=0.01,
)
正权重表示奖励(最大化),负权重表示代价(最小化)。
纠错,疑问,交流: 请进入讨论区或 请点击进入页面,扫码加入微信群或Q群进行交流
获取最新文章: 扫一扫加入“创客智造”公众号
欢迎加入我们的openduckmini交流群,微信扫描右侧二维码立即进群交流


















