Basic SOCP Example in Julia

We will show how to solve this problem both natively in Clarabel.jl and also by solving with Clarabel.jl within JuMP.

Clarabel.jl native interface

To solve the problem directly within Clarabel.jl, start by creating the solver and settings:

using Clarabel, SparseArrays

P = sparse([0. 0.;0. 1.].*2)

q = [0., 0.]

A = sparse([0.  0.
           -2.  0.;
            0. -1.])
b = [ 1.
     -2.;
     -2.]

cones = [Clarabel.SecondOrderConeT(3)]

settings = Clarabel.Settings(max_iter = 15, verbose = true)

solver   = Clarabel.Solver()

Clarabel.setup!(solver, P, q, A, b, cones, settings)

result = Clarabel.solve!(solver)
-------------------------------------------------------------
           Clarabel.jl v0.5.0  -  Clever Acronym
                   (c) Paul Goulart
                University of Oxford, 2022
-------------------------------------------------------------

problem:
  variables     = 2
  constraints   = 3
  nnz(P)        = 1
  nnz(A)        = 2
  cones (total) = 1
    : SecondOrder = 1,  numel = 3

settings:
  linear algebra: direct / qdldl, precision: Float64
  max iter = 15, time limit = Inf,  max step = 0.990
  tol_feas = 1.0e-08, tol_gap_abs = 1.0e-08, tol_gap_rel = 1.0e-08,
  static reg : on, ϵ1 = 1.0e-08, ϵ2 = 4.9e-32
  dynamic reg: on, ϵ = 1.0e-13, δ = 2.0e-07
  iter refine: on, reltol = 1.0e-13, abstol = 1.0e-12,
               max iter = 10, stop ratio = 5.0
  equilibrate: on, min_scale = 1.0e-04, max_scale = 1.0e+04
               max iter = 10

iter    pcost        dcost       gap       pres      dres      k/t        μ       step
---------------------------------------------------------------------------------------------
  0   4.9292e-01  -1.2888e-01  6.22e-01  1.79e-01  6.48e-17  1.00e+00  2.35e+00   ------
  1   9.3926e-01   7.9266e-01  1.47e-01  6.38e-03  5.39e-17  4.74e-02  1.60e-01  9.67e-01
  2   9.9952e-01   9.9654e-01  2.97e-03  6.30e-05  5.24e-20  8.14e-04  2.68e-03  9.90e-01
  3   1.0000e+00   9.9997e-01  2.98e-05  6.30e-07  1.15e-21  8.16e-06  2.69e-05  9.90e-01
  4   1.0000e+00   1.0000e+00  2.98e-07  6.30e-09  5.33e-17  8.16e-08  2.69e-07  9.90e-01
  5   1.0000e+00   1.0000e+00  2.98e-09  6.30e-11  5.33e-17  8.16e-10  2.69e-09  9.90e-01
---------------------------------------------------------------------------------------------
Terminated with status = solved
solve time =  688μs
>>> Clarabel - Results
Status: SOLVED
Iterations: 5
Objective: 1.000
Solve time:  688μs
result.x
2-element Vector{Float64}:
 1.0000000000000002
 0.9999999997592767

Using JuMP

We can solve the same problem using Clarabel.jl as the backend solver within JuMP. Here is the same problem again:

using Clarabel, JuMP

model = JuMP.Model(Clarabel.Optimizer)
set_optimizer_attribute(model, "verbose", true)

@variable(model, x[1:2])
@constraint(model, [1, 2x[1]-2, x[2] - 2] in SecondOrderCone())
@objective(model, Min, x[2]^2 )

optimize!(model)
-------------------------------------------------------------
           Clarabel.jl v0.5.0  -  Clever Acronym
                   (c) Paul Goulart
                University of Oxford, 2022
-------------------------------------------------------------

problem:
  variables     = 2
  constraints   = 3
  nnz(P)        = 1
  nnz(A)        = 2
  cones (total) = 1
    : SecondOrder = 1,  numel = 3

settings:
  linear algebra: direct / qdldl, precision: Float64
  max iter = 200, time limit = Inf,  max step = 0.990
  tol_feas = 1.0e-08, tol_gap_abs = 1.0e-08, tol_gap_rel = 1.0e-08,
  static reg : on, ϵ1 = 1.0e-08, ϵ2 = 4.9e-32
  dynamic reg: on, ϵ = 1.0e-13, δ = 2.0e-07
  iter refine: on, reltol = 1.0e-13, abstol = 1.0e-12,
               max iter = 10, stop ratio = 5.0
  equilibrate: on, min_scale = 1.0e-04, max_scale = 1.0e+04
               max iter = 10

