The MOSEK Python optimizer API manual Version 7.0 (Revision 141)

Optimizer API for Python - Documentation - Mosek Optimizer API for Python - Documentation - Mosek

25.11.2015 Views

44 CHAPTER 5. BASIC API TUTORIAL The second argument specify the semidefinite variable index j; in this example there is only a single variable, so the index is 0. The next three arguments give the number of matrices used in the linear combination, their indices (as returned by Task.appendsparsesymmat), and the weights for the individual matrices, respectively. In this example, we form the objective matrix coefficient directly from a single symmetric matrix. Similary, a constraint matrix coefficient A ij is setup by the function Task.putbaraij: 123 task.putbaraij(0, 0, [syma0], [1.0]) 124 task.putbaraij(1, 0, [syma1], [1.0]) [ sdo1.py ] where the second argument specifies the constraint number (the corresponding row of A), and the third argument specifies the semidefinite variable index (the corresponding column of A). The next three arguments specify a weighted combination of symmetric matrices used to form the constraint matrix coefficient. After the problem is solved, we read the solution using Task.getbarxj: 146 task.getbarxj(mosek.soltype.itr, 0, barx) [ sdo1.py ] The function returns the half-vectorization of x j (the lower triangular part stacked as a column vector), where the semidefinite variable index j is given in the second argument, and the third argument is a pointer to an array for storing the numerical values. 5.5 Quadratic optimization MOSEK can solve quadratic and quadratically constrained convex problems. This class of problems can be formulated as follows: 1 minimize 2 xT Q o x + c T x + c f subject to lk c ≤ 1 n−1 ∑ 2 xT Q k x + a k,j x j ≤ u c k, k = 0, . . . , m − 1, j=0 l x j ≤ x j ≤ u x j , j = 0, . . . , n − 1. (5.7) Without loss of generality it is assumed that Q o and Q k are all symmetric because x T Qx = 0.5x T (Q + Q T )x. This implies that a non-symmetric Q can be replaced by the symmetric matrix 1 2 (Q + QT ). The problem is required to be convex. More precisely, the matrix Q o must be positive semi-definite and the kth constraint must be of the form

5.5. QUADRATIC OPTIMIZATION 45 with a negative semi-definite Q k or of the form lk c ≤ 1 n−1 ∑ 2 xT Q k x + a k,j x j (5.8) j=0 n−1 1 ∑ 2 xT Q k x + a k,j x j ≤ u c k. (5.9) j=0 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. 5.5.1 Example: Quadratic objective The following is an example of a quadratic, linearly constrained problem: This can be written equivalently as minimize x 2 1 + 0.1x 2 2 + x 2 3 − x 1 x 3 − x 2 subject to 1 ≤ x 1 + x 2 + x 3 x ≥ 0 where Q o = ⎡ ⎣ 0 0.2 0 2 0 − 1 − 1 0 2 minimize 1/2x T Q o x + c T x subject to Ax ≥ b x ≥ 0, ⎤ ⎦ , c = ⎡ ⎣ 0 − 1 0 ⎤ ⎦ , A = [ 1 1 1 ] , and b = 1. Please note that MOSEK always assumes that there is a 1/2 in front of the x T Qx term in the objective. Therefore, the 1 in front of x 2 0 becomes 2 in Q, i.e. Q o 0,0 = 2. 5.5.1.1 Source code 1 ## [ qo1.py ] 2 # Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved. 3 # 4 # File: qo1.py 5 # 6 # Purpose: Demonstrate how to solve a quadratic 7 # optimization problem using the MOSEK Python API. 8 ## 9

5.5. QUADRATIC OPTIMIZATION 45<br />

with a negative semi-definite Q k or of the form<br />

lk c ≤ 1 n−1<br />

∑<br />

2 xT Q k x + a k,j x j (5.8)<br />

j=0<br />

n−1<br />

1 ∑<br />

2 xT Q k x + a k,j x j ≤ u c k. (5.9)<br />

j=0<br />

with a positive semi-definite Q k . This implies that quadratic equalities are not allowed. Specifying a<br />

non-convex problem will result in an error when the <strong>optimizer</strong> is called.<br />

5.5.1 Example: Quadratic objective<br />

<strong>The</strong> following is an example of a quadratic, linearly constrained problem:<br />

This can be written equivalently as<br />

minimize x 2 1 + 0.1x 2 2 + x 2 3 − x 1 x 3 − x 2<br />

subject to 1 ≤ x 1 + x 2 + x 3<br />

x ≥ 0<br />

where<br />

Q o =<br />

⎡<br />

⎣ 0 0.2 0<br />

2 0 − 1<br />

− 1 0 2<br />

minimize 1/2x T Q o x + c T x<br />

subject to Ax ≥ b<br />

x ≥ 0,<br />

⎤<br />

⎦ , c =<br />

⎡<br />

⎣ 0 − 1<br />

0<br />

⎤<br />

⎦ , A = [ 1 1 1 ] , and b = 1.<br />

Please note that <strong>MOSEK</strong> always assumes that there is a 1/2 in front of the x T Qx term in the objective.<br />

<strong>The</strong>refore, the 1 in front of x 2 0 becomes 2 in Q, i.e. Q o 0,0 = 2.<br />

5.5.1.1 Source code<br />

1 ##<br />

[ qo1.py ]<br />

2 # Copyright: Copyright (c) <strong>MOSEK</strong> ApS, Denmark. All rights reserved.<br />

3 #<br />

4 # File: qo1.py<br />

5 #<br />

6 # Purpose: Demonstrate how to solve a quadratic<br />

7 # optimization problem using the <strong>MOSEK</strong> <strong>Python</strong> <strong>API</strong>.<br />

8 ##<br />

9

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!