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

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)

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)

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

Saved successfully!

Ooh no, something went wrong!