Skip to content

nav: a differentiable power model, fitted to the P3 ledger, and energy in the dynamic loss (P8c) - #782

Merged
transfix merged 1 commit into
masterfrom
feat/p8c-power-model
Oct 24, 2026
Merged

transfix merged 1 commit into
masterfrom
feat/p8c-power-model

Conversation

@transfix

Copy link
Copy Markdown
Owner

P8c of docs/roadmap/NAV-VEHICLE-PHYSICS-ROADMAP.md: a differentiable power model, calibrated to the P3 energy ledger, with energy as an optional term in the dynamic rollout's loss.

The model

diff::power_step (inc/cvc/nav/detail/dyn_power.h) gives what the dynamic vehicle draws from its fuel or battery over a step. It reads the vehicle's state at the end of the step:

E = dt (k_pos P+ + k_neg P- + p_idle + c_v |vx| + c_v3 |vx|^3 + c_lat m (vx r)^2 |vx|),   P = m a vx
  • Smooth: P+ and P- are smooth positive and negative parts, and |vx| a smooth magnitude.
  • Derivatives: in vx, a and r.
  • SI form: nav/dyn_power.h holds the SI parameters and converts them into the normalized frame by length exponent, as dyn_bike_params_of does.
  • Each term: k_pos and k_neg are a Willans line (k_neg ≤ 0 is regeneration). p_idle is the draw at rest. c_v and c_v3 cover rolling and drag where the dynamic vehicle leaves them out. c_lat is cornering loss.

Calibrated to the ledger

  • Energy in the log: planar_sample now carries each tick's energy (nav_plant::energy_step), and samples.csv gains an energy column.
  • The fit: fit_power / evaluate_power in physics/plant_log.h. It is least squares on each tick's energy, with each coefficient's sign held.
    • It reads the drive force the dynamic vehicle needs to make the plant's motion: the measured acceleration, less that vehicle's own drag, rolling resistance and grade. The trainer's rollout carries the same force as dyn_state::a.
    • A first version read nav's command instead, and missed all the resistance work at cruise. Jolt's driver model holds speed with its own trim, so the command there is about zero.
  • From the CLI: cvc plant-log --fit-power fits every seed but the last, reports the last, and writes the result to residual.json's "power".

Measured (PlantLogPowerConvergence, Windows, Jolt): each vehicle fitted on four seeds of both scenes (30 s each) and judged on two held-out seeds:

Vehicle Held-out tick r² Total Swerve City Worst run
Humvee 0.83 +1.4% +9.0% −1.2% 13%
RoboRacer 0.91 −1.4% +17.7% −3.5% 35%

Limits, also in docs/NAV_TRAINING.md:

  • The RoboRacer's slowest swerve seed is over-predicted.
  • Humvee runs at the same speed can draw very different energy, most likely from the gearbox. A planar model has no gear.

In the loss

  • Settings: train_config::w_energy and train_config::power, with rollout_kind::dynamic.
  • The term: each step adds w_energy / window · E_t / (hdt · P_full) per agent. That's the window's mean power as a fraction of full power, so it is O(1), like w_coll.
  • Gradient: the term's adjoint lands on (vx, r, a) after the step, before the rollout's reverse pass.
  • Off by default: w_energy = 0 leaves the loss and its gradient bit for bit.
  • Checks: it throws without the dynamic rollout, or with a power model whose mass isn't dyn's.

Tests (all passing locally on Windows with Jolt)

  • nav_coef_train_test (17 tests):
    • PowerStepMatchesFiniteDifferenceAndConverts: the step's derivatives against central differences, and the SI conversion.
    • EnergyTermNeedsADynamicVehicleAndIsOffByDefault: the checks, and bit-for-bit off.
    • EnergyGradcheckMatchesFiniteDifference: the energy term's own gradient, direction error 9.0e-4.
    • EnergyTrainingDrawsLessEnergy: over 60 steps from the same start, the energy term falls 0.970 → 0.937 with it, against 0.962 without it.
  • physics_plant_log_test (11 tests):
    • FitRecoversAKnownPowerModel.
    • LogsTheEnergyEachTickDrew (Jolt).
    • PlantLogPowerConvergence.FitsTheLedgerAndPredictsHeldOutSeeds (Jolt, about 85 s; in a Convergence suite, so the recipe CI skips it).
  • cvc_cli_test PlantLog* (6 tests).
  • Unchanged and passing: physics_energy_test (11), nav_plant_test (18), nav_dyn_bike_test (20), nav_dyn_4w_test (16).

