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

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:

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]

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

Saved successfully!

Ooh no, something went wrong!