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

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.8.1  -  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  1.07e-16  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  8.79e-15  8.16e-08  2.69e-07  9.90e-01
  5   1.0000e+00   1.0000e+00  2.98e-09  6.30e-11  2.13e-16  8.16e-10  2.69e-09  9.90e-01
---------------------------------------------------------------------------------------------
Terminated with status = solved
solve time =  543μs
>>> Clarabel - Results
Status: SOLVED
Iterations: 5
Objective: 1.000
Solve time:  543μs
result.x
2-element Vector{Float64}:
 0.9999999999999999
 0.9999999997592811

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.8.1  -  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  1.07e-16  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  8.79e-15  8.16e-08  2.69e-07  9.90e-01
  5   1.0000e+00   1.0000e+00  2.98e-09  6.30e-11  2.13e-16  8.16e-10  2.69e-09  9.90e-01
---------------------------------------------------------------------------------------------
Terminated with status = solved
solve time =  551μs

Here is the solution

JuMP.value.(x)
2-element Vector{Float64}:
 0.9999999999999999
 0.9999999997592811

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)
Problem statistics
  problem is DCP         : true
  number of variables    : 1 (2 scalar elements)
  number of constraints  : 1 (1 scalar elements)
  number of coefficients : 5
  number of atoms        : 9

Solution summary
  termination status : OPTIMAL
  primal status      : FEASIBLE_POINT
  dual status        : FEASIBLE_POINT
  objective value    : 1.0

Expression graph
  minimize
   └─ qol_elem (convex; positive)
      ├─ index (affine; real)
      │  └─ 2-element real variable (id: 107…663)
      └─ [1.0;;]
  subject to
   └─ ≤ constraint (convex)
      └─ + (convex; real)
         ├─ norm2 (convex; positive)
         │  └─ …
         └─ [-1;;]

Here is our solution

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

and the solver termination status again

problem.status
OPTIMAL::TerminationStatusCode = 1

This page was generated using Literate.jl.