4. Using the MOSEK command line tool


This chapter introduces the MOSEK command line tool which allows the user to solve optimization problems specified in a text file. The main reasons to use the command line tool are

4.1. Getting started

The syntax for the mosek command line tool is

  mosek [options] filename

[options] are some options which modify the behavior of MOSEK such as whether the optimization problem is minimized or maximized. filename is the name of the file which contains the problem data. E.g the

  mosek -min afiro.mps

command line tells MOSEK to read data from the afiro.mps file and to minimize the objective function.

By default the solution to the optimization problem is stored in the files afiro.sol and afiro.bas. The .sol and .bas files contains the interior and basis solution respectively. For problems with integer variables the solution is written to a file with the extension .int.

For a complete list of command line parameters type

  mosek -h

or see Appendix A.

4.2. Examples

Using several examples we will subsequently demonstrate how to use the MOSEK command line tool.

4.2.1. Linear optimization

A linear optimization problem is a problem where a linear objective function is optimized subject to linear constraints. An example of a linear optimization problem is

\begin{math}\nonumber{}\begin{array}{lccccc}\nonumber{}\mbox{minimize} & -10x_{1} &  & -9x_{2}, &  & \\\nonumber{}\mbox{subject to} & 7/10x_{1} & + & 1x_{2} & \leq{} & 630,\\\nonumber{} & 1/2x_{1} & + & 5/6x_{2} & \leq{} & 600,\\\nonumber{} & 1x_{1} & + & 2/3x_{2} & \leq{} & 708,\\\nonumber{} & 1/10x_{1} & + & 1/4x_{2} & \leq{} & 135,\\\nonumber{} & x_{1}, &  & x_{2} & \geq{} & 0.\end{array}\end{math} (4.2.1)

The solution of the example (4.2.1) using MOSEK consists of three steps:

  • Creating an input file describing the problem.
  • Optimizing the problem using MOSEK.
  • Viewing the solution reports.

The input file for MOSEK is a plain text file containing a description of the problem and it must be in either the MPS, the LP, or the OPF format. Below we present the example encoded as an OPF file:

[comment]
  Example lo1.mps converted to OPF. 
[/comment]

[hints]
  # Give a hint about the size of the different elements in the problem. 
  # These need only be estimates, but in this case they are exact.
  [hint NUMVAR] 2 [/hint]
  [hint NUMCON] 4 [/hint]
  [hint NUMANZ] 8 [/hint]
[/hints]

[variables]
  # All variables that will appear in the problem
  x1 x2 
[/variables]

[objective minimize 'obj']
   - 10 x1 - 9 x2
[/objective]

[constraints]
  [con 'c1']  0.7 x1 +              x2 <= 630 [/con]
  [con 'c2']  0.5 x1 + 0.8333333333 x2 <= 600 [/con]
  [con 'c3']      x1 + 0.66666667   x2 <= 708 [/con]
  [con 'c4']  0.1 x1 + 0.25         x2 <= 135 [/con]
[/constraints]

[bounds]
  # By default all variables are free. The following line will 
  # change this to all variables being nonnegative.
  [b] 0 <= * [/b]
[/bounds]

For details on the syntax of the OPF format please consult Appendix D.

After the input file has been created, the problem can be optimized. Assuming that the input file has been given the name lo1.opf, then the problem is optimized using the command line

  mosek lo1.opf

Two solution report files lo1.sol and lo1.bas are generated where the first file contains the interior solution and the second file contains the basic solution. In this case the lo1.bas file has the format:

NAME                : EXAMPLE
PROBLEM STATUS      : PRIMAL_AND_DUAL_FEASIBLE
SOLUTION STATUS     : OPTIMAL
OBJECTIVE NAME      : obj
PRIMAL OBJECTIVE    : -7.66799999e+003
DUAL OBJECTIVE      : -7.66799999e+003

CONSTRAINTS
INDEX  NAME             AT ACTIVITY           LOWER LIMIT        UPPER LIMIT        DUAL LOWER         DUAL UPPER
1      c1               UL 6.30000000e+002    NONE               6.30000000e+002    0.00000000e+000    4.37499996e+000
2      c2               BS 4.80000000e+002    NONE               6.00000000e+002    0.00000000e+000    0.00000000e+000
3      c3               UL 7.08000000e+002    NONE               7.08000000e+002    0.00000000e+000    6.93750003e+000
4      c4               BS 1.17000000e+002    NONE               1.35000000e+002    0.00000000e+000    0.00000000e+000

