The MOSEK Python optimizer API manual Version 7.0 (Revision 141)
Optimizer API for Python - Documentation - Mosek Optimizer API for Python - Documentation - Mosek
42 CHAPTER 5. BASIC API TUTORIAL 104 task.appendsparsesymmat(BARVARDIM[0], 105 barci, 106 barcj, 107 barcval) 108 109 syma0 = \ 110 task.appendsparsesymmat(BARVARDIM[0], 111 barai[0], 112 baraj[0], 113 baraval[0]) 114 115 syma1 = \ 116 task.appendsparsesymmat(BARVARDIM[0], 117 barai[1], 118 baraj[1], 119 baraval[1]) 120 121 task.putbarcj(0, [symc], [1.0]) 122 123 task.putbaraij(0, 0, [syma0], [1.0]) 124 task.putbaraij(1, 0, [syma1], [1.0]) 125 126 # Input the objective sense (minimize/maximize) 127 task.putobjsense(mosek.objsense.minimize) 128 129 task.writedata("sdo1.task") 130 131 # Solve the problem and print summary 132 task.optimize() 133 task.solutionsummary(mosek.streamtype.msg) 134 135 # Get status information about the solution 136 prosta = task.getprosta(mosek.soltype.itr) 137 solsta = task.getsolsta(mosek.soltype.itr) 138 139 if (solsta == mosek.solsta.optimal or 140 solsta == mosek.solsta.near optimal): 141 xx = zeros(numvar, float) 142 task.getxx(mosek.soltype.itr, xx) 143 144 lenbarvar = BARVARDIM[0] * (BARVARDIM[0]+1) / 2 145 barx = zeros(int(lenbarvar), float) 146 task.getbarxj(mosek.soltype.itr, 0, barx) 147 148 print("Optimal solution:\nx=%s\nbarx=%s" % (xx,barx)) 149 elif (solsta == mosek.solsta.dual infeas cer or 150 solsta == mosek.solsta.prim infeas cer or 151 solsta == mosek.solsta.near dual infeas cer or 152 solsta == mosek.solsta.near prim infeas cer): 153 print("Primal or dual infeasibility certificate found.\n") 154 elif solsta == mosek.solsta.unknown: 155 print("Unknown solution status") 156 else: 157 print("Other solution status") 158 159 # call the main function 160 try: 161 main ()
5.4. SEMIDEFINITE OPTIMIZATION 43 162 except mosek.Exception as e: 163 print ("ERROR: %s" % str(e.errno)) 164 if e.msg is not None: 165 print ("\t%s" % e.msg) 166 sys.exit(1) 167 except: 168 import traceback 169 traceback.print exc() 170 sys.exit(1) 171 sys.exit(0) 5.4.1.2 Source code comments This example introduces several new functions. The first new function Task.appendbarvars is used to append the semidefinite variable: 79 task.appendbarvars(BARVARDIM) [ sdo1.py ] Symmetric matrices are created using the function Task.appendsparsesymmat: 103 symc = \ 104 task.appendsparsesymmat(BARVARDIM[0], 105 barci, 106 barcj, 107 barcval) 108 109 syma0 = \ 110 task.appendsparsesymmat(BARVARDIM[0], 111 barai[0], 112 baraj[0], 113 baraval[0]) 114 115 syma1 = \ 116 task.appendsparsesymmat(BARVARDIM[0], 117 barai[1], 118 baraj[1], 119 baraval[1]) [ sdo1.py ] The second argument specifies the dimension of the symmetric variable and the third argument gives the number of non-zeros in the lower triangular part of the matrix. The next three arguments specify the non-zeros in the lower-triangle in triplet format, and the last argument will be updated with a unique index of the created symmetric matrix. After one or more symmetric matrices have been created using Task.appendsparsesymmat, we can combine them to setup a objective matrix coefficient c j using Task.putbarcj, which forms a linear combination of one more symmetric matrices: 121 task.putbarcj(0, [symc], [1.0]) [ sdo1.py ]
- Page 13 and 14: CONTENTS xiii A.2.254Task.readparam
- Page 15 and 16: CONTENTS xv B.1.45 dparam.mio near
- Page 17 and 18: CONTENTS xvii B.2.68 iparam.log sim
- Page 19 and 20: CONTENTS xix B.2.160iparam.sim refa
- Page 21 and 22: CONTENTS xxi D.18 Long integer info
- Page 23 and 24: Contact information Phone +45 3917
- Page 25 and 26: License agreement Before using the
- Page 27 and 28: Chapter 1 Changes and new features
- Page 29 and 30: 1.4. API CHANGES 7 1.3.3 Mixed-inte
- Page 31 and 32: 1.10. SUMMARY OF API CHANGES 9 •
- Page 33 and 34: 1.10. SUMMARY OF API CHANGES 11 •
- Page 35 and 36: 1.10. SUMMARY OF API CHANGES 13 •
- Page 37 and 38: Chapter 2 About this manual This ma
- Page 39 and 40: Chapter 3 Getting support and help
- Page 41 and 42: Chapter 4 Testing installation and
- Page 43 and 44: Chapter 5 Basic API tutorial In thi
- Page 45 and 46: 5.1. THE BASICS 23 5 # 6 # Purpose:
- Page 47 and 48: 5.2. LINEAR OPTIMIZATION 25 and the
- Page 49 and 50: 5.2. LINEAR OPTIMIZATION 27 Load a
- Page 51 and 52: 5.2. LINEAR OPTIMIZATION 29 Optimiz
- Page 53 and 54: 5.2. LINEAR OPTIMIZATION 31 88 task
- Page 55 and 56: 5.2. LINEAR OPTIMIZATION 33 51 mose
- Page 57 and 58: 5.3. CONIC QUADRATIC OPTIMIZATION 3
- Page 59 and 60: 5.3. CONIC QUADRATIC OPTIMIZATION 3
- Page 61 and 62: 5.4. SEMIDEFINITE OPTIMIZATION 39 m
- Page 63: 5.4. SEMIDEFINITE OPTIMIZATION 41 4
- Page 67 and 68: 5.5. QUADRATIC OPTIMIZATION 45 with
- Page 69 and 70: 5.5. QUADRATIC OPTIMIZATION 47 68 #
- Page 71 and 72: 5.5. QUADRATIC OPTIMIZATION 49 5.5.
- Page 73 and 74: 5.5. QUADRATIC OPTIMIZATION 51 81 a
- Page 75 and 76: 5.6. THE SOLUTION SUMMARY 53 • Th
- Page 77 and 78: 5.7. INTEGER OPTIMIZATION 55 21 # f
- Page 79 and 80: 5.7. INTEGER OPTIMIZATION 57 137 sy
- Page 81 and 82: 5.8. THE SOLUTION SUMMARY FOR MIXED
- Page 83 and 84: 5.9. RESPONSE HANDLING 61 Note that
- Page 85 and 86: 5.10. PROBLEM MODIFICATION AND REOP
- Page 87 and 88: 5.10. PROBLEM MODIFICATION AND REOP
- Page 89 and 90: 5.11. SOLUTION ANALYSIS 67 151 # Pu
- Page 91 and 92: 5.13. CONVENTIONS EMPLOYED IN THE A
- Page 93 and 94: 5.13. CONVENTIONS EMPLOYED IN THE A
- Page 95 and 96: 5.13. CONVENTIONS EMPLOYED IN THE A
- Page 97 and 98: 5.13. CONVENTIONS EMPLOYED IN THE A
- Page 99 and 100: Chapter 6 Nonlinear API tutorial Th
- Page 101 and 102: 6.1. SEPARABLE CONVEX (SCOPT) INTER
- Page 103 and 104: 6.1. SEPARABLE CONVEX (SCOPT) INTER
- Page 105 and 106: 6.1. SEPARABLE CONVEX (SCOPT) INTER
- Page 107 and 108: Chapter 7 Advanced API tutorial Thi
- Page 109 and 110: 7.1. THE PROGRESS CALL-BACK 87 71 p
- Page 111 and 112: 7.2. SOLVING LINEAR SYSTEMS INVOLVI
- Page 113 and 114: 7.2. SOLVING LINEAR SYSTEMS INVOLVI
42 CHAPTER 5. BASIC <strong>API</strong> TUTORIAL<br />
104 task.appendsparsesymmat(BARVARDIM[0],<br />
105 barci,<br />
106 barcj,<br />
107 barcval)<br />
108<br />
109 syma0 = \<br />
110 task.appendsparsesymmat(BARVARDIM[0],<br />
111 barai[0],<br />
112 baraj[0],<br />
113 baraval[0])<br />
114<br />
115 syma1 = \<br />
116 task.appendsparsesymmat(BARVARDIM[0],<br />
117 barai[1],<br />
118 baraj[1],<br />
119 baraval[1])<br />
120<br />
121 task.putbarcj(0, [symc], [1.0])<br />
122<br />
123 task.putbaraij(0, 0, [syma0], [1.0])<br />
124 task.putbaraij(1, 0, [syma1], [1.0])<br />
125<br />
126 # Input the objective sense (minimize/maximize)<br />
127 task.putobjsense(mosek.objsense.minimize)<br />
128<br />
129 task.writedata("sdo1.task")<br />
130<br />
131 # Solve the problem and print summary<br />
132 task.optimize()<br />
133 task.solutionsummary(mosek.streamtype.msg)<br />
134<br />
135 # Get status information about the solution<br />
136 prosta = task.getprosta(mosek.soltype.itr)<br />
137 solsta = task.getsolsta(mosek.soltype.itr)<br />
138<br />
139 if (solsta == mosek.solsta.optimal or<br />
140 solsta == mosek.solsta.near optimal):<br />
<strong>141</strong> xx = zeros(numvar, float)<br />
142 task.getxx(mosek.soltype.itr, xx)<br />
143<br />
144 lenbarvar = BARVARDIM[0] * (BARVARDIM[0]+1) / 2<br />
145 barx = zeros(int(lenbarvar), float)<br />
146 task.getbarxj(mosek.soltype.itr, 0, barx)<br />
147<br />
148 print("Optimal solution:\nx=%s\nbarx=%s" % (xx,barx))<br />
149 elif (solsta == mosek.solsta.dual infeas cer or<br />
150 solsta == mosek.solsta.prim infeas cer or<br />
151 solsta == mosek.solsta.near dual infeas cer or<br />
152 solsta == mosek.solsta.near prim infeas cer):<br />
153 print("Primal or dual infeasibility certificate found.\n")<br />
154 elif solsta == mosek.solsta.unknown:<br />
155 print("Unknown solution status")<br />
156 else:<br />
157 print("Other solution status")<br />
158<br />
159 # call the main function<br />
160 try:<br />
161 main ()