The source files for all examples can be found in /examples.

Quadratic Program

We want to solve the following quadratic program with decision variable x:

\[\begin{array}{ll} \text{minimize} & 1/2 x^\top P x + q^\top x \\ \text{subject to} & l \leq A x \leq u \end{array}\]

The problem can be solved with COSMO in the following way. Start by defining the problem data

using COSMO, SparseArrays, LinearAlgebra, Test

q = [1; 1.];
P = sparse([4. 1; 1 2]);
A = [1. 1; 1 0; 0 1];
l = [1.; 0; 0];
u = [1; 0.7; 0.7];

First, we decide to solve the problem with two one-sided constraints using COSMO.Nonnegatives as the convex set:

Aa = [-A; A]
ba = [u; -l]
constraint1 = COSMO.Constraint(Aa, ba, COSMO.Nonnegatives);

Next, we define the settings object, the model and then assemble everything:

settings = COSMO.Settings(verbose=true);
model = COSMO.Model();
assemble!(model, P, q, constraint1, settings = settings);
res = COSMO.optimize!(model);
------------------------------------------------------------------
          COSMO v0.8.8 - A Quadratic Objective Conic Solver
                         Michael Garstka
                University of Oxford, 2017 - 2022
------------------------------------------------------------------

Problem:  x ∈ R^{2},
          constraints: A ∈ R^{6x2} (8 nnz),
          matrix size to factor: 8x8,
          Floating-point precision: Float64
Sets:     Nonnegatives of dim: 6
Settings: ϵ_abs = 1.0e-05, ϵ_rel = 1.0e-05,
          ϵ_prim_inf = 1.0e-04, ϵ_dual_inf = 1.0e-04,
          ρ = 0.1, σ = 1e-06, α = 1.6,
          max_iter = 5000,
          scaling iter = 10 (on),
          check termination every 25 iter,
          check infeasibility every 40 iter,
          KKT system solver: QDLDL
Acc:      Anderson Type2{QRDecomp},
          Memory size = 8, RestartedMemory,
          Safeguarded: true, tol: 2.0
Setup Time: 0.1ms

Iter:	Objective:	Primal Res:	Dual Res:	Rho:
1	-2.0835e-01	1.2796e+00	2.3510e-01	1.0000e-01
25	 1.8800e+00	2.3567e-16	8.8818e-16	1.0000e-01

------------------------------------------------------------------
>>> Results
Status: Solved
Iterations: 30 (incl. 5 safeguarding iter)
Optimal objective: 1.88
Runtime: 0.001s (0.54ms)

Alternatively, we can also use two-sided constraints with COSMO.Box:

constraint1 = COSMO.Constraint(A, zeros(3), COSMO.Box(l, u));

model = COSMO.Model();
assemble!(model, P, q, constraint1, settings = settings);
res_box = COSMO.optimize!(model);
------------------------------------------------------------------
          COSMO v0.8.8 - A Quadratic Objective Conic Solver
                         Michael Garstka
                University of Oxford, 2017 - 2022
------------------------------------------------------------------

Problem:  x ∈ R^{2},
          constraints: A ∈ R^{3x2} (4 nnz),
          matrix size to factor: 5x5,
          Floating-point precision: Float64
Sets:     Box of dim: 3
Settings: ϵ_abs = 1.0e-05, ϵ_rel = 1.0e-05,
          ϵ_prim_inf = 1.0e-04, ϵ_dual_inf = 1.0e-04,
          ρ = 0.1, σ = 1e-06, α = 1.6,
          max_iter = 5000,
          scaling iter = 10 (on),
          check termination every 25 iter,
          check infeasibility every 40 iter,
          KKT system solver: QDLDL
Acc:      Anderson Type2{QRDecomp},
          Memory size = 5, RestartedMemory,
          Safeguarded: true, tol: 2.0
Setup Time: 10.54ms

Iter:	Objective:	Primal Res:	Dual Res:	Rho:
1	-7.8808e-03	1.0079e+00	2.0033e+02	1.0000e-01
25	 1.8800e+00	7.8558e-17	6.2804e-16	1.0000e-01

------------------------------------------------------------------
>>> Results
Status: Solved
Iterations: 26 (incl. 1 safeguarding iter)
Optimal objective: 1.88
Runtime: 0.119s (118.84ms)

Let's check that the solution is correct:

@testset "QP Problem" begin
  @test norm(res.x[1:2] - [0.3; 0.7], Inf) < 1e-3
  @test norm(res_box.x[1:2] - [0.3; 0.7], Inf) < 1e-3
  @test abs(res.obj_val - 1.88) < 1e-3
  @test abs(res_box.obj_val - 1.88) < 1e-3
end
nothing
Test Summary: | Pass  Total  Time
QP Problem    |    4      4  0.2s

This page was generated using Literate.jl.