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.

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

5.2.2 Row-wise input<br />

In the previous example the A matrix is set one column at a time. Alternatively the same matrix can<br />

be set one row at a time or the two methods can be mixed as in the example in section 5.10. <strong>The</strong><br />

following example show how to set the A matrix by rows.<br />

1 #<br />

[ lo2.py ]<br />

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

3 #<br />

4 # File: lo2.py<br />

5 #<br />

6 # Purpose: Demonstrates how to solve small linear<br />

7 # optimization problem using the <strong>MOSEK</strong> <strong>Python</strong> <strong>API</strong>.<br />

8 ##<br />

9<br />

10 import sys<br />

11<br />

12 import mosek<br />

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

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

15 try:<br />

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

17 except ImportError:<br />

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

19<br />

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

21 # for symbolic purposes:<br />

22 inf = 0.0<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 # Bound keys for constraints<br />

43 bkc = [mosek.boundkey.fx,<br />

44 mosek.boundkey.lo,<br />

45 mosek.boundkey.up]<br />

46 # Bound values for constraints<br />

47 blc = [30.0, 15.0, -inf]<br />

48 buc = [30.0, +inf, 25.0]<br />

49 # Bound keys for variables<br />

50 bkx = [mosek.boundkey.lo,

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

Saved successfully!

Ooh no, something went wrong!