iter    pcost        dcost       gap       pres      dres      k/t        μ       step
---------------------------------------------------------------------------------------------
  0   4.9292e-01  -1.2888e-01  6.22e-01  1.79e-01  6.48e-17  1.00e+00  2.35e+00   ------
  1   9.3926e-01   7.9266e-01  1.47e-01  6.38e-03  5.39e-17  4.74e-02  1.60e-01  9.67e-01
  2   9.9952e-01   9.9654e-01  2.97e-03  6.30e-05  5.24e-20  8.14e-04  2.68e-03  9.90e-01
  3   1.0000e+00   9.9997e-01  2.98e-05  6.30e-07  1.15e-21  8.16e-06  2.69e-05  9.90e-01
  4   1.0000e+00   1.0000e+00  2.98e-07  6.30e-09  5.33e-17  8.16e-08  2.69e-07  9.90e-01
  5   1.0000e+00   1.0000e+00  2.98e-09  6.30e-11  5.33e-17  8.16e-10  2.69e-09  9.90e-01
---------------------------------------------------------------------------------------------
Terminated with status = solved
solve time =  707μs

Here is the solution

JuMP.value.(x)
2-element Vector{Float64}:
 1.0000000000000002
 0.9999999997592767

and the solver termination status again

JuMP.termination_status(model)
OPTIMAL::TerminationStatusCode = 1

Using Convex.jl

One more time using Clarabel.jl as the backend solver within Convex.jl:

using Clarabel, Convex

x = Variable(2)
problem = minimize(square(x[2]))
problem.constraints = [norm([2x[1];x[2]] - [2;2], 2) <= 1]
solve!(problem, Clarabel.Optimizer; silent_solver = false)
-------------------------------------------------------------
           Clarabel.jl v0.5.0  -  Clever Acronym
                   (c) Paul Goulart
                University of Oxford, 2022
-------------------------------------------------------------

problem:
  variables     = 5
  constraints   = 9
  nnz(P)        = 0
  nnz(A)        = 9
  cones (total) = 4
    : Zero        = 1,  numel = 1
    : Nonnegative = 1,  numel = 2
    : SecondOrder = 2,  numel = (3,3)

settings:
  linear algebra: direct / qdldl, precision: Float64
  max iter = 200, time limit = Inf,  max step = 0.990
  tol_feas = 1.0e-08, tol_gap_abs = 1.0e-08, tol_gap_rel = 1.0e-08,
  static reg : on, ϵ1 = 1.0e-08, ϵ2 = 4.9e-32
  dynamic reg: on, ϵ = 1.0e-13, δ = 2.0e-07
  iter refine: on, reltol = 1.0e-13, abstol = 1.0e-12,
               max iter = 10, stop ratio = 5.0
  equilibrate: on, min_scale = 1.0e-04, max_scale = 1.0e+04
               max iter = 10

iter    pcost        dcost       gap       pres      dres      k/t        μ       step
---------------------------------------------------------------------------------------------
  0   0.0000e+00  -2.9024e+00  2.90e+00  5.63e-01  1.51e-01  1.00e+00  2.51e+00   ------
  1   5.1761e-01  -8.7840e-02  6.05e-01  2.01e-01  4.25e-02  4.40e-01  5.59e-01  8.09e-01
  2   6.6756e-01   6.2124e-01  4.63e-02  2.33e-02  4.70e-03  8.74e-02  6.47e-02  9.04e-01
  3   8.8598e-01   8.9192e-01  5.94e-03  9.08e-03  1.51e-03  6.05e-02  1.81e-02  8.68e-01
  4   9.9565e-01   9.9523e-01  4.27e-04  2.87e-04  4.94e-05  1.35e-03  6.09e-04  9.68e-01
  5   9.9983e-01   9.9982e-01  4.49e-06  1.67e-05  2.92e-06  9.86e-05  3.65e-05  9.90e-01
  6   1.0000e+00   1.0000e+00  3.94e-08  2.29e-07  4.02e-08  1.38e-06  5.01e-07  9.86e-01
  7   1.0000e+00   1.0000e+00  1.22e-09  7.91e-09  1.39e-09  4.77e-08  1.73e-08  9.66e-01
---------------------------------------------------------------------------------------------
Terminated with status = solved
solve time =  959μs

Here is our solution

evaluate(x)
2-element Vector{Float64}:
 1.0
 0.9999999565274064

and the solver termination status again

problem.status
OPTIMAL::TerminationStatusCode = 1

This page was generated using Literate.jl.