VARIABLES
INDEX  NAME             AT ACTIVITY           LOWER LIMIT        UPPER LIMIT        DUAL LOWER         DUAL UPPER
1      x1               BS 5.39999998e+002    0.00000000e+000    NONE               0.00000000e+000    0.00000000e+000
2      x2               BS 2.52000001e+002    0.00000000e+000    NONE               0.00000000e+000    0.00000000e+000

The interpretation of the solution file should be obvious. E.g the optimal values of x1 and x2 are 539.99 and 252.00 respectively. A detailed discussion of the solution file format is given in Appendix F.

4.2.2. Quadratic optimization

An example of a quadratic optimization problem is

\begin{math}\nonumber{}\begin{array}{lcccl}\nonumber{}\mbox{minimize} &  &  & x_{1}^{2}+0.1x_{2}^{2}+x_{3}^{2}-x_{1}x_{3}-x_{2} & \\\nonumber{}\mbox{subject to} & 1 & \leq{} & x_{1}+x_{2}+x_{3}, & \\\nonumber{} &  &  & x\geq{}0. &\end{array}\end{math} (4.2.2)

The problem is a quadratic optimization problem because all the constraints are linear and the objective can be stated on the form

\begin{displaymath}\nonumber{}0.5x^{T}Qx+c^{T}x\end{displaymath}

where in this particular case we have that

\begin{math}\nonumber{}Q=\left[\begin{array}{ccc}\nonumber{}2 & 0 & -1\\\nonumber{}0 & 0.2 & 0\\\nonumber{}-1 & 0 & 2\end{array}\right]\mbox{ and }c=\left[\begin{array}{c}\nonumber{}0\\\nonumber{}-1\\\nonumber{}0\end{array}\right].\end{math} (4.2.3)

MOSEK assumes that Q is symmetric and positive semi-definite. If these assumptions are not satisfied, MOSEK will most likely not compute a valid solution. Recall a matrix is symmetric if it satisfies the condition

\begin{displaymath}\nonumber{}Q=Q^{T}\end{displaymath}

and it is positive semi-definite if

\begin{displaymath}\nonumber{}x^{T}Qx\geq{}0,\mbox{ for all }x.\end{displaymath}

An OPF file specifying the example can have the format:

[comment]
  Example qo1.mps converted to OPF.
[/comment]

[hints]
  [hint NUMVAR] 3 [/hint]
  [hint NUMCON] 1 [/hint]
  [hint NUMANZ] 3 [/hint]
[/hints]

[variables]
  x1 x2 x3 
[/variables]

[objective minimize 'obj']
   # The quadratic terms are often multiplied by 1/2, 
   # but this is not required.

   - x2 + 0.5 ( 2 x1 ^ 2 - 2 x3 * x1 + 0.2 x2 ^ 2 + 2 x3 ^ 2 )
[/objective]

[constraints]
  [con 'c1'] 1 <= x1 + x2 + x3 [/con]
[/constraints]

[bounds]
  [b] 0 <= * [/b]
[/bounds]

Please note that the quadratic terms in objective are stated very naturally in the OPF format as follows

   - x2 + 0.5 ( 2 x1 ^ 2 - 2 x3 * x1 + 0.2 x2 ^ 2 + 2 x3 ^ 2 )

The example is solved using the

  mosek qo1.opf

command line. In this case only one solution file named qo1.sol is produced. A .bas file is only produced for linear problems.

4.2.3. Conic optimization

Conic optimization is a generalization of linear optimization which allows the formulation of nonlinear convex optimization problems.

The main idea in conic optimization is to include constraints of the form

\begin{displaymath}\nonumber{}x^{t}\in{}\mathcal{C}\end{displaymath}

in the optimization problem where [[MathCmd 8]] consists of a subset of the variables and [[MathCmd 9]] is a convex cone. Recall that [[MathCmd 9]] is a convex cone if and only if [[MathCmd 9]] is a convex set and

\begin{displaymath}\nonumber{}x\in{}\mathcal{C}\Rightarrow \alpha x\in{}\mathcal{C}\mbox{ for all }\alpha \geq{}0.\end{displaymath}

MOSEK cannot handle arbitrary conic constraints, only the two types

\begin{math}\nonumber{}\left\lbrace{}x\in{}\mathbb{R}^{{n+1}}:~x_{1}\geq{}\sqrt{\sum \limits _{{j=2}}^{{n+1}}x_{j}^{2}}\right\rbrace{}\end{math} (4.2.4)

