Evaluate a piecewise polynomial (spline)

Hi,

I have to evaluate a piecewise polynomial (PP) in the cost function of the solver, but I’m not sure how I can include that in the CasADi expression. The piecewise polynomial is the result of Matlab’s spline(…) function for a large amount of data. So in pure Matlab one would use ppval(…) for the evaluation at a certain value “x”. In my case “x” is a SX.sym from CasADi. Does anybody know how I could do that?

I’ve already tried to manually do the calculation (find the right interval with a for-loop, then compute the polynomial with the corresponding coefficients). But it seems like if-statements and for-loops don’t work with CasADi.

BR
Martin

Hi Martin,

CasADi implements its own splines
https://web.casadi.org/docs/#using-lookup-tables
There is also a forum topic you should check out

Cheers,
Jonathan

Hi Jonathan,

CasADi implements its own splines

If I use CasADi splines I have the same problem as with ppval(…). The position where I evaluate the spline is an optimization variable (SX.sym). For example using the following lines of code

x = -1 : 0.1 : 1;
y = randn(size(x));

% state variable of dynamics equation
x_eval = SX.sym('x_eval'); 

pp_casadi = casadi.interpolant('pp', 'bspline', {x}, y);

% meaningless cost function just for demonstration
expr_ext_cost = pp_casadi(x_eval)^2;

results in the following error during code generation:

Error using casadi.Function/call (line 935)
.../casadi/core/function_internal.cpp:1821: 'eval_sx' not defined for BSplineInterpolant

Error in casadi.Function/paren (line 1763)
        res = self.call(varargin);

Error in casadi.Function/subsref (line 1744)
        [varargout{1:nargout}]= paren(self, s.subs{:});

Error in acadosNLPFormulation (line 46)
    expr_ext_cost = pp_casadi(x_eval)^2;

BR
Martin

Hi Martin,

CasADi splines have to be used with MX variables.
The following works for me:

import casadi.*

x = -1 : 0.1 : 1;
y = randn(size(x));

% state variable of dynamics equation
x_eval = MX.sym('x_eval'); 

% meaningless cost function just for demonstration
pp_casadi = casadi.interpolant('pp', 'bspline', {x}, y);
expr_ext_cost = pp_casadi(x_eval)^2;

Best,
Jonathan

Yes thanks, indeed it works with MX variables for me too. However, I also have to change all other variables of my optimization problem from SX to MX, otherwise I get another compilation error.

Is there a special reason why in the Matlab examples (except for the windturbine) all variables are SX? Does using MX increase computation times during MPC execution or something like that?

BR
Martin

One reason is that structure detection, especially simplification of expressions is not really possible for MX variables.
For example for the gnsf_irk integrator, the implicit DAE expression is automatically transformed into another structure, such that linear dependencies can be exploited in the integrator.
I developed this during my master thesis, see https://cdn.syscop.de/publications/Frey2019.pdf https://cdn.syscop.de/publications/Frey2018.pdf

I was also thinking implementing some basic structure detection for cost and constraint expressions in Matlab, where similar issues occur with MX.

Performance wise, MX should be better for vectorized operations. However, the conceptional idea of acados is to only generate the nonlinearities as CasADi functions and perform the linear algebra with BLASFEO. Thus, I think that the effects on performance are rather small, but one could investigate this.

Best,
Jonathan