6.3 Conic Quadratic Optimization

The structure of a typical conic optimization problem is

\[\begin{split}\begin{array}{lccccl} \mbox{minimize} & & & c^T x+c^f & & \\ \mbox{subject to} & l^c & \leq & A x & \leq & u^c, \\ & l^x & \leq & x & \leq & u^x, \\ & & & Fx+g & \in & \D, \end{array}\end{split}\]

(see Sec. 12 (Problem Formulation and Solutions) for detailed formulations). We recommend Sec. 6.2 (From Linear to Conic Optimization) for a tutorial on how problems of that form are represented in MOSEK and what data structures are relevant. Here we discuss how to set-up problems with the (rotated) quadratic cones.

MOSEK supports two types of quadratic cones, namely:

  • Quadratic cone:

    \[\Q^n = \left\lbrace x \in \real^n: x_0 \geq \sqrt{\sum_{j=1}^{n-1} x_j^2} \right\rbrace.\]
  • Rotated quadratic cone:

    \[\Qr^n = \left\lbrace x \in \real^n: 2 x_0 x_1 \geq \sum_{j=2}^{n-1} x_j^2,\quad x_0\geq 0,\quad x_1 \geq 0 \right\rbrace.\]

For example, consider the following constraint:

\[(x_4, x_0, x_2) \in \Q^3\]

which describes a convex cone in \(\real^3\) given by the inequality:

\[x_4 \geq \sqrt{x_0^2 + x_2^2}.\]

For other types of cones supported by MOSEK, see Sec. 15.11 (Supported domains) and the other tutorials in this chapter. Different cone types can appear together in one optimization problem.

6.3.1 Example CQO1

Consider the following conic quadratic problem which involves some linear constraints, a quadratic cone and a rotated quadratic cone.

(6.10)\[\begin{split}\begin{array} {lccc} \mbox{minimize} & x_4 + x_5 + x_6 & & \\ \mbox{subject to} & x_1+x_2+ 2 x_3 & = & 1, \\ & x_1,x_2,x_3 & \geq & 0, \\ & x_4 \geq \sqrt{x_1^2 + x_2^2}, & & \\ & 2 x_5 x_6 \geq x_3^2 & & \end{array}\end{split}\]

The two conic constraints can be expressed in the ACC form as shown in (6.11)

(6.11)\[\begin{split}\left[\begin{array}{cccccc}0&0&0&1&0&0\\1&0&0&0&0&0\\0&1&0&0&0&0\\0&0&0&0&1&0\\0&0&0&0&0&1\\0&0&1&0&0&0\end{array}\right] \left[\begin{array}{c}x_1\\x_2\\x_3\\x_4\\x_5\\x_6\end{array}\right] + \left[\begin{array}{c}0\\0\\0\\0\\0\\0\end{array}\right] \in \Q^3 \times \Q_r^3.\end{split}\]

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 conic constraints

In order to append the conic constraints we first input the matrix \(\afef\) and vector \(\afeg\) appearing in (6.11). The matrix \(\afef\) is sparse and we input only its nonzeros using Task.putafefentrylist. Since \(\afeg\) is zero, nothing needs to be done about this vector.

Each of the conic constraints is appended using the function Task.appendacc. In the first case we append the quadratic cone determined by the first three rows of \(\afef\) and then the rotated quadratic cone depending on the remaining three rows of \(\afef\).

        # Create a matrix F such that F * x = [x(3),x(0),x(1),x(4),x(5),x(2)] 
        task.appendafes(6)
        task.putafefentrylist(range(6),                      # Rows
                              [3, 0, 1, 4, 5, 2],            # Columns 
                              [1.0] * 6)

        # Quadratic cone (x(3),x(0),x(1)) \in QUAD_3 
        quadcone  = task.appendquadraticconedomain(3)
        task.appendacc(quadcone,                # Domain 
                       [0, 1, 2],               # Rows from F
                       None)                    # Unused

        # Rotated quadratic cone (x(4),x(5),x(2)) \in RQUAD_3 
        rquadcone = task.appendrquadraticconedomain(3);
        task.appendacc(rquadcone,               # Domain 
                       [3, 4, 5],               # Rows from F 
                       None)                    # Unused 

