Clarabel.rs

repository·main·Indexed 20 days ago

https://github.com/oxfordcontrol/clarabel.rs

A high-performance interior point solver for convex conic optimization problems implemented in Rust, with Python bindings. It supports Linear Programs (LPs), Quadratic Programs (QPs), Second-Order Cone Programs (SOCPs), and Semidefinite Programs (SDPs), as well as exponential, power cone, and generalized power cone constraints. Clarabel.rs utilizes a novel homogeneous embedding to handle quadratic objectives directly without epigraphical reformulation.

Tokens
9.1K
Snippets
29
Records
43
Agent score
69%

What's inside clarabel.rs

  1. What is Clarabel.rs

    main

    Clarabel.rs is a Rust implementation of an interior point numerical solver for convex optimization problems. It uses a novel homogeneous embedding that allows it to handle quadratic objectives directly without requiring epigraphical reformulation, making it potentially faster than standard HSDE-based solvers for quadratic problems.

    It solves problems of the form:

    Minimize: $\frac{1}{2}x^T P x + q^T x$ Subject to: $Ax + s = b$ and $s \in \mathcal{K}$

    Where $\mathcal{K}$ is a composition of convex cones.

    Supported Problem Types:

    • Linear Programs (LPs)
    • Quadratic Programs (QPs)
    • Second-Order Cone Programs (SOCPs)
    • Semidefinite Programs (SDPs)
    • Problems with exponential, power cone, and generalized power cone constraints.
  2. Understand the purpose of ClarabelRs

    main

    The ClarabelRs package is a Julia wrapper for the Rust implementation of Clarabel.

    Warning: This package is intended only for development and benchmarking purposes by the Clarabel developers. It is unstable and may be modified, withdrawn, or broken without warning.

    For standard users, the native Julia implementation (Clarabel.jl) is the preferred choice. Because the main Clarabel.jl package provides a shared Modeling Interface (MOI) wrapper, tools like JuMP are compatible with both the native Julia version and this Rust-based wrapper.

  3. Install the native Julia Clarabel package

    main

    If you want to use the Clarabel solver in Julia for production or general use, it is recommended to use the native Julia implementation (Clarabel.jl) rather than this Rust wrapper. You can install it directly via the Julia package manager:

    using Pkg
    Pkg.add("Clarabel")

    Or using the REPL package mode:

    # In the Julia REPL, press "]" to enter package mode
    add Clarabel
    # In the Julia REPL
    add Clarabel
  4. What is Clarabel.rs and what problems does it solve?

    main

    Clarabel.rs is a Rust implementation of an interior point numerical solver for convex optimization problems. It uses a novel homogeneous embedding that allows it to handle quadratic objectives directly without requiring epigraphical reformulation, making it faster than standard HSDE-based solvers for quadratic problems.

    It solves problems in the following form:

    $$\begin{array}{rl} \text{minimize} & \frac{1}{2}x^T P x + q^T x\
    \text{subject to} & Ax + s = b \
    & s \in \mathcal{K} \end{array}$$

    Where:

    • $x$ is the decision variable.
    • $P$ is a positive semidefinite matrix ($P=P^\top \succeq 0$).
    • $q$ is a vector.
    • $A$ is a constraint matrix.
    • $b$ is a constraint vector.
    • $\mathcal{K}$ is a convex set composed of convex cones.

    Supported Problem Types:

    • Linear Programs (LPs)
    • Quadratic Programs (QPs)
    • Second-Order Cone Programs (SOCPs)
    • Semidefinite Programs (SDPs)
    • Problems with exponential, power cone, and generalized power cone constraints.
  5. Implementing a custom Clarabel solver

    main

    To implement a new solver for a specific problem format in Clarabel, you must implement a collection of mutually associated traits. These traits define how problem data, variables, residuals, KKT systems, and settings interact.

    Note: In nearly all cases, users do not need to implement these traits manually. Instead, you should use the types provided in the Default solver implementation, which are already configured for the standard problem formats supported by Clarabel.

  6. Understand SolverStatus and convergence states

    main

    The SolverStatus enum (accessed via DefaultInfo::get_status) indicates the result of the optimization process. Clarabel uses several statuses to communicate whether a solution was found or why the solver stopped:

    • Solved: The solver met the primary convergence tolerances.
    • AlmostSolved: The solver met 'reduced' (relaxed) tolerances, often used when the solver hits a limit like MaxIterations or MaxTime but is close to a solution.
    • PrimalInfeasible / DualInfeasible: The problem is mathematically infeasible in the primal or dual space.
    • AlmostPrimalInfeasible / AlmostDualInfeasible: The solver detected signs of infeasibility using relaxed tolerances.
    • MaxIterations: The solver stopped because it reached the maximum allowed iterations.
    • MaxTime: The solver stopped because it reached the time limit.
    • InsufficientProgress: The solver stopped because residuals were diverging or progress was too slow to continue effectively.
    • Unsolved: The default state before or if the solver fails to reach any other state.
  7. Understand the FloatT trait for Clarabel solver types

    main

    The FloatT trait is the primary abstraction for floating-point types used throughout the Clarabel solver. All internal calculations are performed using types that implement this trait.

    By default, FloatT requires a type to satisfy CoreFloatT (which includes standard num_traits like Float, FloatConst, and FromPrimitive). Depending on the enabled features, additional constraints are applied:

    • sdp feature: If enabled, FloatT is restricted to types that implement BlasFloatT (typically f32 and f64) to ensure compatibility with BLAS/LAPACK libraries.
    • faer-sparse feature: If enabled, FloatT is restricted to types implementing faer_traits::RealField.

    If SDP support is disabled, Clarabel can theoretically support any floating-point type that satisfies the CoreFloatT bounds.

  8. Configure `QDLDLSettings` for factorization

    main

    Use the QDLDLSettings builder to customize the factorization process. Key configuration options include:

    • amd_dense_scale: "dense scale" parameter for AMD ordering (default 1.0).
    • perm: An optional user-supplied custom permutation vector.
    • logical: If true, performs a symbolic-only factorization (no numerical values computed).
    • Dsigns: Optional user-supplied signs for the diagonal elements of $D$ in $LDL^T$.
    • regularize_enable: Enables regularization during factorization (default true).
    • regularize_eps: Regularization epsilon parameter.
    • regularize_delta: Regularization delta parameter.
    let settings = QDLDLSettingsBuilder::<f64>::default()
        .logical(false)
        .regularize_enable(true)
        .amd_dense_scale(1.5)
        .build();
    
    let factorization = QDLDLFactorisation::new(&Ain, Some(settings))?;
  9. Create a CscMatrix from a dense array

    main

    You can easily construct a CscMatrix from a slice of arrays (representing rows) using the From trait. This is convenient for small matrices where manual CSC construction is cumbersome.

    use clarabel::algebra::CscMatrix;
    
    let A = CscMatrix::from(&[
         [1.0, 2.0],
         [3.0, 0.0],
         [0.0, 4.0],
    ]);
  10. Create a CscMatrix from triplets

    main

    If your data is in triplet format (row indices, column indices, and values), use new_from_triplets. This method handles unsorted data and consolidates repeated entries by adding them together.

    Note: This method panics if the input vectors I, J, and V have mismatched lengths.

    use clarabel::algebra::CscMatrix;
    
    // m: rows, n: cols, I: row indices, J: col indices, V: values
    let B = CscMatrix::new_from_triplets(3, 4, rows, cols, vals);