The MOSEK Python optimizer API manual Version 7.0 (Revision 141)
Optimizer API for Python - Documentation - Mosek Optimizer API for Python - Documentation - Mosek
50 CHAPTER 5. BASIC API TUTORIAL 23 24 # Define a stream printer to grab output from MOSEK 25 def streamprinter(text): 26 sys.stdout.write(text) 27 sys.stdout.flush() 28 29 # We might write everything directly as a script, but it looks nicer 30 # to create a function. 31 def main (): 32 # Make a MOSEK environment 33 env = mosek.Env () 34 # Attach a printer to the environment 35 env.set Stream (mosek.streamtype.log, streamprinter) 36 37 # Create a task 38 task = env.Task(0,0) 39 # Attach a printer to the task 40 task.set Stream (mosek.streamtype.log, streamprinter) 41 42 43 # Set up and input bounds and linear coefficients 44 bkc = [ mosek.boundkey.lo ] 45 blc = [ 1.0 ] 46 buc = [ inf ] 47 48 bkx = [ mosek.boundkey.lo, 49 mosek.boundkey.lo, 50 mosek.boundkey.lo ] 51 blx = [ 0.0, 0.0, 0.0 ] 52 bux = [ inf, inf, inf ] 53 54 c = [ 0.0, -1.0, 0.0 ] 55 56 asub = [ array([0]), array([0]), array([0]) ] 57 aval = [ array([1.0]), array([1.0]), array([1.0]) ] 58 59 numvar = len(bkx) 60 numcon = len(bkc) 61 NUMANZ = 3 62 # Append ’numcon’ empty constraints. 63 # The constraints will initially have no bounds. 64 task.appendcons(numcon) 65 66 #Append ’numvar’ variables. 67 # The variables will initially be fixed at zero (x=0). 68 task.appendvars(numvar) 69 70 #Optionally add a constant term to the objective. 71 task.putcfix(0.0) 72 73 for j in range(numvar): 74 # Set the linear term c j in the objective. 75 task.putcj(j,c[j]) 76 # Set the bounds on variable j 77 # blx[j]
5.5. QUADRATIC OPTIMIZATION 51 81 asub[j], # Row index of non-zeros in column j. 82 aval[j]) # Non-zero Values of column j. 83 84 for i in range(numcon): 85 task.putbound(mosek.accmode.con,i,bkc[i],blc[i],buc[i]) 86 87 # Set up and input quadratic objective 88 89 qsubi = [ 0, 1, 2, 2 ] 90 qsubj = [ 0, 1, 0, 2 ] 91 qval = [ 2.0, 0.2, -1.0, 2.0 ] 92 93 task.putqobj(qsubi,qsubj,qval) 94 95 # The lower triangular part of the Q^0 96 # matrix in the first constraint is specified. 97 # This corresponds to adding the term 98 # - x0^2 - x1^2 - 0.1 x2^2 + 0.2 x0 x2 99 100 qsubi = [ 0, 1, 2, 2 ] 101 qsubj = [ 0, 1, 2, 0 ] 102 qval = [ -2.0, -2.0, -0.2, 0.2 ] 103 104 # put Q^0 in constraint with index 0. 105 106 task.putqconk (0, qsubi,qsubj, qval); 107 108 # Input the objective sense (minimize/maximize) 109 task.putobjsense(mosek.objsense.minimize) 110 111 # Optimize the task 112 task.optimize() 113 114 # Print a summary containing information 115 # about the solution for debugging purposes 116 task.solutionsummary(mosek.streamtype.msg) 117 118 prosta = task.getprosta(mosek.soltype.itr) 119 solsta = task.getsolsta(mosek.soltype.itr) 120 121 # Output a solution 122 xx = zeros(numvar, float) 123 task.getxx(mosek.soltype.itr, 124 xx) 125 126 if solsta == mosek.solsta.optimal or solsta == mosek.solsta.near optimal: 127 print("Optimal solution: %s" % xx) 128 elif solsta == mosek.solsta.dual infeas cer: 129 print("Primal or dual infeasibility.\n") 130 elif solsta == mosek.solsta.prim infeas cer: 131 print("Primal or dual infeasibility.\n") 132 elif solsta == mosek.solsta.near dual infeas cer: 133 print("Primal or dual infeasibility.\n") 134 elif solsta == mosek.solsta.near prim infeas cer: 135 print("Primal or dual infeasibility.\n") 136 elif mosek.solsta.unknown: 137 print("Unknown solution status") 138 else:
- 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 and 64: 5.4. SEMIDEFINITE OPTIMIZATION 41 4
- Page 65 and 66: 5.4. SEMIDEFINITE OPTIMIZATION 43 1
- Page 67 and 68: 5.5. QUADRATIC OPTIMIZATION 45 with
- Page 69 and 70: 5.5. QUADRATIC OPTIMIZATION 47 68 #
- Page 71: 5.5. QUADRATIC OPTIMIZATION 49 5.5.
- 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
- Page 115 and 116: 7.2. SOLVING LINEAR SYSTEMS INVOLVI
- Page 117 and 118: 7.2. SOLVING LINEAR SYSTEMS INVOLVI
- Page 119 and 120: Chapter 8 A case study 8.1 Portfoli
- Page 121 and 122: 8.1. PORTFOLIO OPTIMIZATION 99 e T
50 CHAPTER 5. BASIC <strong>API</strong> TUTORIAL<br />
23<br />
24 # Define a stream printer to grab output from <strong>MOSEK</strong><br />
25 def streamprinter(text):<br />
26 sys.stdout.write(text)<br />
27 sys.stdout.flush()<br />
28<br />
29 # We might write everything directly as a script, but it looks nicer<br />
30 # to create a function.<br />
31 def main ():<br />
32 # Make a <strong>MOSEK</strong> environment<br />
33 env = mosek.Env ()<br />
34 # Attach a printer to the environment<br />
35 env.set Stream (mosek.streamtype.log, streamprinter)<br />
36<br />
37 # Create a task<br />
38 task = env.Task(0,0)<br />
39 # Attach a printer to the task<br />
40 task.set Stream (mosek.streamtype.log, streamprinter)<br />
41<br />
42<br />
43 # Set up and input bounds and linear coefficients<br />
44 bkc = [ mosek.boundkey.lo ]<br />
45 blc = [ 1.0 ]<br />
46 buc = [ inf ]<br />
47<br />
48 bkx = [ mosek.boundkey.lo,<br />
49 mosek.boundkey.lo,<br />
50 mosek.boundkey.lo ]<br />
51 blx = [ 0.0, 0.0, 0.0 ]<br />
52 bux = [ inf, inf, inf ]<br />
53<br />
54 c = [ 0.0, -1.0, 0.0 ]<br />
55<br />
56 asub = [ array([0]), array([0]), array([0]) ]<br />
57 aval = [ array([1.0]), array([1.0]), array([1.0]) ]<br />
58<br />
59 numvar = len(bkx)<br />
60 numcon = len(bkc)<br />
61 NUMANZ = 3<br />
62 # Append ’numcon’ empty constraints.<br />
63 # <strong>The</strong> constraints will initially have no bounds.<br />
64 task.appendcons(numcon)<br />
65<br />
66 #Append ’numvar’ variables.<br />
67 # <strong>The</strong> variables will initially be fixed at zero (x=0).<br />
68 task.appendvars(numvar)<br />
69<br />
70 #Optionally add a constant term to the objective.<br />
71 task.putcfix(0.0)<br />
72<br />
73 for j in range(numvar):<br />
74 # Set the linear term c j in the objective.<br />
75 task.putcj(j,c[j])<br />
76 # Set the bounds on variable j<br />
77 # blx[j]