Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Upcoming version (not yet released)
Added
^^^^^

- Added ``frac_at_max``, ``promote_rate``, and ``demote_rate`` outputs to
``terrain_levels_vel`` to distinguish mastered terrains from plateaus
:issue:`1287`.
- Added ``play --checkpoint-file latest`` to load the last checkpoint of the newest
local run. Contribution by @Kaweees.
- Added ``GridPatternCfg.offset`` to shift the ray grid in frame-local XY.
Expand Down
8 changes: 8 additions & 0 deletions docs/source/curriculum.rst
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ Environments that reach the maximum level are randomly reassigned to
any row, maintaining coverage across all difficulty levels. See
:ref:`terrain` for details on configuring the terrain grid itself.

Because solved environments wrap to a random row, ``mean`` settles near
the middle of the grid both when a terrain is mastered and when the
policy is stuck partway up. To distinguish ongoing progress from a
plateau, ``terrain_levels_vel`` also logs ``frac_at_max`` (fraction of
all environments currently on the top row), ``promote_rate`` (fraction
of the resetting batch promoted), and ``demote_rate`` (fraction of the
resetting batch demoted), alongside per-sub-terrain mean levels.


Writing custom curriculum functions
------------------------------------
Expand Down
17 changes: 14 additions & 3 deletions src/mjlab/tasks/velocity/mdp/curriculums.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,29 @@ def terrain_levels_vel(
# Update terrain levels.
terrain.update_env_origins(env_ids, move_up, move_down)

# Compute per-terrain-type mean levels.
# Compute per-terrain-type mean levels and progression signals.
terrain_origins = terrain.terrain_origins
assert terrain_origins is not None
top_row = terrain_origins.shape[0] - 1
levels = terrain.terrain_levels.float()
if move_up.numel() > 0:
promote_rate = move_up.float().mean()
demote_rate = move_down.float().mean()
else:
promote_rate = torch.zeros((), device=levels.device)
demote_rate = torch.zeros((), device=levels.device)

result: dict[str, torch.Tensor] = {
"mean": torch.mean(levels),
"max": torch.max(levels),
"frac_at_max": (terrain.terrain_levels == top_row).float().mean(),
"promote_rate": promote_rate,
"demote_rate": demote_rate,
}

# In curriculum mode num_cols == num_terrains (one column per type),
# so the column index directly maps to the sub-terrain name.
sub_terrain_names = list(terrain_generator.sub_terrains.keys())
terrain_origins = terrain.terrain_origins
assert terrain_origins is not None
num_cols = terrain_origins.shape[1]
if num_cols == len(sub_terrain_names):
types = terrain.terrain_types
Expand Down
57 changes: 57 additions & 0 deletions tests/test_velocity_terrain_curriculum.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,60 @@ def test_early_termination_owes_the_time_left():
terrain_levels_vel(env, torch.arange(1), command_name="twist")

assert terrain.terrain_levels.tolist() == [2]


def test_promote_and_demote_rates_reflect_reset_batch():
"""promote_rate and demote_rate measure only the resetting batch."""
terrain = _FakeTerrain(num_envs=6, size=8.0)
terrain.terrain_levels[:] = 3
env = _make_env(terrain, common_step_counter=1, walked=0.0, commanded=4.0)
# Reset envs [0, 1, 2, 3]:
# env 0: walked 4.0 >= 3.2 -> promote
# env 1: walked 3.5 >= 3.2 -> promote
# env 2: walked 2.5 in [2.0, 3.2] -> stay
# env 3: walked 1.0 < 2.0 -> demote
# Envs 4, 5 do not reset and must not dilute the rates.
asset = env.scene["robot"]
asset.data.root_link_pos_w = torch.tensor(
[
[4.0, 0.0, 0.0],
[3.5, 0.0, 0.0],
[2.5, 0.0, 0.0],
[1.0, 0.0, 0.0],
[10.0, 0.0, 0.0],
[0.0, 0.0, 0.0],
]
)

out = terrain_levels_vel(env, torch.tensor([0, 1, 2, 3]), command_name="twist")

assert torch.isclose(out["promote_rate"], torch.tensor(0.5))
assert torch.isclose(out["demote_rate"], torch.tensor(0.25))


def test_frac_at_max_counts_envs_on_top_row():
"""frac_at_max reports the fraction of all envs on max_terrain_level - 1."""
terrain = _FakeTerrain(num_envs=4, size=8.0)
top_row = terrain.max_terrain_level - 1
terrain.terrain_levels = torch.tensor([0, top_row, top_row, 5], dtype=torch.long)
# Use common_step_counter=0 so levels are frozen and rates are 0.
env = _make_env(terrain, common_step_counter=0, walked=10.0, commanded=10.0)

out = terrain_levels_vel(env, torch.arange(4), command_name="twist")

assert torch.isclose(out["frac_at_max"], torch.tensor(0.5))
assert out["promote_rate"].item() == 0.0
assert out["demote_rate"].item() == 0.0


def test_empty_reset_batch_reports_zero_rates_without_nan():
"""Empty env_ids returns finite zero promote/demote rates."""
terrain = _FakeTerrain(num_envs=4, size=8.0)
env = _make_env(terrain, common_step_counter=1, walked=5.0, commanded=5.0)

out = terrain_levels_vel(env, torch.empty(0, dtype=torch.long), command_name="twist")

assert not torch.isnan(out["promote_rate"])
assert not torch.isnan(out["demote_rate"])
assert out["promote_rate"].item() == 0.0
assert out["demote_rate"].item() == 0.0
Loading