CasADi Interface#

For most purposes, it is recommended to use the the high-level problem API to compose optimization problems using CasADi. However, in some cases, you may want access to the alpaqa─CasADi interface directly. This lower-level interface is documented here.

alpaqa.casadi_generator.generate_casadi_problem(f: ~casadi.casadi.Function, g: ~casadi.casadi.Function | None, second_order: ~typing.Literal['no', 'full', 'prod', 'L', 'L_prod', 'psi', 'psi_prod'] = 'no', name: str = 'alpaqa_problem', sym: ~typing.Callable = <function GenSX.sym>) CodeGenerator[source]

Convert the objective and constraint functions into a CasADi code generator.

Parameters:
  • f – Objective function.

  • g – Constraint function.

  • second_order – Whether to generate functions for evaluating Hessians.

  • name – Optional string description of the problem (used for filename).

  • sym – Symbolic variable constructor, usually either casadi.SX.sym (default) or casadi.MX.sym.

Returns:

Code generator that generates the functions and derivatives used by the solvers.

alpaqa.casadi_generator.generate_casadi_control_problem(f: Function, l: Function, l_N: Function, h: Function = None, h_N: Function = None, c: Function = None, c_N: Function = None, name: str = 'alpaqa_control_problem') CodeGenerator[source]

Convert the dynamics and cost functions into a CasADi code generator.

Parameters:
  • f – Dynamics.

  • name – Optional string description of the problem (used for filename).

Returns:

Code generator that generates the functions and derivatives used by the solvers.

alpaqa.casadi_generator.write_casadi_problem_data(sofile, C, D, param, l1_reg, penalty_alm_split, name)[source]

Write the CSV file with constant data for the problem.

alpaqa.casadi_generator.write_casadi_control_problem_data(sofile, U, D, D_N, x_init, param, penalty_alm_split=0, penalty_alm_split_N=None)[source]

Write the CSV file with constant data for the problem.

alpaqa.casadi_loader.generate_and_compile_casadi_problem_no_load(f: Function, g: Function, *, C=None, D=None, param=None, l1_reg=None, penalty_alm_split=None, second_order: Literal['no', 'full', 'prod', 'L', 'L_prod', 'psi', 'psi_prod'] = 'no', name: str = 'alpaqa_problem', **kwargs) Path[source]

Compile the objective and constraint functions into a alpaqa Problem.

Parameters:
  • f – Objective function f(x).

  • g – Constraint function g(x).

  • C – Bound constraints on x.

  • D – Bound constraints on g(x).

  • param – Problem parameter values.

  • l1_reg – L1-regularization on x.

  • penalty_alm_split – This many components at the beginning of g(x) are handled using a quadratic penalty method rather than an augmented Lagrangian method.

  • second_order – Whether to generate functions for evaluating Hessians.

  • name – Optional string description of the problem (used for filename).

  • kwargs – Parameters passed to casadi_generator.generate_casadi_problem().

Returns:

Path to the shared object file with CasADi functions that can be loaded by the solvers.

Note

If you copy the shared object file, don’t forget to also copy the accompanying CSV file with problem data.

alpaqa.casadi_loader.generate_and_compile_casadi_problem(*args, **kwargs) CasADiProblem[source]

Calls generate_and_compile_casadi_problem_no_load() and loads the resulting problem file.

Returns:

Problem specification that can be passed to the solvers.

alpaqa.load_casadi_problem(so_name: str) alpaqa._alpaqa.float64.CasADiProblem

Load a compiled CasADi problem.

class alpaqa.CasADiProblem

C++ documentation: alpaqa::CasADiProblem

See alpaqa.Problem for the full documentation.

__init__(self: alpaqa._alpaqa.float64.CasADiProblem, other: alpaqa._alpaqa.float64.CasADiProblem) None

Create a copy

check(self: alpaqa._alpaqa.float64.CasADiProblem) None
eval_f(self: alpaqa._alpaqa.float64.CasADiProblem, x: numpy.ndarray[numpy.float64[m, 1]]) float
eval_f_grad_f(*args, **kwargs)

Overloaded function.

  1. eval_f_grad_f(self: alpaqa._alpaqa.float64.CasADiProblem, x: numpy.ndarray[numpy.float64[m, 1]], grad_fx: numpy.ndarray[numpy.float64[m, 1], flags.writeable]) -> float

  2. eval_f_grad_f(self: alpaqa._alpaqa.float64.CasADiProblem, x: numpy.ndarray[numpy.float64[m, 1]]) -> tuple

eval_g(*args, **kwargs)

Overloaded function.

  1. eval_g(self: alpaqa._alpaqa.float64.CasADiProblem, x: numpy.ndarray[numpy.float64[m, 1]], gx: numpy.ndarray[numpy.float64[m, 1], flags.writeable]) -> None

  2. eval_g(self: alpaqa._alpaqa.float64.CasADiProblem, x: numpy.ndarray[numpy.float64[m, 1]]) -> numpy.ndarray[numpy.float64[m, 1]]

