找回密码
 立即注册

QQ登录

只需一步,快速开始

本帖最后由 lou 于 2020-4-27 11:24 编辑

Geek专栏:Pybulet Gym 源码解析:双足机器人模型 HumanoidPyBulletEnv-v0



今天Geek专栏为大家带来
乐聚机器人王松博士的
“PybuletGym 源码解析:双足机器人模型 HumanoidPyBulletEnv-v0”

OpenAI gym 是当前使用最为广泛的用于研究强化学习的工具箱,但 Gym 的物理仿真环境使用的是 Mujoco,不开源且收费,这一点一直被人诟病。而 Pybullet-gym 是对 Openai Gym Mujoco 环境的开源实现,用于替代 Mujoco 做为强化学习的仿真环境。封装了 Pybullet 的接口,无缝的接入了 Gym 环境。
关于如何创建Gym 自定义环境可以参考上一期极客专栏《OpenAI Gym源码阅读:创建自定义强化学习环境》

示例代码

完整使用 HumanoidPyBulletEnv-v0 模型的示例代码,在pybulletgym/examples/https://github.com/benelot/pybul ... ybulletgym/examples)路径下可以找到。

  1. import gym
  2. import pybulletgym.envs
复制代码

当示例代码引入Pybullet-gym 库时,就完成了对 Pybullet 自定义 Gym 环境的注册。
根据OpenAI Gym 的文档,下面是使用随机策略,调用HumanoidPyBulletEnv-v0 的测试代码

  1. import gym
  2. import time
  3. import numpy as np
  4. import pybullet as p
  5. import pybulletgym.envs
  6. def main():
  7.    env = gym.make("HumanoidPyBulletEnv-v0")
  8.    env.render(mode="human")
  9.    for i_episode in range(20):
  10.         observation = env.reset()
  11.         for t in range(100):
  12.            env.render()
  13.             print(observation)
  14.             action = env.action_space.sample()
  15.             observation, reward, done, info = env.step(action)
  16.            if done:
  17.                 print("Episode finished after {} timesteps".format(t + 1))
  18.                 break
  19.      env.close()
  20.   if __name__ == '__main__':
  21.      main()
复制代码

动作与观测

首先查看 HumanoidPyBulletEnv-v0 运动空间和观测空间的维度大小

  1. 1  print(f'env.action_space.shape = {env.action_space.shape}')
  2. 2  print(f'env.observation_space.shape = {env.observation_space.shape}')
  3. 3  >> env.action_space.shape = (17,)
  4. 4  >> env.observation_space.shape = (44,)
复制代码

可知 HumanoidPyBulletEnv-v0 运动空间维度为17,动作空间维度为44
查看注册环境源码,可知 HumanoidPyBulletEnv-v0 入口类为HumanoidBulletEnv

  1. 1  register(
  2. 2      id='HumanoidPyBulletEnv-v0',
  3. 3      entry_point='pybulletgym.envs.roboschool.envs.locomotion.humanoid_env:HumanoidBulletEnv',
  4. 4      max_episode_steps=1000
  5. 5      )
复制代码

根据 HumanoidBulletEnv 初始化 __init__ 的参数,可知机器人实例由 Humanoid() 构建,顺藤摸瓜,获得 HumanoidBulletEnv 运动空间维度的详细定义

  1. 1  self.motor_names  = ["abdomen_z", "abdomen_y", "abdomen_x"]
  2. 2  self.motor_power  = [100, 100, 100]
  3. 3  self.motor_names += ["right_hip_x", "right_hip_z", "right_hip_y", "right_knee"]
  4. 4  self.motor_power += [100, 100, 300, 200]
  5. 5  self.motor_names += ["left_hip_x", "left_hip_z", "left_hip_y", "left_knee"]
  6. 6  self.motor_power += [100, 100, 300, 200]
  7. 7  self.motor_names += ["right_shoulder1", "right_shoulder2", "right_elbow"]
  8. 8  self.motor_power += [75, 75, 75]
  9. 9  self.motor_names += ["left_shoulder1", "left_shoulder2", "left_elbow"]
  10. 10  self.motor_power += [75, 75, 75]
复制代码

执行 step 时,pybullet-gym 中使用力矩对机器人电机进行控制

  1. def set_torque(self, torque):
  2. self._p.setJointMotorControl2(bodyIndex=self.bodies[self.bodyIndex],jointIndex=self.jointIndex,controlMode=pybullet.TORQUE_CONTROL, force=torque)  # positionGain=0.1, velocityGain=0.1)
复制代码


接下来查看观测空间定义,状态观测是由observation, reward, done, info =env.step(action) 获得,因此查看源码walker_base_env.py 可知状态计算方式


  1. 1  np.clip(np.concatenate([more] + [j] + [self.feet_contact]), -5, +5)
复制代码

大致包含机器人的离地面高度、机器人欧拉角、各个关节相对位置、足底是否触地等状态。状态维度为一共为 46,详细的定义,以及为什么这么定义的原因未知,参见这条 issue 的讨论openai/gym/issues/585https://github.com/openai/gym/issues/585),看来 OpenAI 被戏称为 CloseAI 是有原因的。

done 根据机器人的高度和偏航角来判断机器人是否摔倒,回合是否结束

  1. 1  alive = float(self.robot.alive_bonus(state[0] + self.robot.initial_z, self.robot.body_rpy[1] # state[0] is body height above ground, body_rpy[1] is pitch
复制代码

奖励函数


对于强化学习问题,最为重要的就是奖励函数的设计,直接关乎训练后 Agent 的行为是否符合预期。HumanoidPyBulletEnv-v0的奖励由下面几部分构成


  1. 1  self.rewards = [
  2. 2      alive,
  3. 3      progress,
  4. 4      electricity_cost,
  5. 5      joints_at_limit_cost,
  6. 6      feet_collision_cost
  7. 7  ]
复制代码

  • alive: 判断机器人是否摔倒
  • progress: 速度的差值
  • electricity_cost: 控制的能量损耗,由扭矩和电机速度计算
  • joints_at_limit_cost: 关节是否卡住
  • feet_collision_cost:足底碰撞检测
Pybullet-Gym 代码逻辑是很清晰,但是由于是移植的 roboschool 的 Humanoid 环境,很多 Agent 代码细节没有文档可以参考,官方的态度是我们只需要关心采用哪些强化学习算法去训练 Agent 就可以了,不需要关注 Agent 的实现细节。
但是如果要训练自定义的 Biped Robot Walk 的话就必须深入看 Gym 的底层代码实现,研究状态、运动、以及奖励函数的具体细节。

补充

Humanoid V1 Wiki 介绍https://github.com/openai/gym/wiki/Humanoid-V1#observation

参考链接

https://github.com/openai/gym
https://github.com/benelot/pybullet-gym
OpenAI Gym 源码阅读:创建自定义强化学习环境
欧拉中的俯仰、横滚、偏航角https://blog.csdn.net/guyubit/article/details/52995676
分享至 : QQ空间
收藏

0 个回复

您需要登录后才可以回帖 登录 | 立即注册