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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

46 CHAPTER 5. BASIC <strong>API</strong> TUTORIAL<br />

10 import sys<br />

11 import os<br />

12<br />

13 import mosek<br />

14<br />

15 # If numpy is installed, use that, otherwise use the<br />

16 # Mosek’s array module.<br />

17 try:<br />

18 from numpy import array,zeros,ones<br />

19 except ImportError:<br />

20 from mosek.array import array, zeros, ones<br />

21<br />

22 # Since the actual value of Infinity is ignores, we define it solely<br />

23 # for symbolic purposes:<br />

24 inf = 0.0<br />

25<br />

26 # Define a stream printer to grab output from <strong>MOSEK</strong><br />

27 def streamprinter(text):<br />

28 sys.stdout.write(text)<br />

29 sys.stdout.flush()<br />

30<br />

31 # We might write everything directly as a script, but it looks nicer<br />

32 # to create a function.<br />

33 def main ():<br />

34 # Open <strong>MOSEK</strong> and create an environment and task<br />

35 # Make a <strong>MOSEK</strong> environment<br />

36 env = mosek.Env ()<br />

37 # Attach a printer to the environment<br />

38 env.set Stream (mosek.streamtype.log, streamprinter)<br />

39 # Create a task<br />

40 task = env.Task()<br />

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

42 # Set up and input bounds and linear coefficients<br />

43 bkc = [ mosek.boundkey.lo ]<br />

44 blc = [ 1.0 ]<br />

45 buc = [ inf ]<br />

46<br />

47 bkx = [ mosek.boundkey.lo,<br />

48 mosek.boundkey.lo,<br />

49 mosek.boundkey.lo ]<br />

50 blx = [ 0.0, 0.0, 0.0 ]<br />

51 bux = [ inf, inf, inf ]<br />

52 c = [ 0.0, -1.0, 0.0 ]<br />

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

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

55<br />

56 numvar = len(bkx)<br />

57 numcon = len(bkc)<br />

58<br />

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

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

61 task.appendcons(numcon)<br />

62<br />

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

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

65 task.appendvars(numvar)<br />

66<br />

67 for j in range(numvar):

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

Saved successfully!

Ooh no, something went wrong!