-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Provide gradients of a MathematicalProgram solution #4267
Comments
This would be awesome. We're trying to run dircol on the Kuka arm right now, and this would let us use the dircol solver that's already implemented in Drake. cc @kuindersma |
Actually, @hongkai-dai, where does this fall on your list of priorities? This is pretty high-pri for the Draper project, so let me know if there's anything I can do to help push this through. |
@pvarin OK, I will try to work on this issue this week. Drake's dircol solver already works. This issue is to find the gradients of the optimal solution, w.r.t some parameters of the optimization problem. For example, a linear program
we want to know how the optimal solution Could you explain why you need this feature? |
Awesome! Thanks! We'd like to do some dynamic planning on the Kuka arm, which is a RigidBodyTree that loads it's model from a URDF. I've been following the Pendulum example to try to use Drake's dircol solver, and from what I can tell we need the RigidBodyPlant to be templated for AutoDiff types. If you have any other ideas on how to accomplish this I'd love to know. Thanks! |
Hmm, if that is the case, could you just comment out https://github.com/RobotLocomotion/drake/blob/master/drake/multibody/rigid_body_plant/rigid_body_plant.cc#L242~L313, and compute
If you do not hit the joint limits (which you can impose a constraint in your optimization, to see the bounds on each joint is tighter than the joint limits.), then you do not have any constraint force, and can compute I am afraid I have some other tasks piled up now. So we prefer to a hacky way to solve this issue for now. |
I agree with @hongkai-dai here. One important reason you cannot take gradients in Summarizing, most likely you don't need collision on a first approximation to run direct collocation for Kuka? and if that is the case you can disable the collision code as honkai-dai mentioned (though hacky I know, buts just to try the strategy). @RussTedrake, how do you guys take gradients through the collision engine in Matlab? what approach do you use when you need say gradients of minimum distance with respect to generalized coordinates? |
@amcastro-tri I've actually been working on a related problem in my work outside of Drake, where I'm taking the gradients of closest-point computations. I ended up implementing the GJK algorithm for generic types, which makes it easy to autodiff all the way through my collision detection code: https://github.com/rdeits/EnhancedGJK.jl I imagine this code won't actually be helpful to you, but I'm happy to share what I learn as I keep working on this. |
Robin, just FYI- I have yet to meet anyone who has coded a numerically
robust GJK implementation. We've had better mileage with v-clip.
…On Thu, Dec 1, 2016 at 1:24 PM, Robin Deits ***@***.***> wrote:
@amcastro-tri <https://github.com/amcastro-tri> I've actually been
working on a related problem in my work outside of Drake, where I'm taking
the gradients of closest-point computations. I ended up implementing the
GJK algorithm for generic types, which makes it easy to autodiff all the
way through my collision detection code: https://github.com/rdeits/
EnhancedGJK.jl
I imagine this code won't actually be helpful to you, but I'm happy to
share what I learn as I keep working on this.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#4267 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ACHwz-K3qhehJdGGQ9lR5aKKpcHzr87xks5rDzsigaJpZM4K9UcT>
.
|
@edrumwri understood, and I haven't even begun to test for numerical robustness. My particular problem is relatively forgiving of errors in individual distance computations, but I am running into issues extracting stable penetration distances when the objects are in collision. Maybe it's time to give V-clip another look... |
Huge number of topics discussed here... almost none of them related to this issue. :) @pvarin - you need autodiff for rigidbodyplant. That is issue #4187 , and is not related to this issue. @amcastro-tri - we should move your discussion there, too. |
This is useful for bi-level optimization, Benoit is likely to work on this over the summer intern. |
for bonus points: It would be really awesome if a system that setup and solved a mathematical program inside e.g. a derivatives/update/output method could easily use this to support autodiff. I don't think implementing |
Just to summarize the math to compute the gradient of the solution here. Say we have a generic optimization problem
where According to KKT condition, at the optimal solution, it satisfies
We can take the total derivative of the two functions above, and get
In order to compute
If we solve this linear equation (assuming the matrix on the LHS is invertible), we get the gradient It is non-trivial to implement this. We need the second order gradient |
Take inverse kinematics as an example, I think we need several infrastructures in order to compute the gradient of the optimal solution
|
We did a lot of nesting AutoDiff before -- it certainly works. Hopefully we don't feel like we've architected ourselves out of that. But what might it look like if we only did it for EvaluatorBase constraints that had support for Symbolic first? |
Agreed, we can make EvaluatorBase to work with nested AutoDiffScalar. I just want to outline the infrastructure we need to achieve the eventual goal -- computing the gradient of the motion planning solution. I can add the scalar type |
This came up in a discussion with @hongkai-dai about how to template the RigidBodyPlant dynamics method on AutoDiffXd. Right now, we cannot, because the call to MathematicalProgram in the middle only makes sense for doubles.
But most mathematical programs have well defined gradients. We should be able to provide them. For instance, for the constrained optimization in with f(vdot, q, v) = 0, once we have a solution, then we can evaluate the gradient of that solution (e.g. with respect to vdot) using dfdvdot + dfdq + dfdv = 0.
Even when we have inequality constrained optimization, this should work. f(x) >=0 can be evaluated as f(x) = 0 for all of the constraints that are active at the solution. For optimizations with objectives, we simply use the KKT conditions.
It should be possible to provide some nice interface for this. Perhaps we can even be clever enough that GetSolution(DecisionVariableMatrix) could be templated on scalar type, which returns the solution for double and the gradients for autodiff?
The text was updated successfully, but these errors were encountered: