Lagrangian as symbolic object

141 views
Skip to first unread message

Markus Svedberg

unread,
Mar 14, 2023, 5:55:23 AM3/14/23
to CasADi
Hello!

Is it possible to retrieve the lagrangian which is used in e.g. IPOPT as a symbolic object? I know the solved dual variables can be retrieved, but I need the actual function, since I want to differentiate the lagrangian w.r.t. some of the varaibles in it. 

Is this doable in casadi as is?

Best,
Markus Svedberg

Joel Andersson

unread,
Mar 14, 2023, 10:55:09 AM3/14/23
to CasADi
The way the Lagrangian is defined in CasADi is simply:  f(x) + trans(lam_g)*g(x). We usually do not include the Lagrange multipliers corresponding to simple bounds (bounds on x), since they will just result in an additive contribution to the gradient.
We also handle inequality constraints the same way as equality constraints by combining the multipliers corresponding to upper and lower bounds.

So if you have symbolic expressions corresponding to the decision variable (x), the objective function (f), the constraint function (g), you can construct the gradient of the Lagrangian as follows:

# Multipliers corresponding to bounds on x
lam_x = MX.sym("lam_x", x.sparsity())
# Multipliers corresponding to bounds on g(x)
lam_g = MX.sym("lam_g", g.sparsity())
# Lagrangian formulation
L = f + dot(lam_g, g) + dot(lam_x, x)
# Gradient of the Lagrangian
grad_L = gradient(L, x)
# Lagrangian formulation with lam_x terms excluded (this is how they are normally formulated in CasADi)
L_alt = f + dot(lam_g, g)
# Gradient of the Lagrangian (equivalent to grad_L above)
grad_L_alt = gradient( L_alt, x) + lam_x


You can find some more detail in https://optimization-online.org/2018/05/6642/

casadi_lagrangian.png

Best regards,
Joel

Markus Svedberg

unread,
Mar 16, 2023, 7:59:19 AM3/16/23
to CasADi
Joel,

Thank you very much, I will try this! I actually need to find the lagragian w.r.t. some of the parameters of problem, but I assume a similar procedure holds in that case.

Best,
Markus

Joel Andersson

unread,
Mar 16, 2023, 1:35:50 PM3/16/23
to CasADi
Right, you can differentiate the Lagrangian with respect to some parameters that are not decision variables. That's how CasADi calculates the multipliers corresponding to fixed parameters (lam_p). As explained in that paper. In that case you can just use L(x, lam_g, p) :=  f(x, p) + trans(lam_g)*g(x, p).

Best regards,
Joel

Joel Andersson

unread,
Mar 16, 2023, 1:36:55 PM3/16/23
to CasADi
Or maybe you meant a subset of x. Anyway, that works too with the same approach. Joel

Markus Svedberg

unread,
Mar 20, 2023, 4:05:00 AM3/20/23
to CasADi
Joeal,

No, a subset of p is what I meant. 

I will try what you suggested


 L(x, lam_g, p) :=  f(x, p) + trans(lam_g)*g(x, p). 

Best,
Markus


Reply all
Reply to author
Forward
0 new messages