and

\begin{math}\nonumber{}\left\lbrace{}x\in{}\mathbb{R}^{{n+2}}:~2x_{1}x_{2}\geq{}\sum \limits _{{j=2}}^{{n+2}}x_{j}^{2},~x_{1},x_{2}\geq{}0\right\rbrace{}.\end{math} (4.2.5)

(4.2.4) is called a quadratic cone whereas (4.2.5) is called a rotated quadratic cone.

Consider the problem

\begin{math}\nonumber{}\begin{array}{lccl}\nonumber{}\mbox{minimize} & 1x_{1}+2x_{2} &  & \\\nonumber{}\mbox{subject to} & \frac{1}{x_{1}}+\frac{2}{x_{2}} & \leq{} & 5,\\\nonumber{} & x\geq{}0 &  &\end{array}\end{math} (4.2.6)

which may not initially look like a conic optimization problem. It can however be reformulated to

\begin{math}\nonumber{}\begin{array}{lccl}\nonumber{}\mbox{minimize} & 1x_{1}+2x_{2} &  & \\\nonumber{}\mbox{subject to} & 2x_{3}+4x_{4} & = & 5,\\\nonumber{} & x_{5}^{2} & \leq{} & 2x_{1}x_{3},\\\nonumber{} & x_{6}^{2} & \leq{} & 2x_{2}x_{4},\\\nonumber{} & x_{5} & = & 1,\\\nonumber{} & x_{6} & = & 1,\\\nonumber{} & x\geq{}0. &  &\end{array}\end{math} (4.2.7)

Problem (4.2.6) and problem (4.2.7) are equivalent because the constraints of (4.2.7)

\begin{displaymath}\nonumber{}\frac{x_{5}^{2}}{x_{1}}=\frac{1}{x_{1}}\leq{}2x_{3}\mbox{ and }\frac{x_{6}^{2}}{x_{2}}\leq{}\frac{1}{x_{2}}\leq{}2x_{4}\end{displaymath}

and hence

\begin{displaymath}\nonumber{}\frac{1}{x_{1}}+\frac{2}{x_{2}}\leq{}2x_{3}+4x_{4}=5.\end{displaymath}

The problem (4.2.7) is a conic quadratic optimization problem.

Using the MOSEK OPF format the problem can be represented as follows:

[comment]
  Example cqo1.mps converted to OPF.
[/comment]

[hints]
  [hint NUMVAR] 6 [/hint]
  [hint NUMCON] 1 [/hint]
  [hint NUMANZ] 2 [/hint]
[/hints]

[variables]
  x1 x2 x3 x4 x5 x6 
[/variables]

[objective minimize 'obj']
   x1 + 2 x2
[/objective]

[constraints]
  [con 'c1']  2 x3 + 4 x4 = 5 [/con]
[/constraints]

[bounds]
  # We let all variables default to the positive orthant
  [b] 0 <= * [/b]
  # ... and change those that differ from the default.
  [b] x5,x6 = 1 [/b]

  # We define two rotated quadratic cones

  # k1: 2 x1 * x3 >= x5^2
  [cone rquad 'k1'] x1, x3, x5 [/cone]

  # k2: 2 x2 * x4 >= x6^2
  [cone rquad 'k2'] x2, x4, x6 [/cone]
[/bounds]

For details on the OPF format please consult Appendix D. Finally, the example can be solved using the

  mosek cqo1.opf

command line call and the solution can be studied by inspecting the cqo1.sol file.

4.3. Passing options to the command line tool

It is possible to modify the behavior of MOSEK by setting appropriate parameters. E.g assume that a linear optimization problem should be solved with the primal simplex optimizer rather than the default interior-point optimizer. This is done by setting the parameter MSK_IPAR_OPTIMIZER to the value MSK_OPTIMIZER_PRIMAL_SIMPLEX. To accomplish this append

-d MSK_IPAR_OPTIMIZER MSK_OPTIMIZER_PRIMAL_SIMPLEX.

to the command line. E.g the command

  mosek  -d MSK_IPAR_OPTIMIZER MSK_OPTIMIZER_PRIMAL_SIMPLEX lo1.opf

solves the problem specified by lo1.opf using the primal simplex optimizer. For further information on the parameters available in MOSEK please see Appendix H.

4.4. Reading and writing problem data files

MOSEK reads and writes problem data files in the formats presented in Table 4.1.

