DoMPCDifferentiator#

class DoMPCDifferentiator(optimizer, **kwargs)[source]#

Bases: NLPDifferentiator

Nonlinear program (NLP) Differentiator for do_mpc objects. Can be used with do_mpc.controller.MPC and do_mpc.estimator.MHE objects. The class inherits the NLPDifferentiator class and overwrites the differentiate() method.

Example:

  1. Setup a do_mpc optimizer object (e.g. do_mpc.controller.MPC or do_mpc.estimator.MHE).

model = ...
mpc = do_mpc.controller.MPC(model)
...
mpc.setup()
  1. Initialize the differentiator with the do_mpc optimizer object.

nlp_diff = DoMPCDifferentiator(mpc)
  1. Configure the differentiator settings with the settings attribute.

nlp_diff.settings.check_LICQ = False
  1. Solve the NLP of the original do_mpc optimizer object.

mpc.make_step(x0)
  1. Call the differentiate() method of the differentiator object to compute the parametric sensitivities at the current optimal solution previously calculated with make_step(). The current parameters and optimal solution are read from the do_mpc optimizer object.

dx_dp_num, dlam_dp_num = nlp_diff.differentiate()
  1. Typically, we are interested in specific segments of the parametric sensitivities. These can be retrieved by powerindexing the sens_num attribute.

du0dx0 = nlp_diff.sens_num['dxdp', indexf['_u', 0, 0], indexf['_x', 0, 0]]

This last step returns the parametric sensitivity of the first input with respect to the initial state.

Parameters:

optimizer (Optimizer) – do_mpc class that inherits the Optimizer class, that is, a do_mpc.controller.MPC or do_mpc.estimator.MHE object.

Methods#

differentiate#

differentiate(self)#

Main method of the class. Computes the parametric sensitivities of the underlying NLP of the MPC or MHE. Should be called after solving the underlying NLP. The current optimal solution and the corresponding parameters are read from the do_mpc object.

Attributes#

sens_num#

DoMPCDifferentiator.sens_num#

The sensitivity structure of the NLP. This can be queried as follows:

from casadi.tools import indexf

du0dx0 = nlp_diff.sens_num['dxdp', indexf['_u', 0, 0], indexf['_x0']]

The powerindices passed to indexf are derived from the attributes:

settings#

DoMPCDifferentiator.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#

DoMPCDifferentiator.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.