Using Prameters in cost_expr_ext_cost

Hi all,

I have formulated an MPC using ACADOS. I am using the cost module “auto” and I want to use a model parameter (sym_p = PDem) in the cost function (I want to track some given power PDem). It looks something like this:

cost_expr_ext_cost = weight * (sym_x(3) + sym_x(4) -sym_p)

When I create an OCP solver instance, I am getting the following error:

“Error using casadi.Function/call (line 942)
…/casadi/core/sx_function.cpp:71: Cannot evaluate “jac_fun:(i0[3])->(o0[3]) SXFunction” since variables [PDem] are free.”

Is it possible to use a parameter in the cost function, or can I only use them in the process model?

I know, I can also use the cost module “nonlinear least squares”, and give sym_p (PDem) as the reference (y_ref). However, the formulation of my cost function would be much simpler if I use the cost module “auto”.

Thanks Jannis

Hi Jannis,

Is it possible to use a parameter in the cost function, or can I only use them in the process model?

Yes, that should be possible.
Please double check that you set the parameters correctly in your model.
Otherwise, you can provide a minimal example to reproduce the error?

Best,
Jonathan

Hi Jonathan,

I have created a minimal example with the ‘pendulum_on_cart_model’.

In the ‘pendulum_on_cart_model’ I have created a parameter called pRef, which is the reference position I would like to track. In the function, line 52, 57 and 114 are added by me, otherwise it is the default ‘pendulum_on_cart_model’

In the main I define the cost in line 121 to line 129.

When calling main I get the error I mentioned earlier:

…/casadi/core/sx_function.cpp:71: Cannot evaluate “jac_fun:(i0[3])->(o0[3]) SXFunction” since variables [pRef] are free.

You can find the minimal example here: minimal_example.zip - Google Drive

Thanks

Jannis

Hi Jannis,

as suspected, something like this

ocp_model.set('sym_p', model.sym_p);

is missing in your code.

Btw, if you are new to using acados with Matlab, I would highly encourage to use the new interface directly, as announced here:

Cheers,
Jonathan

2 Likes

Hi Jonathan,

sorry for the late response, I was on vacation.

That solved my problem.

I took a look at the new interface and I wondered how to include the simulink_opts for specifying the in and output of the s-function in the new interface?

Thanks Jannis