leap_c.diff_mpc.function

Provides an implemenation of differentiable MPC based on acados.

Attributes

Classes

AcadosDiffMpcCtx

Context for differentiable MPC with acados.

AcadosDiffMpcFunction

Differentiable MPC function based on acados.

Module Contents

class leap_c.diff_mpc.function.AcadosDiffMpcCtx[source]

Context for differentiable MPC with acados.

This context holds the results of the forward pass. This information is needed for the backward pass and to calculate the sensitivities. It also contains fields for caching the sensitivity calculations.

Variables:
  • iterate – The solution iterate from the forward pass. Can be used for, e.g., initializing the next solve.

  • status – The status of the solver after the forward pass. 0 indicates success, non-zero values indicate various errors.

  • log – Statistics from the forward solve containing info like success rates and timings.

  • solver_input – The input used for the forward pass.

  • needs_input_grad – A list of booleans indicating which inputs require gradients.

  • du0_dp_global – Sensitivity of the control solution of the initial stage with respect to acados global parameters (i.e., differentiable parameters).

  • du0_dx0 – Sensitivity of the control solution of the initial stage with respect to the initial state.

  • dvalue_du0 – Sensitivity of the objective value solution with respect to the control input of the first stage. Only available if said control was provided.

  • dvalue_dx0 – Sensitivity of the objective value solution solution with respect to the initial state.

  • dx_dp_global – Sensitivity of the whole state trajectory solution with respect to acados global parameters (i.e., differentiable parameters).

  • du_dp_global – Sensitivity of the whole control trajectory solution with respect to acados global parameters (i.e., differentiable parameters).

  • dvalue_dp_global – Sensitivity of the objective value solution with respect to acados global.

du0_dp_global: numpy.ndarray | None = None
du0_dx0: numpy.ndarray | None = None
du_dp_global: numpy.ndarray | None = None
dvalue_dp_global: numpy.ndarray | None = None
dvalue_du0: numpy.ndarray | None = None
dvalue_dx0: numpy.ndarray | None = None
dx_dp_global: numpy.ndarray | None = None
iterate: acados_template.acados_ocp_iterate.AcadosOcpFlattenedBatchIterate
log: dict[str, float] | None
needs_input_grad: tuple[bool] | None = None
solver_input: leap_c.diff_mpc.data.AcadosOcpSolverInput
status: numpy.ndarray
class leap_c.diff_mpc.function.AcadosDiffMpcFunction(ocp: acados_template.AcadosOcp, initializer: leap_c.diff_mpc.initializer.AcadosDiffMpcInitializer | None = None, discount_factor: float | None = None, export_directory: pathlib.Path | None = None, n_batch_init: int | None = None, num_threads_batch_solver: int | None = None, verbose: bool = True)[source]

Bases: leap_c.autograd.function.DiffFunction

Differentiable MPC function based on acados.

Variables:
  • ocp – The acados ocp object defining the optimal control problem structure.

  • forward_batch_solver – The acados batch solver used for the forward pass.

  • backward_batch_solver – The acados batch solver used for the backward pass.

  • initializer – The initializer used to provide initial guesses for the solver, if none are provided explicitly or on a retry. Uses a zero iterate by default.

backward(ctx: AcadosDiffMpcCtx, u0_grad: numpy.ndarray | None, x_grad: numpy.ndarray | None, u_grad: numpy.ndarray | None, value_grad: numpy.ndarray | None) tuple[numpy.ndarray | None, numpy.ndarray | None, numpy.ndarray | None, None, None][source]

Perform the backward pass via implicit differentiation.

Parameters:
  • ctx – The ctx object from the forward pass.

  • u0_grad – Gradient with respect to the control solution of the first stage.

  • x_grad – Gradient with respect to the whole state trajectory solution.

  • u_grad – Gradient with respect to the whole control trajectory solution.

  • value_grad – Gradient with respect to the objective value solution.

Returns:

  • grad_x0: Gradient with respect to the initial state.

  • grad_u0: Gradient with respect to the initial control.

  • grad_p_global: Gradient with respect to the acados global parameters.

  • grad_p_stagewise: Always None (not supported for differentiation).

  • grad_p_stagewise_sparse_idx: Always None (not supported for differentiation).

Return type:

A tuple containing the gradients with respect to the inputs in the following order

forward(ctx: AcadosDiffMpcCtx | None, x0: numpy.ndarray, u0: numpy.ndarray | None = None, p_global: numpy.ndarray | None = None, p_stagewise: numpy.ndarray | None = None, p_stagewise_sparse_idx: numpy.ndarray | None = None) tuple[AcadosDiffMpcCtx, numpy.ndarray, numpy.ndarray, numpy.ndarray, numpy.ndarray][source]

Perform the forward pass by solving the problem instances.

Parameters:
  • ctx – A context object for the forward pass. If provided, it will be used to warmstart the solve (e.g., by using the saved iterate).

  • x0 – Initial states with shape (B, x_dim).

  • u0 – Initial actions with shape (B, u_dim). Defaults to None.

  • p_global – Flat differentiable parameters, shape (B, N_differentiable).

  • p_stagewise – Stage-wise non-differentiable parameters, shape (B, N_horizon + 1, N_non_differentiable).

  • p_stagewise_sparse_idx – Not yet supported.

Returns:

  • ctx: The context object containing information from the forward pass.

  • sol_u0: The control solution of the first stage, shape (B, u_dim).

  • x: The state trajectory solution, shape (B, N_horizon + 1, x_dim).

  • u: The control trajectory solution, shape (B, N_horizon, u_dim).

  • sol_value: The objective value solution, shape (B, 1).

Return type:

A tuple containing

sensitivity(ctx: AcadosDiffMpcCtx, field_name: AcadosDiffMpcSensitivityOptions) numpy.ndarray[source]

Retrieves a specific sensitivity field from the context object.

Recalculates the sensitivity if not already present.

Parameters:
  • ctx – The ctx object generated by the forward pass.

  • field_name – The name of the sensitivity field to retrieve.

Returns:

The requested sensitivity as a numpy array.

Raises:

ValueError – If field_name is not recognized.

initializer
ocp
leap_c.diff_mpc.function.AcadosDiffMpcSensitivityOptions
leap_c.diff_mpc.function.DEFAULT_NUM_THREADS_BATCH_SOLVER = 4
leap_c.diff_mpc.function.DEFAULT_N_BATCH_INIT = 256
leap_c.diff_mpc.function.TO_ACADOS_SOLVER_GRADOPTS: dict[str, str]