Skip to content

coef_train: terrain-risk steering lever + opt-in w_risk exposure loss - #91

Merged
transfix merged 2 commits into
mainfrom
feat/nav-risk-lever
Sep 23, 2026
Merged

transfix merged 2 commits into
mainfrom
feat/nav-risk-lever

Conversation

@transfix

Copy link
Copy Markdown
Contributor

PR2 of the terrain-risk training program (buckets landed in #90). Makes the policy act on the terrain-risk signal — learn to spend less time in risk areas — instead of just measuring it. Everything defaults off, so a plain coef_train call is byte-identical to the geometry-only trainer.

The lever (three pieces)

  • Risk-lookahead feature — coef_feats(material=…) appends the WORST terrain risk between the vehicle and its carrot (the terrain twin of the grip μ-probe; MAX not MIN), differentiable through the trajectory. add_risk_feature(model) widens a trained net to see it, output-identical at init (the widen-basin trick, like widen_coef_mlp for grip). CoefMLP grows use_mu/use_risk flags so a 6-in net's optional column is unambiguous.
  • Reroute force + exposure loss — train_bicycle(material=, w_risk=, lam_soft=, lam_hard=): the material force F_soft = -lam_soft·∇risk steers around risk, and w_risk·Σ risk(pose) penalizes dwelling in it (the 3rd last_loss_terms summand). Only the bicycle rollout can learn this; a risk net without material= is rejected.
  • Matched corpus — material.city_material_grid() returns a (MaterialGrid, MaterialIdRaster) pair so the differentiable risk force/feature/loss and the reported buckets see the SAME terrain.
  • CLI — --w-risk/--lam-soft/--lam-hard/--risk-lookahead (+ --grid). A risk .cvcnav is a torch research artifact (the C++ coef_feats has no risk column yet) — the CLI warns, and --score skips widened nets (the Swarm drive is base-5-feature).

Measurement (evidence it works)

Smoke study (grid 64, 40 steps, lam_soft=0.4, 2 seeds — indicative):

arm risk exposure reach
w_risk=0 0.108 0.166
w_risk=8 0.082 (−24%) 0.172

Both seeds show the drop; reach is unhurt — a Pareto improvement, and evidence the additive feature + fixed reroute already work (the critic predicted α/β/γ alone can only "slow down"; the fixed F_soft supplies the reroute). A learned lam head (PR2b) is a planned enhancement, not a prerequisite; a full 3-seed/longer-horizon study on a stable/GPU box is the validation of record (BPTT is CPU-fragile).

Tests

tests/test_risk_lever.py: feature append + byte-identical default, MAX-ahead + differentiability, add_risk_feature identity/flags/stacking (→7 with grip), the w_risk loss wiring, and the risk-needs-material guard. Two train_bicycle tests updated for the new error message and 3-tuple last_loss_terms. 90 tests green locally across the material/coef/nav/vehicle suites.

Next: PR3 scorecard-driven curriculum; a cross-repo PR wires the native sim_world material_id + a learned-lam deployable head + C++/Python parity gate (which also makes the Swarm drive risk-aware so --score covers risk models).

Make the policy learn to spend less time in terrain-risk areas — the "act on the
signal" half of the material nav-stats work (buckets landed in #90). Three pieces,
all default-off so a plain call is byte-identical to the geometry-only trainer:

- sdf_nav.coef_feats(material=): appends the WORST terrain risk between the vehicle
  and its carrot (the terrain twin of the mu-probe, MAX not MIN), differentiable
  through the trajectory. add_risk_feature(model) widens a trained net to see it,
  output-identical at init (the widen-basin trick, like widen_coef_mlp for grip);
  CoefMLP grows use_mu/use_risk flags to disambiguate a 6-in net's optional column.
- train_bicycle(material=, w_risk=, lam_soft=, lam_hard=): the material reroute force
  (F_soft = -lam_soft*grad risk) steers around risk, and a w_risk*Sum(risk) term
  penalizes dwelling in it (reported as the 3rd last_loss_terms summand). Only the
  bicycle rollout can learn this; a risk net without material= is rejected.
- material.city_material_grid(): a matched (MaterialGrid, MaterialIdRaster) pair so the
  differentiable risk force/feature/loss and the reported buckets see the SAME terrain.
- CLI: --w-risk/--lam-soft/--lam-hard/--risk-lookahead + --grid. A risk .cvcnav is a
  torch research artifact (the C++ coef_feats has no risk column yet) — the CLI warns,
  and --score skips widened nets (the Swarm drive is base-5-feature).

Smoke measurement (grid 64, 40 steps, lam_soft 0.4, 2 seeds): w_risk=8 cuts terrain-risk
exposure ~24% (0.108->0.082) with reach unhurt (0.166->0.172) on both seeds — a Pareto
improvement, and evidence the additive feature + fixed reroute already work (a learned
lam head is an enhancement, not a prerequisite). Full 3-seed study documented as follow-up.

tests/test_risk_lever.py pins the feature append + byte-identical default, MAX-ahead +
differentiability, add_risk_feature identity/flags/stacking, the w_risk loss wiring, and
the risk-needs-material guard. Updated two train_bicycle tests for the new error message
and the 3-tuple last_loss_terms.
… test + temper the smoke claim

Three review-driven improvements (no behavior change to the lever):
- eval_risk_exposure(model): the direct bicycle-rollout harness behind the smoke table,
  so the numbers are one-command reproducible and a widened net has a real eval path
  (feeds the risk column automatically; Swarm/scorecard_eval stay base-5-feature).
- test_risk_feature_is_max_ahead: was fwd>=here (a tautology since the probe set includes
  o); now a hand-built clear-underfoot / risky-ahead scene asserts fwd > here strictly, so
  a degenerate lookahead (reach=0 / o-only) fails it. Adds an eval_risk_exposure smoke test.
- docs: temper 'reach not hurt / Pareto improvement' to 'exposure down, reach not measurably
  changed' (the +0.006 reach delta is within 2-seed noise) and point at eval_risk_exposure.
@transfix
transfix merged commit 09e21d0 into main Sep 23, 2026
3 checks passed
@transfix
transfix deleted the feat/nav-risk-lever branch September 23, 2026 05:53
transfix added a commit that referenced this pull request Sep 23, 2026
…#94)

* sdf_nav: learned lam head — the deployable per-position reroute lever

The reroute force F_soft = -lam_soft*grad(risk) is what actually steers around terrain
risk; PR #91 drove it with a FIXED lam_soft dial. This adds a learned lam head so the
policy outputs lam_soft per position (stronger near risk, weaker in the open) — the
deployable lever, and the piece the critic flagged as the real answer to "reroute, not
just slow down".

- CoefMLP(use_lam=): a 4th OUTPUT lam_soft. The output stack stays one uniform
  softplus(net + log(expm1(out_bias))) — full_out_bias() appends lam_init — so the
  exporter and the C++ forward need NO per-output special case; only the drive reads
  output[3]. forward() still returns (a,b,g) (backward compatible); coeffs_and_lam()
  returns the 4th. add_lam_head(model) widens identity-preserving (a/b/g bit-unchanged,
  lam starts at lam_init), mirroring widen_coef_mlp/add_risk_feature.
- train_bicycle + eval_risk_exposure + the Swarm drive use the LEARNED lam for a use_lam
  net instead of the fixed dial; a lam net needs material= (raises otherwise) and forces
  the torch drive. coef_export writes the 4-output .cvcnav; scorecard_eval attaches the
  grid so a lam net is scored as deployed. CLI: --learned-lam (--lam-soft = the init).

Measured (grid 64, 40 steps, 2 seeds): learned lam trims terrain-risk exposure a further
~3% over the fixed dial (0.082 -> 0.079) at equal reach, consistent across seeds. A risk
.cvcnav stays a torch artifact until the coordinated C++ coef_mlp/drive update (next).

tests/test_lam_head.py: identity+flags, forward backward-compat, coeffs_and_lam guard,
full_out_bias + 4-output export, learns-off-init, needs-material, and Swarm scoring.

* review: reject lam_init<=0 (dead/NaN lam head)

Adversarial review CONFIRMED (forward-compat + export dims clean): add_lam_head /
CoefMLP(use_lam) with lam_init<=0 silently broke — log(expm1(0))=-inf freezes the lam
output at 0 with a dead gradient (the 'learned' lever never trains), and lam_init<0 gives
log(expm1(<0))=NaN that poisons the drive/loss/weights/.cvcnav. Reachable via the documented
--learned-lam --lam-soft 0 (--lam-soft 0 is valid for FIXED mode but a dead learned init).
Guard lam_init>0 in CoefMLP.__init__ (covers add_lam_head) + a clear CLI error in main.
Tests: constructor/add_lam_head/CLI rejection.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant