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

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

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

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

Saved successfully!

Ooh no, something went wrong!