eval_grad_L(self: alpaqa._alpaqa.float64.CasADiProblem, x: numpy.ndarray[numpy.float64[m, 1]], y: numpy.ndarray[numpy.float64[m, 1]], grad_L: numpy.ndarray[numpy.float64[m, 1], flags.writeable], work_n: numpy.ndarray[numpy.float64[m, 1], flags.writeable]) None
eval_grad_f(*args, **kwargs)

Overloaded function.

  1. eval_grad_f(self: alpaqa._alpaqa.float64.CasADiProblem, x: numpy.ndarray[numpy.float64[m, 1]], grad_fx: numpy.ndarray[numpy.float64[m, 1], flags.writeable]) -> None

  2. eval_grad_f(self: alpaqa._alpaqa.float64.CasADiProblem, x: numpy.ndarray[numpy.float64[m, 1]]) -> numpy.ndarray[numpy.float64[m, 1]]

eval_grad_g_prod(*args, **kwargs)

Overloaded function.

  1. eval_grad_g_prod(self: alpaqa._alpaqa.float64.CasADiProblem, x: numpy.ndarray[numpy.float64[m, 1]], y: numpy.ndarray[numpy.float64[m, 1]], grad_gxy: numpy.ndarray[numpy.float64[m, 1], flags.writeable]) -> None

  2. eval_grad_g_prod(self: alpaqa._alpaqa.float64.CasADiProblem, x: numpy.ndarray[numpy.float64[m, 1]], y: numpy.ndarray[numpy.float64[m, 1]]) -> numpy.ndarray[numpy.float64[m, 1]]

eval_grad_gi(self: alpaqa._alpaqa.float64.CasADiProblem, x: numpy.ndarray[numpy.float64[m, 1]], i: int, grad_gi: numpy.ndarray[numpy.float64[m, 1], flags.writeable]) None
eval_grad_ψ(*args, **kwargs)

Overloaded function.

  1. eval_grad_ψ(self: alpaqa._alpaqa.float64.CasADiProblem, x: numpy.ndarray[numpy.float64[m, 1]], y: numpy.ndarray[numpy.float64[m, 1]], Σ: numpy.ndarray[numpy.float64[m, 1]], grad_ψ: numpy.ndarray[numpy.float64[m, 1], flags.writeable], work_n: numpy.ndarray[numpy.float64[m, 1], flags.writeable], work_m: numpy.ndarray[numpy.float64[m, 1], flags.writeable]) -> None

  2. eval_grad_ψ(self: alpaqa._alpaqa.float64.CasADiProblem, x: numpy.ndarray[numpy.float64[m, 1]], y: numpy.ndarray[numpy.float64[m, 1]], Σ: numpy.ndarray[numpy.float64[m, 1]]) -> numpy.ndarray[numpy.float64[m, 1]]

eval_hess_L(self: alpaqa._alpaqa.float64.CasADiProblem, x: numpy.ndarray[numpy.float64[m, 1]], y: numpy.ndarray[numpy.float64[m, 1]], scale: float = 1.0) Tuple[object, alpaqa._alpaqa.Symmetry]

Returns the Hessian of the Lagrangian and its symmetry.

eval_hess_L_prod(self: alpaqa._alpaqa.float64.CasADiProblem, x: numpy.ndarray[numpy.float64[m, 1]], y: numpy.ndarray[numpy.float64[m, 1]], scale: float, v: numpy.ndarray[numpy.float64[m, 1]], Hv: numpy.ndarray[numpy.float64[m, 1], flags.writeable]) None
eval_hess_ψ(self: alpaqa._alpaqa.float64.CasADiProblem, x: numpy.ndarray[numpy.float64[m, 1]], y: numpy.ndarray[numpy.float64[m, 1]], Σ: numpy.ndarray[numpy.float64[m, 1]], scale: float = 1.0) Tuple[object, alpaqa._alpaqa.Symmetry]

Returns the Hessian of the augmented Lagrangian and its symmetry.

eval_hess_ψ_prod(self: alpaqa._alpaqa.float64.CasADiProblem, x: numpy.ndarray[numpy.float64[m, 1]], y: numpy.ndarray[numpy.float64[m, 1]], Σ: numpy.ndarray[numpy.float64[m, 1]], scale: float, v: numpy.ndarray[numpy.float64[m, 1]], Hv: numpy.ndarray[numpy.float64[m, 1], flags.writeable]) None
eval_inactive_indices_res_lna(*args, **kwargs)

Overloaded function.

  1. eval_inactive_indices_res_lna(self: alpaqa._alpaqa.float64.CasADiProblem, γ: float, x: numpy.ndarray[numpy.float64[m, 1]], grad_ψ: numpy.ndarray[numpy.float64[m, 1]], J: numpy.ndarray[numpy.int64[m, 1], flags.writeable]) -> int

  2. eval_inactive_indices_res_lna(self: alpaqa._alpaqa.float64.CasADiProblem, γ: float, x: numpy.ndarray[numpy.float64[m, 1]], grad_ψ: numpy.ndarray[numpy.float64[m, 1]]) -> numpy.ndarray[numpy.int64[m, 1]]

