get gradient value out of IPOPT solver

707 views
Skip to first unread message

RFTV

unread,
Feb 6, 2020, 4:55:12 AM2/6/20
to CasADi
Dear,

I'm new to CasADi but I think it is great! I made a NMPC controller using IPOPT interface in CasADi (MATLAB) and it works great! Only now I was wondering if I could get the gradient value out of the IPOPT solver. The standard output of the solver is:

- sol.f  --> objective function value
- sol.g (gradient???- or constraint value?)
- sol.lam_g (??, maybe Lagrange multipliers?)
- sol.lam_p (??, maybe Lagrange multipliers?)
- sol.lam_x (??, maybe Lagrange multipliers?)
- sol.x --> output of decision variables

Could somebody maybe clear this up please? I could not find it in the manual. 

Also, if sol.g is not the gradient, is there a way to extract this from IPOPT?

Thanks in advance,

RFTV

Bruno M L

unread,
Feb 6, 2020, 7:51:01 AM2/6/20
to CasADi
Hi!

I believe sol.g is the value of the nonlinear constraints.

Joris Gillis

unread,
Feb 7, 2020, 5:06:40 PM2/7/20
to CasADi
Hi there!

This is documented in `help(nlpsol)` or https://web.casadi.org/python-api/#nlp
NLPSOL_X x Decision variables at the optimal solution (nx x 1)
NLPSOL_F f Cost function value at the optimal solution (1 x 1)
NLPSOL_G g Constraints function at the optimal solution (ng x 1)
NLPSOL_LAM_X lam_x Lagrange multipliers for bounds on X at the solution (nx x 1)
NLPSOL_LAM_G lam_g Lagrange multipliers for bounds on G at the solution (ng x 1)
NLPSOL_LAM_P lam_p Lagrange multipliers for bounds on P at the solution (np x 1)

Ipopt requires several callbacks for numerical evaluation of objective, gradient of objective, constraints etc.
You can introspect these functions with sol.get_function.

To get a gradient at the solution you would do:
nlp_grad_f = sol.get_function('nlp_grad_f');
[~,grad] = nlp_grad_f(sol.x, p) % p = fixed parameters of the problem; might be empty ( [] ) in your case


Best regards,
   Joris
Message has been deleted
Message has been deleted
Message has been deleted

RFTV

unread,
Feb 17, 2020, 6:20:21 AM2/17/20
to CasADi
Okay, thanks a lot!

I found the help information, but it was not clear to me how to actually acquire it. 

"nlp_grad_f = sol.get_function('nlp_grad_f');
[~,grad] = nlp_grad_f(sol.x, p) % p = fixed parameters of the problem; might be empty ( [] ) in your case"

Okay, thanks! No, p is used a reference state/control and also as estimated states of different subsystems. So I guess I should use that too. 

Thanks a lot for the quick replies and easy solution.

This forum is great!

Best,

RFTV

Reply all
Reply to author
Forward
0 new messages