# 7.5 Semidefinite Optimization¶

Semidefinite optimization is a generalization of conic optimization, allowing the use of matrix variables belonging to the convex cone of positive semidefinite matrices

$\PSD^r = \left\lbrace X \in \Symm^r: z^T X z \geq 0, \quad \forall z \in \real^r \right\rbrace,$

where $$\Symm^r$$ is the set of $$r \times r$$ real-valued symmetric matrices.

MOSEK can solve semidefinite optimization problems of the form

$\begin{split}\begin{array}{lccccll} \mbox{minimize} & & & \sum_{j=0}^{n-1} c_j x_j + \sum_{j=0}^{p-1} \left\langle \barC_j, \barX_j \right\rangle + c^f & & &\\ \mbox{subject to} & l_i^c & \leq & \sum_{j=0}^{n-1} a_{ij} x_j + \sum_{j=0}^{p-1} \left\langle \barA_{ij}, \barX_j \right\rangle & \leq & u_i^c, & i = 0, \ldots, m-1,\\ & l_j^x & \leq & x_j & \leq & u_j^x, & j = 0, \ldots, n-1,\\ & & & x \in \K, \barX_j \in \PSD^{r_j}, & & & j = 0, \ldots, p-1 \end{array}\end{split}$

where the problem has $$p$$ symmetric positive semidefinite variables $$\barX_j\in \PSD^{r_j}$$ of dimension $$r_j$$ with symmetric coefficient matrices $$\barC_j\in \Symm^{r_j}$$ and $$\barA_{i,j}\in \Symm^{r_j}$$. We use standard notation for the matrix inner product, i.e., for $$A,B\in \real^{m\times n}$$ we have

$\left\langle A,B \right\rangle := \sum_{i=0}^{m-1} \sum_{j=0}^{n-1} A_{ij} B_{ij}.$

In Fusion the user can enter the linear expressions in a more convenient way, without having to cast the problem exactly in the above form.

We demonstrate the setup of semidefinite variables and the matrices $$\barC$$, $$\barA$$ on the following examples:

## 7.5.1 Example SDO1¶

We consider the simple optimization problem with semidefinite and conic quadratic constraints:

(7.7)$\begin{split}\begin{array} {llcc} \mbox{minimize} & \left\langle \left[ \begin{array} {ccc} 2 & 1 & 0 \\ 1 & 2 & 1 \\ 0 & 1 & 2 \end{array} \right], \barX \right\rangle + x_0 & & \\ \mbox{subject to} & \left\langle \left[ \begin{array} {ccc} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{array} \right], \barX \right\rangle + x_0 & = & 1, \\ & \left\langle \left[ \begin{array}{ccc} 1 & 1 & 1 \\ 1 & 1 & 1 \\ 1 & 1 & 1 \end{array} \right], \barX \right\rangle + x_1 + x_2 & = & 1/2, \\ & x_0 \geq \sqrt{{x_1}^2 + {x_2}^2}, & \barX \succeq 0, & \end{array}\end{split}$

The problem description contains a 3-dimensional symmetric semidefinite variable which can be written explicitly as:

$\begin{split}\barX = \left[ \begin{array} {ccc} \barX_{00} & \barX_{10} & \barX_{20} \\ \barX_{10} & \barX_{11} & \barX_{21} \\ \barX_{20} & \barX_{21} & \barX_{22} \end{array} \right] \in \PSD^3,\end{split}$

and a conic quadratic variable $$(x_0, x_1, x_2) \in \Q^3$$. The objective is to minimize

$2(\barX_{00} + \barX_{10} + \barX_{11} + \barX_{21} + \barX_{22}) + x_0,$

subject to the two linear constraints

$\begin{split}\begin{array}{ccc} \barX_{00} + \barX_{11} + \barX_{22} + x_0 & = & 1, \\ \barX_{00} + \barX_{11} + \barX_{22} + 2(\barX_{10} + \barX_{20} + \barX_{21}) + x_1 + x_2 & = & 1/2. \end{array}\end{split}$

Our implementation in Fusion begins with creating a new model:

      using (Model M  = new Model("sdo1"))


We create a symmetric semidefinite variable $$\barX$$ and another variable representing $$x$$. For simplicity we immediately declare that $$x$$ belongs to a quadratic cone

        Variable X  = M.Variable("X", Domain.InPSDCone(3));
Variable x  = M.Variable("x", Domain.InQCone(3));


In this elementary example we are going to create an explicit matrix representation of the problem