Format name Standard Read Write File type File extension Reference
OPF No Yes Yes ASCII/UTF8 opf Appendix D
MPS Yes Yes Yes ASCII mps Appendix B
LP Partially Yes Yes ASCII lp Appendix C
OSiL XML Yes No Yes ASCII/UTF8 xml Appendix E
Binary task format No Yes Yes Binary mbt
Table 4.1: Supported file formats.

The columns of Table 4.1 show:

4.4.1. Reading compressed data files

MOSEK can read and write data files compressed with gzip

For mosek to recognize a file as a gzip compressed file it must have the extension .gz. E.g the command

  mosek myfile.mps.gz

will automatically decompress the file while reading it.

4.4.2. Converting from one format and to another

It is possible to use MOSEK to convert a problem file from one format to another. For instance assume the MPS file myprob.mps should be converted to an LP file named myprob.lp. This can be achieved with the command

  mosek myprob.mps -out myprob.lp -x

Converting an MPS file to a LP file and back to an MPS file permutes the rows and columns of the original problem; this has no influence on the problem, but variables and constraints may appear in a different order.

4.5. Hot-start

Often a sequence of closely related optimization problems has to be solved. In such a case it can be expected that a previous optimal solution can serve as a good starting point when the modified problem is optimized.

Currently, only the simplex optimizer and the mixed-integer optimizer in MOSEK can exploit a guess for the optimal solution. The simplex optimizer can exploit an arbitrary guess for the optimal solution whereas the mixed-integer optimizer requires a feasible integer solution. For both the simplex optimizer and the mixed-integer optimizer it holds that if the guess is good then the optimizer may be able to reduce the solution time significantly when exploiting the guess.

4.5.1. An example

Assume that the example

\begin{math}\nonumber{}\begin{array}{lccccc}\nonumber{}\mbox{minimize} & c_{1}x_{1} &  & -9x_{2}, &  & \\\nonumber{}\mbox{subject to} & 7/10x_{1} & + & 1x_{2} & \leq{} & 630,\\\nonumber{} & 1/2x_{1} & + & 5/6x_{2} & \leq{} & 600,\\\nonumber{} & 1x_{1} & + & 2/3x_{2} & \leq{} & 708,\\\nonumber{} & 1/10x_{1} & + & 1/4x_{2} & \leq{} & 135,\\\nonumber{} & x_{1}, &  & x_{2} & \geq{} & 0.\end{array}\end{math} (4.5.1)

should be solved for [[MathCmd 20]] identical to -5 and -10. Clearly, a solution for one [[MathCmd 20]] value will also be feasible for another value. Therefore, it might be worthwhile to exploit the previous optimal solution when reoptimizing the problem.

Assume that two MPS files have been created each corresponding to one of the [[MathCmd 20]] values then the commands

  mosek lo1.mps -baso .\lo1.bas
  mosek lo1-b.mps -basi .\lo1.bas -baso .\lo1-b.bas
  -d MSK_IPAR_OPTIMIZER MSK_OPTIMIZER_PRIMAL_SIMPLEX

demonstrates how to exploit the previous optimal solution in the second optimization.

In the first line MOSEK optimizes the first version of the optimization problem where [[MathCmd 20]] is identical to -10. The -baso .\lo1.bascommand line option makes sure that the optimal basic solution is written to the file .\lo1.bas.

In the second line the second instance of the problem is optimized. The -basi .\lo1.bascommand line option forces MOSEK to read the previous optimal solution which MOSEK will try to exploit automatically. The -baso .\lo1-b.bascommand line option makes sure that the optimal basic solution is written to the .\lo1-b.basfile. Finally, the

-d MSK_IPAR_OPTIMIZER MSK_OPTIMIZER_PRIMAL_SIMPLEX

command line option makes sure that the primal simplex optimizer is used for the reoptimization. This is important because the interior-point optimizer used by default does not exploit a previous optimal solution.

4.6. Further information

Additional information about the MOSEK command line tool is available in Appendix A.

4.7. Solution file filtering

The MOSEK solution files can be very space consuming for large problems. One way to cut down the solution file size is only to include variables which optimal value is in a certain interesting range i.e [0.01,0.99]. This can be done by setting the MOSEK parameters

MSK_SPAR_SOL_FILTER_XX_LOW   0.01
MSK_SPAR_SOL_FILTER_XX_UPR   0.99

For further details consult the parameters MSK_SPAR_SOL_FILTER_XC_LOW and MSK_SPAR_SOL_FILTER_XC_UPR.

Wed Feb 29 16:20:49 2012