6.10 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:
Without loss of generality it is assumed that \(Q^o\) and \(Q^k\) are all symmetric because
This implies that a non-symmetric \(Q\) can be replaced by the symmetric matrix \(\half(Q+Q^T)\).
The problem is required to be convex. More precisely, the matrix \(Q^o\) must be positive semi-definite and the \(k\)th constraint must be of the form
with a negative semi-definite \(Q^k\) or of the form
with a positive semi-definite \(Q^k\). 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
If the convexity (i.e. semidefiniteness) conditions are not met MOSEK will not produce reliable results or work at all.
6.10.1 Example: Quadratic Objective¶
We look at a small problem with linear constraints and quadratic objective:
The matrix formulation of (6.42) has:
with the bounds:
Please note the explicit \(\half\) in the objective function of (6.40) which implies that diagonal elements must be doubled in \(Q\), i.e. \(Q_{11}=2\) even though \(1\) is the coefficient in front of \(x_1^2\) in (6.42).
Setting up the linear part
The linear parts (constraints, variables, objective) are set up using exactly the same methods as for linear problems, and we refer to Sec. 6.1 (Linear Optimization) for all the details. The same applies to technical aspects such as defining an optimization task, retrieving the solution and so on.
Setting up the quadratic objective
The quadratic objective is specified using the function Task.putqobj
. Since \(Q^o\) is symmetric only the lower triangular part of \(Q^o\) is inputted. In fact entries from above the diagonal may not appear in the input.
The lower triangular part of the matrix \(Q^o\) is specified using an unordered sparse triplet format (for details, see Sec. 15.1.4 (Matrix Formats)):
int[] qsubi = {0, 1, 2, 2 };
int[] qsubj = {0, 1, 0, 2 };
double[] qval = {2.0, 0.2, -1.0, 2.0};
Please note that
only non-zero elements are specified (any element not specified is 0 by definition),
the order of the non-zero elements is insignificant, and
only the lower triangular part should be specified.
Finally, this definition of \(Q^o\) is loaded into the task:
task.putqobj(qsubi, qsubj, qval);
Source code
package com.mosek.example;
import mosek.*;
public class qo1 {
static final int numcon = 1; /* Number of constraints. */
static final int numvar = 3; /* Number of variables. */
static final int NUMANZ = 3; /* Number of numzeros in A. */
static final int NUMQNZ = 4; /* Number of nonzeros in Q. */
public static void main (String[] args) {
// Since the value infinity is never used, we define
// 'infinity' symbolic purposes only
double infinity = 0;
double[] c = {0.0, -1.0, 0.0};
mosek.boundkey[] bkc = { mosek.boundkey.lo };
double[] blc = {1.0};
double[] buc = {infinity};
mosek.boundkey[] bkx = { mosek.boundkey.lo,
mosek.boundkey.lo,
mosek.boundkey.lo
};
double[] blx = {0.0,
0.0,
0.0
};
double[] bux = {infinity,
infinity,
infinity
};
int[][] asub = { {0}, {0}, {0} };
double[][] aval = { {1.0}, {1.0}, {1.0} };
try (Task task = new Task()) {
// Directs the log task stream to the user specified
// method task_msg_obj.stream
task.set_Stream(
mosek.streamtype.log,
new mosek.Stream()
{ public void stream(String msg) { System.out.print(msg); }});
/* Give MOSEK an estimate of the size of the input data.
This is done to increase the speed of inputting data.
However, it is optional. */
/* Append 'numcon' empty constraints.
The constraints will initially have no bounds. */
task.appendcons(numcon);
/* Append 'numvar' variables.
The variables will initially be fixed at zero (x=0). */
task.appendvars(numvar);
for (int j = 0; j < numvar; ++j) {
/* Set the linear term c_j in the objective.*/
task.putcj(j, c[j]);
/* Set the bounds on variable j.
blx[j] <= x_j <= bux[j] */
task.putvarbound(j, bkx[j], blx[j], bux[j]);
/* Input column j of A */
task.putacol(j, /* Variable (column) index.*/
asub[j], /* Row index of non-zeros in column j.*/
aval[j]); /* Non-zero Values of column j. */
}
/* Set the bounds on constraints.
for i=1, ...,numcon : blc[i] <= constraint i <= buc[i] */
for (int i = 0; i < numcon; ++i)
task.putconbound(i, bkc[i], blc[i], buc[i]);
/*
The lower triangular part of the Q
matrix in the objective is specified.
*/
int[] qsubi = {0, 1, 2, 2 };
int[] qsubj = {0, 1, 0, 2 };
double[] qval = {2.0, 0.2, -1.0, 2.0};
/* Input the Q for the objective. */
task.putqobj(qsubi, qsubj, qval);
/* Solve the problem */
mosek.rescode r = task.optimize();
System.out.println (" Mosek warning:" + r.toString());
// Print a summary containing information
// about the solution for debugging purposes
task.solutionsummary(mosek.streamtype.msg);
/* Get status information about the solution */
mosek.solsta solsta = task.getsolsta(mosek.soltype.itr);
/* Get the solution */
double xx[] = task.getxx(mosek.soltype.itr); // Interior solution.
switch (solsta) {
case optimal:
System.out.println("Optimal primal solution\n");
for (int j = 0; j < numvar; ++j)
System.out.println ("x[" + j + "]:" + xx[j]);
break;
case dual_infeas_cer:
case prim_infeas_cer:
System.out.println("Primal or dual infeasibility\n");
break;
case unknown:
System.out.println("Unknown solution status.\n");
break;
default:
System.out.println("Other solution status");
break;
}
}
catch (mosek.Exception e) {
System.out.println ("An error/warning was encountered");
System.out.println (e.toString());
throw e;
}
} /* Main */
}
6.10.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.41).
Consider the problem:
This is equivalent to
where
The linear parts and quadratic objective are set up the way described in the previous tutorial.
Setting up quadratic constraints
To add quadratic terms to the constraints we use the function Task.putqconk
.
int[] qsubi = {0, 1, 2, 2 };
int[] qsubj = {0, 1, 2, 0 };
double[] qval = { -2.0, -2.0, -0.2, 0.2};
/* put Q^0 in constraint with index 0. */
task.putqconk (0,
qsubi,
qsubj,
qval);
While Task.putqconk
adds quadratic terms to a specific constraint, it is also possible to input all quadratic terms in one chunk using the Task.putqcon
function.
Source code
package com.mosek.example;
import mosek.*;
public class qcqo1 {
static final int numcon = 1; /* Number of constraints. */
static final int numvar = 3; /* Number of variables. */
static final int NUMANZ = 3; /* Number of numzeros in A. */
static final int NUMQNZ = 4; /* Number of nonzeros in Q. */
public static void main (String[] args) {
// Since the value infinity is never used, we define
// 'infinity' symbolic purposes only
double infinity = 0;
double[] c = {0.0, -1.0, 0.0};
mosek.boundkey[] bkc = {mosek.boundkey.lo};
double[] blc = {1.0};
double[] buc = {infinity};
mosek.boundkey[] bkx
= {mosek.boundkey.lo,
mosek.boundkey.lo,
mosek.boundkey.lo
};
double[] blx = {0.0,
0.0,
0.0
};
double[] bux = {infinity,
infinity,
infinity
};
int[][] asub = { {0}, {0}, {0} };
double[][] aval = { {1.0}, {1.0}, {1.0} };
try (mosek.Task task = new mosek.Task()) {
// Directs the log task stream to the user specified
// method task_msg_obj.stream
task.set_Stream(
mosek.streamtype.log,
new mosek.Stream()
{ public void stream(String msg) { System.out.print(msg); }});
/* Give MOSEK an estimate of the size of the input data.
This is done to increase the speed of inputting data.
However, it is optional. */
/* Append 'numcon' empty constraints.
The constraints will initially have no bounds. */
task.appendcons(numcon);
/* Append 'numvar' variables.
The variables will initially be fixed at zero (x=0). */
task.appendvars(numvar);
for (int j = 0; j < numvar; ++j) {
/* Set the linear term c_j in the objective.*/
task.putcj(j, c[j]);
/* Set the bounds on variable j.
blx[j] <= x_j <= bux[j] */
task.putvarbound(j, bkx[j], blx[j], bux[j]);
/* Input column j of A */
task.putacol(j, /* Variable (column) index.*/
asub[j], /* Row index of non-zeros in column j.*/
aval[j]); /* Non-zero Values of column j. */
}
/* Set the bounds on constraints.
for i=1, ...,numcon : blc[i] <= constraint i <= buc[i] */
for (int i = 0; i < numcon; ++i)
task.putconbound(i, bkc[i], blc[i], buc[i]);
/*
* The lower triangular part of the Q
* matrix in the objective is specified.
*/
int[] qosubi = { 0, 1, 2, 2 };
int[] qosubj = { 0, 1, 0, 2 };
double[] qoval = { 2.0, 0.2, -1.0, 2.0 };
/* Input the Q for the objective. */
task.putqobj(qosubi, qosubj, qoval);
/*
* The lower triangular part of the Q^0
* matrix in the first constraint is specified.
* This corresponds to adding the term
* x0^2 - x1^2 - 0.1 x2^2 + 0.2 x0 x2
*/
int[] qsubi = {0, 1, 2, 2 };
int[] qsubj = {0, 1, 2, 0 };
double[] qval = { -2.0, -2.0, -0.2, 0.2};
/* put Q^0 in constraint with index 0. */
task.putqconk (0,
qsubi,
qsubj,
qval);
task.putobjsense(mosek.objsense.minimize);
/* Solve the problem */
try {
mosek.rescode termcode = task.optimize();
} catch (mosek.Warning e) {
System.out.println (" Mosek warning:");
System.out.println (e.toString ());
}
// Print a summary containing information
// about the solution for debugging purposes
task.solutionsummary(mosek.streamtype.msg);
/* Get status information about the solution */
mosek.solsta solsta = task.getsolsta(mosek.soltype.itr);
double xx[] = task.getxx(mosek.soltype.itr); // Interior solution.
switch (solsta) {
case optimal:
System.out.println("Optimal primal solution\n");
for (int j = 0; j < numvar; ++j)
System.out.println ("x[" + j + "]:" + xx[j]);
break;
case dual_infeas_cer:
case prim_infeas_cer:
System.out.println("Primal or dual infeasibility.\n");
break;
case unknown:
System.out.println("Unknown solution status.\n");
break;
default:
System.out.println("Other solution status");
break;
}
}
catch (mosek.Exception e) {
System.out.println ("An error/warning was encountered");
System.out.println (e.msg);
throw e;
}
} /* Main */
}