Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

SQP-craft: smooth constrained optimization in Rust

sqp-craft

Sequential quadratic programming (SQP) for smooth constrained nonlinear problems, with a native Rust solver: damped BFGS updates of a dense Hessian approximation, step QPs solved by active-set or Clarabel, and an ℓ1 merit line search. Since 0.19.0 the qp_solver="auto" default uses active-set with elastic steps for full/block BFGS and Clarabel for partitioned/exact curvature. Results report stationarity, complementarity and dual-sign residuals of the KKT conditions, and kkt_residuals and check_derivatives check points and derivatives independently.

Install

python -m pip install sqp-craft

Wheels (abi3, CPython 3.10 or newer) cover Linux x86_64, Windows x64 and macOS x86_64/arm64 and need no compiler. On other platforms pip builds the source distribution, which needs Rust 1.85 or newer. The package depends on nothing else at run time.

Example

from sqp_craft import ProblemSpec, check_derivatives, solve

class Model:
    """minimize (x0-2)^2 + (x1-1)^2  subject to  x0^2 - x1 <= 0,  x0 + x1 <= 2"""
    def functions(self, x):
        return (x[0] - 2) ** 2 + (x[1] - 1) ** 2, [x[0] ** 2 - x[1], x[0] + x[1]]

    def derivatives(self, x):  # gradient, then the Jacobian values in CSR order
        return [2 * (x[0] - 2), 2 * (x[1] - 1)], [2 * x[0], -1.0, 1.0, 1.0]

inf = float("inf")
spec = ProblemSpec(lower=[-5, -5], upper=[5, 5],
                   constraint_lower=[-inf, -inf], constraint_upper=[0, 2],
                   jacobian_indptr=[0, 2, 4], jacobian_indices=[0, 1, 0, 1])
assert check_derivatives(spec, Model(), [0.5, 0.5]).passed
result = solve(spec, Model(), [0.5, 0.5], method="sqp")
print(result.status, result.candidate, result.evaluation.objective)
# converged, candidate ≈ [1, 1], objective ≈ 1

Pass method="sqp": the default method="dummy" only evaluates the start point. SqpOptions configures the solver (iteration, callback and time limits, KKT tolerances, qp_solver="auto", "clarabel" or "active-set", elastic_mode=None for the policy default or an explicit boolean); invalid values raise ValueError. Callbacks run serially on the calling thread.

Limits

  • Smooth (twice continuously differentiable) objective and constraints with first derivatives; kinks, noise and branches break BFGS and the line search.
  • Full and uniform-block BFGS accept up to 256 variables. Experimental structured SQP uses sparse curvature and Clarabel for larger models with declared structure.
  • A local method: converged means a first-order KKT point within the tolerances, not a global optimum. Use several starts for multimodal models.

More

MIT license.

See backend selection for migration. Explicit backends retain nonelastic behavior unless elastic_mode=True is requested. Reports expose the resolved qp_solver and elastic_mode, including before the first QP on cancelled or limited runs.