Clarabel Conic Interior Point Solver for Rust / Python

0.11.1 · active · verified Fri Apr 10

Clarabel is an interior point numerical solver for convex optimization problems, implemented in Rust and featuring a Python interface. It efficiently solves a variety of conic programs including Linear Programs (LPs), Quadratic Programs (QPs), Second-Order Cone Programs (SOCPs), Semidefinite Programs (SDPs), and problems with exponential and power cone constraints. The current version is 0.11.1, and the library maintains an active release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define and solve a simple quadratic program (QP) using Clarabel. It shows how to define the objective function matrix (P) and vector (q), constraint matrix (A) and vector (b), and the cone structure (K). The problem data is expected in `numpy` arrays and `scipy.sparse.csc_matrix` format.

import clarabel
import numpy as np
from scipy import sparse

# Define problem data for a simple QP: minimize 0.5 * x.T * P * x + q.T * x
# subject to A * x + s = b, s in K

P = sparse.csc_matrix([[3., 1., -1.], [1., 4., 2.], [-1., 2., 5.]])
P = sparse.triu(P).tocsc() # Clarabel expects upper triangular part in CSC format
q = np.array([1., 2., -3.])

A = sparse.csc_matrix([
    [1., 1., -1.], # Equality constraint (ZeroConeT)
    [0., 1., 0.],  # Inequality constraint (NonnegativeConeT)
    [0., 0., 1.],  # Inequality constraint (NonnegativeConeT)
    [0., 0., 0.]   # Placeholder for SOC (SecondOrderConeT)
])

b = np.array([1., 2., 2., 0.])

# Define cone constraints: ZeroConeT(dim), NonnegativeConeT(dim), SecondOrderConeT(dim)
cones = [clarabel.ZeroConeT(1), clarabel.NonnegativeConeT(2), clarabel.SecondOrderConeT(1)]

# Instantiate and solve the solver
settings = clarabel.DefaultSettings()
settings.verbose = False # Disable verbose output
solver = clarabel.DefaultSolver(P, q, A, b, cones, settings)
solver.solve()

# Print results
print(f"Solution x: {solver.solution.x}")
print(f"Objective value: {solver.solution.obj_val}")
print(f"Solver status: {solver.solution.status}")

view raw JSON →