$\begin{split}\barC =\left[ \begin{array}{ccc} 2 & 1 & 0 \\ 1 & 2 & 1\\ 0 & 1 & 2\end{array}\right],\ \barA_1 =\left[ \begin{array}{ccc} 1 & 0 & 0 \\ 0 & 1 & 0\\ 0 & 0 & 1\end{array}\right],\ \barA_2 =\left[ \begin{array}{ccc} 1 & 1 & 1 \\ 1 & 1 & 1\\ 1 & 1 & 1\end{array}\right].\end{split}$

and use it in the model via the dot product operation $$\langle\cdot,\cdot\rangle$$ which applies to matrices as well as to vectors. This way we create each of the linear constraints and the objective as one expression.

        // Objective

// Constraints
Domain.EqualsTo(0.5));


Now it remains to solve the problem with Model.Solve.

Listing 7.5 Fusion implementation of problem (7.7). Click here to download.
using System;
using mosek.fusion;

namespace mosek.fusion.example
{
public class sdo1
{
public static void Main(string[] args)
{
using (Model M  = new Model("sdo1"))
{
// Setting up the variables
Variable X  = M.Variable("X", Domain.InPSDCone(3));
Variable x  = M.Variable("x", Domain.InQCone(3));

// Setting up constant coefficient matrices
Matrix C  = Matrix.Dense ( new double[,] { {2, 1, 0}, {1, 2, 1}, {0, 1, 2}} );
Matrix A1 = Matrix.Eye(3);
Matrix A2 = Matrix.Ones(3,3);

// Objective

// Constraints
Domain.EqualsTo(0.5));

M.Solve();

Console.WriteLine("[{0}]", (new Utils.StringBuffer()).A(X.Level()).ToString());
Console.WriteLine("[{0}]", (new Utils.StringBuffer()).A(x.Level()).ToString());
}
}
}
}


## 7.5.2 Example SDO2¶

We now demonstrate how to define more than one semidefinite variable using the following problem with two matrix variables and two types of constraints:

(7.8)$\begin{split}\begin{array}{lrll} \mbox{minimize} & \langle C_1,\barX_1\rangle + \langle C_2,\barX_2\rangle & & \\ \mbox{subject to} & \langle A_1,\barX_1\rangle + \langle A_2,\barX_2\rangle & = & b, \\ & (\barX_2)_{01} & \leq & k, \\ & \barX_1, \barX_2 & \succeq & 0. \end{array}\end{split}$

In our example $$\dim(\barX_1)=3$$, $$\dim(\barX_2)=4$$, $$b=23$$, $$k=-3$$ and

$\begin{split}C_1= \left[\begin{array}{ccc} 1 & 0 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 6 \end{array}\right], A_1= \left[\begin{array}{ccc} 1 & 0 & 1 \\ 0 & 0 & 0 \\ 1 & 0 & 2 \end{array}\right],\end{split}$
$\begin{split}C_2= \left[\begin{array}{cccc} 1 & -3 & 0 & 0\\ -3 & 2 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 \\ \end{array}\right], A_2= \left[\begin{array}{cccc} 0 & 1 & 0 & 0\\ 1 & -1 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & -3 \\ \end{array}\right],\end{split}$

are constant symmetric matrices.

Note that this problem does not contain any scalar variables, but they could be added in the same fashion as in Sec. 7.5.1 (Example SDO1).

The code representing the above problem is shown below.

Listing 7.6 Implementation of model (7.8). Click here to download.
using System;
using mosek.fusion;

namespace mosek.fusion.example
{
public class sdo2
{
public static void Main(string[] args)
{

// Sample data in sparse, symmetric triplet format
int[]    C1_k = {0, 2};
int[]    C1_l = {0, 2};
double[] C1_v = {1, 6};
int[]    A1_k = {0, 2, 0, 2};
int[]    A1_l = {0, 0, 2, 2};
double[] A1_v = {1, 1, 1, 2};
int[]    C2_k = {0, 1, 0, 1, 2};
int[]    C2_l = {0, 0, 1, 1, 2};
double[] C2_v = {1, -3, -3, 2, 1};
int[]    A2_k = {1, 0, 1, 3};
int[]    A2_l = {0, 1, 1, 3};
double[] A2_v = {1, 1, -1, -3};
double b = 23;
double k = -3;

// Convert input data into Fusion sparse matrices
Matrix C1 = Matrix.Sparse(3, 3, C1_k, C1_l, C1_v);
Matrix C2 = Matrix.Sparse(4, 4, C2_k, C2_l, C2_v);
Matrix A1 = Matrix.Sparse(3, 3, A1_k, A1_l, A1_v);
Matrix A2 = Matrix.Sparse(4, 4, A2_k, A2_l, A2_v);

using (Model M  = new Model("sdo2"))
{
// Two semidefinite variables
Variable X1 = M.Variable(Domain.InPSDCone(3));
Variable X2 = M.Variable(Domain.InPSDCone(4));

// Objective

// Equality constraint

// Inequality constraint
M.Constraint(X2.Index(new int[] {0,1}), Domain.LessThan(k));

// Solve
M.SetLogHandler(Console.Out);
M.Solve();

// Print solution
Console.WriteLine("Solution (vectorized):");
Console.WriteLine("[{0}]", (new Utils.StringBuffer()).A(X1.Level()).ToString());
Console.WriteLine("[{0}]", (new Utils.StringBuffer()).A(X2.Level()).ToString());
}
}
}
}


