The MOSEK Python optimizer API manual Version 7.0 (Revision 141)
Optimizer API for Python - Documentation - Mosek Optimizer API for Python - Documentation - Mosek
58 CHAPTER 5. BASIC API TUTORIAL 18 except ImportError: 19 from mosek.array import array, zeros, ones 20 21 # Since the actual value of Infinity is ignores, we define it solely 22 # for symbolic purposes: 23 inf = 0.0 24 25 26 # Define a stream printer to grab output from MOSEK 27 def streamprinter(text): 28 sys.stdout.write(text) 29 sys.stdout.flush() 30 31 32 # We might write everything directly as a script, but it looks nicer 33 # to create a function. 34 def main (): 35 # Make a MOSEK environment 36 env = mosek.Env () 37 # Attach a printer to the environment 38 env.set Stream (mosek.streamtype.log, streamprinter) 39 40 # Create a task 41 task = env.Task(0,0) 42 # Attach a printer to the task 43 task.set Stream (mosek.streamtype.log, streamprinter) 44 45 46 bkc = [ mosek.boundkey.up ] 47 blc = [ -inf, ] 48 buc = [ 2.5 ] 49 50 bkx = [ mosek.boundkey.lo, 51 mosek.boundkey.lo, 52 mosek.boundkey.lo, 53 mosek.boundkey.lo ] 54 55 blx = [0.0, 0.0, 0.0, 0.0 ] 56 bux = [ inf, inf, inf, inf ] 57 58 c = [ 7.0, 10.0, 1.0, 5.0 ] 59 60 asub = [ 0, 0, 0, 0 ] 61 acof = [ 1.0, 1.0, 1.0, 1.0] 62 63 ptrb = [ 0, 1, 2, 3 ] 64 ptre = [ 1, 2, 3, 4 ] 65 66 numvar = len(bkx) 67 numcon = len(bkc) 68 69 # Input linear data 70 task.inputdata(numcon,numvar, 71 c,0.0, 72 ptrb, ptre, asub, acof, 73 bkc, blc, buc, 74 bkx, blx, bux) 75
5.8. THE SOLUTION SUMMARY FOR MIXED INTEGER PROBLEMS 59 76 # Input objective sense 77 task.putobjsense(mosek.objsense.maximize) 78 79 # Define variables to be integers 80 task.putvartypelist([ 0, 1, 2 ], 81 [ mosek.variabletype.type int, 82 mosek.variabletype.type int, 83 mosek.variabletype.type int]) 84 85 # Construct an initial feasible solution from the 86 # values of the integer valuse specified 87 task.putintparam(mosek.iparam.mio construct sol, 88 mosek.onoffkey.on); 89 90 # Assign values 0,2,0 to integer variables. Important to 91 # assign a value to all integer constrained variables. 92 task.putxxslice(mosek.soltype.itg,0,3,[0.0, 2.0, 0.0]) 93 94 # Optimize 95 task.optimize() 96 97 # Did mosek construct a feasible initial solution ? 98 if task.getintinf(mosek.iinfitem.mio construct solution) > 0: 99 print("Objective value of constructed integer solution: %-24.12e" % task.getdouinf(mosek.dinfitem.mio construct solut 100 else: 101 print("Intial integer solution construction failed."); 102 103 if task.solutiondef(mosek.soltype.itg): 104 105 # Output a solution 106 xx = zeros(numvar, float) 107 task.getxx(mosek.soltype.itg, xx) 108 print("Integer optimal solution") 109 for j in range(0,numvar) : 110 print("\tx[%d] = %e" % (j,xx[j])) 111 else: 112 print("No integer solution is available.") 113 114 # call the main function 115 try: 116 main () 117 except mosek.Exception as e: 118 print ("ERROR: %s" % str(e.errno)) 119 if e.msg is not None: 120 print ("\t%s" % e.msg) 121 sys.exit(1) 122 except: 123 import traceback 124 traceback.print exc() 125 sys.exit(1) 5.8 The solution summary for mixed integer problems The solution summary for a mixed-integer problem may look like
- 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 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: 5.7. INTEGER OPTIMIZATION 57 137 sy
- 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
- Page 123 and 124: 8.1. PORTFOLIO OPTIMIZATION 101 is
- Page 125 and 126: 8.1. PORTFOLIO OPTIMIZATION 103 15
- Page 127 and 128: 8.1. PORTFOLIO OPTIMIZATION 105 63
- Page 129 and 130: 8.1. PORTFOLIO OPTIMIZATION 107 8.1
5.8. THE SOLUTION SUMMARY FOR MIXED INTEGER PROBLEMS 59<br />
76 # Input objective sense<br />
77 task.putobjsense(mosek.objsense.maximize)<br />
78<br />
79 # Define variables to be integers<br />
80 task.putvartypelist([ 0, 1, 2 ],<br />
81 [ mosek.variabletype.type int,<br />
82 mosek.variabletype.type int,<br />
83 mosek.variabletype.type int])<br />
84<br />
85 # Construct an initial feasible solution from the<br />
86 # values of the integer valuse specified<br />
87 task.putintparam(mosek.iparam.mio construct sol,<br />
88 mosek.onoffkey.on);<br />
89<br />
90 # Assign values 0,2,0 to integer variables. Important to<br />
91 # assign a value to all integer constrained variables.<br />
92 task.putxxslice(mosek.soltype.itg,0,3,[0.0, 2.0, 0.0])<br />
93<br />
94 # Optimize<br />
95 task.optimize()<br />
96<br />
97 # Did mosek construct a feasible initial solution ?<br />
98 if task.getintinf(mosek.iinfitem.mio construct solution) > 0:<br />
99 print("Objective value of constructed integer solution: %-24.12e" % task.getdouinf(mosek.dinfitem.mio construct solut<br />
100 else:<br />
101 print("Intial integer solution construction failed.");<br />
102<br />
103 if task.solutiondef(mosek.soltype.itg):<br />
104<br />
105 # Output a solution<br />
106 xx = zeros(numvar, float)<br />
107 task.getxx(mosek.soltype.itg, xx)<br />
108 print("Integer optimal solution")<br />
109 for j in range(0,numvar) :<br />
110 print("\tx[%d] = %e" % (j,xx[j]))<br />
111 else:<br />
112 print("No integer solution is available.")<br />
113<br />
114 # call the main function<br />
115 try:<br />
116 main ()<br />
117 except mosek.Exception as e:<br />
118 print ("ERROR: %s" % str(e.errno))<br />
119 if e.msg is not None:<br />
120 print ("\t%s" % e.msg)<br />
121 sys.exit(1)<br />
122 except:<br />
123 import traceback<br />
124 traceback.print exc()<br />
125 sys.exit(1)<br />
5.8 <strong>The</strong> solution summary for mixed integer problems<br />
<strong>The</strong> solution summary for a mixed-integer problem may look like