Skip to content

heuristic for estimating the number of non-zeros in constraint_jacobian#1241

Merged
thowell merged 3 commits intogoogle-deepmind:mainfrom
thowell:constraint_jacobian_nonzeros_heuristic
Mar 24, 2026
Merged

heuristic for estimating the number of non-zeros in constraint_jacobian#1241
thowell merged 3 commits intogoogle-deepmind:mainfrom
thowell:constraint_jacobian_nonzeros_heuristic

Conversation

@thowell
Copy link
Collaborator

@thowell thowell commented Mar 17, 2026

Estimate the number of non-zeros (njmax_nnz) in the sparse constraint Jacobian.

Non-contact constraints (assume all active)

Type Rows Non-zeros per row
Connect equality 3 _body_pair_nnz(body1, body2)
Weld equality 6 _body_pair_nnz(body1, body2)
Joint equality 1 1 or 2
Tendon equality 1 merged column union of both tendons
Flex equality per edge flexedge_J_rownnz
DOF friction 1 1
Tendon friction 1 ten_J_rownnz
Joint limit 1 1 (slide/hinge) or 3 (ball)
Tendon limit 1 ten_J_rownnz

Contact constraints

  1. Enumerate all enabled collision pairs:
    • Contact pairs (pair_geom1/2)
    • Filter geom-geom (filtered by contype/conaffinity, deduplicated by body pair)
    • Flex vertex-to-geom and flex self-collision
  2. Compute _body_pair_nnz(body1, body2) for each pair
  3. Take the maximum non-zeros across all pairs: max_contact_nnz
  4. Contact non-zeros = njmax × max_contact_nnz

Estimate

njmax_nnz = min(non_contact_nnz + njmax × max_contact_nnz, njmax × nv)

Helper: _body_pair_nnz(body1, body2)

Counts unique DOFs in the kinematic tree union of two bodies by walking up
both dof_parentid chains simultaneously (same algorithm as runtime constraint kernels).

Validation for benchmark scenes* with sparse constraint Jacobians

Scene nv njmax nconmax njmax*nv heuristic actual ratio savings
aloha_cloth 2716 6300 920 17,110,800 103,710 66,740 1.55 99%
aloha_pot 23 128 24 2,944 1,818 1,414 1.29 38%
apollo_flat 25 64 16 1,600 1,190 405 2.94 26%
apollo_hfield 25 128 32 3,200 2,342 693 3.38 27%
apollo_terrain 25 96 48 2,400 1,766 789 2.24 26%
cloth 2706 3000 200 8,118,000 42,486 22,644 1.88 99%
franka_panda 9 5 1 45 45 4 11.25 0%
humanoid 27 64 24 1,728 1,365 579 2.36 21%
three_humanoids 81 192 100 15,552 5,823 1,674 3.48 63%

*aloha sdf skipped

Copy link
Collaborator

@erikfrey erikfrey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, just two minor comments

@thowell thowell force-pushed the constraint_jacobian_nonzeros_heuristic branch from aa2ef9d to fd6da8c Compare March 22, 2026 11:11
@thowell thowell merged commit 04fc3e1 into google-deepmind:main Mar 24, 2026
10 checks passed
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.

2 participants