MOSEK Optimizer API For Python PDF
MOSEK Optimizer API For Python PDF
Release [Link]
MOSEK ApS
2017
[Link]
[Link]
CONTENTS
1 Introduction 1
1.1 Why the Optimizer API for Python? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 License agreement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2 Installation 3
2.1 Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.2 Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.3 Testing the Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3 Basic Tutorials 5
3.1 The Basics Tutorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.2 Linear Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.3 Conic Quadratic Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.4 Semidefinite Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.5 Quadratic Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.6 Integer Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.7 Optimizer Termination Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.8 Problem Modification and Reoptimization . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3.9 Solution Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
3.10 Solver Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
4 Nonlinear Tutorials 49
4.1 Separable Convex (SCopt) Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
5 Advanced Tutorials 53
5.1 The Progress Call-back . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
5.2 Solving Linear Systems Involving the Basis Matrix . . . . . . . . . . . . . . . . . . . . . . 55
5.3 Calling BLAS/LAPACK Routines from MOSEK . . . . . . . . . . . . . . . . . . . . . . . 62
5.4 Computing a Sparse Cholesky Factorization . . . . . . . . . . . . . . . . . . . . . . . . . 64
5.5 Converting a quadratically constrained problem to conic form . . . . . . . . . . . . . . . 69
5.6 MOSEK OptServer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
6 Guidelines 77
6.1 Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
6.2 Efficiency Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
6.3 The license system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
7 Case Studies 81
7.1 Portfolio Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
[Link]
9.2 File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
9.3 Verbosity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
ii
[Link]
17.1 The LP File Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
17.2 The MPS File Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
17.3 The OPF Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
17.4 The CBF Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
17.5 The XML (OSiL) Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
17.6 The Task Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
17.7 The JSON Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
17.8 The Solution File Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Bibliography 391
iii
[Link]
iv
[Link]
CHAPTER
ONE
INTRODUCTION
The MOSEK Optimization Suite [Link] is a powerfull software package capable of solving large-scale
optimization problems of the following kind:
• linear,
• convex quadratic,
• conic quadratic (also known as second-order cone),
• semidefinite,
• and general convex.
Integer constrained variables are supported for all problem classes except for semidefinite and general
convex problems. In order to obtain an overview of features in the MOSEK Optimization Suite consult
the product introduction guide.
The Optimizer API for Python provides an object-oriented interface to the MOSEK optimizers. This
object oriented design is common to Java, Python and .NET and is based on a thin class-based interface
to the native C optimizer API. The overhead introduced by this mapping is minimal.
The Optimizer API for Python can be used with any application running on recent Python 2 and 3
interpreters. It consists of a single mosek package which can be used in Python scripts and interactive
shells making it suited for fast prototyping and inspection of models.
Before using the MOSEK software, please read the license agreement available in the distribu-
tion at <MSKHOME>/mosek/8/[Link] or on the MOSEK website [Link]
license-agreement.
[Link]
MOSEK Optimizer API for Python, Release [Link]
MOSEK uses some third-party open-source libraries. Their license details follows.
zlib
MOSEK includes the zlib library obtained from the zlib website. The license agreement for zlib is shown
in Listing 1.1.
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
fplib
MOSEK includes the floating point formatting library developed by David M. Gay obtained from the
netlib website. The license agreement for fplib is shown in Listing 1.2.
2 Chapter 1. Introduction
[Link]
CHAPTER
TWO
INSTALLATION
In this section we discuss how to install and setup the MOSEK Optimizer API for Python.
2.1 Compatibility
The MOSEK Optimizer API for Python requires Python with numpy. Below the supported Python
versions are shown
Platform Python PyPy2.7
Linux 64 bit 2.7, 3.4 and newer Yes
Mac OS 64 bit 2.7, 3.4 and newer Yes
Windows 32 and 64 bit 2.7, 3.4 and newer Yes
2.2 Instructions
Instructions for installing MOSEK Optimization Suite can in located in the Installation Guide.
Subsequently, let <MSKHOME> denote where MOSEK is installed
The relevant files of the MOSEK Optimizer API for Python are organized as reported in Table 2.1.
Table 2.1: Relevant files for the MOSEK Optimizer API for Python.
Relative Path Description Label
<MSKHOME>/mosek/8/tools/platform/<PLATFORM>/python/2 Python 2 install PYTHON2DIR
<MSKHOME>/mosek/8/tools/platform/<PLATFORM>/python/3 Python 3 install PYTHON3DIR
<MSKHOME>/mosek/8/tools/examples/python Examples EXDIR
<MSKHOME>/mosek/8/tools/examples/data Additional data MISCDIR
To install MOSEK run the [Link] script located in <PYTHON2DIR> or <PYTHON3DIR> depending on
the Python version you want to use. For instance, to install MOSEK for Python 3 on a user level, i.e.,
with no special system privilegdes needed, type
[Link]
MOSEK Optimizer API for Python, Release [Link]
on Windows.
For a system wide installation drop the --user flag.
A simple PIP installer is available on the MOSEK Github repository. Atlthough there is a MOSEK
package in the official PIP repository then it is not created by MOSEK. It cannot be recommended to
use that package.
The MOSEK Optimizer API for Python installation can be tested by running some of the enclosed
examples. Indeed to run one of the distributed examples, open a terminal and change folder to <EXDIR>
i.e.
cd <EXDIR>
then use your Python interpreter to run the example. For instance to execute example lo1 type
python [Link]
4 Chapter 2. Installation
[Link]
CHAPTER
THREE
BASIC TUTORIALS
In this section a number of examples is provided to demonstrate the functionality required for solving
linear, conic, semidefinite and quadratic problems as well as mixed integer problems.
• Basic tutorial : This is the simplest tutorial: it solves a linear optimization problem read from file.
It will show how
– setup the MOSEK environment and problem task,
– run the solver and
– check the optimization results.
• Linear optimization tutorial : It shows how to input a linear program. It will show how
– define variables and their bounds,
– define constraints and their bounds,
– define a linear objective function,
– input a linear program but rows or by column.
– retrieve the solution.
• Conic quadratic optimization tutorial : The basic steps needed to formulate a conic quadratic
program are introduced:
– define quadratic cones,
– assign the relevant variables to their cones.
• Semidefinite optimization tutorial : How to input semidefintite optimization problems is the topic
of this tutorial, and in particular how to
– input semidefinite matrices and in sparse format,
– add semidefinite matrix variable and
– formulate linear constraints and objective function based on matrix variables.
• Mixed-Integer optimization tutorial : This tutorial shows how integrality conditions can be speci-
fied.
• Quadratic optimization tutorial : It shows how to input quadratic terms in the objective function
and constraints.
• Response code tutorial : How to deal with the termination and solver status code is the topic of
this tutorial:
– what are termination and termination code,
– how to check for errors and
– which are the best practice to deal with them.
This is a very important tutorial, every user should go through it.
• Reoptimization tutorial : This tutorial gives information on how to
[Link]
MOSEK Optimizer API for Python, Release [Link]
The simplest program using the MOSEK Python interface can be described shortly:
1. Create an environment.
2. Set up environment specific data and initialize the environment.
3. Create a task.
4. Load a problem into the task.
5. Optimize the problem.
6. Fetch the result.
7. Delete the environment and task.
The first MOSEK related step in any program that employs MOSEK is to create an environment
object. The environment contains environment specific data such as information about the license file,
streams for environment messages etc. When this is done one or more task objects can be created. Each
task is associated with a single environment and defines a complete optimization problem as well as task
message streams and optimization parameters.
In Python, the creation of an environment and a task would look something like this:
# Create an environment
env = Env()
# Create a task
task = [Link]()
When done, tasks and environments may be disposed explicitly by calling the dispose method. This is
not strictly necessary, but it will free up allocated resources and checked-out licenses immediately instead
of when the garbage collector runs. From Python 2.6 and later the with construction can be used to
dispose objects automatically when they drop of out of the with scope:
# Create an environment
with Env() as env:
# Create a task
with [Link]() as task:
Please note that multiple tasks should, if possible, share the same environment.
[Link]
MOSEK Optimizer API for Python, Release [Link]
The simple example in Listing 3.1 shows a working Python program which
• creates an environment and a task,
• reads a problem from a file,
• optimizes the problem, and
• writes the solution to a file.
def streamprinter(msg):
[Link] (msg)
[Link] ()
if len([Link]) <= 1:
print ("Missing argument, syntax is:")
print (" simple inputfile [ solutionfile ]")
else:
# Create the mosek environment.
with [Link] () as env:
[Link] ([Link][1])
[Link] ([Link][2])
Use the [Link] function to write a problem to a file. By default, when not choosing any
specific file format for the parameter iparam.write_data_format , MOSEK will determine the output
file format by the extension of the file name:
[Link]
MOSEK Optimizer API for Python, Release [Link]
[Link] ([Link][2])
[Link] ([Link][1])
An optimization problem consists of several components; objective, objective sense, constraints, variable
bounds etc.
Therefore, the interface provides a number of methods to operate on the task specific data, all of which
are listed under the Task class-specification.
Setting parameters
Apart from the problem data, the task contains a number of parameters defining the behavior of
MOSEK. For example the [Link] parameter defines which optimizer to use. There are
three kinds of parameters in MOSEK
• Integer parameters that can be set with [Link] ,
• Double parameters that can be set with [Link] , and
• string parameters that can be set with [Link] ,
The values for integer parameters are either simple integer values or enum values. See Section 3.10 for
more details on how to set parameters.
A complete list of all parameters is found in Section 16.4 .
The simplest optimization problem is a purely linear problem. A linear optimization problem is a problem
of the following form:
Minimize or maximize the objective function
𝑛−1
∑︁
𝑐𝑗 𝑥𝑗 + 𝑐𝑓
𝑗=0
𝑙𝑗𝑥 ≤ 𝑥𝑗 ≤ 𝑢𝑥𝑗 , 𝑗 = 0, . . . , 𝑛 − 1,
[Link]
MOSEK Optimizer API for Python, Release [Link]
⎡ ⎤
𝑐0
𝑐=⎣ ..
. ⎦,
⎢ ⎥
𝑐𝑛−1
• 𝑐𝑓 which is a constant,
• 𝐴 which is a 𝑚 × 𝑛 matrix of coefficients is given by
⎡ ⎤
𝑎0,0 ··· 𝑎0,(𝑛−1)
𝐴=⎣ .. ..
. . ⎦,
⎢ ⎥
···
𝑎(𝑚−1),0 ··· 𝑎(𝑚−1),(𝑛−1)
• 𝑙𝑐 and 𝑢𝑐 which specify the lower and upper bounds on constraints respectively, and
• 𝑙𝑥 and 𝑢𝑥 which specifies the lower and upper bounds on variables respectively.
Note: Please note the unconventional notation using 0 as the first index rather than 1. Hence, 𝑥0 is
the first element in variable vector 𝑥.
0 ≤ 𝑥0 ≤ ∞,
0 ≤ 𝑥1 ≤ 10,
0 ≤ 𝑥2 ≤ ∞,
0 ≤ 𝑥3 ≤ ∞.
[Link]
MOSEK Optimizer API for Python, Release [Link]
Create an environment.
Before setting up the optimization problem, a MOSEK environment must be created. All tasks in the
program should share the same environment.
We also connect a call-back function to the task log stream. Messages related to the task are passed
to the call-back function. In this case the stream call-back function writes its messages to the standard
output stream.
Before any problem data can be set, variables and constraints must be added to the problem via calls to
the functions [Link] and [Link] .
New variables can now be referenced from other functions with indexes in 0, . . . , numvar − 1 and new
constraints can be referenced with indexes in 0, . . . , numcon − 1. More variables and/or constraints can
be appended later as needed, these will be assigned indexes from numvar/numcon and up.
Next step is to set the problem data. We loop over each variable index 𝑗 = 0, . . . , numvar − 1 calling
functions to set problem data. We first set the objective coefficient 𝑐𝑗 = c[j] by calling the function
[Link] .
[Link](j,c[j])