The MOSEK Python optimizer API manual Version 7.0 (Revision 141)
Optimizer API for Python - Documentation - Mosek Optimizer API for Python - Documentation - Mosek
86 CHAPTER 7. ADVANCED API TUTORIAL 13 # 14 # The first argument tells which optimizer to use 15 # i.e. psim is primal simplex, dsim is dual simplex 16 # and intpnt is interior-point. 17 ## 18 from future import with statement 19 20 import sys 21 22 import mosek 23 from mosek import * 24 25 26 def makeUserCallback(maxtime): 27 def userCallback(caller, 28 douinf, 29 intinf, 30 lintinf): 31 opttime = 0.0 32 33 if caller == callbackcode.begin intpnt: 34 print ("Starting interior-point optimizer") 35 elif caller == callbackcode.intpnt: 36 itrn = intinf[iinfitem.intpnt iter ] 37 pobj = douinf[dinfitem.intpnt primal obj] 38 dobj = douinf[dinfitem.intpnt dual obj ] 39 stime = douinf[dinfitem.intpnt time ] 40 opttime = douinf[dinfitem.optimizer time ] 41 42 print ("Iterations: %-3d" % itrn) 43 print (" Elapsed time: %6.2f(%.2f) " % (opttime,stime)) 44 print (" Primal obj.: %-18.6e Dual obj.: %-18.6e" % (pobj,dobj)) 45 elif caller == callbackcode.end intpnt: 46 print ("Interior-point optimizer finished.") 47 elif caller == callbackcode.begin primal simplex: 48 print ("Primal simplex optimizer started.") 49 elif caller == callbackcode.update primal simplex: 50 itrn = intinf[iinfitem.sim primal iter ] 51 pobj = douinf[dinfitem.sim obj ] 52 stime = douinf[dinfitem.sim time ] 53 opttime = douinf[dinfitem.optimizer time ] 54 55 print ("Iterations: %-3d" % itrn) 56 print (" Elapsed time: %6.2f(%.2f)" % (opttime,stime)) 57 print (" Obj.: %-18.6e" % pobj ) 58 elif caller == callbackcode.end primal simplex: 59 print ("Primal simplex optimizer finished.") 60 elif caller == callbackcode.begin dual simplex: 61 print ("Dual simplex optimizer started.") 62 elif caller == callbackcode.update dual simplex: 63 itrn = intinf[iinfitem.sim dual iter ] 64 pobj = douinf[dinfitem.sim obj ] 65 stime = douinf[dinfitem.sim time ] 66 opttime = douinf[dinfitem.optimizer time ] 67 print ("Iterations: %-3d" % itrn) 68 print (" Elapsed time: %6.2f(%.2f)" % (opttime,stime)) 69 print (" Obj.: %-18.6e" % pobj) 70 elif caller == callbackcode.end dual simplex:
7.1. THE PROGRESS CALL-BACK 87 71 print ("Dual simplex optimizer finished.") 72 elif caller == callbackcode.begin bi: 73 print ("Basis identification started.") 74 elif caller == callbackcode.end bi: 75 print ("Basis identification finished.") 76 else: 77 pass 78 79 if opttime >= maxtime: 80 # mosek is spending too much time. Terminate it. 81 return 1 82 83 return 0 84 return userCallback 85 86 def msgPrinter(msg): 87 sys.stdout.write(msg) 88 sys.stdout.flush() 89 90 def main(args): 91 92 if len(args) < 3: 93 print ("Too few input arguments. Syntax:") 94 print ("\tcallback.py psim inputfile") 95 print ("\tcallback.py dsim inputfile") 96 print ("\tcallback.py intpnt inputfile") 97 return 98 99 with mosek.Env() as env: 100 with mosek.Task(env) as task: 101 filename = args[2] 102 task.readdata(filename) 103 104 task.set Stream(streamtype.log, msgPrinter) 105 106 if args[1] == ’psim’: 107 task.putintparam(iparam.optimizer,optimizertype.primal simplex) 108 elif args[1] == "dsim": 109 task.putintparam(iparam.optimizer,optimizertype.dual simplex) 110 elif args[1] == "intpnt": 111 task.putintparam(iparam.optimizer,optimizertype.intpnt) 112 113 # Turn all MOSEK logging off (note that errors and other messages 114 # are still sent through the log stream) 115 task.putintparam(iparam.log, 0) 116 117 usercallback = makeUserCallback(maxtime = 3600) 118 task.set Progress(usercallback) 119 120 task.optimize() 121 122 task.solutionsummary(streamtype.msg) 123 124 if name == ’ main ’: 125 main(sys.argv)
- 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 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: Chapter 7 Advanced API tutorial Thi
- 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
- Page 131 and 132: 8.1. PORTFOLIO OPTIMIZATION 109 110
- Page 133 and 134: 8.1. PORTFOLIO OPTIMIZATION 111 z j
- Page 135 and 136: 8.1. PORTFOLIO OPTIMIZATION 113 Var
- Page 137 and 138: 8.1. PORTFOLIO OPTIMIZATION 115 56
- Page 139 and 140: 8.1. PORTFOLIO OPTIMIZATION 117 172
- Page 141 and 142: Chapter 9 Usage guidelines The purp
- Page 143 and 144: 9.3. WRITING TASK DATA TO A FILE 12
- Page 145 and 146: Chapter 10 Problem formulation and
- Page 147 and 148: 10.1. LINEAR OPTIMIZATION 125 be a
- Page 149 and 150: 10.2. CONIC QUADRATIC OPTIMIZATION
- Page 151 and 152: 10.2. CONIC QUADRATIC OPTIMIZATION
- Page 153 and 154: 10.3. SEMIDEFINITE OPTIMIZATION 131
- Page 155 and 156: 10.4. QUADRATIC AND QUADRATICALLY C
- Page 157 and 158: Chapter 11 The optimizers for conti
7.1. THE PROGRESS CALL-BACK 87<br />
71 print ("Dual simplex <strong>optimizer</strong> finished.")<br />
72 elif caller == callbackcode.begin bi:<br />
73 print ("Basis identification started.")<br />
74 elif caller == callbackcode.end bi:<br />
75 print ("Basis identification finished.")<br />
76 else:<br />
77 pass<br />
78<br />
79 if opttime >= maxtime:<br />
80 # mosek is spending too much time. Terminate it.<br />
81 return 1<br />
82<br />
83 return 0<br />
84 return userCallback<br />
85<br />
86 def msgPrinter(msg):<br />
87 sys.stdout.write(msg)<br />
88 sys.stdout.flush()<br />
89<br />
90 def main(args):<br />
91<br />
92 if len(args) < 3:<br />
93 print ("Too few input arguments. Syntax:")<br />
94 print ("\tcallback.py psim inputfile")<br />
95 print ("\tcallback.py dsim inputfile")<br />
96 print ("\tcallback.py intpnt inputfile")<br />
97 return<br />
98<br />
99 with mosek.Env() as env:<br />
100 with mosek.Task(env) as task:<br />
101 filename = args[2]<br />
102 task.readdata(filename)<br />
103<br />
104 task.set Stream(streamtype.log, msgPrinter)<br />
105<br />
106 if args[1] == ’psim’:<br />
107 task.putintparam(iparam.<strong>optimizer</strong>,<strong>optimizer</strong>type.primal simplex)<br />
108 elif args[1] == "dsim":<br />
109 task.putintparam(iparam.<strong>optimizer</strong>,<strong>optimizer</strong>type.dual simplex)<br />
110 elif args[1] == "intpnt":<br />
111 task.putintparam(iparam.<strong>optimizer</strong>,<strong>optimizer</strong>type.intpnt)<br />
112<br />
113 # Turn all <strong>MOSEK</strong> logging off (note that errors and other messages<br />
114 # are still sent through the log stream)<br />
115 task.putintparam(iparam.log, 0)<br />
116<br />
117 usercallback = makeUserCallback(maxtime = 3600)<br />
118 task.set Progress(usercallback)<br />
119<br />
120 task.optimize()<br />
121<br />
122 task.solutionsummary(streamtype.msg)<br />
123<br />
124 if name == ’ main ’:<br />
125 main(sys.argv)