leap_c.parameters.manager¶
Classes¶
acados parameter management. |
Module Contents¶
- class leap_c.parameters.manager.AcadosParameterManager(N_horizon: int, casadi_type: Literal['SX', 'MX'] = 'SX')[source]¶
acados parameter management.
Handles parameter registration, validation, CasADi symbol creation, and provides default numpy implementations for combining parameters. Framework-specific methods (e.g.
combine_differentiable_parameters_torch()) preserve differentiability through the composition.Stage-varying differentiable parameters (
splitsare not"global") are implemented via a one-hot indicator vector that is appended to the non-differentiable parameters. At stagekonlyindicator[k]is 1;get()returns a weighted sum over all stage blocks so the same symbolic expression evaluates to the correct block value at every stage.Warning
If you forget to set the indicator correctly in
combine_non_differentiable_parameters(), every stage will silently evaluate to zero for all stage-varying differentiable parameters.- Variables:
parameters – Dictionary of parameter names to _AcadosParameter instances.
N_horizon – The horizon length for the ocp.
casadi_type – The CasADi symbolic type used for the parameters, either “SX” or “MX”.
differentiable_default_flat – Differentiable parameters’ default values as a flattened NDArray.
non_differentiable_default_flat – Non-differentiable parameters’ default values as a flattened NDArray.
differentiable_symbols – CasADi SX/MX expression for the differentiable parameters.
non_differentiable_symbols – CasADi SX/MX expression for the non-differentiable parameters.
- assign_to_ocp(ocp: acados_template.AcadosOcp) None[source]¶
Synchronize the parameter manager’s symbols and defaults onto the OCP.
Sets
model.p,model.p_global,parameter_values, andp_global_valueson the provided acados OCP instance. Should be called before solver code generation.- Parameters:
ocp – An acados
AcadosOcpinstance.
- combine_differentiable_parameters_jax(batch_size: int | None = None, **overwrites: Any) Any[source]¶
Combine differentiable parameters into a flat JAX array, preserving differentiability.
Note
This method is a placeholder. Contributions implementing JAX-native operations (e.g. using
jax.numpy) are welcome.- Parameters:
batch_size – Batch size.
**overwrites – Named parameter overrides as JAX or numpy arrays.
- Returns:
JAX array of shape
(batch_size, N_differentiable).- Raises:
ImportError – If jax is not installed.
NotImplementedError – Until a JAX implementation is contributed.
- combine_differentiable_parameters_torch(batch_size: int | None = None, device: AcadosParameterManager.combine_differentiable_parameters_torch.torch = None, dtype: AcadosParameterManager.combine_differentiable_parameters_torch.torch = None, **overwrites: AcadosParameterManager.combine_differentiable_parameters_torch.torch) AcadosParameterManager.combine_differentiable_parameters_torch.torch[source]¶
Combine differentiable parameters into a flat tensor, preserving differentiability.
Uses
torch.cat, indexing, and reshaping — all differentiable — so gradients flow back to the original parameter tensors in theoverwritesdict.- Parameters:
batch_size – Batch size. Required.
device – Target device for the output tensor. Required.
dtype – Target dtype for the output tensor. Required.
**overwrites – Named parameter overrides as tensors or numpy arrays.
- Returns:
Tensor of shape
(batch_size, N_differentiable).- Raises:
ImportError – If torch is not installed.
- combine_non_differentiable_parameters(batch_size: int | None = None, **overwrite: numpy.ndarray) numpy.ndarray[source]¶
Combine all non-differentiable parameters into a single numpy array.
- Parameters:
batch_size – The batch size for the parameters. Not needed if overwrite is provided.
**overwrite – Overwrite values for specific parameters. The keys should correspond to the parameter names to overwrite. The values need to be np.ndarray with shape
(batch_size, N_horizon, pdim), wherepdimis the number of dimensions of the parameter to overwrite. Expects the time dimension to beN_horizon + 1(Nstages 0..N).
- Returns:
shape
(batch_size, N_horizon + 1, np)withnpbeing the number ofparameter_values.- Return type:
np.ndarray
- get(name: str) casadi.SX | casadi.MX | numpy.ndarray[source]¶
Get the symbolic variable for a parameter.
For stage-varying differentiable parameters (those with
splits), the returned expression is a weighted sum over all stage blocks, gated by theindicatorvector in the non-differentiable parameters. The expression evaluates to the correct block value at each stage, but only if the indicator is set correctly viacombine_non_differentiable_parameters(). If the indicator is all-zero (e.g. the default), every stage silently evaluates to zero for these parameters.- Parameters:
name – The name of the parameter to retrieve.
- Returns:
CasADi
SX/MXexpression for"differentiable"and"non-differentiable"parameters.
- Raises:
ValueError – If
nameis not registered with this manager.
- register_parameter(name: str, default: numpy.ndarray, differentiable: bool = False, splits: leap_c.parameters.utils.ParamSplits = 'global') casadi.SX | casadi.MX[source]¶
Register a parameter and return a CasADi symbolic for immediate use.
The returned symbolic is a real CasADi SX (or MX) expression (not a placeholder). It can be used directly in cost, dynamics, and constraint expressions.
- Parameters:
name – The name of the parameter.
default – The default value(s) for the parameter.
differentiable – If True, the parameter supports sensitivities (differentiable). If False, the parameter is changeable at runtime but not differentiable (non-differentiable). Defaults to
False.splits – Defines how the parameter varies across stages. See
_AcadosParameterfor details. Defaults to"global".
- Returns:
A CasADi symbolic expression for the parameter.
- casadi_type: Literal['SX', 'MX']¶
- property differentiable_default_flat: numpy.ndarray¶
- property differentiable_symbols: casadi.SX | casadi.MX¶
- property non_differentiable_default_flat: numpy.ndarray¶
- property non_differentiable_symbols: casadi.SX | casadi.MX¶