Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix running AD through jaxsim.math.Rotation.from_axis_angle #277

Merged
merged 1 commit into from
Nov 5, 2024

Conversation

diegoferigo
Copy link
Member

@diegoferigo diegoferigo commented Oct 30, 2024

In these days, I tried to solve an unconstrained non-linear optimization problem using the JaxSim functions to construct the cost function. I used the L-BFGS solver from optax, and it works flawlessly 🎉

What I'm trying to solve is quite usual in AMI. I want to compute the initial configuration of a humanoid robot such that the knees and the elbows are slightly bent, the soles of the feet are in contact, and the feet are slightly more distant w.r.t. the configuration with zero joint positions. The optimized variables are the joint positions and the base height. The corresponding optimization problem computes FK in the cost function (exploiting AD support through the JaxSim function).

I had to fix the gradient propagation as in this PR to prevent having problems either with zero division in the jitted function or the same problem in the gradient computed with AD. The trick I used it the double-where workaround, as described in:


📚 Documentation preview 📚: https://jaxsim--277.org.readthedocs.build//277/

@diegoferigo diegoferigo self-assigned this Oct 30, 2024
@diegoferigo diegoferigo force-pushed the fix_ad_through_axis_angle branch from be9f3bf to 73eee01 Compare November 5, 2024 11:37
@diegoferigo diegoferigo marked this pull request as ready for review November 5, 2024 11:38
@diegoferigo
Copy link
Member Author

Note: probably a similar trick can be used to remove the small tolerances that we always had in SoftContacts:

# There are few operations like computing the norm of a vector with zero length
# or computing the square root of zero that are problematic in an AD context.
# To avoid these issues, we introduce a small tolerance ε to their arguments
# and make sure that we do not check them against zero directly.
ε = jnp.finfo(float).eps

Copy link
Collaborator

@flferretti flferretti left a comment

Choose a reason for hiding this comment

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

Thanks Diego, LGTM!

@diegoferigo diegoferigo merged commit bd59060 into main Nov 5, 2024
24 checks passed
@diegoferigo diegoferigo deleted the fix_ad_through_axis_angle branch November 5, 2024 13:46
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