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.

114 CHAPTER 8. A CASE STUDY<br />

<strong>The</strong> following example code demonstrates how to implement the model (8.14).<br />

1 """<br />

2 File : case portfolio 3.py<br />

3<br />

[ case portfolio 3.py ]<br />

4 Copyright : Copyright (c) <strong>MOSEK</strong> ApS, Denmark. All rights reserved.<br />

5<br />

6 Description : Implements a basic portfolio optimization model.<br />

7 """<br />

8<br />

10<br />

9 import mosek<br />

11 try:<br />

12 from numpy import zeros<br />

13 except ImportError:<br />

14 from mosek.array import zeros<br />

15<br />

16 def streamprinter(text):<br />

17 print("%s" % text),<br />

18<br />

19 if name == ’ main ’:<br />

20<br />

21 n = 3<br />

22 gamma = 0.05<br />

23 mu = [0.1073, 0.0737, 0.0627]<br />

24 GT = [[0.1667, 0.0232, 0.0013],<br />

25 [0.0000, 0.1033, -0.0022],<br />

26 [0.0000, 0.0000, 0.0338]]<br />

27 x0 = [0.0, 0.0, 0.0]<br />

28 w = 1.0<br />

29 m = [0.01, 0.01, 0.01]<br />

30<br />

31 # This value has no significance.<br />

32 inf = 0.0<br />

33<br />

34 with mosek.Env() as env:<br />

35 with env.Task(0,0) as task:<br />

36 task.set Stream(mosek.streamtype.log,streamprinter)<br />

37<br />

38 rtemp = w<br />

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

40 rtemp += x0[j]<br />

41<br />

42 # Constraints.<br />

43 task.appendcons(1+9*n)<br />

44 task.putconbound(0,mosek.boundkey.fx,rtemp,rtemp)<br />

45 task.putconname(0,"budget")<br />

46<br />

47 task.putconboundlist(range(1+0,1+n),n*[mosek.boundkey.fx],n*[0.0],n*[0.0])<br />

48 for j in range(1,1+n) :<br />

49 task.putconname(j,"GT[%d]" % j)<br />

50<br />

51 task.putconboundlist(range(1+n,1+2*n),n*[mosek.boundkey.lo],[-x0[j] for j in range(0,n)],n*[inf])<br />

52 for i in range(0,n):<br />

53 task.putconname(1+n+i,"zabs1[%d]" % (1+i))<br />

54<br />

55 task.putconboundlist(range(1+2*n,1+3*n),n*[mosek.boundkey.lo],x0,n*[inf])

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

Saved successfully!

Ooh no, something went wrong!