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.

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

streams for environment messages etc. When this is done one or more task objects can be created.<br />

Each task is associated with a single environment and defines a complete optimization problem as well<br />

as task message streams and optimization parameters.<br />

When done, tasks and environments may be disposed explicitly by calling the del method. This<br />

is not strictly necessary, but it will free up allocated resources and checked-out licenses immediately<br />

instead of when the garbage collector runs.<br />

In <strong>Python</strong>, the creation of an environment and a task would look something like this:<br />

# Create an environment<br />

env = mosek.Env()<br />

# You may connect streams and other callbacks to env here.<br />

# Create a task<br />

task = env.Task()<br />

# Load a problem into the task, optimize etc.<br />

From <strong>Python</strong> 2.6 and later the with construction can be used to dispose objects automatically when<br />

they drop of out of the with-scope:<br />

# Create an environment<br />

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

# You may connect streams and other callbacks to env here.<br />

# Create a task<br />

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

# Load a problem into the task, optimize etc.<br />

Please note that multiple tasks should, if possible, share the same environment.<br />

1 #<br />

5.1.2 Example: Simple working example<br />

<strong>The</strong> following simple example shows a working <strong>Python</strong> program which<br />

• creates an environment and a task,<br />

• reads a problem from a file,<br />

• optimizes the problem, and<br />

• writes the solution to a file.<br />

[ simple.py ]<br />

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

3 #<br />

4 # File: simple.py

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

Saved successfully!

Ooh no, something went wrong!