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

40 CHAPTER 5. BASIC API TUTORIAL and x 00 + x 11 + x 22 + x 0 = 1, x 00 + x 11 + x 22 + 2(x 10 + x 20 + x 21 ) + x 1 + x 2 = 1/2. 5.4.1.1 Source code 1 ## [ sdo1.py ] 2 # Copyright: Copyright (c) MOSEK ApS, Denmark. All rights reserved. 3 # 4 # File: sdo1.py 5 # 6 # Purpose: Demonstrates how to solve a small mixed semidefinite and conic quadratic 7 # optimization problem using the MOSEK Python API. 8 ## 9 10 import sys 11 import mosek 12 13 # If numpy is installed, use that, otherwise use the 14 # Mosek’s array module. 15 try: 16 from numpy import array,zeros,ones 17 except ImportError: 18 from mosek.array import array, zeros, ones 19 20 # Since the value of infinity is ignored, we define it solely 21 # for symbolic purposes 22 inf = 0.0 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 def main (): 30 # Make mosek environment 31 env = mosek.Env() 32 33 # Create a task object and attach log stream printer 34 task = env.Task(0,0) 35 task.set Stream(mosek.streamtype.log, streamprinter) 36 37 # Bound keys for constraints 38 bkc = [mosek.boundkey.fx, 39 mosek.boundkey.fx] 40 41 # Bound values for constraints 42 blc = [1.0, 0.5] 43 buc = [1.0, 0.5] 44 45 # Below is the sparse representation of the A

5.4. SEMIDEFINITE OPTIMIZATION 41 46 # matrix stored by row. 47 asub = [ array([0]), 48 array([1, 2])] 49 aval = [ array([1.0]), 50 array([1.0, 1.0]) ] 51 52 conesub = [0, 1, 2] 53 54 barci = [0, 1, 1, 2, 2] 55 barcj = [0, 0, 1, 1, 2] 56 barcval = [2.0, 1.0, 2.0, 1.0, 2.0] 57 58 barai = [array([0, 1, 2]), 59 array([0, 1, 2, 1, 2, 2])] 60 baraj = [array([0, 1, 2]), 61 array([0, 0, 0, 1, 1, 2])] 62 baraval = [array([1.0, 1.0, 1.0]), 63 array([1.0, 1.0, 1.0, 1.0, 1.0, 1.0])] 64 65 numvar = 3 66 numcon = len(bkc) 67 BARVARDIM = [3] 68 69 # Append ’numvar’ variables. 70 # The variables will initially be fixed at zero (x=0). 71 task.appendvars(numvar) 72 73 # Append ’numcon’ empty constraints. 74 # The constraints will initially have no bounds. 75 task.appendcons(numcon) 76 77 # Append matrix variables of sizes in ’BARVARDIM’. 78 # The variables will initially be fixed at zero. 79 task.appendbarvars(BARVARDIM) 80 81 # Set the linear term c 0 in the objective. 82 task.putcj(0, 1.0) 83 84 for j in range(numvar): 85 # Set the bounds on variable j 86 # blx[j]

5.4. SEMIDEFINITE OPTIMIZATION 41<br />

46 # matrix stored by row.<br />

47 asub = [ array([0]),<br />

48 array([1, 2])]<br />

49 aval = [ array([1.0]),<br />

50 array([1.0, 1.0]) ]<br />

51<br />

52 conesub = [0, 1, 2]<br />

53<br />

54 barci = [0, 1, 1, 2, 2]<br />

55 barcj = [0, 0, 1, 1, 2]<br />

56 barcval = [2.0, 1.0, 2.0, 1.0, 2.0]<br />

57<br />

58 barai = [array([0, 1, 2]),<br />

59 array([0, 1, 2, 1, 2, 2])]<br />

60 baraj = [array([0, 1, 2]),<br />

61 array([0, 0, 0, 1, 1, 2])]<br />

62 baraval = [array([1.0, 1.0, 1.0]),<br />

63 array([1.0, 1.0, 1.0, 1.0, 1.0, 1.0])]<br />

64<br />

65 numvar = 3<br />

66 numcon = len(bkc)<br />

67 BARVARDIM = [3]<br />

68<br />

69 # Append ’numvar’ variables.<br />

70 # <strong>The</strong> variables will initially be fixed at zero (x=0).<br />

71 task.appendvars(numvar)<br />

72<br />

73 # Append ’numcon’ empty constraints.<br />

74 # <strong>The</strong> constraints will initially have no bounds.<br />

75 task.appendcons(numcon)<br />

76<br />

77 # Append matrix variables of sizes in ’BARVARDIM’.<br />

78 # <strong>The</strong> variables will initially be fixed at zero.<br />

79 task.appendbarvars(BARVARDIM)<br />

80<br />

81 # Set the linear term c 0 in the objective.<br />

82 task.putcj(0, 1.0)<br />

83<br />

84 for j in range(numvar):<br />

85 # Set the bounds on variable j<br />

86 # blx[j]

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

Saved successfully!

Ooh no, something went wrong!