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.

5.9. RESPONSE HANDLING 61<br />

Note that if we want to report, e.g., that the <strong>optimizer</strong> terminated due to a time limit or because it<br />

stalled but with a feasible solution, we have to consider both the termination code, and the solution<br />

status.<br />

<strong>The</strong> following pseudo code demonstrates a best practice way of dealing with the status codes.<br />

if ( the solution status is as expected )<br />

{<br />

<strong>The</strong> normal case:<br />

Do whatever that was planned. Note the response code is<br />

ignored because the solution has the expected status.<br />

Of course we may check the response anyway if we like.<br />

}<br />

else<br />

{<br />

Exceptional case:<br />

Based on solution status, response and termination codes take<br />

appropriate action.<br />

}<br />

In the following example the pseudo code has implemented. <strong>The</strong> idea of the example is to read<br />

an optimization problem from a file, e.g., an MPS file and optimize it. Based on status codes an<br />

appropriate action is taken, which in this case is to print a suitable message.<br />

1 #<br />

[ response.py ]<br />

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

3 #<br />

4 # File: response.py<br />

5 #<br />

6 # Purpose: This examples demonstrates proper response handling.<br />

7 #<br />

8<br />

9 import mosek<br />

10 import sys<br />

11<br />

12 def streamprinter(text):<br />

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

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

15<br />

16 def main(args):<br />

17 if len(args) < 1:<br />

18 print ("No input file specified")<br />

19 return<br />

20 else:<br />

21 print ("Inputfile: %s" % args[0])<br />

22<br />

23 try:<br />

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

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

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

27<br />

28 task.readdata(args[0])<br />

29<br />

30 trmcode = task.optimize()<br />

31<br />

32 solsta = task.getsolsta(mosek.soltype.itr)<br />

33

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

Saved successfully!

Ooh no, something went wrong!