Model Predictive Control

class leap_c.mpc.AcadosStatus(value)[source]

Bases: IntEnum

ACADOS_MAXITER = 2
ACADOS_MINSTEP = 3
ACADOS_NAN_DETECTED = 1
ACADOS_QP_FAILURE = 4
ACADOS_READY = 5
ACADOS_SUCCESS = 0
ACADOS_TIMEOUT = 7
ACADOS_UNBOUNDED = 6
class leap_c.mpc.Mpc(ocp: AcadosOcp, ocp_sensitivity: AcadosOcp | None = None, discount_factor: float | None = None, init_state_fn: Callable[[MpcInput], AcadosOcpFlattenedBatchIterate] | None = None, n_batch_max: int = 256, num_threads_in_batch_methods: int = 1, export_directory: Path | None = None, export_directory_sensitivity: Path | None = None, throw_error_if_u0_is_outside_ocp_bounds: bool = True)[source]

Bases: ABC

Mpc abstract base class.

property N: int
property default_full_mpcparameter: MpcParameter

Return the full default MpcParameter.

property default_p_global: ndarray | None

Return the default p_global.

property default_p_stagewise: ndarray | None

Return the default p_stagewise.

property default_sens_mpcparameter: MpcParameter

Return the default MpcParameter for sensitivity solver. It does not contain the LS-parameters

last_solve_diagnostics(ocp_solver: AcadosOcpSolver | AcadosOcpBatchSolver) dict | list[dict][source]

Print statistics for the last solve and collect QP-diagnostics for the solvers.

Simpler information about the last call is stored in self.last_call_stats.

property num_threads_batch_methods: int

The number of threads to use in the batch methods.

property ocp_batch_sensitivity_solver: AcadosOcpBatchSolver
property ocp_batch_solver: AcadosOcpBatchSolver
property ocp_sensitivity_solver: AcadosOcpSolver
property ocp_solver: AcadosOcpSolver
property p_global_dim: int

Return the dimension of p_global.

policy(state: ndarray, p_global: ndarray | None, p_stagewise: ndarray | None = None, p_stagewise_sparse_idx: ndarray | None = None, solver_state: AcadosOcpFlattenedBatchIterate | None = None) tuple[ndarray, AcadosOcpFlattenedBatchIterate, ndarray][source]

Compute the policy for a given state.

Parameters:
  • state – The state for which to compute the policy.

  • p_global – The global parameters.

  • p_stagewise – The stagewise parameters.

  • p_stagewise_sparse_idx – The sparse indices of the stagewise parameters.

  • solver_state – The iterate of the solver to use as initialization. If None, the solver is initialized using its init_state_fn.

Returns:

The action (or control), the state and status of the solver.

class leap_c.mpc.MpcInput(x0: ndarray, u0: ndarray | None = None, parameters: MpcParameter | None = None)[source]

Bases: NamedTuple

A named tuple to store the input of the MPC planner.

x0

The initial states in shape (B, x_dim) or (x_dim, ).

Type:

numpy.ndarray

u0

The initial actions in shape (B, u_dim) or (u_dim, ).

Type:

numpy.ndarray | None

parameters

The parameters of the MPC planner.

Type:

leap_c.mpc.MpcParameter | None

get_sample(i: int) MpcInput[source]

Get the sample at index i from the batch.

is_batched() bool[source]
parameters: MpcParameter | None

Alias for field number 2

u0: ndarray | None

Alias for field number 1

x0: ndarray

Alias for field number 0

class leap_c.mpc.MpcOutput(status: ndarray | Tensor | None = None, u0: ndarray | Tensor | None = None, Q: ndarray | Tensor | None = None, V: ndarray | Tensor | None = None, dvalue_dx0: ndarray | None = None, dvalue_du0: ndarray | None = None, dvalue_dp_global: ndarray | None = None, du0_dp_global: ndarray | None = None, du0_dx0: ndarray | None = None)[source]

Bases: NamedTuple

A named tuple to store the solution of the MPC planner.

status

The status of the solver.

Type:

numpy.ndarray | torch.Tensor | None

u0

The first optimal action.

Type:

numpy.ndarray | torch.Tensor | None

Q

The state-action value function.

Type:

numpy.ndarray | torch.Tensor | None

V

The value function.

Type:

numpy.ndarray | torch.Tensor | None

dvalue_du0

The sensitivity of the value function with respect to the initial action.

Type:

numpy.ndarray | None

dvalue_dp_global

The sensitivity of the value function with respect to the global parameters.

Type:

numpy.ndarray | None

du0_dp_global

The sensitivity of the initial action with respect to the global parameters.

Type:

numpy.ndarray | None

du0_dx0

The sensitivity of the initial action with respect to the initial state.

Type:

numpy.ndarray | None

Q: ndarray | Tensor | None

Alias for field number 2

V: ndarray | Tensor | None

Alias for field number 3

du0_dp_global: ndarray | None

Alias for field number 7

du0_dx0: ndarray | None

Alias for field number 8

dvalue_dp_global: ndarray | None

Alias for field number 6

dvalue_du0: ndarray | None

Alias for field number 5

dvalue_dx0: ndarray | None

Alias for field number 4

status: ndarray | Tensor | None

Alias for field number 0

