Casadi MHE optimation

447 views
Skip to first unread message

pegah darabi

unread,
Apr 16, 2021, 10:32:57 AM4/16/21
to CasADi
Hello friends.
I have used Casadi for the moving horizon estimation optimization method and there is an error in the solver:
'gradient' only defined for scalar outputs: Use 'jacobian' instead.
 Casadi is completely new to me and I do not know how I can solve this error. If anyone has experience in this field please help me.

Joris Gillis

unread,
Apr 16, 2021, 10:39:02 AM4/16/21
to CasADi
Dear Pegah,

This indicates that your optimization objective is not scalar.
Please check its dimensions with `size(.)` in Matlab or `*.shape` in Python.

Best regards,
  Joris

pegah darabi

unread,
Apr 16, 2021, 10:47:47 AM4/16/21
to CasADi
Hi Joris, 
Thanks for answering so quickly. I did not understand what you mean. I wrote the program in MATLAB. Which size of variable should I check? My optimization constrains are three equations. And I want to estimate the modes of my control system. 
should I send my Code?
Best regards,
pegah

Joris Gillis

unread,
Apr 16, 2021, 11:08:11 AM4/16/21
to pegah darabi, CasADi
An objective is an expression that should be minimized by the solver, by means of adapting the decision variables (unknowns) in the problem. In an estimation problem, and objective is typically a sum of squared terms. If you omit the sum, you end up with a non-scalar expression.

Best,
  Joris

--
Sent from CasADi's user forum at http://forum.casadi.org.
---
You received this message because you are subscribed to the Google Groups "CasADi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to casadi-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/casadi-users/1aac0143-3650-4e6b-b4e8-d063c050747bn%40googlegroups.com.

Andrea Tuveri

unread,
Jun 3, 2021, 6:34:20 AM6/3/21
to CasADi
Hi Joris, 

I have a similar problem when I try to implement direct collocation for the MHE. Here you find the code ( https://gist.github.com/detu92/59b4f5554b6543d7f4c2bd574aef5c03 ).

I create the solver outside of the loop and then call it when measurements are available. Everything works fine when I give a constant state noise (line 78), that then enters in the objective in line 218.

Considering that for my process  constant state noise would be a very strong assumption, I wanted to have it variable and I tought about defining it as in line 142-143 and then give it to the solver as a variable in line 250. However, when I do so, I get the same error that Pegah mentioned:

"ex.is_scalar()" failed:

'gradient' only defined for scalar outputs: Use
'jacobian' instead.

I am very new to CasAdi, so I am probably doing something wrong. Is there a way you can help me with solving that? :)

Best regards,
Andrea

Marc-Simon Schäfer

unread,
Jun 10, 2021, 11:51:45 AM6/10/21
to CasADi
Hi Andrea,

you can do this in a time-variable way by appending W in your parameter vector in line 250. Something like

'p', vertcat(Y_in, U_in, X0_var, P_mat_in, W)

Later in your online-code just fill your updated W in the parameter vector.

Best regards,
Marc-Simon

Andrea Tuveri

unread,
Jun 15, 2021, 10:00:19 AM6/15/21
to CasADi
Hei Marc-Simon,

Thanks for the replay. 

Yes this is what I previously did, but I have got the same error message as Pegah mentioned before in this conversation 

('"ex.is_scalar()" failed:

'gradient' only defined for scalar outputs: Use
'jacobian' instead).

Berst regards,
Andrea

Marc-Simon Schäfer

unread,
Jun 15, 2021, 10:41:43 AM6/15/21
to CasADi
Hi Andrea,

as Joris mention above: check your objective function if it is a scalar. Especially if line 218 results in a scalar expression. Looks like [1 x n ] * [n x n].

Best regards

Andrea Tuveri

unread,
Jun 16, 2021, 3:32:06 PM6/16/21
to CasADi
Hei Marc-Simon,

Thanks a lot again! Yes, there was a mistake from my side in the dimension of W, (line 142). Now everything works.

Best,
Andrea
Reply all
Reply to author
Forward
0 new messages