Files
UniversalHumanoid/tests/test_adapters.py
T
2026-11-03 19:46:08 +08:00

83 lines
3.4 KiB
Python

"""CPU-only adapter/reward checks in the selected mjlab Python environment."""
from types import SimpleNamespace
import pytest
torch = pytest.importorskip("torch")
pytest.importorskip("mjlab")
from universal_humanoid.config import Experiment # noqa: E402
from universal_humanoid.rewards import planar_velocity_exp # noqa: E402
from universal_humanoid.tasks import build_configs # noqa: E402
pytestmark = pytest.mark.integration
@pytest.mark.parametrize("task", ["g1.flat", "t1.flat"])
def test_factories_are_independent(task):
a, _ = build_configs(Experiment(task=task, reward_weights={"track_linear_velocity": 7}))
b, _ = build_configs(Experiment(task=task))
assert a.rewards["track_linear_velocity"].weight == 7
assert b.rewards["track_linear_velocity"].weight != 7
@pytest.mark.parametrize(
"exp, match",
[
(Experiment(reward_weights={"typo": 1}), "Unknown reward weight"),
(Experiment(reward_params={"track_linear_velocity": {"stdd": 1}}), "Unknown parameters"),
(
Experiment(extra_rewards={"bad": {"function": "os.system", "weight": 1}}),
"Unknown local",
),
(
Experiment(extra_rewards={"pose": {"function": "joint_speed_l2", "weight": 1}}),
"shadows",
),
],
)
def test_reward_changes_fail_loudly(exp, match):
with pytest.raises(ValueError, match=match):
build_configs(exp)
def test_evaluation_keeps_fall_terminations_and_finite_episodes():
exp = Experiment(episode_seconds=5)
cfg, _ = build_configs(exp, evaluation=True, command=(0.3, 0.1, 0.2))
assert cfg.episode_length_s == 5
assert "fell_over" in cfg.terminations and "time_out" in cfg.terminations
assert not cfg.curriculum
assert all(event.mode == "reset" for event in cfg.events.values())
assert cfg.commands["twist"].ranges.lin_vel_x == (0.3, 0.3)
assert cfg.commands["twist"].ranges.heading is None
assert not cfg.commands["twist"].heading_command
assert not cfg.observations["actor"].enable_corruption
@pytest.mark.parametrize("task", ["g1.flat", "t1.flat"])
@pytest.mark.parametrize("profile", ["nominal", "randomized", "stress"])
def test_evaluation_randomization_profiles(task, profile):
cfg, _ = build_configs(Experiment(task=task), evaluation=True, profile=profile)
position = cfg.events["reset_robot_joints"].params["position_range"]
assert position == ((0.0, 0.0) if profile == "nominal" else (-0.03, 0.03))
assert ("evaluation_push" in cfg.events) == (profile == "stress")
assert cfg.observations["actor"].enable_corruption == (profile == "stress")
assert not cfg.observations["critic"].enable_corruption
assert "fell_over" in cfg.terminations and "time_out" in cfg.terminations
assert all(term.mode in ("reset", "interval") for term in cfg.events.values())
def test_reward_shape_finiteness_and_direction():
velocity = torch.tensor([[0.3, 0, 0], [0.9, 0, 0]], dtype=torch.float32)
env = SimpleNamespace(
scene={"robot": SimpleNamespace(data=SimpleNamespace(root_link_lin_vel_b=velocity))},
command_manager=SimpleNamespace(get_command=lambda _: torch.tensor([[0.3, 0, 0]] * 2)),
)
reward = planar_velocity_exp(env, std=0.5)
assert reward.shape == (2,)
assert torch.isfinite(reward).all()
assert reward[0] == 1 and reward[1] < reward[0]
with pytest.raises(ValueError, match="positive"):
planar_velocity_exp(env, std=0)