6.7 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
where \(\Symm^r\) is the set of \(r \times r\) real-valued symmetric matrices.
MOSEK can solve semidefinite optimization problems stated in the primal form,
where the problem has \(p\) symmetric positive semidefinite variables \(\barX_j\in \PSD^{r_j}\) of dimension \(r_j\). The symmetric coefficient matrices \(\barC_j\in \Symm^{r_j}\) and \(\barA_{i,j}\in \Symm^{r_j}\) are used to specify PSD terms in the linear objective and the linear constraints, respectively. The symmetric coefficient matrices \(\barF_{i,j}\in \Symm^{r_j}\) are used to specify PSD terms in the affine conic constraints. Note that \(q\) ((6.22)) is the total dimension of all the cones, i.e. \(q=\text{dim}(\K_1 \times \ldots \times \K_k)\), given there are \(k\) ACCs. We use standard notation for the matrix inner product, i.e., for \(A,B\in \real^{m\times n}\) we have
In addition to the primal form presented above, semidefinite problems can be expressed in their dual form. Constraints in this form are usually called linear matrix inequalities (LMIs). LMIs can be easily specified in MOSEK using the vectorized positive semidefinite cone which is defined as:
Vectorized semidefinite domain:
\[\PSD^{d,\mathrm{vec}} = \left\{(x_1,\ldots,x_{d(d+1)/2})\in \real^n~:~ \mathrm{sMat}(x)\in\PSD^d\right\},\]where \(n=d(d+1)/2\) and,
\[\begin{split}\mathrm{sMat}(x) = \left[\begin{array}{cccc}x_1 & x_2/\sqrt{2} & \cdots & x_{d}/\sqrt{2} \\ x_2/\sqrt{2} & x_{d+1} & \cdots & x_{2d-1}/\sqrt{2} \\ \cdots & \cdots & \cdots & \cdots \\ x_{d}/\sqrt{2} & x_{2d-1}/\sqrt{2} & \cdots & x_{d(d+1)/2}\end{array}\right],\end{split}\]or equivalently
\[\PSD^{d,\mathrm{vec}} = \left\{\mathrm{sVec}(X)~:~X\in\PSD^d\right\},\]where
\[\mathrm{sVec}(X) = (X_{11},\sqrt{2}X_{21},\ldots,\sqrt{2}X_{d1},X_{22},\sqrt{2}X_{32},\ldots,X_{dd}).\]
In other words, the domain consists of vectorizations of the lower-triangular part of a positive semidefinite matrix, with the non-diagonal elements additionally rescaled. LMIs can be expressed by restricting appropriate affine expressions to this cone type.
For other types of cones supported by MOSEK, see Sec. 15.10 (Supported domains) and the other tutorials in this chapter. Different cone types can appear together in one optimization problem.
We demonstrate the setup of semidefinite variables and their coefficient matrices in the following examples:
Sec. 6.7.1 (Example SDO1): A problem with one semidefinite variable and linear and conic constraints.
Sec. 6.7.2 (Example SDO2): A problem with two semidefinite variables with a linear constraint and bound.
Sec. 6.7.3 (Example SDO_LMI: Linear matrix inequalities and the vectorized semidefinite domain): A problem with linear matrix inequalities and the vectorized semidefinite domain.
6.7.1 Example SDO1¶
We consider the simple optimization problem with semidefinite and conic quadratic constraints:
The problem description contains a 3-dimensional symmetric semidefinite variable which can be written explicitly as:
and an affine conic constraint (ACC) \((x_0, x_1, x_2) \in \Q^3\). The objective is to minimize
subject to the two linear constraints
Setting up the linear and conic part
The linear and conic parts (constraints, variables, objective, ACC) are set up using the methods described in the relevant tutorials; Sec. 6.1 (Linear Optimization), Sec. 6.2 (From Linear to Conic Optimization). Here we only discuss the aspects directly involving semidefinite variables.
Appending semidefinite variables
First, we need to declare the number of semidefinite variables in the problem, similarly to the number of linear variables and constraints. This is done with the function MSK_appendbarvars
.
r = MSK_appendbarvars(task, NUMBARVAR, DIMBARVAR);
Appending coefficient matrices
Coefficient matrices \(\barC_j\) and \(\barA_{ij}\) are constructed as weighted combinations of sparse symmetric matrices previously appended with the function MSK_appendsparsesymmat
.
r = MSK_appendsparsesymmat(task,
DIMBARVAR[0],
5,
barc_i,
barc_j,
barc_v,
&idx);
The arguments specify the dimension of the symmetric matrix, followed by its description in the sparse triplet format. Only lower-triangular entries should be included. The function produces a unique index of the matrix just entered in the collection of all coefficient matrices defined by the user.
After one or more symmetric matrices have been created using MSK_appendsparsesymmat
, we can combine them to set up the objective matrix coefficient \(\barC_j\) using MSK_putbarcj
, which forms a linear combination of one or more symmetric matrices. In this example we form the objective matrix directly, i.e. as a weighted combination of a single symmetric matrix.
r = MSK_putbarcj(task, 0, 1, &idx, &falpha);
Similarly, a constraint matrix coefficient \(\barA_{ij}\) is set up by the function MSK_putbaraij
.
r = MSK_putbaraij(task, 0, 0, 1, &idx, &falpha);
Retrieving the solution
After the problem is solved, we read the solution using MSK_getbarxj
:
MSK_getbarxj(task,
MSK_SOL_ITR, /* Request the interior solution. */
0,
barx);
The function returns the half-vectorization of \(\barX_j\) (the lower triangular part stacked as a column vector), where the semidefinite variable index \(j\) is passed as an argument.
Source code
#include <stdio.h>
#include "mosek.h" /* Include the MOSEK definition file. */
#define NUMCON 2 /* Number of constraints. */
#define NUMVAR 3 /* Number of conic quadratic variables */
#define NUMANZ 3 /* Number of non-zeros in A */
#define NUMAFE 3 /* Number of affine expressions */
#define NUMFNZ 3 /* Number of non-zeros in F */
#define NUMBARVAR 1 /* Number of semidefinite variables */
static void MSKAPI printstr(void *handle,
const char str[])
{
printf("%s", str);
} /* printstr */
int main(int argc, const char *argv[])
{
MSKrescodee r;
MSKint32t DIMBARVAR[] = {3}; /* Dimension of semidefinite cone */
MSKint64t LENBARVAR[] = {3 * (3 + 1) / 2}; /* Number of scalar SD variables */
MSKboundkeye bkc[] = { MSK_BK_FX, MSK_BK_FX };
double blc[] = { 1.0, 0.5 };
double buc[] = { 1.0, 0.5 };
MSKint32t barc_i[] = {0, 1, 1, 2, 2},
barc_j[] = {0, 0, 1, 1, 2};
double barc_v[] = {2.0, 1.0, 2.0, 1.0, 2.0};
MSKint32t aptrb[] = {0, 1},
aptre[] = {1, 3},
asub[] = {0, 1, 2}; /* column subscripts of A */
double aval[] = {1.0, 1.0, 1.0};
MSKint32t bara_i[] = {0, 1, 2, 0, 1, 2, 1, 2, 2},
bara_j[] = {0, 1, 2, 0, 0, 0, 1, 1, 2};
double bara_v[] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
MSKint32t conesub[] = {0, 1, 2};
MSKint64t afeidx[] = {0, 1, 2};
int varidx[] = {0, 1, 2};
double f_val[] = {1, 1, 1};
MSKint32t i, j;
MSKint64t idx;
double falpha = 1.0;
MSKrealt *xx;
MSKrealt *barx;
MSKenv_t env = NULL;
MSKtask_t task = NULL;
/* Create the mosek environment. */
r = MSK_makeenv(&env, NULL);
if (r == MSK_RES_OK)
{
/* Create the optimization task. */
r = MSK_maketask(env, NUMCON, 0, &task);
if (r == MSK_RES_OK)
{
MSK_linkfunctotaskstream(task, MSK_STREAM_LOG, NULL, printstr);
/* Append 'NUMCON' empty constraints.
The constraints will initially have no bounds. */
if (r == MSK_RES_OK)
r = MSK_appendcons(task, NUMCON);
/* Append 'NUMVAR' variables.
The variables will initially be fixed at zero (x=0). */
if (r == MSK_RES_OK)
r = MSK_appendvars(task, NUMVAR);
/* Append 'NUMAFE' affine expressions.*/
if (r == MSK_RES_OK)
r = MSK_appendafes(task, NUMAFE);
/* Append 'NUMBARVAR' semidefinite variables. */
if (r == MSK_RES_OK) {
r = MSK_appendbarvars(task, NUMBARVAR, DIMBARVAR);
}
/* Optionally add a constant term to the objective. */
if (r == MSK_RES_OK)
r = MSK_putcfix(task, 0.0);
/* Set the linear term c_j in the objective.*/
if (r == MSK_RES_OK)
r = MSK_putcj(task, 0, 1.0);
for (j = 0; j < NUMVAR && r == MSK_RES_OK; ++j)
r = MSK_putvarbound(task,
j,
MSK_BK_FR,
-MSK_INFINITY,
MSK_INFINITY);
/* Set the linear term barc_j in the objective.*/
if (r == MSK_RES_OK)
r = MSK_appendsparsesymmat(task,
DIMBARVAR[0],
5,
barc_i,
barc_j,
barc_v,
&idx);
if (r == MSK_RES_OK)
r = MSK_putbarcj(task, 0, 1, &idx, &falpha);
/* Set the bounds on constraints.
for i=1, ...,NUMCON : blc[i] <= constraint i <= buc[i] */
for (i = 0; i < NUMCON && r == MSK_RES_OK; ++i)
r = MSK_putconbound(task,
i, /* Index of constraint.*/
bkc[i], /* Bound key.*/
blc[i], /* Numerical value of lower bound.*/
buc[i]); /* Numerical value of upper bound.*/
/* Input A row by row */
for (i = 0; i < NUMCON && r == MSK_RES_OK; ++i)
r = MSK_putarow(task,
i,
aptre[i] - aptrb[i],
asub + aptrb[i],
aval + aptrb[i]);
/* Append the affine conic constraint with quadratic cone */
if (r == MSK_RES_OK)
{
r = MSK_putafefentrylist(task, NUMFNZ, afeidx, varidx, f_val);
if (r == MSK_RES_OK)
r = MSK_appendquadraticconedomain(task, 3, NULL);
if (r == MSK_RES_OK)
r = MSK_appendacc(task, 0, 3, afeidx, NULL);
}
/* Add the first row of barA */
if (r == MSK_RES_OK)
r = MSK_appendsparsesymmat(task,
DIMBARVAR[0],
3,
bara_i,
bara_j,
bara_v,
&idx);
if (r == MSK_RES_OK)
r = MSK_putbaraij(task, 0, 0, 1, &idx, &falpha);
/* Add the second row of barA */
if (r == MSK_RES_OK)
r = MSK_appendsparsesymmat(task,
DIMBARVAR[0],
6,
bara_i + 3,
bara_j + 3,
bara_v + 3,
&idx);
if (r == MSK_RES_OK)
r = MSK_putbaraij(task, 1, 0, 1, &idx, &falpha);
if (r == MSK_RES_OK)
{
MSKrescodee trmcode;
/* Run optimizer */
r = MSK_optimizetrm(task, &trmcode);
/* Print a summary containing information
about the solution for debugging purposes*/
MSK_solutionsummary(task, MSK_STREAM_MSG);
if (r == MSK_RES_OK)
{
MSKsolstae solsta;
MSK_getsolsta(task, MSK_SOL_ITR, &solsta);
switch (solsta)
{
case MSK_SOL_STA_OPTIMAL:
xx = (MSKrealt *) MSK_calloctask(task, NUMVAR, sizeof(MSKrealt));
barx = (MSKrealt *) MSK_calloctask(task, LENBARVAR[0], sizeof(MSKrealt));
MSK_getxx(task,
MSK_SOL_ITR,
xx);
MSK_getbarxj(task,
MSK_SOL_ITR, /* Request the interior solution. */
0,
barx);
printf("Optimal primal solution\n");
for (i = 0; i < NUMVAR; ++i)
printf("x[%d] : % e\n", i, xx[i]);
for (i = 0; i < LENBARVAR[0]; ++i)
printf("barx[%d]: % e\n", i, barx[i]);
MSK_freetask(task, xx);
MSK_freetask(task, barx);
break;
case MSK_SOL_STA_DUAL_INFEAS_CER:
case MSK_SOL_STA_PRIM_INFEAS_CER:
printf("Primal or dual infeasibility certificate found.\n");
break;
case MSK_SOL_STA_UNKNOWN:
printf("The status of the solution could not be determined. Termination code: %d.\n", trmcode);
break;
default:
printf("Other solution status.");
break;
}
}
else
{
printf("Error while optimizing.\n");
}
}
if (r != MSK_RES_OK)
{
/* In case of an error print error code and description. */
char symname[MSK_MAX_STR_LEN];
char desc[MSK_MAX_STR_LEN];
printf("An error occurred while optimizing.\n");
MSK_getcodedesc(r,
symname,
desc);
printf("Error %s - '%s'\n", symname, desc);
}
}
/* Delete the task and the associated data. */
MSK_deletetask(&task);
}
/* Delete the environment and the associated data. */
MSK_deleteenv(&env);
return (r);
} /* main */
6.7.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:
In our example \(\dim(\barX_1)=3\), \(\dim(\barX_2)=4\), \(b=23\), \(k=-3\) and
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. 6.7.1 (Example SDO1).
Other than in Sec. 6.7.1 (Example SDO1) we don’t append coefficient matrices separately but we directly input all nonzeros in each constraint and all nonzeros in the objective at once. Every term of the form \((\barA_{i,j})_{k,l}(\barX_j)_{k,l}\) is determined by four indices \((i,j,k,l)\) and a coefficient value \(v=(\barA_{i,j})_{k,l}\). Here \(i\) is the number of the constraint in which the term appears, \(j\) is the index of the semidefinite variable it involves and \((k,l)\) is the position in that variable. This data is passed in the call to MSK_putbarablocktriplet
. Note that only the lower triangular part should be specified explicitly, that is one always has \(k\geq l\). Semidefinite terms \((\barC_j)_{k,l}(\barX_j)_{k,l}\) of the objective are specified in the same way in MSK_putbarcblocktriplet
but only include \((j,k,l)\) and \(v\).
For explanations of other data structures used in the example see Sec. 6.7.1 (Example SDO1).
The code representing the above problem is shown below.
#include <stdio.h>
#include "mosek.h" /* Include the MOSEK definition file. */
static void MSKAPI printstr(void *handle,
const char str[])
{
printf("%s", str);
} /* printstr */
int main(int argc, const char *argv[])
{
MSKrescodee r;
/* Input data */
MSKint32t numbarvar = 2;
MSKint32t dimbarvar[] = {3, 4}; /* Dimension of semidefinite variables */
/* Objective coefficients concatenated */
MSKint32t Cj[] = { 0, 0, 1, 1, 1, 1 }; /* Which symmetric variable (j) */
MSKint32t Ck[] = { 0, 2, 0, 1, 1, 2 }; /* Which entry (k,l)->v */
MSKint32t Cl[] = { 0, 2, 0, 0, 1, 2 };
MSKrealt Cv[] = { 1.0, 6.0, 1.0, -3.0, 2.0, 1.0 };
/* Equality constraints coefficients concatenated */
MSKint32t Ai[] = { 0, 0, 0, 0, 0, 0 }; /* Which constraint (i = 0) */
MSKint32t Aj[] = { 0, 0, 0, 1, 1, 1 }; /* Which symmetric variable (j) */
MSKint32t Ak[] = { 0, 2, 2, 1, 1, 3 }; /* Which entry (k,l)->v */
MSKint32t Al[] = { 0, 0, 2, 0, 1, 3 };
MSKrealt Av[] = { 1.0, 1.0, 2.0, 1.0, -1.0, -3.0 };
/* The second constraint - one-term inequality */
MSKint32t A2i = 1; /* Which constraint (i = 1) */
MSKint32t A2j = 1; /* Which symmetric variable (j = 1) */
MSKint32t A2k = 1; /* Which entry A(1,0) = A(0,1) = 0.5 */
MSKint32t A2l = 0;
MSKrealt A2v = 0.5;
/* Constraint bounds and values */
MSKint32t numcon = 2;
MSKboundkeye bkc[] = { MSK_BK_FX, MSK_BK_UP };
double blc[] = { 23.0, -MSK_INFINITY };
double buc[] = { 23.0, -3.0 };
MSKint32t i, j, dim;
MSKrealt *barx;
MSKenv_t env = NULL;
MSKtask_t task = NULL;
/* Create the mosek environment. */
r = MSK_makeenv(&env, NULL);
if (r == MSK_RES_OK)
{
/* Create the optimization task. */
r = MSK_maketask(env, 0, 0, &task);
if (r == MSK_RES_OK)
{
MSK_linkfunctotaskstream(task, MSK_STREAM_LOG, NULL, printstr);
/* Append empty constraints.
The constraints will initially have no bounds. */
if (r == MSK_RES_OK)
r = MSK_appendcons(task, numcon);
/* Append semidefinite variables. */
if (r == MSK_RES_OK)
r = MSK_appendbarvars(task, numbarvar, dimbarvar);
/* Set objective (6 nonzeros).*/
if (r == MSK_RES_OK)
r = MSK_putbarcblocktriplet(task, 6, Cj, Ck, Cl, Cv);
/* Set the equality constraint (6 nonzeros).*/
if (r == MSK_RES_OK)
r = MSK_putbarablocktriplet(task, 6, Ai, Aj, Ak, Al, Av);
/* Set the inequality constraint (1 nonzero).*/
if (r == MSK_RES_OK)
r = MSK_putbarablocktriplet(task, 1, &A2i, &A2j, &A2k, &A2l, &A2v);
/* Set constraint bounds */
if (r == MSK_RES_OK)
r = MSK_putconboundslice(task, 0, 2, bkc, blc, buc);
if (r == MSK_RES_OK)
{
MSKrescodee trmcode;
/* Run optimizer */
r = MSK_optimizetrm(task, &trmcode);
MSK_solutionsummary(task, MSK_STREAM_MSG);
if (r == MSK_RES_OK)
{
MSKsolstae solsta;
MSK_getsolsta(task, MSK_SOL_ITR, &solsta);
switch (solsta)
{
case MSK_SOL_STA_OPTIMAL:
/* Retrieve the soution for all symmetric variables */
printf("Solution (lower triangular part vectorized):\n");
for(i = 0; i < numbarvar; i++) {
dim = dimbarvar[i] * (dimbarvar[i] + 1) / 2;
barx = (MSKrealt *) MSK_calloctask(task, dim, sizeof(MSKrealt));
MSK_getbarxj(task, MSK_SOL_ITR, i, barx);
printf("X%d: ", i + 1);
for (j = 0; j < dim; ++j)
printf("%.3f ", barx[j]);
printf("\n");
MSK_freetask(task, barx);
}
break;
case MSK_SOL_STA_DUAL_INFEAS_CER:
case MSK_SOL_STA_PRIM_INFEAS_CER:
printf("Primal or dual infeasibility certificate found.\n");
break;
case MSK_SOL_STA_UNKNOWN:
printf("The status of the solution could not be determined. Termination code: %d.\n", trmcode);
break;
default:
printf("Other solution status.");
break;
}
}
else
{
printf("Error while optimizing.\n");
}
}
if (r != MSK_RES_OK)
{
/* In case of an error print error code and description. */
char symname[MSK_MAX_STR_LEN];
char desc[MSK_MAX_STR_LEN];
printf("An error occurred while optimizing.\n");
MSK_getcodedesc(r,
symname,
desc);
printf("Error %s - '%s'\n", symname, desc);
}
}
/* Delete the task and the associated data. */
MSK_deletetask(&task);
}
/* Delete the environment and the associated data. */
MSK_deleteenv(&env);
return (r);
} /* main */
6.7.3 Example SDO_LMI: Linear matrix inequalities and the vectorized semidefinite domain¶
The standard form of a semidefinite problem is usually either based on semidefinite variables (primal form) or on linear matrix inequalities (dual form). However, MOSEK allows mixing of these two forms, as shown in (6.25)
The first affine expression is restricted to a linear domain and could also be modelled as a linear constraint (instead of an ACC). The lower triangular part of the linear matrix inequality (second constraint) can be vectorized and restricted to the MSK_DOMAIN_SVEC_PSD_CONE
. This allows us to express the constraints in (6.25) as the affine conic constraints shown in (6.26).
Vectorization of the LMI is performed as explained in Sec. 15.10 (Supported domains).
Setting up the linear part
The linear parts (objective, constraints, variables) and the semidefinite terms in the linear expressions are defined exactly as shown in the previous examples.
Setting up the affine conic constraints with semidefinite terms
To define the affine conic constraints, we first set up the affine expressions. The \(F\) matrix and the \(g\) vector are defined as usual. Additionally, we specify the coefficients for the semidefinite variables. The semidefinite coefficients shown in (6.26) are setup using the function MSK_putafebarfblocktriplet
.
r = MSK_putafebarfblocktriplet(task,
2,
barf_i,
barf_j,
barf_k,
barf_l,
barf_v);
These affine expressions are then included in their corresponding domains to construct the affine conic constraints. Lastly, the ACCs are appended to the task.
MSKint64t acc1_afeidx[] = {0};
if (r == MSK_RES_OK)
r = MSK_appendrplusdomain(task, 1, NULL);
if (r == MSK_RES_OK)
r = MSK_appendacc(task, 0, 1, acc1_afeidx, NULL);
/* Append the SVEC_PSD domain and the corresponding ACC */
MSKint64t acc2_afeidx[] = {1, 2, 3};
if (r == MSK_RES_OK)
r = MSK_appendsvecpsdconedomain(task, 3, NULL);
if (r == MSK_RES_OK)
r = MSK_appendacc(task, 1, 3, acc2_afeidx, NULL);
Source code
#include <stdio.h>
#include <math.h>
#include "mosek.h" /* Include the MOSEK definition file. */
#define NUMVAR 2 /* Number of scalar variables */
#define NUMAFE 4 /* Number of affine expressions */
#define NUMFNZ 6 /* Number of non-zeros in F */
#define NUMBARVAR 1 /* Number of semidefinite variables */
static void MSKAPI printstr(void *handle,
const char str[])
{
printf("%s", str);
} /* printstr */
int main(int argc, const char *argv[])
{
MSKrescodee r;
const MSKint32t DIMBARVAR[] = {2}; /* Dimension of semidefinite cone */
MSKint64t LENBARVAR[] = {2 * (2 + 1) / 2}; /* Number of scalar SD variables */
const MSKint32t barc_j[] = {0, 0},
barc_k[] = {0, 1},
barc_l[] = {0, 1};
const MSKrealt barc_v[] = {1, 1};
const MSKint64t barf_i[] = {0,0};
const MSKint32t barf_j[] = {0,0},
barf_k[] = {0,1},
barf_l[] = {0,0};
const MSKrealt barf_v[] = {0,1};
const MSKint64t afeidx[] = {0, 0, 1, 2, 2, 3};
const MSKint32t varidx[] = {0, 1, 1, 0, 1, 0};
const MSKrealt f_val[] = {-1, -1, 3, sqrt(2), sqrt(2), 3},
g[] = {0, -1, 0, -1};
MSKrealt *xx, *barx;
MSKint32t i, j;
MSKenv_t env = NULL;
MSKtask_t task = NULL;
/* Create the mosek environment. */
r = MSK_makeenv(&env, NULL);
if (r == MSK_RES_OK)
{
/* Create the optimization task. */
r = MSK_maketask(env, 0, 0, &task);
if (r == MSK_RES_OK)
{
r = MSK_linkfunctotaskstream(task, MSK_STREAM_LOG, NULL, printstr);
/* Append 'NUMAFE' empty affine expressions. */
if (r == MSK_RES_OK)
r = MSK_appendafes(task, NUMAFE);
/* Append 'NUMVAR' scalar variables.
The variables will initially be fixed at zero (x=0). */
if (r == MSK_RES_OK)
r = MSK_appendvars(task, NUMVAR);
/* Append 'NUMBARVAR' semidefinite variables. */
if (r == MSK_RES_OK)
{
r = MSK_appendbarvars(task, NUMBARVAR, DIMBARVAR);
}
/* Set the constant term in the objective. */
if (r == MSK_RES_OK)
r = MSK_putcfix(task, 1.0);
/* Set c_j and the bounds for each scalar variable*/
for (j = 0; j < NUMVAR && r == MSK_RES_OK; ++j)
{
r = MSK_putcj(task, j, 1.0);
if (r==MSK_RES_OK)
r = MSK_putvarbound(task, j, MSK_BK_FR, -MSK_INFINITY, MSK_INFINITY);
}
/* Set the linear term barc_j in the objective.*/
if (r == MSK_RES_OK)
r = MSK_putbarcblocktriplet(task,
2,
barc_j,
barc_k,
barc_l,
barc_v);
/* Set the F matrix */
if (r == MSK_RES_OK)
r = MSK_putafefentrylist(task, NUMFNZ, afeidx, varidx, f_val);
/* Set the g vector */
if (r == MSK_RES_OK)
r = MSK_putafegslice(task, 0, NUMAFE, g);
/* Set the barF matrix */
if (r == MSK_RES_OK)
r = MSK_putafebarfblocktriplet(task,
2,
barf_i,
barf_j,
barf_k,
barf_l,
barf_v);
/* Append R+ domain and the corresponding ACC */
MSKint64t acc1_afeidx[] = {0};
if (r == MSK_RES_OK)
r = MSK_appendrplusdomain(task, 1, NULL);
if (r == MSK_RES_OK)
r = MSK_appendacc(task, 0, 1, acc1_afeidx, NULL);
/* Append the SVEC_PSD domain and the corresponding ACC */
MSKint64t acc2_afeidx[] = {1, 2, 3};
if (r == MSK_RES_OK)
r = MSK_appendsvecpsdconedomain(task, 3, NULL);
if (r == MSK_RES_OK)
r = MSK_appendacc(task, 1, 3, acc2_afeidx, NULL);
if (r == MSK_RES_OK)
{
MSKrescodee trmcode;
/* Run optimizer */
r = MSK_optimizetrm(task, &trmcode);
/* Print a summary containing information
about the solution for debugging purposes*/
MSK_solutionsummary(task, MSK_STREAM_MSG);
if (r == MSK_RES_OK)
{
MSKsolstae solsta;
MSK_getsolsta(task, MSK_SOL_ITR, &solsta);
switch (solsta)
{
case MSK_SOL_STA_OPTIMAL:
xx = (MSKrealt *) MSK_calloctask(task, NUMVAR, sizeof(MSKrealt));
barx = (MSKrealt *) MSK_calloctask(task, LENBARVAR[0], sizeof(MSKrealt));
MSK_getxx(task,MSK_SOL_ITR,xx);
MSK_getbarxj(task, MSK_SOL_ITR, 0, barx);
printf("Optimal primal solution\n");
for (i = 0; i < NUMVAR; ++i)
printf("x[%d] : % e\n", i, xx[i]);
for (i = 0; i < LENBARVAR[0]; ++i)
printf("barx[%d]: % e\n", i, barx[i]);
MSK_freetask(task, xx);
MSK_freetask(task, barx);
break;
case MSK_SOL_STA_DUAL_INFEAS_CER:
case MSK_SOL_STA_PRIM_INFEAS_CER:
printf("Primal or dual infeasibility certificate found.\n");
break;
case MSK_SOL_STA_UNKNOWN:
printf("The status of the solution could not be determined. Termination code: %d.\n", trmcode);
break;
default:
printf("Other solution status.");
break;
}
}
else
printf("Error while optimizing.\n");
}
if (r != MSK_RES_OK)
{
/* In case of an error print error code and description. */
char symname[MSK_MAX_STR_LEN];
char desc[MSK_MAX_STR_LEN];
printf("An error occurred while optimizing.\n");
MSK_getcodedesc(r, symname, desc);
printf("Error %s - '%s'\n", symname, desc);
}
}
/* Delete the task and the associated data. */
MSK_deletetask(&task);
}
/* Delete the environment and the associated data. */
MSK_deleteenv(&env);
return (r);
} /* main */