NLPDifferentiator#
- class NLPDifferentiator(nlp, nlp_bounds, **kwargs)[source]#
Bases:
objectBase class for nonlinear program (NLP) differentiator. This class can be used independently from
do-mpcto differentiate a given NLP.Note
This is an experimental feature. The API might change in the future.
Example:
- Consider an NLP created with CasADi, including
optimization variables
xoptimization parameters
pobjective function
fconstraints
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)
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) }
Initialize the NLP differentiator with the NLP and the NLP bounds.
nlp_diff = NLPDifferentiator(nlp, nlp_bounds)
Configure the differentiator settings with the
settingsattribute.
nlp_diff.settings.check_LICQ = False
Solve the parametric NLP for the parameters
p0, e.g. with the CasADi solverca_solver. Pass the same bounds that were used previously.
p0 = np.array([1.]) r = solver(p=p0, **nlp_bounds)
Calculate the parametric NLP sensitivity matrices with
differentiate()considering the solutionrand the corresponding parametersp0.
dxdp, dlamdp = nlp_diff.get_sensitivity_matrices(r, p0)
- Parameters:
nlp (
Dict) – Dictionary with keysx,p,f,g.nlp_bounds (
Dict) – Dictionary with keyslbx,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_dpis of shape(n_x, n_p).Note
Please read the documentation of the class
NLPDifferentiatorfor 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.NLPDifferentiatorSettingsfor 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.NLPDifferentiatorStatusfor more information.