25.11.2015 Views

The MOSEK Python optimizer API manual Version 7.0 (Revision 141)

Optimizer API for Python - Documentation - Mosek

Optimizer API for Python - Documentation - Mosek

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

104 CHAPTER 8. A CASE STUDY<br />

73 task.putvarboundlist(range(offsett+0,offsett+n),n*[mosek.boundkey.fr],n*[-inf],n*[inf])<br />

74 for j in range(0,n):<br />

75 task.putvarname(offsett+j,"t[%d]" % (1+j))<br />

76<br />

77 task.appendcone(mosek.conetype.quad,0.0,[offsets] + range(offsett,offsett+n))<br />

78 task.putconename(0,"stddev")<br />

79<br />

80 task.putobjsense(mosek.objsense.maximize)<br />

81<br />

82 # Turn all log output off.<br />

83 task.putintparam(mosek.iparam.log,1)<br />

84<br />

85 # Dump the problem to a human readable OPF file.<br />

86 #task.writedata("dump.opf")<br />

87<br />

88 task.optimize()<br />

89<br />

90 # Display the solution summary for quick inspection of results.<br />

91 task.solutionsummary(mosek.streamtype.msg)<br />

92<br />

93 expret = 0.0<br />

94 x = zeros(n,float)<br />

95 task.getxxslice(mosek.soltype.itr,offsetx+0,offsetx+n,x)<br />

96 for j in range(0,n):<br />

97 expret += mu[j]*x[j]<br />

98<br />

99 stddev = zeros(1,float)<br />

100 task.getxxslice(mosek.soltype.itr,offsets+0,offsets+1,stddev)<br />

101<br />

102 print("\nExpected return %e for gamma %e\n" % (expret,stddev[0]))<br />

<strong>The</strong> above code produce the result<br />

Interior-point solution summary<br />

Problem status : PRIMAL AND DUAL FEASIBLE<br />

Solution status : OPTIMAL<br />

Primal. obj: 7.4766497707e-002 Viol. con: 2e-008 var: 0e+000 cones: 3e-009<br />

Dual. obj: 7.4766522618e-002 Viol. con: 0e+000 var: 4e-008 cones: 0e+000<br />

Expected return 7.476650e-02 for gamma 5.000000e-02<br />

<strong>The</strong> source code should be self-explanatory but a few comments are nevertheless in place. In the lines<br />

[ case portfolio 1.py ]<br />

52 # Offset of variables into the <strong>API</strong> variable.<br />

53 offsetx = 0<br />

54 offsets = n<br />

55 offsett = n+1<br />

offsets into the <strong>MOSEK</strong> <strong>API</strong> variables are stored and those offsets are used later. <strong>The</strong> code<br />

[ case portfolio 1.py ]<br />

58 task.putclist(range(offsetx+0,offsetx+n),mu)<br />

59 task.putaijlist(n*[0],range(offsetx+0,offsetx+n),n*[1.0])<br />

60 for j in range(0,n):<br />

61 task.putaijlist(n*[1+j],range(offsetx+0,offsetx+n),GT[j])<br />

62

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

Saved successfully!

Ooh no, something went wrong!