Not in this PR

  • The Jolt gate: that a policy trained with the energy term draws less per km on Jolt, for what it gives up in reach, over paired seeds.
  • The four-wheel plant's rollout.
  • The GRL-SNAM torch twin: like any change to the loss, it must reach sdf_nav's train_bicycle before the two are compared.

…y in the dynamic loss (P8c)

diff::power_step (detail/dyn_power.h) prices what the dynamic vehicle draws from its fuel or
battery over a step, from its state at the step's end:

  E = dt (k_pos P+ + k_neg P- + p_idle + c_v |vx| + c_v3 |vx|^3 + c_lat m (vx r)^2 |vx|)

with P = m a vx the drive power, smooth parts, and its derivatives in vx, a and r. nav/dyn_power.h
holds its SI form and the conversion into the normalized frame (by length exponent, as
dyn_bike_params_of).

Calibration (physics/plant_log.h):
- planar_sample carries each tick's energy (nav_plant::energy_step); samples.csv gains the column.
- fit_power fits the six coefficients by sign-constrained least squares on the ticks' energies,
  reading the drive force the dynamic vehicle needs to make the plant's motion (the measured
  acceleration less its own drag, rolling resistance and grade); evaluate_power reports r2, the
  total and the worst run on held-out runs.
- cvc plant-log --fit-power fits every seed but the last and reports the last (residual.json's
  "power").
- Fitted on four seeds of the swerve and the city and judged on two held out, the totals come
  within +1.4% (Humvee) and -1.4% (RoboRacer); the swerve alone misses by +9.0% and +17.7%
  (PlantLogPowerConvergence).

The loss (train_config::w_energy, train_config::power; rollout_kind::dynamic):
- each step adds w_energy / window * E_t / (hdt * P_full) per agent, P_full the source power at
  full drive and top speed, so the term is O(1); its adjoint lands on (vx, r, a)_{t+1};
- 0 leaves the loss and its gradient bit for bit; without the dynamic rollout or with another
  vehicle's mass it throws;
- the energy term's gradcheck: direction error 9.0e-4; over 60 steps it lowers the energy term
  0.970 -> 0.937, against 0.962 without it.

Open: the Jolt gate (a policy trained with it draws less per km for what it gives up in reach),
the four-wheel plant's rollout, and the torch twin.
@transfix
transfix merged commit 41673b3 into master Oct 24, 2026
24 checks passed
@transfix
transfix deleted the feat/p8c-power-model branch October 24, 2026 05:43
transfix added a commit that referenced this pull request Oct 24, 2026
Carries the master merge (#776 squash, #774, #775, #777, #780, #782,
#784, #785) up the H1 stack.
transfix added a commit that referenced this pull request Oct 29, 2026
…at hit no other vehicle (#875)

PlantLogPowerConvergence.FitsTheLedgerAndPredictsHeldOutSeeds has failed on every package-linux
release lane since #782 added it (41673b3): the RoboRacer's held-out tick r2 is 0.598 against
> 0.8, the same on every push run since. It was calibrated on Windows, where it is 0.91, and the
Linux fit has not moved since.

The runs that drag it down are city runs in which the vehicle hit another one. On Windows the
RoboRacer's two worst held-out runs (-35%, -8%) are the only two that touched another vehicle:
two contact ticks and two impact ticks each, then a long stall with the wheels spinning. Every
other run is within 8%, though nearly every city run touches the scene's walls. On Linux, four of
its city runs miss, by -52% to +31%. The planar power model has no collisions and no wheel spin
(docs/NAV_TRAINING.md), so no term of its state sees that energy. Which runs collide depends on
the platform's floating point.

- planar_sample gains vehicle_contact: the other agent the vehicle touched that tick, from
  nav_plant::vehicle_contact_step (-1 none).
- The test judges the held-out prediction on the runs that hit no other vehicle (no
  vehicle_contact and no impact past kImpactG), and requires them to be at least three quarters
  of the held-out runs. The others are printed with their errors. The fit itself is unchanged and
  still uses every run and tick.
- docs/NAV_TRAINING.md: the measured table on that basis (Windows: Humvee r2 0.90, RoboRacer
  0.97, 22 of 24 runs each), and the excluded runs.
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