0% found this document useful (0 votes)
791 views416 pages

MOSEK Optimizer API For Python PDF

Uploaded by

Joyce Herrera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
791 views416 pages

MOSEK Optimizer API For Python PDF

Uploaded by

Joyce Herrera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MOSEK Optimizer API for Python

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

8 Errors and Warnings 101


8.1 Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
8.2 Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101

9 Managing I/O 103


9.1 Stream I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

[Link]
9.2 File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
9.3 Verbosity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104

10 Problem Formulation and Solutions 107


10.1 Linear Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
10.2 Conic Quadratic Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
10.3 Semidefinite Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
10.4 Quadratic and Quadratically Constrained Optimization . . . . . . . . . . . . . . . . . . . 114
10.5 General Convex Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115

11 The Optimizers for Continuous Problems 117


11.1 Presolve . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
11.2 Linear Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
11.3 Conic Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
11.4 Nonlinear Convex Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
11.5 Using Multiple Threads in an Optimizer . . . . . . . . . . . . . . . . . . . . . . . . . . . 127

12 The Optimizer for Mixed-integer Problems 129


12.1 Some Concepts and Facts Related to Mixed-integer Optimization . . . . . . . . . . . . . 129
12.2 The Mixed-integer Optimizer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
12.3 Termination Criterion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
12.4 Parameters Affecting the Termination of the Integer Optimizer. . . . . . . . . . . . . . . 131
12.5 How to Speed Up the Solution Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
12.6 Understanding Solution Quality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132

13 Problem Analyzer 133


13.1 General Characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
13.2 Objective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
13.3 Linear Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
13.4 Constraint and Variable Bounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
13.5 Quadratic Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
13.6 Conic Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136

14 Analyzing Infeasible Problems 137


14.1 Example: Primal Infeasibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
14.2 Locating the cause of Primal Infeasibility . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
14.3 Locating the Cause of Dual Infeasibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
14.4 The Infeasibility Report . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
14.5 Theory Concerning Infeasible Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
14.6 The Certificate of Primal Infeasibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
14.7 The certificate of dual infeasibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142

15 Sensitivity Analysis 143


15.1 Sensitivity Analysis for Linear Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
15.2 Sensitivity Analysis with MOSEK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149

16 API Reference 153


16.1 API Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
16.2 Functions grouped by topic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
16.3 The Interface for Separable Convex Optimization . . . . . . . . . . . . . . . . . . . . . . 165
16.4 Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
16.5 Response codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
16.6 Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
16.7 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
16.8 Class Env . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
16.9 Class Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
16.10 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327

17 Supported File Formats 329

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

18 Interface changes 381


18.1 Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
18.2 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
18.3 Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
18.4 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
18.5 Response Codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387

Bibliography 391

API Index 393

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.

1.1 Why the Optimizer API for Python?

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.

1.2 License agreement

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.

Listing 1.1: zlib license.


zlib.h -- interface of the 'zlib' general purpose compression library
version 1.2.7, May 2nd, 2012

Copyright (C) 1995-2012 Jean-loup Gailly and Mark Adler

This software is provided 'as-is', without any express or implied


warranty. In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,


including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

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.

Jean-loup Gailly Mark Adler


jloup@[Link] madler@[Link]

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.

Listing 1.2: fplib license.


/****************************************************************
*
* The author of this software is David M. Gay.
*
* Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose without fee is hereby granted, provided that this entire notice
* is included in all copies of any software which is or includes a copy
* or modification of this software and in all copies of the supporting
* documentation for such software.
*
* THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
* REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
* OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
*
***************************************************************/

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

2.2.1 Conda Install

Please follow the instructions in the Installation Guide.

2.2.2 Manual Install

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

Install the Package

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]

$ python3 <PYTHON3DIR>/[Link] install --user

on Linux and Mac OS or

C:\> python3 <PYTHON3DIR>\[Link] install --user

on Windows.
For a system wide installation drop the --user flag.

2.2.3 PIP Install

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.

2.3 Testing the Installation

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]

– modify linear constraints,


– add new variables/constraints and
– reoptimize the given problem, i.e. run the MOSEK optimizer again.
• Solution analysis : This tutorial shows how the user can analyze the solution returned by the
solver.
• Parameter setting tutorial : This tutorial shows how to set the solver parameters.

3.1 The Basics Tutorial

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.

