Terminal cost always 0 when using parameters for cost function weights in 'external cost' formulation

Hello,
I am using the Matlab interface and am trying to implement an NMPC.
To tune this NMPC effectively, it would be helpful to be able to set the cost function weights after compilation of the MEX files.
I have tried setting them through parameters as suggested in this forum, but realized that there is a difference in the results between the model with the hard coded weights and the weights set by parameters. I use the cost type ext_cost.

I was able to get rid of the difference by setting the terminal cost of the version with the hard coded weights to 0.
I have reproduced this with the “pendulum_on_cart_model” from the Matlab examples of acados.
modified pendulum on cart example

The problem does not exist when using cost type linear_ls or ‘nonlinear_ls’, but does for using auto if it is reformulated automatically to linear LS.
I thus assume this is a bug in the implementation of the external cost module.

I can at least for now reformulate my cost to a nonlinear least squares form, but I wanted to inform you about my findings.
If you can hint me where this bug might be, I can have a look at it and see if I can fix it.

1 Like

Hi Christian,

The bug is probably in the cost detection then.
Maybe I should add a note that this is not thoroughly tested for parametric cost functions.
But of course a fix would be appreciated, you can maybe check this file: https://github.com/acados/acados/blob/master/interfaces/acados_matlab_octave/detect_cost_type.m

You can do this for linear_ls and/or nonlinear_ls cost types by using something like:

ocp.set('cost_W', W, stage_index)

This seems like the best option for what you want to do.

Best,
Jonathan