DoMPCDifferentiator#
- class DoMPCDifferentiator(optimizer, **kwargs)[source]#
Bases:
NLPDifferentiator
Nonlinear program (NLP) Differentiator for
do_mpc
objects. Can be used withdo_mpc.controller.MPC
anddo_mpc.estimator.MHE
objects. The class inherits theNLPDifferentiator
class and overwrites thedifferentiate()
method.Example:
Setup a
do_mpc
optimizer object (e.g.do_mpc.controller.MPC
ordo_mpc.estimator.MHE
).
model = ... mpc = do_mpc.controller.MPC(model) ... mpc.setup()
Initialize the differentiator with the
do_mpc
optimizer object.
nlp_diff = DoMPCDifferentiator(mpc)
Configure the differentiator settings with the
settings
attribute.
nlp_diff.settings.check_LICQ = False
Solve the NLP of the original
do_mpc
optimizer object.
mpc.make_step(x0)
Call the
differentiate()
method of the differentiator object to compute the parametric sensitivities at the current optimal solution previously calculated withmake_step()
. The current parameters and optimal solution are read from thedo_mpc
optimizer object.
dx_dp_num, dlam_dp_num = nlp_diff.differentiate()
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 theOptimizer
class, that is, ado_mpc.controller.MPC
ordo_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.