3.1.1 The environment and the 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]()

# Load a problem into the task, optimize etc.

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:

# Load a problem into the task, optimize etc.

Please note that multiple tasks should, if possible, share the same environment.

6 Chapter 3. Basic Tutorials

[Link]
MOSEK Optimizer API for Python, Release [Link]

3.1.2 Example: Simple Working Example

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.

Listing 3.1: A simple code solving a problem loaded from file.


import mosek
import sys

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:

# Create a task object linked with the environment env.


# We create it with 0 variables and 0 constraints initially,
# since we do not know the size of the problem.
with [Link] (0, 0) as task:
task.set_Stream ([Link], streamprinter)

# We assume that a problem file was given as the first command


# line argument (received in `argv')

[Link] ([Link][1])

# Solve the problem


[Link] ()

# Print a summary of the solution


[Link] ([Link])

# If an output file was specified, write a solution


if len([Link]) >= 3:
# We define the output format to be OPF, and tell MOSEK to
# leave out parameters and problem data from the output file.
[Link] ([Link].write_data_format, [Link])
[Link] ([Link].opf_write_solutions, [Link])
[Link] ([Link].opf_write_hints, [Link])
[Link] ([Link].opf_write_parameters, [Link])
[Link] ([Link].opf_write_problem, [Link])

[Link] ([Link][2])

Reading and Writing Problems

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:

3.1. The Basics Tutorial 7

[Link]
MOSEK Optimizer API for Python, Release [Link]

[Link] ([Link][2])

Similarly, controlled by iparam.read_data_format , the function [Link] can read a problem


from a file:

[Link] ([Link][1])

Working with the problem data

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 .

3.2 Linear Optimization

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

subject to the linear constraints


𝑛−1
∑︁
𝑙𝑘𝑐 ≤ 𝑎𝑘𝑗 𝑥𝑗 ≤ 𝑢𝑐𝑘 , 𝑘 = 0, . . . , 𝑚 − 1,
𝑗=0

and the bounds

𝑙𝑗𝑥 ≤ 𝑥𝑗 ≤ 𝑢𝑥𝑗 , 𝑗 = 0, . . . , 𝑛 − 1,

where we have used the problem elements:


• 𝑚 and 𝑛 which are the number of constraints and variables respectively,
• 𝑥 which is the variable vector of length 𝑛,
• 𝑐 which is a coefficient vector of size 𝑛

8 Chapter 3. Basic Tutorials

[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 𝑥.

3.2.1 Example LO1

The following is an example of a linear optimization problem:

maximize 3𝑥0 + 1𝑥1 + 5𝑥2 + 1𝑥3


subject to 3𝑥0 + 1𝑥1 + 2𝑥2 = 30,
(3.1)
2𝑥0 + 1𝑥1 + 3𝑥2 + 1𝑥3 ≥ 15,
2𝑥1 + 3𝑥3 ≤ 25,

having the bounds

0 ≤ 𝑥0 ≤ ∞,
0 ≤ 𝑥1 ≤ 10,
0 ≤ 𝑥2 ≤ ∞,
0 ≤ 𝑥3 ≤ ∞.

Solving the problem

To solve the problem above we go through the following steps:


1. Create an environment.
2. Create an optimization task.
3. Load a problem into the task object.
4. Optimization.
5. Extracting the solution.
Below we explain each of these steps.

3.2. Linear Optimization 9

[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.

# Make mosek environment


with [Link]() as env:

Create an optimization task.

Next, an empty task object is created:

# Create a task object


with [Link](0,0) as task:
# Attach a log stream printer to the task
task.set_Stream ([Link], streamprinter)

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.

Load a problem into the task object.

Before any problem data can be set, variables and constraints must be added to the problem via calls to
the functions [Link] and [Link] .

# Append 'numcon' empty constraints.


# The constraints will initially have no bounds.
[Link](numcon)

# Append 'numvar' variables.


# The variables will initially be fixed at zero (x=0).
[Link](numvar)

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])

The bounds on variables are stored in the arrays

# Bound keys for variables


bkx = [[Link],
[Link],
[Link],
[Link]]

# Bound values for variables


blx = [ 0.0, 0.0, 0.0, 0.0]
bux = [+inf, 10.0, +inf, +inf]

and are set with calls to [Link] .