MATLAB
In this example we shall solve the following small quadratic program:
\[\begin{split}\begin{array}{ll}
\mbox{minimize} & (1/2) x^T \begin{bmatrix}3 & -1\\ -1 & 2 \end{bmatrix}
x + \begin{bmatrix}-1 \\ -1\end{bmatrix}^T x \\
\mbox{subject to} & \begin{bmatrix} -1 \\ 1 \end{bmatrix}^T x = -1 \\
& \begin{bmatrix} 1 & 0\\ 0 & 1\end{bmatrix} x \leq \begin{bmatrix}0.3 \\ -0.5\end{bmatrix}
\end{array}\end{split}\]
over variable \(x \in \mathbf{R}^2\). This problem corresponds to data:
\[\begin{split}\begin{array}{cccc}
P = \begin{bmatrix}3 & -1\\ -1 & 2 \end{bmatrix}, &
A = \begin{bmatrix}-1 & 1\\ 1 & 0\\ 0 & 1\end{bmatrix}, &
b = \begin{bmatrix}-1 \\ 0.3 \\ -0.5\end{bmatrix}, &
c = \begin{bmatrix}-1 \\ -1\end{bmatrix}.
\end{array}\end{split}\]
And the cone \(\mathcal{K}\) consists of a zero cone (z
) of length 1
and a positive cone (l
) of dimension 2. Note that the order of the
rows in \(A\) and \(b\) corresponds to the order in Cones, so
the row corresponding to the zero cone comes first, followed by the rows for the
positive cone.
Matlab code to solve this is below.
% First, make sure SCS is in the path so MATLAB can call it
addpath("/Users/bodonoghue/git/scs-matlab")
% Set up data
data.P = sparse([3., -1.; -1., 2.]);
data.A = sparse([-1., 1.; 1., 0.; 0., 1.]);
data.b = [-1; 0.3; -0.5];
data.c = [-1.; -1.];
cone.z = 1;
cone.l = 2;
% Optional solver settings
settings = struct('eps_abs', 1e-9, 'eps_rel', 1e-9);
% Solve!
[x, y, s, info] = scs(data, cone, settings);
disp(sprintf("SCS took %i iters", info.iter))
disp("Optimal solution vector x*:");
disp(x)
disp("Optimal dual vector y*:");
disp(y)
After following the matlab install instructions, we can run the code yielding output:
< M A T L A B (R) >
Copyright 1984-2021 The MathWorks, Inc.
R2021a Update 3 (9.10.0.1684407) 64-bit (maci64)
May 27, 2021
For online documentation, see https://www.mathworks.com/support
For product information, visit www.mathworks.com.
------------------------------------------------------------------
SCS v3.0.0 - Splitting Conic Solver
(c) Brendan O'Donoghue, Stanford University, 2012
------------------------------------------------------------------
problem: variables n: 2, constraints m: 3
cones: z: primal zero / dual free vars: 1
l: linear vars: 2
settings: eps_abs: 1.0e-09, eps_rel: 1.0e-09, eps_infeas: 1.0e-07
alpha: 1.50, scale: 1.00e-01, adaptive_scale: 1
max_iters: 100000, normalize: 1, warm_start: 0
acceleration_lookback: 10, acceleration_interval: 10
lin-sys: sparse-direct
nnz(A): 4, nnz(P): 3
------------------------------------------------------------------
iter | pri res | dua res | gap | obj | scale | time (s)
------------------------------------------------------------------
0| 1.53e+00 1.00e+00 4.87e+00 1.10e+00 1.00e-01 1.53e-04
100| 6.18e-11 1.56e-12 1.30e-10 1.23e+00 1.00e-01 3.37e-04
------------------------------------------------------------------
status: solved
timings: total: 5.26e-03s = setup: 4.89e-03s + solve: 3.74e-04s
lin-sys: 1.96e-05s, cones: 7.60e-06s, accel: 9.94e-05s
------------------------------------------------------------------
objective = 1.235000
------------------------------------------------------------------
SCS took 100 iters
Optimal solution vector x*:
0.3000
-0.7000
Optimal dual vector y*:
2.7000
2.1000
0