6.9 Quadratic Optimization

MOSEK can solve quadratic and quadratically constrained problems, as long as they are convex. This class of problems can be formulated as follows:

(6.28)minimize12xTQox+cTx+cfsubject tolkc12xTQkx+j=0n1ak,jxjukc,k=0,,m1,ljxxjujx,j=0,,n1.

Without loss of generality it is assumed that Qo and Qk are all symmetric because

xTQx=12xT(Q+QT)x.

This implies that a non-symmetric Q can be replaced by the symmetric matrix 12(Q+QT).

The problem is required to be convex. More precisely, the matrix Qo must be positive semi-definite and the kth constraint must be of the form

(6.29)lkc12xTQkx+j=0n1ak,jxj

with a negative semi-definite Qk or of the form

12xTQkx+j=0n1ak,jxjukc.

with a positive semi-definite Qk. This implies that quadratic equalities are not allowed. Specifying a non-convex problem will result in an error when the optimizer is called.

A matrix is positive semidefinite if all the eigenvalues of Q are nonnegative. An alternative statement of the positive semidefinite requirement is

xTQx0,x.

If the convexity (i.e. semidefiniteness) conditions are not met MOSEK will not produce reliable results or work at all.

6.9.1 Example: Quadratic Objective

We look at a small problem with linear constraints and quadratic objective:

(6.30)minimizex12+0.1x22+x32x1x3x2subject to1x1+x2+x30x.

The matrix formulation of (6.30) has:

Qo=[20100.20102],c=[010],A=[111],

with the bounds:

lc=1,uc=,lx=[000] and ux=[]

Please note the explicit 12 in the objective function of (6.28) which implies that diagonal elements must be doubled in Q, i.e. Q11=2 even though 1 is the coefficient in front of x12 in (6.30).

Using mosekopt

In Listing 6.17 we show how to use mosekopt to solve problem (6.30). This is the preferred way.

Listing 6.17 How to solve problem (6.30) using mosekopt. Click here to download.
function qo2()

clear prob;

% c vector.
prob.c = [0 -1 0]';

% Define the data.

% First the lower triangular part of q in the objective 
% is specified in a sparse format. The format is:
%
%   Q(prob.qosubi(t),prob.qosubj(t)) = prob.qoval(t), t=1,...,4

prob.qosubi = [ 1  3 2   3]';
prob.qosubj = [ 1  1 2   3]';
prob.qoval  = [ 2 -1 0.2 2]';

% a, the constraint matrix
subi  = ones(3,1);
subj  = 1:3;
valij = ones(3,1);

prob.a = sparse(subi,subj,valij);

% Lower bounds of constraints.
prob.blc  = [1.0]';

% Upper bounds of constraints.
prob.buc  = [inf]';

% Lower bounds of variables.
prob.blx  = sparse(3,1);

% Upper bounds of variables.
prob.bux = [];   % There are no bounds.

[r,res] = mosekopt('minimize',prob);

% Display return code.
fprintf('Return code: %d\n',r);

% Display primal solution for the constraints.
res.sol.itr.xc'

% Display primal solution for the variables.
res.sol.itr.xx'

This sequence of commands looks much like the one that was used to solve the linear optimization example using mosekopt except that the definition of the Q matrix in prob. mosekopt requires that Q is specified in a sparse format. Indeed the vectors qosubi, qosubj, and qoval are used to specify the coefficients of Q in the objective using the principle

Qqosubi(t),qosubj(t)=qoval(t),fort=1,,length(qosubi).

An important observation is that due to Q being symmetric, only the lower triangular part of Q should be specified.

Using mskqpopt

In Listing 6.18 we show how to use mskqpopt to solve problem (6.30).

Listing 6.18 Function solving problem (6.30) using mskqpopt. Click here to download.
function qo1()

% Set up Q.
q     = [[2 0 -1];[0 0.2 0];[-1 0 2]];

% Set up the linear part of the problem.
c     = [0 -1 0]';
a     = ones(1,3);
blc   = [1.0];
buc   = [inf];
blx   = sparse(3,1);
bux   = [];

% Optimize the problem.
[res] = mskqpopt(q,c,a,blc,buc,blx,bux);

% Show the primal solution.
res.sol.itr.xx

It should be clear that the format for calling mskqpopt is very similar to calling msklpopt except that the Q matrix is included as the first argument of the call. Similarly, the solution can be inspected by viewing the res.sol field.

6.9.2 Example: Quadratic constraints

In this section we show how to solve a problem with quadratic constraints. Please note that quadratic constraints are subject to the convexity requirement (6.29).

Consider the problem:

minimizex12+0.1x22+x32x1x3x2subject to1x1+x2+x3x12x220.1x32+0.2x1x3,x0.

This is equivalent to

(6.31)minimize12xTQox+cTxsubject to12xTQ0x+Axb,x0,

where

Qo=[20100.20102],c=[010]T,A=[111],b=1.
Q0=[200.20200.200.2].

The linear parts and quadratic objective are set up the way described in the previous tutorial.

Setting up quadratic constraints

Listing 6.19 Script implementing problem (6.31). Click here to download.
function qcqo1()
clear prob;

% Specify the linear objective terms.
prob.c      = [0, -1, 0];

% Specify the quadratic terms of the constraints.
prob.qcsubk = [1     1    1   1  ]';
prob.qcsubi = [1     2    3   3  ]';
prob.qcsubj = [1     2    3   1  ]';
prob.qcval  = [-2.0 -2.0 -0.2 0.2]';

% Specify the quadratic terms of the objective.
prob.qosubi = [1     2    3    3  ]';
prob.qosubj = [1     2    3    1  ]';
prob.qoval  = [2.0   0.2  2.0 -1.0]';

% Specify the linear constraint matrix
prob.a      = [1 1 1];

% Specify the lower bounds
prob.blc    = [1];
prob.blx    = zeros(3,1);

[r,res]     = mosekopt('minimize',prob);

% Display the solution.
fprintf('\nx:');
fprintf(' %-.4e',res.sol.itr.xx');
fprintf('\n||x||: %-.4e',norm(res.sol.itr.xx));