lou 发表于 2020-4-23 11:32:30

Geek专栏:Pybulet Gym 源码解析:双足机器人模型 HumanoidPyBul...

本帖最后由 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)路径下可以找到。

import gym
import pybulletgym.envs

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

import gym
import time
import numpy as np
import pybullet as p
import pybulletgym.envs
def main():
   env = gym.make("HumanoidPyBulletEnv-v0")
   env.render(mode="human")
   for i_episode in range(20):
      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()
if __name__ == '__main__':
   main()

动作与观测

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

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

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

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

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

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

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

def set_torque(self, torque):
self._p.setJointMotorControl2(bodyIndex=self.bodies,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 可知状态计算方式


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

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

1alive = float(self.robot.alive_bonus(state + self.robot.initial_z, self.robot.body_rpy # state is body height above ground, body_rpy is pitch
奖励函数


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


1self.rewards = [
2      alive,
3      progress,
4      electricity_cost,
5      joints_at_limit_cost,
6      feet_collision_cost
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)
页: [1]
查看完整版本: Geek专栏:Pybulet Gym 源码解析:双足机器人模型 HumanoidPyBul...