Callable Objective Function for Casadi using Spline of scipy

607 views
Skip to first unread message

Syed Taha Munir

unread,
Jan 29, 2015, 9:38:18 AM1/29/15
to casadi...@googlegroups.com
Dear All
 
In order to use the non linear solvers in casadi we have to write our objective fucntion in the form of Symbolic data type as facilitated in casadi .
 
But I have to write an Objective Function which is not in the form of equation , instead I have recieved data set of 29 values and the regression is not working good in my case.I have constructed my objective function using spline function  using from" scipy.interpolate import UnivariateSpline "and solved it with Scipy libraries "from scipy.optimize import minimize" because they accept the callable functions as the objective functions but Scipy solvers are really slow as i have more than 5000 variables
 Can i implement the same strategy of spline function in Casadi building some callable objective function for NLP solvers to benefit from the speed of CasADi solvers.
 
Thanking you in anticipation
Kind regards
Syed Taha Munir

Joel Andersson

unread,
Jan 29, 2015, 10:29:52 AM1/29/15
to casadi...@googlegroups.com
Hello Syed,

If you want to use CasADi for the optimization, it difficult to use the splines from scipy. You would have to reimplement it in CasADi symbolics, using a series of "if_else" calls. Unfortunately, CasADi does not have a better way to handle look-up tables.

The current implementation of "if_else" in CasADi is now "short circuiting", so both the "true' branch and the "false" branch will always be evaluated. This is usually fine for small lookup tables, but for large data sets, this could become a bottleneck.

Best regards,
Joel

Syed Taha Munir

unread,
Jan 29, 2015, 10:49:09 AM1/29/15
to casadi...@googlegroups.com
Thanks for prompt reply Joel
 
I had tried to implement my cost function by dividing my curve into two pieces and implemented sort of piecewise regression of 2nd or 3rd order on both sides using if_else in casadi.
C2[0],C2[1],C[2] = Coeffecients of 2nd order regression for first part of curve
C2r[0],C2r[1],Cr[2] = Coeffecients of 2nd order regression for 2nd part of curve
 
 
f=if_else(x[A]>0,((C2[0]*x[A]**2)+(C2[1]*x[A])+C2[2]),((C2r[0]*x[A]**2)+(C2r[1]*x[A])+C2r[2]))
 
But spline itself make it piecewise between every two point which is working very efficiently in my situation. how can i implement this in casadi using if_else because i have only 29 pairs of data points and I can try to implement it
 
Kind regards

Greg Horn

unread,
Jan 29, 2015, 11:50:59 AM1/29/15
to casadi...@googlegroups.com
It is also possible to embed Python code in casadi using CustomFunction. You would basically create a special Function which you can use `call` on and casadi would know how to propagate derivatives.


This is not for the faint of heart

Syed Taha Munir

unread,
Jan 30, 2015, 2:29:20 AM1/30/15
to casadi...@googlegroups.com
Hi Greg
Thanks for the reply and the link .
Do you think that , it is possible to write "scipy.interpolate import UnivariateSpline" as a custom function.
 
Kind regards
Taha

Greg Horn

unread,
Jan 30, 2015, 5:07:39 AM1/30/15
to casadi...@googlegroups.com
Hi Syed,

It's possible to embed any possible python code you could dream up as a CustomFunction, so yes UnivariateSpline falls into that category.

The problem is that it is up to you to make sure that is actually a smooth function, and you've implemented the derivative rule correctly (and efficiently). Otherwise, your NLP solver will not converge and you won't know why. Also, I believe it's more difficult to embed higher-order derivatives.

Please let us know if this is successful. It'd be a very useful code to add to the official casadi examples and unit tests.
Reply all
Reply to author
Forward
0 new messages