镜像自地址
https://github.com/MINT-SJTU/Evo-RL.git
已同步 2026-11-20 18:17:06 +00:00
57 行
1.9 KiB
Python
57 行
1.9 KiB
Python
#!/usr/bin/env python
|
|
|
|
import numpy as np
|
|
|
|
from lerobot.scripts.lerobot_value_infer import (
|
|
_binarize_advantages,
|
|
_compute_dense_rewards_from_targets,
|
|
_compute_n_step_advantages,
|
|
_compute_task_thresholds,
|
|
)
|
|
|
|
|
|
def test_compute_dense_rewards_from_targets_terminal_handling():
|
|
episode_indices = np.array([0, 0, 0, 1, 1], dtype=np.int64)
|
|
frame_indices = np.array([0, 1, 2, 0, 1], dtype=np.int64)
|
|
targets = np.array([-0.6, -0.4, -0.2, -0.8, -0.5], dtype=np.float32)
|
|
|
|
rewards = _compute_dense_rewards_from_targets(targets, episode_indices, frame_indices)
|
|
expected = np.array([-0.2, -0.2, -0.2, -0.3, -0.5], dtype=np.float32)
|
|
assert np.allclose(rewards, expected)
|
|
|
|
|
|
def test_compute_n_step_advantages_simple_case():
|
|
rewards = np.array([-0.2, -0.2, -0.2], dtype=np.float32)
|
|
values = np.array([-0.5, -0.3, -0.1], dtype=np.float32)
|
|
episode_indices = np.array([0, 0, 0], dtype=np.int64)
|
|
frame_indices = np.array([0, 1, 2], dtype=np.int64)
|
|
|
|
advantages = _compute_n_step_advantages(
|
|
rewards=rewards,
|
|
values=values,
|
|
episode_indices=episode_indices,
|
|
frame_indices=frame_indices,
|
|
n_step=2,
|
|
)
|
|
|
|
expected = np.array([0.0, -0.1, -0.1], dtype=np.float32)
|
|
assert np.allclose(advantages, expected)
|
|
|
|
|
|
def test_compute_task_thresholds_and_binarize_with_interventions():
|
|
task_indices = np.array([0, 0, 0, 1, 1], dtype=np.int64)
|
|
advantages = np.array([-0.4, -0.1, 0.3, -0.2, 0.2], dtype=np.float32)
|
|
interventions = np.array([0, 1, 0, 0, 0], dtype=np.float32)
|
|
|
|
thresholds = _compute_task_thresholds(task_indices, advantages, positive_ratio=0.5)
|
|
|
|
indicators = _binarize_advantages(
|
|
task_indices=task_indices,
|
|
advantages=advantages,
|
|
thresholds=thresholds,
|
|
interventions=interventions,
|
|
force_intervention_positive=True,
|
|
)
|
|
|
|
assert indicators.tolist() == [0, 1, 1, 0, 1]
|