The first argument selects the domain, which must be appended before being used, and must have the dimension matching the number of affine expressions appearing in the constraint. Variants of this method are available to append multiple ACCs at a time. It is also possible to define the matrix \(\afef\) using a variety of methods (row after row, column by column, individual entries, etc.) similarly as for the linear constraint matrix \(A\).

For a more thorough exposition of the affine expression storage (AFE) matrix \(\afef\) and vector \(\afeg\) see Sec. 6.2 (From Linear to Conic Optimization).

Source code

Listing 6.4 Source code solving problem (6.10). Click here to download.
import sys
import mosek

# Since the actual value of Infinity is ignores, we define it solely
# for symbolic purposes:
inf = 0.0

# Define a stream printer to grab output from MOSEK
def streamprinter(text):
    sys.stdout.write(text)
    sys.stdout.flush()


def main():
    # Create a task
    with mosek.Task() as task:
        # Attach a printer to the task
        task.set_Stream(mosek.streamtype.log, streamprinter)

        bkc = [mosek.boundkey.fx]
        blc = [1.0]
        buc = [1.0]

        c = [0.0, 0.0, 0.0,
             1.0, 1.0, 1.0]
        bkx = [mosek.boundkey.lo, mosek.boundkey.lo, mosek.boundkey.lo,
               mosek.boundkey.fr, mosek.boundkey.fr, mosek.boundkey.fr]
        blx = [0.0, 0.0, 0.0,
               -inf, -inf, -inf]
        bux = [inf, inf, inf,
               inf, inf, inf]

        asub = [[0], [0], [0]]
        aval = [[1.0], [1.0], [2.0]]

        numvar = len(bkx)
        numcon = len(bkc)
        NUMANZ = 4

        # 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 j in range(numvar):
            # 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])

        for j in range(len(aval)):
            # Input column j of A
            task.putacol(j,                  # Variable (column) index.
                         # Row index of non-zeros in column j.
                         asub[j],
                         aval[j])            # Non-zero Values of column j.

        for i in range(numcon):
            task.putconbound(i, bkc[i], blc[i], buc[i])

        # Input the affine conic constraints
        # Create a matrix F such that F * x = [x(3),x(0),x(1),x(4),x(5),x(2)] 
        task.appendafes(6)
        task.putafefentrylist(range(6),                      # Rows
                              [3, 0, 1, 4, 5, 2],            # Columns 
                              [1.0] * 6)

        # Quadratic cone (x(3),x(0),x(1)) \in QUAD_3 
        quadcone  = task.appendquadraticconedomain(3)
        task.appendacc(quadcone,                # Domain 
                       [0, 1, 2],               # Rows from F
                       None)                    # Unused

        # Rotated quadratic cone (x(4),x(5),x(2)) \in RQUAD_3 
        rquadcone = task.appendrquadraticconedomain(3);
        task.appendacc(rquadcone,               # Domain 
                       [3, 4, 5],               # Rows from F 
                       None)                    # Unused 

        # Input the objective sense (minimize/maximize)
        task.putobjsense(mosek.objsense.minimize)

        # Optimize the task
        task.optimize()
        # Print a summary containing information
        # about the solution for debugging purposes
        task.solutionsummary(mosek.streamtype.msg)
        prosta = task.getprosta(mosek.soltype.itr)
        solsta = task.getsolsta(mosek.soltype.itr)

        # Output a solution
        xx = task.getxx(mosek.soltype.itr)

        if solsta == mosek.solsta.optimal:
            print("Optimal solution: %s" % xx)
        elif solsta == mosek.solsta.dual_infeas_cer:
            print("Primal or dual infeasibility.\n")
        elif solsta == mosek.solsta.prim_infeas_cer:
            print("Primal or dual infeasibility.\n")
        elif mosek.solsta.unknown:
            print("Unknown solution status")
        else:
            print("Other solution status")


# call the main function
try:
    main()
except mosek.MosekException as e:
    print("ERROR: %s" % str(e.errno))
    print("\t%s" % e.msg)
    sys.exit(1)
except:
    import traceback
    traceback.print_exc()
    sys.exit(1)