Casadi and external API function embedding for NLP

272 views
Skip to first unread message

Mike

unread,
Aug 8, 2016, 10:03:15 AM8/8/16
to CasADi

Hi,

I am trying to integrate Casadi with a physics engine for solving a trajectory optimization problem. The physics engine has an API that gives me local liberalizations of its current state and input using finite difference. (I have no access to any analytical models of the system.)
So it will be like xdot = Ax+ Bu where A and B comes from the external physics engine for  a given x and u. I want to put this function as a dynamic constraint at the collocation points. Is there a way to incorporate this external function to the Casadi computational graph?
Is this possible and if possible, could anyone please provide an example on how to handle this? 

Best,

Michael

Joel Andersson

unread,
Aug 8, 2016, 10:45:49 AM8/8/16
to CasADi
Dear Michael,

The user guide (http://guide.casadi.org) has a chapter on how to embed external functions into computational graphs. The support for external functions is not complete however - the focus of CasADi has always been to efficiently and automatically calculate derivatives by algorithmic differentiation.

Fundamentally, I think you need to ask yourself if CasADi is really the tool you want to use for this application. If you want to use a collocation algorithm, maybe you are better of just using some other interface to e.g. IPOPT where you manually specify the Jacobian of the constraints? From your description it sounds like A and B (while dependent on x and u) are good approximations for d(xdot)/dx and d(xdot)/du, respectively. So it's just about putting the blocks in the right place.

Best regards,
Joel

Mike

unread,
Aug 8, 2016, 1:24:44 PM8/8/16
to CasADi
Dear Joel,

Thanks for the reply. It is not because the A and B matrices are very accurate, but since I don't have access to the analytical model of the sytem, (19DOF robot), this is the only way that I know to get the local model of the robot to implement constraints. I understand that Casadi was mainly intended for automatic differenctiation. Why I prefer it is, since I can switch between python and matlab interface without much change in code. But is there any alternate way (other than this A and B matrices) to use Casadi in this scenario with a Physics engine when you don't know the analytic equations?  Because Casadi is quite fast for the differentiation of cost functions and others apart from this A and B matrices. I thought I will try before I move on to a different library. 

Best,

Michael

Joel Andersson

unread,
Aug 11, 2016, 10:18:56 AM8/11/16
to CasADi
There are different ways of coupling external functions, as described in the user guide. None of the ways work very well when derivatives are involved. That's not to say that they don't work at all, or that we've been trying to get it to work, it's just that we're not quite there in terms of support.

One thing you can consider is to use CasADi only for the cost function and its derivatives. And export C code for the objective and/or gradient of the objective and/or Hessian of the objective.

Joel


Reply all
Reply to author
Forward
0 new messages