## 7.5.3 Example SDO3¶

Here we demonstrate how to use the facilities provided in Fusion to set up a model with many semidefinite variables of the same dimension more efficiently than via looping. We consider a problem with $$n$$ semidefinite variables of dimension $$d$$ and $$k$$ constraints:

(7.9)$\begin{split}\begin{array}{rrl} \mbox{minimize} & \sum_j \mathrm{tr}(\barX_j) & \\ \mbox{subject to} & \sum_j \langle A_{ij},\barX_j\rangle \geq b_i, & i=1,\ldots,k, \\ & \barX_j \succeq 0 & j=1,\ldots,n, \end{array}\end{split}$

with symmetric data matrices $$A_{ij}$$.

The key construction is:

Listing 7.7 Creating a stack of semidefinite variables.
        Variable X = M.Variable(Domain.InPSDCone(d, n));


It creates $$n$$ symmetric, semidefinite matrix variables of dimension $$d$$ arranged in a single variable object X of shape $$(n,d,d)$$. Individual matrix variables can be accessed as slices from $$(i,0,0)$$ to $$(i+1,d,d)$$ (reshaped into shape $$(d,d)$$ if necessary). It is also possible to operate on the full variable X when constructing expressions that involve entries of all the semidefinite matrices in a natural way. The source code example illustrates both these approaches.

Listing 7.8 Implementation of model (7.9). Click here to download.
using System;
using mosek.fusion;

namespace mosek.fusion.example
{
public class sdo3
{

// A helper method computing a semidefinite slice of a 3-dim variable
public static Variable Slice(Variable X, int d, int j) {
return
X.Slice(new int[] {j,0,0}, new int[] {j+1,d,d})
.Reshape(new int[] {d,d});
}

public static void Main(string[] args)
{

// Sample input data
int n = 100;
int d = 4;
int k = 3;
double[] b = {9,10,11};
double[][,] A = new double[n*k][,];
var rand = new Random();
for(int i=0; i<n*k; i++) {
A[i] = new double[d,d];
for(int s1=0; s1<d; s1++)
for(int s2=0; s2<=s1; s2++)
A[i][s1,s2] = A[i][s2,s1] = rand.NextDouble();
}

// Create a model with n semidefinite variables od dimension d x d
using (Model M  = new Model("sdo3"))
{
Variable X = M.Variable(Domain.InPSDCone(d, n));

// Pick indexes of diagonal entries for the objective
int[,] alldiag = new int[d*n,3];
for(int j=0; j<n; j++) for(int s=0; s<d; s++) {
alldiag[j*d+s,0] = j;
alldiag[j*d+s,1] = alldiag[j*d+s,2] = s;
}
M.Objective(ObjectiveSense.Minimize, Expr.Sum( X.Pick(alldiag) ));

// Each constraint is a sum of inner products
// Each semidefinite variable is a slice of X
for(int i=0; i< k; i++) {
for(int j=0; j<n; j++)
addlist[j] = Expr.Dot(A[i*n+j], Slice(X, d, j));
}

// Solve
M.Solve();

// Get results. Each variable is a slice of X
Console.WriteLine("Contributing variables:");
for(int j=0; j<n; j++) {
double[] Xj = Slice(X, d, j).Level();
double maxval = 0;
for(int s=0; s<d*d; s++) maxval = Math.Max(maxval, Xj[s]);
if (maxval > 1e-6) {
Console.WriteLine("X" + j + "=");
for(int s1=0; s1<d; s1++) {
for(int s2=0; s2<d; s2++)
Console.Write(Xj[s1*d+s1] + "  ");
Console.WriteLine();
}
}
}
}
}
}
}