eval_jac_g(self: alpaqa._alpaqa.float64.CasADiProblem, x: numpy.ndarray[numpy.float64[m, 1]]) Tuple[object, alpaqa._alpaqa.Symmetry]

Returns the Jacobian of the constraints and its symmetry.

eval_proj_diff_g(*args, **kwargs)

Overloaded function.

  1. eval_proj_diff_g(self: alpaqa._alpaqa.float64.CasADiProblem, z: numpy.ndarray[numpy.float64[m, 1]], e: numpy.ndarray[numpy.float64[m, 1], flags.writeable]) -> None

  2. eval_proj_diff_g(self: alpaqa._alpaqa.float64.CasADiProblem, z: numpy.ndarray[numpy.float64[m, 1]]) -> numpy.ndarray[numpy.float64[m, 1]]

eval_proj_multipliers(self: alpaqa._alpaqa.float64.CasADiProblem, y: numpy.ndarray[numpy.float64[m, 1], flags.writeable], M: float) None
eval_prox_grad_step(*args, **kwargs)

Overloaded function.

  1. eval_prox_grad_step(self: alpaqa._alpaqa.float64.CasADiProblem, γ: float, x: numpy.ndarray[numpy.float64[m, 1]], grad_ψ: numpy.ndarray[numpy.float64[m, 1]], x_hat: numpy.ndarray[numpy.float64[m, 1], flags.writeable], p: numpy.ndarray[numpy.float64[m, 1], flags.writeable]) -> float

  2. eval_prox_grad_step(self: alpaqa._alpaqa.float64.CasADiProblem, γ: float, x: numpy.ndarray[numpy.float64[m, 1]], grad_ψ: numpy.ndarray[numpy.float64[m, 1]]) -> Tuple[numpy.ndarray[numpy.float64[m, 1]], numpy.ndarray[numpy.float64[m, 1]], float]

eval_ψ(*args, **kwargs)

Overloaded function.

  1. eval_ψ(self: alpaqa._alpaqa.float64.CasADiProblem, x: numpy.ndarray[numpy.float64[m, 1]], y: numpy.ndarray[numpy.float64[m, 1]], Σ: numpy.ndarray[numpy.float64[m, 1]], ŷ: numpy.ndarray[numpy.float64[m, 1], flags.writeable]) -> float

  2. eval_ψ(self: alpaqa._alpaqa.float64.CasADiProblem, x: numpy.ndarray[numpy.float64[m, 1]], y: numpy.ndarray[numpy.float64[m, 1]], Σ: numpy.ndarray[numpy.float64[m, 1]]) -> Tuple[float, numpy.ndarray[numpy.float64[m, 1]]]

eval_ψ_grad_ψ(*args, **kwargs)

Overloaded function.

  1. eval_ψ_grad_ψ(self: alpaqa._alpaqa.float64.CasADiProblem, x: numpy.ndarray[numpy.float64[m, 1]], y: numpy.ndarray[numpy.float64[m, 1]], Σ: numpy.ndarray[numpy.float64[m, 1]], grad_ψ: numpy.ndarray[numpy.float64[m, 1], flags.writeable], work_n: numpy.ndarray[numpy.float64[m, 1], flags.writeable], work_m: numpy.ndarray[numpy.float64[m, 1], flags.writeable]) -> float

  2. eval_ψ_grad_ψ(self: alpaqa._alpaqa.float64.CasADiProblem, x: numpy.ndarray[numpy.float64[m, 1]], y: numpy.ndarray[numpy.float64[m, 1]], Σ: numpy.ndarray[numpy.float64[m, 1]]) -> Tuple[float, numpy.ndarray[numpy.float64[m, 1]]]

get_box_C(self: alpaqa._alpaqa.float64.CasADiProblem) alpaqa._alpaqa.float64.Box
get_box_D(self: alpaqa._alpaqa.float64.CasADiProblem) alpaqa._alpaqa.float64.Box
property m

Number of general constraints, dimension of \(g(x)\)

property n

Number of decision variables, dimension of \(x\)

property param

Parameter vector \(p\) of the problem

provides_eval_grad_L(self: alpaqa._alpaqa.float64.CasADiProblem) bool
provides_eval_grad_gi(self: alpaqa._alpaqa.float64.CasADiProblem) bool
provides_eval_grad_ψ(self: alpaqa._alpaqa.float64.CasADiProblem) bool
provides_eval_hess_L(self: alpaqa._alpaqa.float64.CasADiProblem) bool
provides_eval_hess_L_prod(self: alpaqa._alpaqa.float64.CasADiProblem) bool
provides_eval_hess_ψ(self: alpaqa._alpaqa.float64.CasADiProblem) bool
provides_eval_hess_ψ_prod(self: alpaqa._alpaqa.float64.CasADiProblem) bool
provides_eval_jac_g(self: alpaqa._alpaqa.float64.CasADiProblem) bool
provides_eval_ψ(self: alpaqa._alpaqa.float64.CasADiProblem) bool
provides_eval_ψ_grad_ψ(self: alpaqa._alpaqa.float64.CasADiProblem) bool
provides_get_box_C(self: alpaqa._alpaqa.float64.CasADiProblem) bool