Create external cost function

Hi :wave:

I am implementing a mhe algorithm with acados python interface. For the mhe I would like to formulate a external cost function. Is there a example, that I can refer to?

Especially, how to set the reference variable?

for example, set x_prior, ymeas as reference.

arrival_cost = 2 * (eta ** N) * mtimes((x - x_prior).T, mtimes(P, (x - x_prior)))
measurement_error_cost = sum(disc_factor[j] * mtimes((y - ymeas).T, mtimes(R, (y - ymeas))) for j in range(N))

Thanks.

Hi :wave:

Here is an example that showcases different cost formulations

Note that you will need to additionally define a separate cost for the initial stage via model.cost_expr_ext_cost_0 in order to additionally include the arrival cost term.

Best, Katrin

Hi Katrin,

thanks for the example and the note.

I have formulated a external cost function. But I have still the issue: how to set the reference term. For β€˜LS’ or β€˜NLS’ man can give the arrival cost and measurement though set the yref for initial stage and other stage, respectively. Apparently this term yref is useless for external cost function. How can I tell acados, which term is a reference in the function equation so that the values can be provided to the solver in the main process?

Best
Lingbing

Ahh sorry, y_ref needs to be defined as parameters p. After solver creation, the values of the parameters can be set stagewise.

Hi,

thanks for the hint.

I have already successfully implemented mhe using external cost function. It performs well in the modified example -pendulum on cart.

for this topic I have just one question:
In which situation should I compute the Hessian matrix manually? for now, I let acados compute it automatically.

If it works well, you don’t need to do anything :grinning:

If you encounter problems that might be due to an indefinite hessian, you can think about using a positive definite approximation.

1 Like