update scripts and recent model of turn around gym
This commit is contained in:
@@ -96,29 +96,29 @@ class WalkEnv(gym.Env):
|
||||
# 中立姿态
|
||||
self.joint_nominal_position = np.array(
|
||||
[
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
1.4,
|
||||
0.0,
|
||||
-0.4,
|
||||
0.0,
|
||||
-1.4,
|
||||
0.0,
|
||||
0.4,
|
||||
0.0,
|
||||
-0.4,
|
||||
0.0,
|
||||
0.0,
|
||||
0.8,
|
||||
-0.4,
|
||||
0.0,
|
||||
0.4,
|
||||
0.0,
|
||||
0.0,
|
||||
-0.8,
|
||||
0.4,
|
||||
0.0,
|
||||
0.0, # 0: Head_yaw (he1)
|
||||
0.0, # 1: Head_pitch (he2)
|
||||
0.0, # 2: Left_Shoulder_Pitch (lae1)
|
||||
0.0, # 3: Left_Shoulder_Roll (lae2)
|
||||
0.0, # 4: Left_Elbow_Pitch (lae3)
|
||||
0.0, # 5: Left_Elbow_Yaw (lae4)
|
||||
0.0, # 6: Right_Shoulder_Pitch (rae1)
|
||||
0.0, # 7: Right_Shoulder_Roll (rae2)
|
||||
0.0, # 8: Right_Elbow_Pitch (rae3)
|
||||
0.0, # 9: Right_Elbow_Yaw (rae4)
|
||||
0.0, # 10: Waist (te1)
|
||||
0.0, # 11: Left_Hip_Pitch (lle1)
|
||||
0.0, # 12: Left_Hip_Roll (lle2)
|
||||
1.0, # 13: Left_Hip_Yaw (lle3)
|
||||
0.0, # 14: Left_Knee_Pitch (lle4)
|
||||
0.0, # 15: Left_Ankle_Pitch (lle5)
|
||||
0.0, # 16: Left_Ankle_Roll (lle6)
|
||||
0.0, # 17: Right_Hip_Pitch (rle1)
|
||||
0.0, # 18: Right_Hip_Roll (rle2)
|
||||
1.0, # 19: Right_Hip_Yaw (rle3)
|
||||
0.0, # 20: Right_Knee_Pitch (rle4)
|
||||
0.0, # 21: Right_Ankle_Pitch (rle5)
|
||||
0.0, # 22: Right_Ankle_Roll (rle6)
|
||||
]
|
||||
)
|
||||
self.joint_nominal_position = np.zeros(self.no_of_actions)
|
||||
@@ -159,7 +159,7 @@ class WalkEnv(gym.Env):
|
||||
# Small reset perturbations for robustness training.
|
||||
self.enable_reset_perturb = False
|
||||
self.reset_beam_yaw_range_deg = float(os.environ.get("GYM_CPU_RESET_BEAM_YAW_RANGE_DEG", "180"))
|
||||
self.reset_target_bearing_range_deg = float(os.environ.get("GYM_CPU_RESET_TARGET_BEARING_RANGE_DEG", "45"))
|
||||
self.reset_target_bearing_range_deg = float(os.environ.get("GYM_CPU_RESET_TARGET_BEARING_RANGE_DEG", "90"))
|
||||
self.reset_target_distance_min = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MIN", "1.2"))
|
||||
self.reset_target_distance_max = float(os.environ.get("GYM_CPU_RESET_TARGET_DISTANCE_MAX", "2.8"))
|
||||
if self.reset_target_distance_min > self.reset_target_distance_max:
|
||||
@@ -173,7 +173,7 @@ class WalkEnv(gym.Env):
|
||||
|
||||
self.reward_smoothness_scale = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_SCALE", "0.06"))
|
||||
self.reward_smoothness_cap = float(os.environ.get("GYM_CPU_REWARD_SMOOTHNESS_CAP", "0.45"))
|
||||
self.reward_head_toward_bonus = float(os.environ.get("GYM_CPU_REWARD_HEAD_TOWARD_BONUS", "0.7"))
|
||||
self.reward_head_toward_bonus = float(os.environ.get("GYM_CPU_REWARD_HEAD_TOWARD_BONUS", "1"))
|
||||
|
||||
self.previous_action = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||
self.last_action_for_reward = np.zeros(len(self.Player.robot.ROBOT_MOTORS))
|
||||
@@ -416,10 +416,12 @@ class WalkEnv(gym.Env):
|
||||
def render(self, mode='human', close=False):
|
||||
return
|
||||
|
||||
|
||||
def compute_reward(self, previous_pos, current_pos, action):
|
||||
height = float(self.Player.world.global_position[2])
|
||||
robot = self.Player.robot
|
||||
|
||||
|
||||
joint_pos_rad = np.deg2rad(
|
||||
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||
)
|
||||
@@ -433,52 +435,96 @@ class WalkEnv(gym.Env):
|
||||
ang_vel = np.deg2rad(robot.gyroscope)
|
||||
rp_ang_vel_mag = float(np.linalg.norm(ang_vel[:2]))
|
||||
|
||||
# is_fallen = height < 0.55
|
||||
# if is_fallen:
|
||||
# remain = max(0, 800 - self.step_counter)
|
||||
# # Strong terminal penalty discourages risky turn-and-fall behaviors.
|
||||
# return -1
|
||||
is_fallen = height < 0.55
|
||||
if is_fallen:
|
||||
# remain = max(0, 800 - self.step_counter)
|
||||
# return -8.0 - 0.01 * remain
|
||||
return -20.0
|
||||
|
||||
|
||||
if np.linalg.norm(current_pos - previous_pos) > 0.005:
|
||||
position_penalty = -3 * float(np.linalg.norm(current_pos - previous_pos))
|
||||
else:
|
||||
position_penalty = 0.0
|
||||
|
||||
# # 目标方向
|
||||
# to_target = self.target_position - current_pos
|
||||
# dist_to_target = float(np.linalg.norm(to_target))
|
||||
# if dist_to_target < 0.5:
|
||||
# return 15.0
|
||||
|
||||
# forward_dir = to_target / dist_to_target if dist_to_target > 0.1 else np.array([1.0, 0.0])
|
||||
# delta_pos = current_pos - previous_pos
|
||||
# forward_step = float(np.dot(delta_pos, forward_dir))
|
||||
# lateral_step = float(np.linalg.norm(delta_pos - forward_dir * forward_step))
|
||||
# Turn-to-target shaping.
|
||||
to_target = self.target_position - current_pos
|
||||
dist_to_target = float(np.linalg.norm(to_target))
|
||||
if dist_to_target > 1e-6:
|
||||
target_yaw = math.atan2(float(to_target[1]), float(to_target[0]))
|
||||
else:
|
||||
target_yaw = 0.0
|
||||
|
||||
# Keep reward simple: turn correctly, stay stable, avoid jerky actions.
|
||||
|
||||
delta_action_norm = float(np.linalg.norm(action - self.last_action_for_reward))
|
||||
# Cap smoothness penalty so it regularizes behavior without dominating total reward.
|
||||
smoothness_penalty = -min(self.reward_smoothness_cap, self.reward_smoothness_scale * delta_action_norm)
|
||||
robot_yaw = math.radians(float(robot.global_orientation_euler[2]))
|
||||
yaw_error = target_yaw - robot_yaw
|
||||
|
||||
posture_penalty = -0.45 * tilt_mag
|
||||
# Main heading objective: face the target direction.
|
||||
# heading_align_reward = 1.0 * math.cos(yaw_error)
|
||||
|
||||
abs_yaw_error = abs(yaw_error)
|
||||
alive_bonus = 2.0 * max(0.0, 1.0 - abs_yaw_error / math.pi)
|
||||
head_toward_bonus = self.reward_head_toward_bonus if abs_yaw_error < math.radians(4.0) else 0.0
|
||||
|
||||
if self.last_yaw_error is None:
|
||||
heading_progress_reward = 0.0
|
||||
else:
|
||||
prev_abs_yaw_error = abs(self.last_yaw_error)
|
||||
yaw_err_delta = prev_abs_yaw_error - abs_yaw_error
|
||||
progress_gate = 1.0 if abs_yaw_error > math.radians(4.0) else 0.0
|
||||
heading_progress_reward = 0.8 * progress_gate * yaw_err_delta
|
||||
heading_progress_reward = float(np.clip(heading_progress_reward, -0.4, 0.4))
|
||||
self.last_yaw_error = yaw_error
|
||||
|
||||
# action_penalty = -0.01 * float(np.linalg.norm(action))
|
||||
smoothness_penalty = -0.05 * float(np.linalg.norm(action - self.last_action_for_reward))
|
||||
|
||||
posture_penalty = -0.6 * tilt_mag
|
||||
# Penalize roll/pitch rotational shake but do not penalize yaw turning directly.
|
||||
ang_vel_penalty = -0.04 * rp_ang_vel_mag
|
||||
ang_vel_penalty = -0.06 * rp_ang_vel_mag
|
||||
|
||||
joint_pos = np.deg2rad(
|
||||
[robot.motor_positions[motor] for motor in robot.ROBOT_MOTORS]
|
||||
) * self.train_sim_flip
|
||||
|
||||
left_hip_roll = float(joint_pos[12])
|
||||
right_hip_roll = float(joint_pos[18])
|
||||
left_hip_pitch = float(joint_pos[11])
|
||||
right_hip_pitch = float(joint_pos[17])
|
||||
|
||||
left_ankle_roll = float(joint_pos[16])
|
||||
right_ankle_roll = float(joint_pos[22])
|
||||
|
||||
hip_spread = left_hip_roll - right_hip_roll
|
||||
ankle_spread = left_ankle_roll - right_ankle_roll
|
||||
stance_metric = 0.6 * abs(hip_spread) + 0.4 * abs(ankle_spread)
|
||||
max_leg_roll = 0.2 # 防止劈叉姿势
|
||||
split_penalty = -0.8 * max(0.0, (-left_hip_roll + right_hip_roll - 2 * max_leg_roll) / max_leg_roll)
|
||||
left_hip_yaw = float(joint_pos[13])
|
||||
right_hip_yaw = float(joint_pos[19])
|
||||
|
||||
# Penalize narrow stance (feet too close) and scissoring (cross-leg pattern).
|
||||
stance_collapse_penalty = -4.0 * max(0.0, self.min_stance_rad - stance_metric)
|
||||
cross_leg_penalty = -1.2 * max(0.0, -(hip_spread * ankle_spread))
|
||||
min_leg_separation = 0.05 # 最小腿间距(防止贴得太近)
|
||||
# 惩罚腿过分靠拢(内收)- 基于两腿间距
|
||||
leg_separation = -left_hip_roll + right_hip_roll
|
||||
inward_penalty = -0.25 * max(0.0, (min_leg_separation - leg_separation) / min_leg_separation)
|
||||
|
||||
|
||||
# 脚踝roll角度检测:防止过度外翻或内翻
|
||||
max_ankle_roll = 0.15 # 最大允许的脚踝roll角度
|
||||
|
||||
# 惩罚脚踝过度外翻/内翻(绝对值过大)
|
||||
ankle_roll_penalty = -0.5 * max(0.0, (abs(left_ankle_roll) + abs(right_ankle_roll) - 2 * max_ankle_roll) / max_ankle_roll)
|
||||
|
||||
# 惩罚两脚踝roll方向相反(不稳定姿势)
|
||||
ankle_roll_cross_penalty = -0.3 * max(0.0, -(left_ankle_roll * right_ankle_roll))
|
||||
|
||||
# 分别惩罚左右大腿过度转动
|
||||
max_hip_yaw = 0.4 # 最大允许的yaw角度
|
||||
left_hip_yaw_penalty = -0.4 * max(0.0, abs(left_hip_yaw) - max_hip_yaw)
|
||||
right_hip_yaw_penalty = -0.4 * max(0.0, abs(right_hip_yaw) - max_hip_yaw)
|
||||
# 智能交叉腿惩罚:只在站立时惩罚,转身时允许交叉腿
|
||||
yaw_rate = float(np.deg2rad(robot.gyroscope[2]))
|
||||
yaw_rate_abs = abs(yaw_rate)
|
||||
|
||||
# 当转身速度较小时才惩罚交叉腿(站立状态)
|
||||
cross_leg_gate = max(0.0, 1.0 - yaw_rate_abs / math.radians(8.0))
|
||||
hip_yaw_cross_penalty = -1.0 * cross_leg_gate * max(0.0, -(left_hip_yaw * right_hip_yaw)) if left_hip_yaw > 0 and right_hip_yaw < 0 else 0.0
|
||||
|
||||
# Torso-lower-body linkage: reward coordinated turning, punish waist-only spinning.
|
||||
waist_speed = abs(float(joint_speed_rad[10]))
|
||||
@@ -492,80 +538,66 @@ class WalkEnv(gym.Env):
|
||||
hip_yaw_mean = 0.5 * (abs(float(joint_pos_rad[13])) + abs(float(joint_pos_rad[19])))
|
||||
yaw_link_reward = 0.12 * math.exp(-abs(waist_yaw - hip_yaw_mean) / 0.22)
|
||||
|
||||
# Turn-to-target shaping.
|
||||
to_target = self.target_position - current_pos
|
||||
dist_to_target = float(np.linalg.norm(to_target))
|
||||
if dist_to_target > 1e-6:
|
||||
target_yaw = math.atan2(float(to_target[1]), float(to_target[0]))
|
||||
else:
|
||||
target_yaw = 0.0
|
||||
target_height = self.initial_height
|
||||
height_error = height - target_height
|
||||
height_error = height - target_height
|
||||
|
||||
height_penalty = -(math.exp(12*abs(height_error))-1) if height_error > 0.04 else 0
|
||||
|
||||
robot_yaw = math.radians(float(robot.global_orientation_euler[2]))
|
||||
yaw_error = self._wrap_to_pi(target_yaw - robot_yaw)
|
||||
# # 在 compute_reward 开头附近,添加高度变化率计算
|
||||
# if not hasattr(self, 'last_height'):
|
||||
# self.last_height = height
|
||||
# self.last_height_time = self.step_counter # 可选,用于时间间隔
|
||||
# height_rate = height - self.last_height # 正为上升,负为下降
|
||||
# self.last_height = height
|
||||
|
||||
# Main heading objective: face the target direction.
|
||||
# heading_align_reward = 1.0 * math.cos(yaw_error)
|
||||
# 惩罚高度下降(负变化率)
|
||||
# height_down_penalty = -5.0 * max(0, -height_rate) # 系数可调,-height_rate 为正表示下降幅度
|
||||
|
||||
abs_yaw_error = abs(yaw_error)
|
||||
# # 在 compute_reward 中
|
||||
# if self.step_counter > 50:
|
||||
# avg_prev_action = np.mean(self.prev_action_history, axis=0)
|
||||
# novelty = float(np.linalg.norm(action - avg_prev_action))
|
||||
# exploration_bonus = 0.05 * novelty
|
||||
# else:
|
||||
# exploration_bonus = 0
|
||||
|
||||
# Reward reducing heading error between consecutive steps.
|
||||
# Use a deadzone and smaller gain to avoid high-frequency jitter near alignment.
|
||||
if self.last_yaw_error is None:
|
||||
heading_progress_reward = 0.0
|
||||
else:
|
||||
prev_abs_yaw_error = abs(self.last_yaw_error)
|
||||
yaw_err_delta = prev_abs_yaw_error - abs_yaw_error
|
||||
progress_gate = 1.0 if abs_yaw_error > math.radians(4.0) else 0.0
|
||||
heading_progress_reward = 0.30 * progress_gate * yaw_err_delta
|
||||
heading_progress_reward = float(np.clip(heading_progress_reward, -0.12, 0.12))
|
||||
self.last_yaw_error = yaw_error
|
||||
|
||||
yaw_rate = float(np.deg2rad(robot.gyroscope[2]))
|
||||
yaw_rate_abs = abs(yaw_rate)
|
||||
turn_dir = float(np.sign(yaw_error))
|
||||
# Continuous turn shaping prevents reward discontinuity near small heading error.
|
||||
turn_gate = min(1.0, abs_yaw_error / math.radians(45.0))
|
||||
turn_rate_reward = 0.45 * turn_gate * math.tanh(2.0 * turn_dir * yaw_rate)
|
||||
head_toward_bonus = self.reward_head_toward_bonus if abs_yaw_error < math.radians(8.0) else 0.0
|
||||
# After roughly aligning with target, prioritize standing stability over continued aggressive turning.
|
||||
aligned_gate = max(0.0, 1.0 - abs_yaw_error / math.radians(18.0))
|
||||
post_turn_ang_vel_penalty = -0.10 * aligned_gate * min(rp_ang_vel_mag, math.radians(60.0))
|
||||
lower_body_speed_mag = float(np.mean(np.abs(joint_speed_rad[11:23])))
|
||||
post_turn_pose_bonus = 0.30 * aligned_gate * math.exp(-tilt_mag / 0.20) * math.exp(-lower_body_speed_mag / 1.10)
|
||||
# Keep feet separation when aligned so robot does not collapse stance after turning.
|
||||
aligned_stance_bonus = 0.10 * aligned_gate * min(1.0, stance_metric / max(self.min_stance_rad, 1e-4))
|
||||
# Once roughly aligned, damp yaw oscillation and reward keeping a stable stance.
|
||||
anti_oscillation_penalty = -0.08 * min(yaw_rate_abs, math.radians(35.0)) if abs_yaw_error < math.radians(7.0) else 0.0
|
||||
stabilize_bonus = 0.45 if (
|
||||
abs_yaw_error < math.radians(12.0)
|
||||
and yaw_rate_abs < math.radians(10.0)
|
||||
and tilt_mag < 0.28
|
||||
) else 0.0
|
||||
|
||||
alive_bonus = max(0.5, 1.5 * math.cos(yaw_error)) # Encourage facing target, but give some baseline reward for not falling even if not facing target yet.
|
||||
# self.prev_action_history[self.history_idx] = action
|
||||
# self.history_idx = (self.history_idx + 1) % 50
|
||||
|
||||
|
||||
total = (
|
||||
alive_bonus
|
||||
+ smoothness_penalty
|
||||
+ posture_penalty
|
||||
+ ang_vel_penalty
|
||||
+ linkage_reward
|
||||
+ waist_only_turn_penalty
|
||||
+ yaw_link_reward
|
||||
+ head_toward_bonus
|
||||
+ heading_progress_reward
|
||||
+ anti_oscillation_penalty
|
||||
+ stabilize_bonus
|
||||
+ post_turn_ang_vel_penalty
|
||||
+ post_turn_pose_bonus
|
||||
+ aligned_stance_bonus
|
||||
# + heading_align_reward
|
||||
+ turn_rate_reward
|
||||
+ stance_collapse_penalty
|
||||
+ cross_leg_penalty
|
||||
)
|
||||
|
||||
# progress_reward +
|
||||
alive_bonus +
|
||||
head_toward_bonus +
|
||||
heading_progress_reward +
|
||||
# lateral_penalty +
|
||||
# action_penalty +
|
||||
smoothness_penalty +
|
||||
posture_penalty
|
||||
+ ang_vel_penalty
|
||||
+ height_penalty
|
||||
+ ankle_roll_penalty
|
||||
+ ankle_roll_cross_penalty
|
||||
+ split_penalty
|
||||
+ inward_penalty
|
||||
# + leg_proximity_penalty
|
||||
# + left_hip_yaw_penalty
|
||||
# + right_hip_yaw_penalty
|
||||
# + hip_yaw_cross_penalty
|
||||
+ position_penalty
|
||||
# + linkage_reward
|
||||
# + waist_only_turn_penalty
|
||||
# + yaw_link_reward
|
||||
# + stance_collapse_penalty
|
||||
# + hip_yaw_yaw_cross_penalty
|
||||
# + stance_collapse_penalty
|
||||
# + cross_leg_penalty
|
||||
# + exploration_bonus
|
||||
# + height_down_penalty
|
||||
)
|
||||
# print(height_error, height_penalty)
|
||||
|
||||
now = time.time()
|
||||
if self.reward_debug_interval_sec > 0 and now - self._reward_debug_last_time >= self.reward_debug_interval_sec:
|
||||
self._reward_debug_last_time = now
|
||||
@@ -573,47 +605,36 @@ class WalkEnv(gym.Env):
|
||||
|
||||
if self._reward_debug_steps_left > 0:
|
||||
self._reward_debug_steps_left -= 1
|
||||
# print(
|
||||
# f"reward_debug: step={self.step_counter}, "
|
||||
# f"alive_bonus:{alive_bonus:.4f}, "
|
||||
# # f"heading_align_reward:{heading_align_reward:.4f}, "
|
||||
# # f"heading_progress_reward:{heading_progress_reward:.4f}, "
|
||||
# f"head_towards_bonus:{head_toward_bonus},"
|
||||
# f"posture_penalty:{posture_penalty:.4f}, "
|
||||
# f"ang_vel_penalty:{ang_vel_penalty:.4f}, "
|
||||
# f"smoothness_penalty:{smoothness_penalty:.4f}, "
|
||||
# f"linkage_reward:{linkage_reward:.4f}, "
|
||||
# f"waist_only_turn_penalty:{waist_only_turn_penalty:.4f}, "
|
||||
# f"yaw_link_reward:{yaw_link_reward:.4f}, "
|
||||
# f"anti_oscillation_penalty:{anti_oscillation_penalty:.4f}, "
|
||||
# f"stabilize_bonus:{stabilize_bonus:.4f}, "
|
||||
# f"turn_rate_reward:{turn_rate_reward:.4f}, "
|
||||
# f"total:{total:.4f}"
|
||||
# )
|
||||
self.debug_log(
|
||||
f"reward_debug: step={self.step_counter}, "
|
||||
f"alive_bonus:{alive_bonus:.4f}, "
|
||||
# f"heading_align_reward:{heading_align_reward:.4f}, "
|
||||
# f"heading_progress_reward:{heading_progress_reward:.4f}, "
|
||||
f"head_towards_bonus:{head_toward_bonus},"
|
||||
f"posture_penalty:{posture_penalty:.4f}, "
|
||||
f"ang_vel_penalty:{ang_vel_penalty:.4f}, "
|
||||
f"smoothness_penalty:{smoothness_penalty:.4f}, "
|
||||
f"heading_progress_reward:{heading_progress_reward:.4f}, "
|
||||
f"linkage_reward:{linkage_reward:.4f}, "
|
||||
f"waist_only_turn_penalty:{waist_only_turn_penalty:.4f}, "
|
||||
f"yaw_link_reward:{yaw_link_reward:.4f}, "
|
||||
f"anti_oscillation_penalty:{anti_oscillation_penalty:.4f}, "
|
||||
f"stabilize_bonus:{stabilize_bonus:.4f}, "
|
||||
f"post_turn_ang_vel_penalty:{post_turn_ang_vel_penalty:.4f}, "
|
||||
f"post_turn_pose_bonus:{post_turn_pose_bonus:.4f}, "
|
||||
f"aligned_stance_bonus:{aligned_stance_bonus:.4f}, "
|
||||
f"turn_rate_reward:{turn_rate_reward:.4f}, "
|
||||
f"stance_collapse_penalty:{stance_collapse_penalty:.4f}, "
|
||||
f"cross_leg_penalty:{cross_leg_penalty:.4f}, "
|
||||
f"height_penalty:{height_penalty:.4f},"
|
||||
f"smoothness_penalty:{smoothness_penalty:.4f},"
|
||||
f"posture_penalty:{posture_penalty:.4f},"
|
||||
f"heading_progress_reward:{heading_progress_reward:.4f},"
|
||||
# f"stance_collapse_penalty:{stance_collapse_penalty:.4f},"
|
||||
# f"cross_leg_penalty:{cross_leg_penalty:.4f},"
|
||||
f"ang_vel_penalty:{ang_vel_penalty:.4f},"
|
||||
f"split_penalty:{split_penalty:.4f},"
|
||||
f"ankle_roll_penalty:{ankle_roll_penalty:.4f},"
|
||||
f"ankle_roll_cross_penalty:{ankle_roll_cross_penalty:.4f},"
|
||||
f"left_hip_yaw_penalty:{left_hip_yaw_penalty:.4f},"
|
||||
f"right_hip_yaw_penalty:{right_hip_yaw_penalty:.4f},"
|
||||
f"hip_yaw_cross_penalty:{hip_yaw_cross_penalty:.4f},"
|
||||
f"inward_penalty:{inward_penalty:.4f},"
|
||||
f"position_penalty:{position_penalty:.4f},"
|
||||
# f"linkage_reward:{linkage_reward:.4f},"
|
||||
# f"waist_only_turn_penalty:{waist_only_turn_penalty:.4f},"
|
||||
# f"yaw_link_reward:{yaw_link_reward:.4f}"
|
||||
# f"leg_proximity_penalty:{leg_proximity_penalty:.4f},"
|
||||
|
||||
# f"stance_collapse_penalty:{stance_collapse_penalty:.4f},"
|
||||
# f"hip_yaw_yaw_cross_penalty:{hip_yaw_yaw_cross_penalty:.4f},"
|
||||
# f"height_down_penalty:{height_down_penalty:.4f}",
|
||||
# f"exploration_bonus:{exploration_bonus:.4f}"
|
||||
f"alive_bonus:{alive_bonus:.4f},"
|
||||
f"abs_yaw_error:{abs_yaw_error:.4f}"
|
||||
f"total:{total:.4f}"
|
||||
)
|
||||
|
||||
# print(f"abs_yaw_error:{abs_yaw_error:.4f}")
|
||||
return total
|
||||
|
||||
|
||||
@@ -621,7 +642,26 @@ class WalkEnv(gym.Env):
|
||||
def step(self, action):
|
||||
|
||||
r = self.Player.robot
|
||||
self.previous_action = action
|
||||
max_action_delta = 0.5# Limit how much the action can change from the previous step to encourage smoother motions.
|
||||
if self.previous_action is not None:
|
||||
action = np.clip(action, self.previous_action - max_action_delta, self.previous_action + max_action_delta)
|
||||
action[0:2] = 0
|
||||
action[3] = 4
|
||||
action[7] = -4
|
||||
action[2] = 0
|
||||
action[6] = 0
|
||||
action[4] = 0
|
||||
action[5] = -5
|
||||
action[8] = 0
|
||||
action[9] = 5
|
||||
action[10] = 0
|
||||
# action[11] = np.clip(action[11], -0.5, 0.5)
|
||||
# action[17] = np.clip(action[17], -0.5, 0.5)
|
||||
# action[12] = -1.0
|
||||
# action[18] = 1.0
|
||||
# action[13] = -1.0
|
||||
# action[19] = 1.0
|
||||
self.previous_action = action.copy()
|
||||
|
||||
self.target_joint_positions = (
|
||||
# self.joint_nominal_position +
|
||||
@@ -631,10 +671,10 @@ class WalkEnv(gym.Env):
|
||||
|
||||
for idx, target in enumerate(self.target_joint_positions):
|
||||
r.set_motor_target_position(
|
||||
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=25, kd=0.6
|
||||
r.ROBOT_MOTORS[idx], target * 180 / math.pi, kp=110, kd=5
|
||||
)
|
||||
|
||||
self.previous_action = action
|
||||
self.previous_action = action.copy()
|
||||
|
||||
self.sync() # run simulation step
|
||||
self.step_counter += 1
|
||||
@@ -644,11 +684,12 @@ class WalkEnv(gym.Env):
|
||||
|
||||
current_pos = np.array(self.Player.world.global_position[:2], dtype=np.float32)
|
||||
|
||||
if self.step_counter % 10 == 0:
|
||||
self.previous_pos = current_pos.copy()
|
||||
|
||||
# Compute reward based on movement from previous step
|
||||
reward = self.compute_reward(self.previous_pos, current_pos, action)
|
||||
|
||||
# Update previous position
|
||||
self.previous_pos = current_pos.copy()
|
||||
self.last_action_for_reward = action.copy()
|
||||
|
||||
# Fall detection and penalty
|
||||
@@ -672,7 +713,7 @@ class Train(Train_Base):
|
||||
if n_envs < 1:
|
||||
raise ValueError("GYM_CPU_N_ENVS must be >= 1")
|
||||
server_warmup_sec = float(os.environ.get("GYM_CPU_SERVER_WARMUP_SEC", "3.0"))
|
||||
n_steps_per_env = int(os.environ.get("GYM_CPU_TRAIN_STEPS_PER_ENV", "256")) # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||
n_steps_per_env = int(os.environ.get("GYM_CPU_TRAIN_STEPS_PER_ENV", "512")) # RolloutBuffer is of size (n_steps_per_env * n_envs)
|
||||
minibatch_size = int(os.environ.get("GYM_CPU_TRAIN_BATCH_SIZE", "512")) # should be a factor of (n_steps_per_env * n_envs)
|
||||
total_steps = 30000000
|
||||
learning_rate = float(os.environ.get("GYM_CPU_TRAIN_LR", "3e-4"))
|
||||
@@ -740,7 +781,7 @@ class Train(Train_Base):
|
||||
)
|
||||
|
||||
model_path = self.learn_model(model, total_steps, model_path, eval_env=eval_env,
|
||||
eval_freq=n_steps_per_env * 20, save_freq=n_steps_per_env * 20, eval_eps=30,
|
||||
eval_freq=n_steps_per_env * 20, save_freq=n_steps_per_env * 20, eval_eps=7,
|
||||
backup_env_file=__file__)
|
||||
except KeyboardInterrupt:
|
||||
sleep(1) # wait for child processes
|
||||
|
||||
Reference in New Issue
Block a user