Giter Site home page Giter Site logo

laikago_py's Introduction

laikago_py

文件目录

  • envs目录下的是环境文件;
  • deeprl目录下的是算法的测试文件,使用stable baselines 3在几个连续状态动作环境中进行了测试;
  • tasks目录下的是任务文件.

envs

部分代码来自开源项目motion_imitation.

├─build_envs
│  ├─env_wrappers
│  ├─sensors
│  ├─utilities
│  └─laikago_task.py
├─laikago_model
├─robots
└─utilities
  • laikago_model目录下的是laikago的模型文件,不需要任何改动。
  • robots目录下的是laikago的仿真模型,包括动力学参数。
  • build_envs负责将模型封装为gym模型。使用locomotion_gym_env封装。

laikago_task.py是task类的基类,提供了各种计算reward的函数,之后在介绍tasks的部分详细介绍。

deeprl

这个项目 使用stable-baseline3 作为训练算法。

我们在HalfCheetah, Hopper和Walker三个环境中对算法进行了测试。

tasks

主要的训练文件,每个task代表一个任务,理论上每个任务对应一套reward机制,但是在实践中,为了尽可能对找到合适的reward,我们往往会设计多种reward机制。

新建每个task,你需要:

  • tasks目录下新建对应的文件夹;
  • 建立对应的xxx_task文件,用于配置强化学习算法的逻辑;
  • 建立对应的xxx_env_builder.py文件,用于链接xxx_task.py文件和locomotion_gym_env.py文件,以及完成其他更高级的配置。

task目录的文件如下:

├─log&model
├─env_builder.py
└─task.py

配置task文件

你需要新建一个Task类,例如StandupTask,你可以继承自envs/build_envs/laikago_task.py,也可以自己写。

无论如何,为了强化学习算法运行正常,请确保你的类中包括以下函数:

  • reward(self, env):返回一个数值;
  • done(self, env):返回bool;

以及

def __call__(self, env):
    return self.reward(env)

如果你想在环境中添加其他功能,例如增加随机扰动,可以在重载update(self, env)函数,例如:

def update(self, env):
    if not self.force:
        return
    force = self._give_force()
    self.body_pos = env._pybullet_client.getBasePositionAndOrientation(self.quadruped)[0]
    env._pybullet_client.applyExternalForce(objectUniqueId=self.quadruped, linkIndex=-1,
                         forceObj=force, posObj=self.body_pos, flags=env._pybullet_client.WORLD_FRAME)

配置env_builder文件

在这个文件中定义build_env()函数,返回一个从gym继承的ENV类,你可以直接使用locomotion_gym_env文件的LocomotionGymEnv完成这个功能,但是这个函数存在的意义在于完成更高级的设置。

在这个函数中完成task和env的绑定,例如

task = runstraight_task.RunstraightTask(mode = mode)
env = locomotion_gym_env.LocomotionGymEnv(gym_config=gym_config, robot_class=robot_class,
                                              env_randomizers=randomizers, robot_sensors=sensors, task=task)

也可以在这个函数中添加符合gym规范的wrapper,例如

env = observation_dictionary_to_array_wrapper.ObservationDictionaryToArrayWrapper(env)

通过引入locomotion_gym_config.py文件进行环境的基本参数设置。

重要的类

LaikagoTask

laikago_task.py文件下LaikagoTask类的封装了基本的reward机制和done机制,直接继承这个类可以让你的task更简洁。

  • _reward_of_toe_collision(self)给出鼓励机器人脚趾与地面接触的奖赏
  • _reward_of_leg_collision(self)给出阻止机器人大腿与地面接触的奖赏
  • _reward_of_upward_ori(self)给出鼓励机器人身体端正的奖赏
  • _reward_of_stand_height(self, max_height=0.4)给出鼓励机器人身体高度
  • _reward_of_energy(self)给出阻止机器人消耗能量
  • _reward_of_sum_vel(self)给出阻止机器人关节角速度过快的奖赏
  • _reward_of_toes_height(self)给出阻止机器人脚趾位置过高
  • _reward_of_toe_upper_distance(self)给出鼓励机器人脚趾与大腿尽可能远的奖赏

上述的奖赏都经过了normalize_reward函数归一化,近似分布在(0,1)之间。

同时集成了一些donenot_done的机制。

一个继承自这个类的reward函数可以写作:

def reward(self, env):
    del env
    sum_vel_r = self._reward_of_sum_vel()
    collision_r = self._reward_of_toe_collision()
    height_r = self._reward_of_stand_height()
    toe_upper_r = self._reward_of_toe_upper_distance()

    reward = sum_vel_r + collision_r + height_r + toe_upper_r
    return reward/4

