Clarabel Solver for Java

We are excited to announce that clarabel4j (Clarabel Solver for Java) is now released and open-source! After ecos4j it is our second library that provides an interface from the Java programming language to a native open source mathematical programming solver. It invokes the native solver Clarabel through Java’s Foreign Function and Memory (FFM) API.

clarabel4j allows solving convex optimization problems such as linear programs (LPs), quadratic programs (QPs), second-order cone programs (SOCPs), and problems with exponential, power cone, and generalized power cone constraints. Many convex optimization problems from a wide range of disciplines such as AI, finance, machine learning, and statistics may be solved with it.

Convex Optimization with clarabel4j

Mathematically, clarabel4j solves the following convex optimization problem

\[\begin{align*} & \text{minimize} & & \frac{1}{2} x^T P x + q^T x \\ & \text{subject to} & & A x + s = b, \qquad s \in \mathcal{K} \end{align*}\]

where \(x \in \mathbf{R}^n\) are the primal variables, \(s \in \mathbf{R}^m\) are slack variables, and \(P \in \mathbf{R}^{n \times n}, \, P=P^T \succeq 0, \, q \in \mathbf{R}^n, \, A \in \mathbf{R}^{m \times n}, \, b \in \mathbf{R}^m\) are the problem data. The convex set \(\mathcal{K}\) is a composition of the following convex cones

\[\begin{align*} & \text{zero cone} & & \{0\}^k \\ & \text{non-negative orthant cone} & & \{s \in \mathbf{R}^k \; | \; s_i \geq 0, \forall i=1, \ldots, k \} \\ & \text{second-order cone} & & \{ (t, s) \in \mathbf{R}^k \; | \; \| s \|_2 \leq t \} \\ & \text{exponential cone} & & \{ (x, y, z) \in \mathbf{R}^3 \; | \; y e^{x/y} \leq z, \, y > 0 \} \\ & \text{power cone} & & \{ (x, y, z) \in \mathbf{R}^3 \; | \; x^a y^{1-a} \geq |z|, \, \\ & & & (x, y) \geq 0 \} \; \text{with} \; a \in (0, 1) \\ & \text{generalized power cone} & & \{ (x, y) \in \mathbf{R}^{\text{len}(a)} \times \mathbf{R}^k \; | \; \\ & & & \prod_{a_i \in a} x_i^{a_i} \geq \| y \|_2, \, x \geq 0 \} \\ & & & \text{with} \; a_i \in (0, 1) \; \text{and} \; \sum_i a_i = 1 \end{align*}\]

Unlike ecos4j, clarabel4j also handles quadratic objectives and (generalized) power cone constraints. Furthermore, the underlying native solver Clarabel improves the algorithm and implementation of ECOS.

Using clarabel4j in Java

Setup

To use clarabel4j, the latest (at the time of writing) JDK 23 is needed as clarabel4j depends on Java’s FFM API.

The library is available at Maven Central. For Maven add the latest version to your pom.xml

<dependency>
    <groupId>com.ustermetrics</groupId>
    <artifactId>clarabel4j</artifactId>
    <version>x.y.z</version>
</dependency>

or using Gradle

implementation 'com.ustermetrics:clarabel4j:x.y.z'

For Windows, Linux, or MacOS users, there is a separate library clarabel4j-native that wraps the native shared library binaries. Using Maven add the latest version from Maven Central to your pom.xml

<dependency>
    <groupId>com.ustermetrics</groupId>
    <artifactId>clarabel4j-native</artifactId>
    <version>x.y.z</version>
    <scope>runtime</scope>
</dependency>

or using Gradle

runtimeOnly 'com.ustermetrics:clarabel4j-native:x.y.z'

Alternatively, install the native Clarabel solver on the machine and add the location to the java.library.path. clarabel4j dynamically loads the native solver.

Since clarabel4j invokes some restricted methods of the FFM API, use the --enable-native-access=com.ustermetrics.clarabel4j or --enable-native-access=ALL-UNNAMED (if you are not using the Java Platform Module System) VM options when running the Java code to avoid warnings from the Java run-time.

Using clarabel4j

Several examples from the Clarabel website can be found in the Model class unit tests of clarabel4j.

For example, for the following quadratic program

\[\begin{align*} & \text{minimize} & & 3 x_1^2 + 2 x_2^2 - x_1 - 4 x_2\\ & \text{subject to} & & -1 \leq x \leq 1, \, x_1 = 2 x_2 \end{align*}\]

the problem data for the objective is

\[ P = 2 \begin{bmatrix} 3 & 0 \\ 0 & 2 \end{bmatrix}, \; q = \begin{bmatrix} -1 \\ -4 \end{bmatrix} \] and for the constraints

\[ A = \left[\begin{array} {rr} 1 & -2 \\ 1 & 0 \\ 0 & 1 \\ -1 & 0 \\ 0 & -1 \end{array}\right], \; b = \begin{bmatrix} 0 \\ 1 \\ 1 \\ 1 \\ 1 \end{bmatrix}, \; \mathcal{K} = \{0\}^1 \times \mathbf{R}^4_{\geq 0} \]

In Java the data can be setup as follows

val p = new Matrix(2, 2, new long[]{0, 1, 2}, new long[]{0, 1}, new double[]{6., 4.});
val q = new double[]{-1., -4.};
val a = new Matrix(5, 2, new long[]{0, 3, 6}, new long[]{0, 1, 3, 0, 2, 4},
        new double[]{1., 1., -1., -2., 1., -1.});
val b = new double[]{0., 1., 1., 1., 1.};
val cones = List.of(new ZeroCone(1), new NonnegativeCone(4));

Note that clarabel4j expects the matrices \(P\) and \(A\) in Compressed Sparse Column (CSC) format.

Then, the quadratic program can be solved and the solution can be checked

try (val model = new Model()) {
    val parameters = Parameters.builder()
            .verbose(false)
            .build();
    model.setParameters(parameters);
    model.setup(p, q, a, b, cones);

    val status = model.optimize();
    
    assertEquals(SOLVED, status);
    assertArrayEquals(new double[]{0.4285714282, 0.2142857141}, model.x(), TOLERANCE);
    assertArrayEquals(new double[]{-1.5714285714, 0., 0., 0., 0.}, model.z(), TOLERANCE);
    assertArrayEquals(new double[]{0., 0.5714285718, 0.7857142859, 1.4285714282, 1.2142857141}, model.s(),
            TOLERANCE);
}

Portfolio optmimization with clarabel4j

We updated the portfolio optimization example from ecos4j with an implementation for clarabel4j. Furthermore, we also implemented the Kotlin Notebook portfolio optimization example with clarabel4j. The notebook can be found here.

Conclusion

This post presents clarabel4j, a powerful Java library for solving many convex optimization problems from a wide range of disciplines such as AI, finance, machine learning, and statistics. As an illustration this post shows two examples, a quadratic program and an example from quant finance how to solve the Markowitz portfolio optimization problem.

The source code of clarabel4j, clarabel4j-native, and the examples can be found on Github: https://github.com/atraplet/clarabel4j, https://github.com/atraplet/clarabel4j-native, and https://github.com/atraplet/portfolio-optimization.

Adrian Trapletti
Adrian Trapletti
PhD, CEO

Quant, software engineer, and consultant mostly investment industry. Long-term contributor and package author R Project for Statistical Computing.