NLPDifferentiator#

class NLPDifferentiator(nlp, nlp_bounds, **kwargs)[source]#

Bases: object

Base class for nonlinear program (NLP) differentiator. This class can be used independently from do-mpc to differentiate a given NLP.

Note

This is an experimental feature. The API might change in the future.

Example:

  1. Consider an NLP created with CasADi, including
    • optimization variables x

    • optimization parameters p

    • objective function f

    • constraints g

import casadi as ca

x = ca.SX.sym('x', 2)
p = ca.SX.sym('p', 1)
f = (1-x[0])**2 + 0.2*(x[1]-x[0]**2)**2
cons_inner = (x[0] + 0.5)**2+x[1]**2

g = ca.vertcat(
    p**2/4 - cons_inner,
    cons_inner - p**2
)

ca_solver = ca.nlpsol('solver', 'ipopt', nlp)
  1. Create dictionaries for the NLP and the NLP bounds:

nlp = {'x':x, 'p':p, 'f':cost, 'g':cons}
nlp_bounds = {
    'lbx': np.array([0, -ca.inf]).reshape(-1,1),
    'ubx': np.array([ca.inf, ca.inf]).reshape(-1,1),
    'lbg': np.array([-ca.inf, -ca.inf]).reshape(-1,1),
    'ubg': np.array([0, 0]).reshape(-1,1)
}
  1. Initialize the NLP differentiator with the NLP and the NLP bounds.

nlp_diff = NLPDifferentiator(nlp, nlp_bounds)
  1. Configure the differentiator settings with the settings attribute.

nlp_diff.settings.check_LICQ = False
  1. Solve the parametric NLP for the parameters p0, e.g. with the CasADi solver ca_solver. Pass the same bounds that were used previously.

p0 = np.array([1.])
r = solver(p=p0, **nlp_bounds)
  1. Calculate the parametric NLP sensitivity matrices with differentiate() considering the solution r and the corresponding parameters p0.

dxdp, dlamdp = nlp_diff.get_sensitivity_matrices(r, p0)
Parameters:
  • nlp (Dict) – Dictionary with keys x, p, f, g.

  • nlp_bounds (Dict) – Dictionary with keys lbx, ubx, lbg, ubg.

Methods#

differentiate#

differentiate(self, nlp_sol, p_num)#

Main method of the class. Call this method to obtain the parametric sensitivities. The sensitivity matrix dx_dp is of shape (n_x, n_p).

Note

Please read the documentation of the class NLPDifferentiator for more information.

Parameters:
  • nlp_sol (dict) – Dictionary containing the optimal solution of the NLP.

  • p_num (DM) – Numerical value of the parameters of the NLP.

Returns:

Tuple[DM, DM] – Parametric sensitivities of the decision variables and lagrange multipliers.

Attributes#

settings#

NLPDifferentiator.settings#

Settings of the NLP differentiator. This is an annotated dataclass that can also be printed for convenience. See do_mpc.differentiator.helper.NLPDifferentiatorSettings for more information.

Example:

nlp_diff = NLPDifferentiator(nlp, nlp_bounds)
nlp_diff.settings.check_licq = False

Note

Settings can also be passed as keyword arguments to the constructor of NLPDifferentiator.

status#

NLPDifferentiator.status#

Status of the NLP differentiator. This is an annotated dataclass that can also be printed for convenience. See do_mpc.differentiator.helper.NLPDifferentiatorStatus for more information.