一个继承自这个类的done函数可以写作:

def done(self, env):
    del env
    if self._not_done_of_too_short() or self._not_done_of_mode(self.mode):
        return False
    else:
        return self._done_of_wrong_stand_ori() or self._done_of_low_height() or self._done_of_too_long()

Sensor

Sensor类下有许多可以用来作为环境obs的传感器。

可以分为environment-sensor和robot-sensor,environment-sensor是环境记录下来的信息,robot-sensor是robot自身读取的信息。

具体的sensor主要有:

  • MotorAngleSensor
  • MotorVelocitiySensor
  • ToeTouchSensor
  • IMUSensor
  • LastActionSensor

也可以继承BoxSpaceSensor类写自己的Sensor,通过xxx_env_builder.py文件绑定到env中。

SensorWrapper

SensorWrapper类可以对sensor的数据进行处理,例如NormalizeSensorWrapper将obs做了归一化,使算法训练的难度减小。

训练

进入根目录,执行train.py文件并确定task的名称和版本,即可开始训练。

例如

python train.py -n standup -v 0

也可以设置SAC的算法参数,例如

python train.py -n standup -v 0 --ent_coef auto_0.1

或者在之前best model的基础之上继续训练

python train.py -n standup -v 0 -l True

测试

进入根目录,执行play.py文件并确定task的名称和版本,即可开始测试。

例如

python play.py -n standup -v 0

可以选择让agent永远不会死亡,例如

python play.py -n standup -m never_done -v 0

项目依赖

conda install pytorch torchvision cudatoolkit=10.1 -c pytorch

laikago_py's People

Contributors

dependabot[bot] avatar franktiantt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

hjydyn roboppi

laikago_py's Issues

hyperparameters

Hello.
How are the hyperparameters in laikago.py, laikago_constants.py and laikago_pose_utils.py designed?
Specifically, I would like to know the principles of these parameter designs:
laikago.py
INIT_RACK_POSITION = [0, 0, 1]
INIT_POSITION = [0, 0, 0.48]
JOINT_DIRECTIONS = np.array([-1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1])# ??
HIP_JOINT_OFFSET = 0.0# ??
UPPER_LEG_JOINT_OFFSET = -0.6 # ??
KNEE_JOINT_OFFSET = 0.66 # ??

MAX_MOTOR_ANGLE_CHANGE_PER_STEP = 0.2 # ??
_DEFAULT_HIP_POSITIONS = (
(0.21, -0.1157, 0),
(0.21, 0.1157, 0),
(-0.21, -0.1157, 0),
(-0.21, 0.1157, 0),
)

ABDUCTION_P_GAIN = 220.0
ABDUCTION_D_GAIN = 0.3
HIP_P_GAIN = 220.0
HIP_D_GAIN = 2.0
KNEE_P_GAIN = 220.0
KNEE_D_GAIN = 2.0

_BODY_B_FIELD_NUMBER = 2
_LINK_A_FIELD_NUMBER = 3

#--------------------------------------
laikago_constants.py
INIT_RACK_POSITION = [0, 0, 1]
INIT_POSITION = [0, 0, 0.48]

INIT_ORIENTATION = pyb.getQuaternionFromEuler([math.pi / 2.0, 0, math.pi / 2.0]) #??

INIT_ABDUCTION_ANGLE = 0 #?
INIT_HIP_ANGLE = 0.67
INIT_KNEE_ANGLE = -1.25

JOINT_DIRECTIONS = collections.OrderedDict(
zip(JOINT_NAMES, (-1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1)))

HIP_JOINT_OFFSET = 0.0 #?
UPPER_LEG_JOINT_OFFSET = -0.6
KNEE_JOINT_OFFSET = 0.66

MAX_MOTOR_ANGLE_CHANGE_PER_STEP = 0.12

HIP_POSITIONS = collections.OrderedDict((
(LEG_NAMES[0], (0.21, -0.1157, 0)),
(LEG_NAMES[1], (0.21, 0.1157, 0)),
(LEG_NAMES[2], (-0.21, -0.1157, 0)),
(LEG_NAMES[3], (-0.21, 0.1157, 0)),
))

#---------------------------------------------------------
laikago_pose_utils.py
LAIKAGO_DEFAULT_ABDUCTION_ANGLE = 0
LAIKAGO_DEFAULT_HIP_ANGLE = 0.67
LAIKAGO_DEFAULT_KNEE_ANGLE = -1.25

Thank you very much.

Laikago can barely run in runstraight-v2

Sorry for this issue, I run runstraight-v2 environment in my SAC implementation. But the laikago almost sits down on the ground directly, have you met this problem before?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.