u0: ndarray | Tensor | None

Alias for field number 1

class leap_c.mpc.MpcParameter(p_global: ndarray | None = None, p_stagewise: List[ndarray] | ndarray | None = None, p_stagewise_sparse_idx: List[ndarray] | ndarray | None = None)[source]

Bases: NamedTuple

A named tuple to store the parameters of the MPC planner.

p_global

The part of p_global that should be learned in shape (n_p_global_learnable, ) or (B, n_p_global_learnable).

Type:

numpy.ndarray | None

p_stagewise

The stagewise parameters in shape (N+1, p_stagewise_dim) or (N+1, len(p_stagewise_sparse_idx)) if the next field is set or (B, N+1, p_stagewise_dim) or (B, N+1, len(p_stagewise_sparse_idx)) if the next field is set. If a multi-phase MPC is used and p_stagewise_sparse_idx is given, this is a list containing the above arrays for the respective phases.

Type:

List[numpy.ndarray] | numpy.ndarray | None

p_stagewise_sparse_idx

If not None, stagewise parameters are set in a sparse manner, using these indices. The indices are in shape (N+1, n_p_stagewise_sparse_idx) or (B, N+1, n_p_stagewise_sparse_idx). If a multi-phase MPC is used this is a list containing the above arrays for the respective phases.

Type:

List[numpy.ndarray] | numpy.ndarray | None

ensure_float64() MpcParameter[source]
get_sample(i: int) MpcParameter[source]

Get the sample at index i from the batch.

is_batched() bool[source]

The empty MpcParameter counts as non-batched.

p_global: ndarray | None

Alias for field number 0

p_stagewise: List[ndarray] | ndarray | None

Alias for field number 1

p_stagewise_sparse_idx: List[ndarray] | ndarray | None

Alias for field number 2

leap_c.mpc.create_zero_init_state_fn(solver: AcadosOcpSolver) Callable[[MpcInput], AcadosOcpFlattenedBatchIterate][source]

Create a function that initializes the solver iterate with zeros.

Parameters:

solver – The solver to initialize.

Returns:

The function that initializes the solver iterate with zeros.

leap_c.mpc.initialize_ocp_solver(ocp_solver: AcadosOcpSolver | AcadosOcpBatchSolver, mpc_input: MpcInput, ocp_iterate: AcadosOcpFlattenedBatchIterate | None, set_params: bool = True, throw_error_if_u0_is_outside_ocp_bounds: bool = True) None[source]

Initializes the fields of the OCP (batch) solver with the given values.

Parameters:
  • ocp_solver – The OCP (batch) solver to initialize.

  • mpc_input – The MPCInput containing the parameters to set in the OCP (batch) solver and the state and possibly control that will be used for the initial conditions.

  • ocp_iterate – The iterate of the solver to use as initialization.

  • set_params – Whether to set the MPC parameters of the OCP (batch) solver.

  • throw_error_if_u0_is_outside_ocp_bounds – If True, an error will be thrown when given an u0 in mpc_input that is outside the box constraints defined in the cop

leap_c.mpc.set_discount_factor(ocp_solver: AcadosOcpSolver | AcadosOcpBatchSolver, discount_factor: float) None[source]
leap_c.mpc.set_ocp_solver_initial_condition(ocp_solver: AcadosOcpSolver | AcadosOcpBatchSolver, mpc_input: MpcInput, throw_error_if_u0_is_outside_ocp_bounds: bool, batch_size: int | None = None) None[source]
leap_c.mpc.set_ocp_solver_iterate(ocp_solver: AcadosOcpSolver | AcadosOcpBatchSolver, ocp_iterate: AcadosOcpFlattenedBatchIterate | None) None[source]
leap_c.mpc.set_ocp_solver_mpc_params(ocp_solver: AcadosOcpSolver | AcadosOcpBatchSolver, mpc_parameter: MpcParameter | None, batch_size: int | None = None) None[source]
leap_c.mpc.set_ocp_solver_mpc_params_global(ocp_solver: AcadosOcpSolver | AcadosOcpBatchSolver, mpc_parameter: MpcParameter, batch_size: int | None = None) None[source]
leap_c.mpc.set_ocp_solver_mpc_params_stagewise(ocp_solver: AcadosOcpSolver | AcadosOcpBatchSolver, mpc_parameter: MpcParameter, batch_size: int | None = None)[source]
leap_c.mpc.set_ocp_solver_to_default(ocp_solver: AcadosOcpSolver | AcadosOcpBatchSolver, default_mpc_parameters: MpcParameter, unset_u0: bool) None[source]

Resets the OCP (batch) solver to remove any “state” to be carried over in the next call. Since the init function or a given iterate is being used to override the state of the solver anyways, we don’t need to call ocp_solver.reset(). This entails: - Setting the parameters to the default values (since the default is consistent over the batch, the given MpcParameter must not be batched). - Unsetting the initial control constraints if they were set.

leap_c.mpc.turn_on_warmstart(acados_ocp: AcadosOcp)[source]
leap_c.mpc.unset_ocp_solver_initial_control_constraints(ocp_solver: AcadosOcpSolver | AcadosOcpBatchSolver) None[source]

Unset the initial control constraints of the OCP (batch) solver.